diff --git a/src/coverlet.core/Instrumentation/Instrumenter.cs b/src/coverlet.core/Instrumentation/Instrumenter.cs index 575be13be..e341fb9c5 100644 --- a/src/coverlet.core/Instrumentation/Instrumenter.cs +++ b/src/coverlet.core/Instrumentation/Instrumenter.cs @@ -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); } diff --git a/test/coverlet.core.tests/Coverage/CoverageTests.ExcludeFromCoverageAttribute.cs b/test/coverlet.core.tests/Coverage/CoverageTests.ExcludeFromCoverageAttribute.cs index b143e4222..6f75f76da 100644 --- a/test/coverlet.core.tests/Coverage/CoverageTests.ExcludeFromCoverageAttribute.cs +++ b/test/coverlet.core.tests/Coverage/CoverageTests.ExcludeFromCoverageAttribute.cs @@ -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 @@ -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(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(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); + } + } } } \ No newline at end of file diff --git a/test/coverlet.core.tests/Samples/Instrumentation.ExcludeFromCoverage.cs b/test/coverlet.core.tests/Samples/Instrumentation.ExcludeFromCoverage.cs index 72b6498c7..46151d572 100644 --- a/test/coverlet.core.tests/Samples/Instrumentation.ExcludeFromCoverage.cs +++ b/test/coverlet.core.tests/Samples/Instrumentation.ExcludeFromCoverage.cs @@ -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 @@ -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; + } } \ No newline at end of file