Skip to content
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

Added some logs #353

Merged
merged 4 commits into from
Feb 28, 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
2 changes: 1 addition & 1 deletion build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</Target>

<Target Name="RunTests" AfterTargets="CopyMSBuildScripts">
<Exec Command="dotnet test &quot;$(MSBuildThisFileDirectory)test\coverlet.core.tests\coverlet.core.tests.csproj&quot; -c $(Configuration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Include=[coverlet.*]*"/>
<Exec Command="dotnet test &quot;$(MSBuildThisFileDirectory)test\coverlet.core.tests\coverlet.core.tests.csproj&quot; -c $(Configuration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Include=[coverlet.*]* -verbosity:minimal"/>
</Target>

<Target Name="CreateNuGetPackage" AfterTargets="RunTests" Condition="$(Configuration) == 'Release'">
Expand Down
29 changes: 19 additions & 10 deletions src/coverlet.core/Coverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public string Identifier
get { return _identifier; }
}

public Coverage(string module,
string[] includeFilters,
string[] includeDirectories,
string[] excludeFilters,
string[] excludedSourceFiles,
string[] excludeAttributes,
bool singleHit,
string mergeWith,
public Coverage(string module,
string[] includeFilters,
string[] includeDirectories,
string[] excludeFilters,
string[] excludedSourceFiles,
string[] excludeAttributes,
bool singleHit,
string mergeWith,
bool useSourceLink,
ILogger logger)
{
Expand All @@ -62,16 +62,24 @@ public void PrepareModules()
{
string[] modules = InstrumentationHelper.GetCoverableModules(_module, _includeDirectories);
string[] excludes = InstrumentationHelper.GetExcludedFiles(_excludedSourceFiles);

Array.ForEach(_excludeFilters ?? Array.Empty<string>(), filter => _logger.LogInformation($"Excluded module filter '{filter}'"));
Array.ForEach(_includeFilters ?? Array.Empty<string>(), filter => _logger.LogInformation($"Included module filter '{filter}'"));
Array.ForEach(excludes ?? Array.Empty<string>(), filter => _logger.LogInformation($"Excluded source files '{filter}'"));

_excludeFilters = _excludeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray();
_includeFilters = _includeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray();

foreach (var module in modules)
{
if (InstrumentationHelper.IsModuleExcluded(module, _excludeFilters) ||
!InstrumentationHelper.IsModuleIncluded(module, _includeFilters))
{
_logger.LogInformation($"Excluded module: '{module}'");
continue;
}

var instrumenter = new Instrumenter(module, _identifier, _excludeFilters, _includeFilters, excludes, _excludeAttributes, _singleHit);
var instrumenter = new Instrumenter(module, _identifier, _excludeFilters, _includeFilters, excludes, _excludeAttributes, _singleHit, _logger);
if (instrumenter.CanInstrument())
{
InstrumentationHelper.BackupOriginalModule(module, _identifier);
Expand All @@ -81,6 +89,7 @@ public void PrepareModules()
{
var result = instrumenter.Instrument();
_results.Add(result);
_logger.LogInformation($"Instrumented module: '{module}'");
}
catch (Exception ex)
{
Expand Down Expand Up @@ -197,7 +206,7 @@ private void CalculateCoverage()
{
if (!File.Exists(result.HitsFilePath))
{
// File not instrumented, or nothing in it called. Warn about this?
_logger.LogWarning($"Hits file:'{result.HitsFilePath}' not found for module: '{result.Module}'");
continue;
}

Expand Down
9 changes: 7 additions & 2 deletions src/coverlet.core/Instrumentation/Instrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using Coverlet.Core.Attributes;
using Coverlet.Core.Helpers;
using Coverlet.Core.Logging;
using Coverlet.Core.Symbols;

using Mono.Cecil;
Expand All @@ -25,6 +26,7 @@ internal class Instrumenter
private readonly string[] _excludedAttributes;
private readonly bool _singleHit;
private readonly bool _isCoreLibrary;
private readonly ILogger _logger;
private InstrumenterResult _result;
private FieldDefinition _customTrackerHitsArray;
private FieldDefinition _customTrackerHitsFilePath;
Expand All @@ -35,7 +37,7 @@ internal class Instrumenter
private MethodReference _customTrackerRecordHitMethod;
private List<string> _asyncMachineStateMethod;

public Instrumenter(string module, string identifier, string[] excludeFilters, string[] includeFilters, string[] excludedFiles, string[] excludedAttributes, bool singleHit)
public Instrumenter(string module, string identifier, string[] excludeFilters, string[] includeFilters, string[] excludedFiles, string[] excludedAttributes, bool singleHit, ILogger logger)
{
_module = module;
_identifier = identifier;
Expand All @@ -44,8 +46,8 @@ public Instrumenter(string module, string identifier, string[] excludeFilters, s
_excludedFiles = excludedFiles ?? Array.Empty<string>();
_excludedAttributes = excludedAttributes;
_singleHit = singleHit;

_isCoreLibrary = Path.GetFileNameWithoutExtension(_module) == "System.Private.CoreLib";
_logger = logger;
}

public bool CanInstrument() => InstrumentationHelper.HasPdb(_module);
Expand Down Expand Up @@ -279,7 +281,10 @@ private void InstrumentMethod(MethodDefinition method)
{
var sourceFile = method.DebugInformation.SequencePoints.Select(s => s.Document.Url).FirstOrDefault();
if (!string.IsNullOrEmpty(sourceFile) && _excludedFiles.Contains(sourceFile))
{
_logger.LogInformation($"Excluded source file: '{sourceFile}'");
return;
}

var methodBody = GetMethodBody(method);
if (methodBody == null)
Expand Down
12 changes: 7 additions & 5 deletions src/coverlet.msbuild.tasks/MSBuildLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ namespace Coverlet.MSbuild.Tasks
{
class MSBuildLogger : ILogger
{
private const string LogPrefix = "[coverlet] ";

private readonly TaskLoggingHelper _log;

public MSBuildLogger(TaskLoggingHelper log) => _log = log;

public void LogVerbose(string message) => _log.LogMessage(MessageImportance.Low, message);
public void LogVerbose(string message) => _log.LogMessage(MessageImportance.Low, $"{LogPrefix}{message}");

// We use `MessageImportance.High` because with `MessageImportance.Normal` doesn't show anything
public void LogInformation(string message)=> _log.LogMessage(MessageImportance.High, message);
public void LogInformation(string message)=> _log.LogMessage(MessageImportance.High, $"{LogPrefix}{message}");

public void LogWarning(string message) => _log.LogWarning(message);
public void LogWarning(string message) => _log.LogWarning($"{LogPrefix}{message}");

public void LogError(string message) => _log.LogError(message);
public void LogError(string message) => _log.LogError($"{LogPrefix}{message}");

public void LogError(Exception exception) => _log.LogErrorFromException(exception);
public void LogError(Exception exception) => _log.LogErrorFromException(exception, true);
}
}
4 changes: 1 addition & 3 deletions test/coverlet.core.tests/CoverageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ public void TestCoverage()
File.Copy(module, Path.Combine(directory.FullName, Path.GetFileName(module)), true);
File.Copy(pdb, Path.Combine(directory.FullName, Path.GetFileName(pdb)), true);

var logger = Mock.Of<ILogger>();

// TODO: Find a way to mimick hits

var coverage = new Coverage(Path.Combine(directory.FullName, Path.GetFileName(module)), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), false, string.Empty, false, logger);
var coverage = new Coverage(Path.Combine(directory.FullName, Path.GetFileName(module)), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), false, string.Empty, false, new Mock<ILogger>().Object);
coverage.PrepareModules();

var result = coverage.GetCoverageResult();
Expand Down
8 changes: 5 additions & 3 deletions test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.IO;
using System.Linq;
using Xunit;
using Coverlet.Core.Instrumentation;

using Coverlet.Core.Samples.Tests;
using Moq;
using Coverlet.Core.Logging;

namespace Coverlet.Core.Instrumentation.Tests
{
Expand All @@ -27,7 +29,7 @@ public void TestCoreLibInstrumentation()
foreach (var file in files)
File.Copy(Path.Combine(OriginalFilesDir, file), Path.Combine(TestFilesDir, file), overwrite: true);

Instrumenter instrumenter = new Instrumenter(Path.Combine(TestFilesDir, files[0]), "_coverlet_instrumented", Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), false);
Instrumenter instrumenter = new Instrumenter(Path.Combine(TestFilesDir, files[0]), "_coverlet_instrumented", Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), false, new Mock<ILogger>().Object);
Assert.True(instrumenter.CanInstrument());
var result = instrumenter.Instrument();
Assert.NotNull(result);
Expand Down Expand Up @@ -119,7 +121,7 @@ private InstrumenterTest CreateInstrumentor(bool fakeCoreLibModule = false, stri
File.Copy(pdb, Path.Combine(directory.FullName, destPdb), true);

module = Path.Combine(directory.FullName, destModule);
Instrumenter instrumenter = new Instrumenter(module, identifier, Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), attributesToIgnore, false);
Instrumenter instrumenter = new Instrumenter(module, identifier, Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), attributesToIgnore, false, new Mock<ILogger>().Object);
return new InstrumenterTest
{
Instrumenter = instrumenter,
Expand Down