diff --git a/build.proj b/build.proj index 2e8ebe484..9e270d169 100644 --- a/build.proj +++ b/build.proj @@ -25,7 +25,7 @@ - + diff --git a/src/coverlet.core/Coverage.cs b/src/coverlet.core/Coverage.cs index 86cb13ce2..8ba48df8d 100644 --- a/src/coverlet.core/Coverage.cs +++ b/src/coverlet.core/Coverage.cs @@ -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) { @@ -62,6 +62,11 @@ public void PrepareModules() { string[] modules = InstrumentationHelper.GetCoverableModules(_module, _includeDirectories); string[] excludes = InstrumentationHelper.GetExcludedFiles(_excludedSourceFiles); + + Array.ForEach(_excludeFilters ?? Array.Empty(), filter => _logger.LogInformation($"Excluded module filter '{filter}'")); + Array.ForEach(_includeFilters ?? Array.Empty(), filter => _logger.LogInformation($"Included module filter '{filter}'")); + Array.ForEach(excludes ?? Array.Empty(), filter => _logger.LogInformation($"Excluded source files '{filter}'")); + _excludeFilters = _excludeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray(); _includeFilters = _includeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray(); @@ -69,9 +74,12 @@ public void PrepareModules() { 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); @@ -81,6 +89,7 @@ public void PrepareModules() { var result = instrumenter.Instrument(); _results.Add(result); + _logger.LogInformation($"Instrumented module: '{module}'"); } catch (Exception ex) { @@ -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; } diff --git a/src/coverlet.core/Instrumentation/Instrumenter.cs b/src/coverlet.core/Instrumentation/Instrumenter.cs index 2bece3356..b1cc157b0 100644 --- a/src/coverlet.core/Instrumentation/Instrumenter.cs +++ b/src/coverlet.core/Instrumentation/Instrumenter.cs @@ -7,6 +7,7 @@ using Coverlet.Core.Attributes; using Coverlet.Core.Helpers; +using Coverlet.Core.Logging; using Coverlet.Core.Symbols; using Mono.Cecil; @@ -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; @@ -35,7 +37,7 @@ internal class Instrumenter private MethodReference _customTrackerRecordHitMethod; private List _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; @@ -44,8 +46,8 @@ public Instrumenter(string module, string identifier, string[] excludeFilters, s _excludedFiles = excludedFiles ?? Array.Empty(); _excludedAttributes = excludedAttributes; _singleHit = singleHit; - _isCoreLibrary = Path.GetFileNameWithoutExtension(_module) == "System.Private.CoreLib"; + _logger = logger; } public bool CanInstrument() => InstrumentationHelper.HasPdb(_module); @@ -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) diff --git a/src/coverlet.msbuild.tasks/MSBuildLogger.cs b/src/coverlet.msbuild.tasks/MSBuildLogger.cs index a09506540..414ad86e9 100644 --- a/src/coverlet.msbuild.tasks/MSBuildLogger.cs +++ b/src/coverlet.msbuild.tasks/MSBuildLogger.cs @@ -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); } } diff --git a/test/coverlet.core.tests/CoverageTests.cs b/test/coverlet.core.tests/CoverageTests.cs index d1a2cc84e..1af091193 100644 --- a/test/coverlet.core.tests/CoverageTests.cs +++ b/test/coverlet.core.tests/CoverageTests.cs @@ -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(); - // TODO: Find a way to mimick hits - var coverage = new Coverage(Path.Combine(directory.FullName, Path.GetFileName(module)), Array.Empty(), Array.Empty(), Array.Empty(), Array.Empty(), Array.Empty(), false, string.Empty, false, logger); + var coverage = new Coverage(Path.Combine(directory.FullName, Path.GetFileName(module)), Array.Empty(), Array.Empty(), Array.Empty(), Array.Empty(), Array.Empty(), false, string.Empty, false, new Mock().Object); coverage.PrepareModules(); var result = coverage.GetCoverageResult(); diff --git a/test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs b/test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs index e67bc2084..efe965e6c 100644 --- a/test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs +++ b/test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs @@ -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 { @@ -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(), Array.Empty(), Array.Empty(), Array.Empty(), false); + Instrumenter instrumenter = new Instrumenter(Path.Combine(TestFilesDir, files[0]), "_coverlet_instrumented", Array.Empty(), Array.Empty(), Array.Empty(), Array.Empty(), false, new Mock().Object); Assert.True(instrumenter.CanInstrument()); var result = instrumenter.Instrument(); Assert.NotNull(result); @@ -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(), Array.Empty(), Array.Empty(), attributesToIgnore, false); + Instrumenter instrumenter = new Instrumenter(module, identifier, Array.Empty(), Array.Empty(), Array.Empty(), attributesToIgnore, false, new Mock().Object); return new InstrumenterTest { Instrumenter = instrumenter,