From 11c15b59eba109f2333254773f82bb3137214e22 Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Wed, 27 Feb 2019 15:26:36 +0100 Subject: [PATCH 1/4] add some logs --- build.proj | 2 +- src/coverlet.core/Coverage.cs | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) 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..9f2712ce9 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,10 @@ public void PrepareModules() { string[] modules = InstrumentationHelper.GetCoverableModules(_module, _includeDirectories); string[] excludes = InstrumentationHelper.GetExcludedFiles(_excludedSourceFiles); + + Array.ForEach(_excludeFilters ?? Array.Empty(), filter => _logger.LogInformation($"Exclude filter '{filter}'")); + Array.ForEach(_includeFilters ?? Array.Empty(), filter => _logger.LogInformation($"Include filter '{filter}'")); + _excludeFilters = _excludeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray(); _includeFilters = _includeFilters?.Where(f => InstrumentationHelper.IsValidFilterExpression(f)).ToArray(); @@ -69,7 +73,10 @@ 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); if (instrumenter.CanInstrument()) @@ -81,6 +88,7 @@ public void PrepareModules() { var result = instrumenter.Instrument(); _results.Add(result); + _logger.LogInformation($"Instrumented module: '{module}'"); } catch (Exception ex) { @@ -197,7 +205,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; } From 1ec4bed404207c1b0c636aacd57d875e0787f31b Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Wed, 27 Feb 2019 15:41:39 +0100 Subject: [PATCH 2/4] updates --- src/coverlet.core/Coverage.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/coverlet.core/Coverage.cs b/src/coverlet.core/Coverage.cs index 9f2712ce9..f620176fb 100644 --- a/src/coverlet.core/Coverage.cs +++ b/src/coverlet.core/Coverage.cs @@ -63,8 +63,9 @@ public void PrepareModules() string[] modules = InstrumentationHelper.GetCoverableModules(_module, _includeDirectories); string[] excludes = InstrumentationHelper.GetExcludedFiles(_excludedSourceFiles); - Array.ForEach(_excludeFilters ?? Array.Empty(), filter => _logger.LogInformation($"Exclude filter '{filter}'")); - Array.ForEach(_includeFilters ?? Array.Empty(), filter => _logger.LogInformation($"Include filter '{filter}'")); + 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(); From 69bed5a655122847c2f8c65a4b672a17f0306c9f Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Thu, 28 Feb 2019 12:22:52 +0100 Subject: [PATCH 3/4] add [coverlet] prefix to logs --- src/coverlet.core/Coverage.cs | 2 +- src/coverlet.core/Instrumentation/Instrumenter.cs | 9 +++++++-- src/coverlet.msbuild.tasks/MSBuildLogger.cs | 12 +++++++----- test/coverlet.core.tests/CoverageTests.cs | 4 +--- .../Instrumentation/InstrumenterTests.cs | 8 +++++--- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/coverlet.core/Coverage.cs b/src/coverlet.core/Coverage.cs index f620176fb..8ba48df8d 100644 --- a/src/coverlet.core/Coverage.cs +++ b/src/coverlet.core/Coverage.cs @@ -79,7 +79,7 @@ public void PrepareModules() 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); 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..bc410ddea 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, From 19ac4f709c5d62933de4317337fabea071005d15 Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Thu, 28 Feb 2019 12:29:51 +0100 Subject: [PATCH 4/4] address PR feedback --- src/coverlet.msbuild.tasks/MSBuildLogger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coverlet.msbuild.tasks/MSBuildLogger.cs b/src/coverlet.msbuild.tasks/MSBuildLogger.cs index bc410ddea..414ad86e9 100644 --- a/src/coverlet.msbuild.tasks/MSBuildLogger.cs +++ b/src/coverlet.msbuild.tasks/MSBuildLogger.cs @@ -7,7 +7,7 @@ namespace Coverlet.MSbuild.Tasks { class MSBuildLogger : ILogger { - private const string LogPrefix = "[coverlet]"; + private const string LogPrefix = "[coverlet] "; private readonly TaskLoggingHelper _log;