Skip to content

Fix ExcludeFromCodeCoverage on props #1114

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
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
3 changes: 2 additions & 1 deletion src/coverlet.core/Instrumentation/Instrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ private void InstrumentType(TypeDefinition type)
continue;
}

PropertyDefinition prop = type.Properties.FirstOrDefault(p => (p.GetMethod ?? p.SetMethod).FullName.Equals(actualMethod.FullName));
PropertyDefinition prop = type.Properties.FirstOrDefault(p => p.GetMethod?.FullName.Equals(actualMethod.FullName) == true ||
p.SetMethod?.FullName.Equals(actualMethod.FullName) == true);
if (prop?.HasCustomAttributes == true)
customAttributes = customAttributes.Union(prop.CustomAttributes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ public void ExcludeFromCodeCoverageNextedTypes()
}, new string[] { path });

TestInstrumentationHelper.GetCoverageResult(path)
.GenerateReport(show:true)
.Document("Instrumentation.ExcludeFromCoverage.cs")
.AssertLinesCovered(BuildConfiguration.Debug, (143, 1))
.AssertLinesCovered(BuildConfiguration.Debug, (145, 1))
.AssertNonInstrumentedLines(BuildConfiguration.Debug, 146, 160);
}
finally
Expand Down Expand Up @@ -218,5 +219,63 @@ public void ExcludeFromCodeCoverage_Issue809()
File.Delete(path);
}
}

[Fact]
public void ExcludeFromCodeCoverageAutoGeneratedGetSet()
{
string path = Path.GetTempFileName();
try
{
FunctionExecutor.Run(async (string[] pathSerialize) =>
{
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<AutoGeneneratedGetSet>(instance =>
{
instance.SetId(10);
Assert.Equal(10, instance.Id);
return Task.CompletedTask;
}, persistPrepareResultToFile: pathSerialize[0]);

return 0;
}, new string[] { path });

TestInstrumentationHelper.GetCoverageResult(path)
.Document("Instrumentation.ExcludeFromCoverage.cs")
.AssertNonInstrumentedLines(BuildConfiguration.Debug, 167)
.AssertLinesCovered(BuildConfiguration.Debug, 169);
}
finally
{
File.Delete(path);
}
}

[Fact]
public void ExcludeFromCodeCoverageAutoGeneratedGet()
{
string path = Path.GetTempFileName();
try
{
FunctionExecutor.Run(async (string[] pathSerialize) =>
{
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<AutoGeneneratedGetOnly>(instance =>
{
instance.SetId(10);
Assert.Equal(10, instance.Id);
return Task.CompletedTask;
}, persistPrepareResultToFile: pathSerialize[0]);

return 0;
}, new string[] { path });

TestInstrumentationHelper.GetCoverageResult(path)
.Document("Instrumentation.ExcludeFromCoverage.cs")
.AssertNonInstrumentedLines(BuildConfiguration.Debug, 177)
.AssertLinesCovered(BuildConfiguration.Debug, 178, 181);
}
finally
{
File.Delete(path);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Remember to use full name because adding new using directives change line numbers

using System.Data;

namespace Coverlet.Core.Samples.Tests
{
public class MethodsWithExcludeFromCodeCoverageAttr
Expand Down Expand Up @@ -158,4 +160,24 @@ public class ExcludeFromCoverageAttrFilterClass4
}
}
}

public class AutoGeneneratedGetSet
{
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int Id { get; set; }

public void SetId(int value) => Id = value;
}

public class AutoGeneneratedGetOnly
{
public int Id
{
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
get;
set;
}

public void SetId(int value) => Id = value;
}
}