Skip to content

Add extension for getservice and replace usages #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/coverlet.collector/DataCollection/CoverageWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Coverlet.Collector.Utilities.Interfaces;
using Coverlet.Core;
using Coverlet.Core.Abstracts;
using Coverlet.Core.Extensions;
using Coverlet.Core.Logging;

namespace Coverlet.Collector.DataCollection
Expand Down Expand Up @@ -30,8 +31,8 @@ public Coverage CreateCoverage(CoverletSettings settings, ILogger coverletLogger
settings.MergeWith,
settings.UseSourceLink,
coverletLogger,
(IInstrumentationHelper)DependencyInjection.Current.GetService(typeof(IInstrumentationHelper)),
(IFileSystem)DependencyInjection.Current.GetService(typeof(IFileSystem)));
DependencyInjection.Current.GetService<IInstrumentationHelper>(),
DependencyInjection.Current.GetService<IFileSystem>());
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/coverlet.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Coverlet.Core;
using Coverlet.Core.Abstracts;
using Coverlet.Core.Enums;
using Coverlet.Core.Extensions;
using Coverlet.Core.Reporters;
using McMaster.Extensions.CommandLineUtils;

Expand Down Expand Up @@ -59,7 +60,7 @@ static int Main(string[] args)
// Adjust log level based on user input.
logger.Level = verbosity.ParsedValue;
}
var fileSystem = (IFileSystem)DependencyInjection.Current.GetService(typeof(IFileSystem));
var fileSystem = DependencyInjection.Current.GetService<IFileSystem>();
Coverage coverage = new Coverage(module.Value,
includeFilters.Values.ToArray(),
includeDirectories.Values.ToArray(),
Expand All @@ -71,7 +72,7 @@ static int Main(string[] args)
mergeWith.Value(),
useSourceLink.HasValue(),
logger,
(IInstrumentationHelper)DependencyInjection.Current.GetService(typeof(IInstrumentationHelper)),
DependencyInjection.Current.GetService<IInstrumentationHelper>(),
fileSystem);
coverage.PrepareModules();

Expand Down
12 changes: 12 additions & 0 deletions src/coverlet.core/Extensions/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Coverlet.Core.Extensions
{
public static class DependencyInjectionExtensions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we'll merge #566 we'll hide as internals...for now it's ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, for now it will stay public.

{
public static T GetService<T>(this IServiceProvider serviceProvider)
{
return (T)serviceProvider.GetService(typeof(T));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why [ExcludeFromCoverage]? core lib won't be instrumented so we don't need to exclude, you can remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, I added it as it was on HelperExtensions. Maybe if core lib won't be instrumented I should remove [ExcludeFromCoverage] from ``HelperExtensions` too?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok...understood...I think it was placed to remove from "autocoverage" that we do on build.

}
}
}
5 changes: 3 additions & 2 deletions src/coverlet.msbuild.tasks/CoverageResultTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Coverlet.Core;
using Coverlet.Core.Abstracts;
using Coverlet.Core.Enums;
using Coverlet.Core.Extensions;
using Coverlet.Core.Reporters;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
Expand Down Expand Up @@ -75,7 +76,7 @@ public override bool Execute()
{
Console.WriteLine("\nCalculating coverage result...");

IFileSystem fileSystem = (IFileSystem)DependencyInjection.Current.GetService(typeof(IFileSystem));
IFileSystem fileSystem = DependencyInjection.Current.GetService<IFileSystem>();
if (InstrumenterState is null || !fileSystem.Exists(InstrumenterState.ItemSpec))
{
_logger.LogError("Result of instrumentation task not found");
Expand All @@ -85,7 +86,7 @@ public override bool Execute()
Coverage coverage = null;
using (Stream instrumenterStateStream = fileSystem.NewFileStream(InstrumenterState.ItemSpec, FileMode.Open))
{
coverage = new Coverage(CoveragePrepareResult.Deserialize(instrumenterStateStream), this._logger, (IInstrumentationHelper)DependencyInjection.Current.GetService(typeof(IInstrumentationHelper)), fileSystem);
coverage = new Coverage(CoveragePrepareResult.Deserialize(instrumenterStateStream), this._logger, DependencyInjection.Current.GetService<IInstrumentationHelper>(), fileSystem);
}

CoverageResult result = coverage.GetCoverageResult();
Expand Down
5 changes: 3 additions & 2 deletions src/coverlet.msbuild.tasks/InstrumentationTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Coverlet.Core;
using Coverlet.Core.Abstracts;
using Coverlet.Core.Extensions;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

Expand Down Expand Up @@ -105,7 +106,7 @@ public override bool Execute()
var excludeFilters = _exclude?.Split(',');
var excludedSourceFiles = _excludeByFile?.Split(',');
var excludeAttributes = _excludeByAttribute?.Split(',');
var fileSystem = (IFileSystem)DependencyInjection.Current.GetService(typeof(IFileSystem));
var fileSystem = DependencyInjection.Current.GetService<IFileSystem>();

Coverage coverage = new Coverage(_path,
includeFilters,
Expand All @@ -118,7 +119,7 @@ public override bool Execute()
_mergeWith,
_useSourceLink,
_logger,
(IInstrumentationHelper)DependencyInjection.Current.GetService(typeof(IInstrumentationHelper)),
DependencyInjection.Current.GetService<IInstrumentationHelper>(),
fileSystem);

CoveragePrepareResult prepareResult = coverage.PrepareModules();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Coverlet.Collector.DataCollection;
using Coverlet.Core.Reporters;
using Coverlet.Core.Abstracts;
using Coverlet.Core.Extensions;

namespace Coverlet.Collector.Tests
{
Expand Down Expand Up @@ -74,7 +75,7 @@ public void OnSessionStartShouldPrepareModulesForCoverage()
null,
_context);
IDictionary<string, object> sessionStartProperties = new Dictionary<string, object>();
Coverage coverage = new Coverage("abc.dll", null, null, null, null, null, true, true, "abc.json", true, It.IsAny<ILogger>(), (IInstrumentationHelper)DependencyInjection.Current.GetService(typeof(IInstrumentationHelper)), (IFileSystem)DependencyInjection.Current.GetService(typeof(IFileSystem)));
Coverage coverage = new Coverage("abc.dll", null, null, null, null, null, true, true, "abc.json", true, It.IsAny<ILogger>(), DependencyInjection.Current.GetService<IInstrumentationHelper>(), DependencyInjection.Current.GetService<IFileSystem>());

sessionStartProperties.Add("TestSources", new List<string> { "abc.dll" });
_mockCoverageWrapper.Setup(x => x.CreateCoverage(It.IsAny<CoverletSettings>(), It.IsAny<ILogger>())).Returns(coverage);
Expand Down