Skip to content

Improve instrumentation tests #527

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
Show file tree
Hide file tree
Changes from 4 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
61 changes: 54 additions & 7 deletions test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -42,10 +43,27 @@ public void TestCoreLibInstrumentation()
Assert.NotNull(result);
}

[Fact]
public void TestInstrument()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void TestInstrument(bool singleHit)
{
var instrumenterTest = CreateInstrumentor();
var instrumenterTest = CreateInstrumentor(singleHit:singleHit);

var result = instrumenterTest.Instrumenter.Instrument();

Assert.Equal(Path.GetFileNameWithoutExtension(instrumenterTest.Module), result.Module);
Assert.Equal(instrumenterTest.Module, result.ModulePath);

instrumenterTest.Directory.Delete(true);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void TestInstrumentCoreLib(bool singleHit)
{
var instrumenterTest = CreateInstrumentor(fakeCoreLibModule: true, singleHit: singleHit);

var result = instrumenterTest.Instrumenter.Instrument();

Expand All @@ -56,9 +74,26 @@ public void TestInstrument()
}

[Fact]
public void TestInstrumentCoreLib()
public void TestInstrumentWithExcludedFiles()
{
var instrumenterTest = CreateInstrumentor(fakeCoreLibModule: true);
var samplesFound = false;
var directory = Directory.GetCurrentDirectory();
while (!samplesFound)
{
var parent = Directory.GetParent(directory);
var search = parent.GetDirectories("Samples", SearchOption.AllDirectories);
if (search.Length != 0)
{
directory = search.First().FullName;
samplesFound = true;
}
else
{
directory = parent.FullName;
}
}

var instrumenterTest = CreateInstrumentor(excludedFiles: new[] { $"{directory}\\Samples.cs" });

var result = instrumenterTest.Instrumenter.Instrument();

Expand Down Expand Up @@ -144,7 +179,7 @@ public void TestInstrument_ClassesWithPropertyWithCustomExcludeAttributeAreExclu
instrumenterTest.Directory.Delete(true);
}

private InstrumenterTest CreateInstrumentor(bool fakeCoreLibModule = false, string[] attributesToIgnore = null)
private InstrumenterTest CreateInstrumentor(bool fakeCoreLibModule = false, string[] attributesToIgnore = null, string[] excludedFiles = null, bool singleHit = false)
{
string module = GetType().Assembly.Location;
string pdb = Path.Combine(Path.GetDirectoryName(module), Path.GetFileNameWithoutExtension(module) + ".pdb");
Expand All @@ -168,7 +203,8 @@ 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, new Mock<ILogger>().Object);
Instrumenter instrumenter = new Instrumenter(module, identifier, Array.Empty<string>(), Array.Empty<string>(), excludedFiles, attributesToIgnore, singleHit, new Mock<ILogger>().Object);

return new InstrumenterTest
{
Instrumenter = instrumenter,
Expand All @@ -187,6 +223,8 @@ class InstrumenterTest
public string Identifier { get; set; }

public DirectoryInfo Directory { get; set; }

public Mock<ILogger> Logger { get; set; }
}

[Fact]
Expand Down Expand Up @@ -253,5 +291,14 @@ public void SkipEmbeddedPpdbWithoutLocalSource()
loggerMock.VerifyNoOtherCalls();
}

[Fact]
public void CanInstrumentReturnsFalseAndLogsCatchedException()
{
var loggerMock = new Mock<ILogger>();
var instrumenter = new Instrumenter("test", "_test_instrumented", Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), Array.Empty<string>(), false, loggerMock.Object);
Assert.False(instrumenter.CanInstrument());
loggerMock.Verify(l => l.LogWarning(It.IsAny<string>()));
}

}
}
15 changes: 15 additions & 0 deletions test/coverlet.core.tests/Samples/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Coverlet.Core.Attributes;

Expand Down Expand Up @@ -227,6 +229,19 @@ public class ClassWithPropertyExcludedByObsoleteAttr
public string Property { get; set; }
}

public class ClassWithSetterOnlyPropertyExcludedByObsoleteAttr
{
[Obsolete]
public string Property {
set => _ = string.Empty;
}
}

public abstract class ClassWithEmptyMethod
{
public abstract void EmptyMethod();
}

