From a014bf0cd0fdb5a65a24df393d254ae98f7f45f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller?= Date: Tue, 24 Jan 2023 07:41:11 +0100 Subject: [PATCH] Path is empty for VB My namespace (#1073) Path is empty for VB My namespace --- Documentation/Changelog.md | 1 + coverlet.sln | 7 ++++ .../Instrumentation/Instrumenter.cs | 3 ++ .../Instrumentation/InstrumenterTests.cs | 42 +++++++++++++++---- .../coverlet.core.tests.csproj | 1 + .../SampleVbClass.vb | 5 +++ ...t.tests.projectsample.vbmynamespace.vbproj | 9 ++++ 7 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 test/coverlet.tests.projectsample.vbmynamespace/SampleVbClass.vb create mode 100644 test/coverlet.tests.projectsample.vbmynamespace/coverlet.tests.projectsample.vbmynamespace.vbproj diff --git a/Documentation/Changelog.md b/Documentation/Changelog.md index d0dcaae50..3cf3a8d9c 100644 --- a/Documentation/Changelog.md +++ b/Documentation/Changelog.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -Incorrect coverage for methods returning IAsyncEnumerable in generic classes [#1383](https://github.com/coverlet-coverage/coverlet/issues/1383) -Wrong branch coverage for async methods .NET Standard 1.x [#1376](https://github.com/coverlet-coverage/coverlet/issues/1376) +-Empty path exception in visual basic projects [#775](https://github.com/coverlet-coverage/coverlet/issues/775) -Allign published nuget package version to github release version [#1413](https://github.com/coverlet-coverage/coverlet/issues/1413) -Sync nuget and github release versions [#1122](https://github.com/coverlet-coverage/coverlet/issues/1122) diff --git a/coverlet.sln b/coverlet.sln index b5e470431..efccfa31f 100644 --- a/coverlet.sln +++ b/coverlet.sln @@ -54,6 +54,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{9A8B19D4 test\Directory.Build.targets = test\Directory.Build.targets EndProjectSection EndProject +Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "coverlet.tests.projectsample.vbmynamespace", "test\coverlet.tests.projectsample.vbmynamespace\coverlet.tests.projectsample.vbmynamespace.vbproj", "{C9B7DC34-3E04-4F20-AED4-73791AF8020D}" +EndProject Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "coverlet.tests.projectsample.fsharp", "test\coverlet.tests.projectsample.fsharp\coverlet.tests.projectsample.fsharp.fsproj", "{1CBF6966-2A67-4D2C-8598-D174B83072F4}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "coverlet.tests.projectsample.netframework", "test\coverlet.tests.projectsample.netframework\coverlet.tests.projectsample.netframework.csproj", "{E69D68C9-78ED-4076-A14B-D07295A4B2A5}" @@ -120,6 +122,10 @@ Global {F8199E19-FA9A-4559-9101-CAD7028121B4}.Debug|Any CPU.Build.0 = Debug|Any CPU {F8199E19-FA9A-4559-9101-CAD7028121B4}.Release|Any CPU.ActiveCfg = Release|Any CPU {F8199E19-FA9A-4559-9101-CAD7028121B4}.Release|Any CPU.Build.0 = Release|Any CPU + {C9B7DC34-3E04-4F20-AED4-73791AF8020D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C9B7DC34-3E04-4F20-AED4-73791AF8020D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C9B7DC34-3E04-4F20-AED4-73791AF8020D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C9B7DC34-3E04-4F20-AED4-73791AF8020D}.Release|Any CPU.Build.0 = Release|Any CPU {1CBF6966-2A67-4D2C-8598-D174B83072F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {1CBF6966-2A67-4D2C-8598-D174B83072F4}.Debug|Any CPU.Build.0 = Debug|Any CPU {1CBF6966-2A67-4D2C-8598-D174B83072F4}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -150,6 +156,7 @@ Global {9A8B19D4-4A24-4217-AEFE-159B68F029A1} = {2FEBDE1B-83E3-445B-B9F8-5644B0E0E134} {1CBF6966-2A67-4D2C-8598-D174B83072F4} = {2FEBDE1B-83E3-445B-B9F8-5644B0E0E134} {E69D68C9-78ED-4076-A14B-D07295A4B2A5} = {2FEBDE1B-83E3-445B-B9F8-5644B0E0E134} + {C9B7DC34-3E04-4F20-AED4-73791AF8020D} = {2FEBDE1B-83E3-445B-B9F8-5644B0E0E134} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {9CA57C02-97B0-4C38-A027-EA61E8741F10} diff --git a/src/coverlet.core/Instrumentation/Instrumenter.cs b/src/coverlet.core/Instrumentation/Instrumenter.cs index 502a356e0..df43496c9 100644 --- a/src/coverlet.core/Instrumentation/Instrumenter.cs +++ b/src/coverlet.core/Instrumentation/Instrumenter.cs @@ -531,6 +531,9 @@ private void InstrumentType(TypeDefinition type) private void InstrumentMethod(MethodDefinition method) { string sourceFile = method.DebugInformation.SequencePoints.Select(s => _sourceRootTranslator.ResolveFilePath(s.Document.Url)).FirstOrDefault(); + + if (string.IsNullOrEmpty(sourceFile)) return; + if (!string.IsNullOrEmpty(sourceFile) && _excludedFilesHelper.Exclude(sourceFile)) { if (!(_excludedSourceFiles ??= new List()).Contains(sourceFile)) diff --git a/test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs b/test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs index 4dc155b18..39d5d06d3 100644 --- a/test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs +++ b/test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs @@ -615,15 +615,8 @@ public int SampleMethod() var instrumenter = new Instrumenter(excludedbyattributeDll, "_xunit_excludedbyattribute", parametes, loggerMock.Object, instrumentationHelper, partialMockFileSystem.Object, new SourceRootTranslator(loggerMock.Object, new FileSystem()), new CecilSymbolHelper()); InstrumenterResult result = instrumenter.Instrument(); - if (expectedExcludes) - { - Assert.Empty(result.Documents); - loggerMock.Verify(l => l.LogVerbose(It.IsAny())); - } - else - { - Assert.NotEmpty(result.Documents); - } + Assert.Empty(result.Documents); + if (expectedExcludes) { loggerMock.Verify(l => l.LogVerbose(It.IsAny())); } } [Fact] @@ -807,5 +800,36 @@ public void TestReachabilityHelper() instrumenterTest.Directory.Delete(true); } + + [Fact] + public void Instrumenter_MethodsWithoutReferenceToSource_AreSkipped() + { + var loggerMock = new Mock(); + + string module = Directory.GetFiles(Directory.GetCurrentDirectory(), "coverlet.tests.projectsample.vbmynamespace.dll").First(); + string pdb = Path.Combine(Path.GetDirectoryName(module), Path.GetFileNameWithoutExtension(module) + ".pdb"); + + DirectoryInfo directory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())); + + File.Copy(module, Path.Combine(directory.FullName, Path.GetFileName(module)), true); + File.Copy(pdb, Path.Combine(directory.FullName, Path.GetFileName(pdb)), true); + + var instrumentationHelper = + new InstrumentationHelper(new ProcessExitHandler(), new RetryHelper(), new FileSystem(), new Mock().Object, + new SourceRootTranslator(module, new Mock().Object, new FileSystem())); + + CoverageParameters parameters = new(); + + var instrumenter = new Instrumenter(Path.Combine(directory.FullName, Path.GetFileName(module)), "_coverlet_tests_projectsample_vbmynamespace", parameters, + loggerMock.Object, instrumentationHelper, new FileSystem(), new SourceRootTranslator(Path.Combine(directory.FullName, Path.GetFileName(module)), loggerMock.Object, new FileSystem()), new CecilSymbolHelper()); + + instrumentationHelper.BackupOriginalModule(Path.Combine(directory.FullName, Path.GetFileName(module)), "_coverlet_tests_projectsample_vbmynamespace"); + + InstrumenterResult result = instrumenter.Instrument(); + + Assert.False(result.Documents.ContainsKey(string.Empty)); + + directory.Delete(true); + } } } diff --git a/test/coverlet.core.tests/coverlet.core.tests.csproj b/test/coverlet.core.tests/coverlet.core.tests.csproj index d5fdc3257..f54c85f67 100644 --- a/test/coverlet.core.tests/coverlet.core.tests.csproj +++ b/test/coverlet.core.tests/coverlet.core.tests.csproj @@ -29,6 +29,7 @@ + diff --git a/test/coverlet.tests.projectsample.vbmynamespace/SampleVbClass.vb b/test/coverlet.tests.projectsample.vbmynamespace/SampleVbClass.vb new file mode 100644 index 000000000..bce7be710 --- /dev/null +++ b/test/coverlet.tests.projectsample.vbmynamespace/SampleVbClass.vb @@ -0,0 +1,5 @@ +Public Class SampleVbClass + Sub SampleSub() + Return + End Sub +End Class diff --git a/test/coverlet.tests.projectsample.vbmynamespace/coverlet.tests.projectsample.vbmynamespace.vbproj b/test/coverlet.tests.projectsample.vbmynamespace/coverlet.tests.projectsample.vbmynamespace.vbproj new file mode 100644 index 000000000..3b626df5a --- /dev/null +++ b/test/coverlet.tests.projectsample.vbmynamespace/coverlet.tests.projectsample.vbmynamespace.vbproj @@ -0,0 +1,9 @@ + + + + coverlet.tests.projectsample.vbmynamespace + net48 + latest + + + \ No newline at end of file