public class ExceptionFilter
{
public void Test()
Expand Down
44 changes: 22 additions & 22 deletions test/coverlet.core.tests/Symbols/CecilSymbolHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void GetBranchPoints_OneBranch()
Assert.Equal(points[0].Offset, points[1].Offset);
Assert.Equal(0, points[0].Path);
Assert.Equal(1, points[1].Path);
Assert.Equal(21, points[0].StartLine);
Assert.Equal(21, points[1].StartLine);
Assert.Equal(23, points[0].StartLine);
Assert.Equal(23, points[1].StartLine);
Assert.NotNull(points[1].Document);
Assert.Equal(points[0].Document, points[1].Document);
}
Expand Down Expand Up @@ -86,8 +86,8 @@ public void GetBranchPoints_TwoBranch()
Assert.Equal(4, points.Count());
Assert.Equal(points[0].Offset, points[1].Offset);
Assert.Equal(points[2].Offset, points[3].Offset);
Assert.Equal(27, points[0].StartLine);
Assert.Equal(28, points[2].StartLine);
Assert.Equal(29, points[0].StartLine);
Assert.Equal(30, points[2].StartLine);
}

[Fact]
Expand All @@ -104,8 +104,8 @@ public void GetBranchPoints_CompleteIf()
Assert.NotNull(points);
Assert.Equal(2, points.Count());
Assert.Equal(points[0].Offset, points[1].Offset);
Assert.Equal(34, points[0].StartLine);
Assert.Equal(34, points[1].StartLine);
Assert.Equal(36, points[0].StartLine);
Assert.Equal(36, points[1].StartLine);
}

#if !RELEASE // Issue https://github.com/tonerdo/coverlet/issues/389
Expand All @@ -126,10 +126,10 @@ public void GetBranchPoints_Switch()
Assert.Equal(points[0].Offset, points[2].Offset);
Assert.Equal(3, points[3].Path);

Assert.Equal(46, points[0].StartLine);
Assert.Equal(46, points[1].StartLine);
Assert.Equal(46, points[2].StartLine);
Assert.Equal(46, points[3].StartLine);
Assert.Equal(48, points[0].StartLine);
Assert.Equal(48, points[1].StartLine);
Assert.Equal(48, points[2].StartLine);
Assert.Equal(48, points[3].StartLine);
}

[Fact]
Expand All @@ -149,10 +149,10 @@ public void GetBranchPoints_SwitchWithDefault()
Assert.Equal(points[0].Offset, points[2].Offset);
Assert.Equal(3, points[3].Path);

Assert.Equal(60, points[0].StartLine);
Assert.Equal(60, points[1].StartLine);
Assert.Equal(60, points[2].StartLine);
Assert.Equal(60, points[3].StartLine);
Assert.Equal(62, points[0].StartLine);
Assert.Equal(62, points[1].StartLine);
Assert.Equal(62, points[2].StartLine);
Assert.Equal(62, points[3].StartLine);
}

[Fact]
Expand All @@ -172,10 +172,10 @@ public void GetBranchPoints_SwitchWithBreaks()
Assert.Equal(points[0].Offset, points[2].Offset);
Assert.Equal(3, points[3].Path);

Assert.Equal(76, points[0].StartLine);
Assert.Equal(76, points[1].StartLine);
Assert.Equal(76, points[2].StartLine);
Assert.Equal(76, points[3].StartLine);
Assert.Equal(78, points[0].StartLine);
Assert.Equal(78, points[1].StartLine);
Assert.Equal(78, points[2].StartLine);
Assert.Equal(78, points[3].StartLine);
}

[Fact]
Expand All @@ -196,10 +196,10 @@ public void GetBranchPoints_SwitchWithMultipleCases()
Assert.Equal(points[0].Offset, points[3].Offset);
Assert.Equal(3, points[3].Path);

Assert.Equal(94, points[0].StartLine);
Assert.Equal(94, points[1].StartLine);
Assert.Equal(94, points[2].StartLine);
Assert.Equal(94, points[3].StartLine);
Assert.Equal(96, points[0].StartLine);
Assert.Equal(96, points[1].StartLine);
Assert.Equal(96, points[2].StartLine);
Assert.Equal(96, points[3].StartLine);
}
#endif

Expand Down