diff --git a/Documentation/Changelog.md b/Documentation/Changelog.md index 45fdb19da..f82005c5d 100644 --- a/Documentation/Changelog.md +++ b/Documentation/Changelog.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased ### Fixed +-Uncovered lines in .NET 8 for inheriting records [#1555](https://github.com/coverlet-coverage/coverlet/issues/1555) +-Fix record constructors not covered when SkipAutoProps is true [#1561](https://github.com/coverlet-coverage/coverlet/issues/1561) -Fix ExcludeFromCodeCoverage does not exclude method in a partial class [#1548](https://github.com/coverlet-coverage/coverlet/issues/1548) -Fix ExcludeFromCodeCoverage does not exclude F# task [#1547](https://github.com/coverlet-coverage/coverlet/issues/1547) -Fix issues where ExcludeFromCodeCoverage ignored [#1431](https://github.com/coverlet-coverage/coverlet/issues/1431) diff --git a/src/coverlet.core/Instrumentation/Instrumenter.cs b/src/coverlet.core/Instrumentation/Instrumenter.cs index c16fa106e..509a992b0 100644 --- a/src/coverlet.core/Instrumentation/Instrumenter.cs +++ b/src/coverlet.core/Instrumentation/Instrumenter.cs @@ -470,7 +470,7 @@ private void InstrumentType(TypeDefinition type) if (actualMethod.IsGetter || actualMethod.IsSetter) { - if (_parameters.SkipAutoProps && actualMethod.CustomAttributes.Any(ca => ca.AttributeType.FullName == typeof(CompilerGeneratedAttribute).FullName)) + if (_parameters.SkipAutoProps && IsCompilerGenerated(actualMethod)) { continue; } @@ -507,13 +507,18 @@ private void InstrumentType(TypeDefinition type) IEnumerable ctors = type.GetConstructors(); foreach (MethodDefinition ctor in ctors) { - if (!ctor.CustomAttributes.Any(IsExcludeAttribute)) + if (!ctor.CustomAttributes.Any(IsExcludeAttribute) && !IsCompilerGenerated(ctor)) { InstrumentMethod(ctor); } } } + private static bool IsCompilerGenerated(IMemberDefinition member) + { + return member.CustomAttributes.Any(ca => ca.AttributeType.FullName == typeof(CompilerGeneratedAttribute).FullName); + } + private void InstrumentMethod(MethodDefinition method) { string sourceFile = method.DebugInformation.SequencePoints.Select(s => _sourceRootTranslator.ResolveFilePath(s.Document.Url)).FirstOrDefault(); @@ -679,7 +684,7 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor } ); - if (method.DeclaringType.CustomAttributes.Any(x => x.AttributeType.FullName == typeof(CompilerGeneratedAttribute).FullName)) + if (IsCompilerGenerated(method.DeclaringType)) { if (_branchesInCompiledGeneratedClass == null) { diff --git a/test/coverlet.core.tests/Coverage/CoverageTests.AutoProps.cs b/test/coverlet.core.tests/Coverage/CoverageTests.AutoProps.cs index b13799a40..6c7c56ab7 100644 --- a/test/coverlet.core.tests/Coverage/CoverageTests.AutoProps.cs +++ b/test/coverlet.core.tests/Coverage/CoverageTests.AutoProps.cs @@ -84,7 +84,7 @@ public void SkipAutoPropsInRecords(bool skipAutoProps) if (skipAutoProps) { - TestInstrumentationHelper.GetCoverageResult(path).GenerateReport(show: true) + TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AutoProps.cs") .AssertNonInstrumentedLines(BuildConfiguration.Debug, 23, 24) .AssertNonInstrumentedLines(BuildConfiguration.Release, 23, 24) @@ -106,7 +106,7 @@ public void SkipAutoPropsInRecords(bool skipAutoProps) } } - [Theory(Skip = "fails reason unknown (no session debug possible)")] + [Theory] [InlineData(true)] [InlineData(false)] public void SkipRecordWithProperties(bool skipAutoProps) @@ -116,7 +116,7 @@ public void SkipRecordWithProperties(bool skipAutoProps) { FunctionExecutor.Run(async (string[] parameters) => { - CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run(instance => + CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run(instance => { return Task.CompletedTask; }, @@ -133,14 +133,56 @@ public void SkipRecordWithProperties(bool skipAutoProps) .AssertNonInstrumentedLines(BuildConfiguration.Release, 29, 29) .AssertLinesCovered(BuildConfiguration.Debug, (32, 1), (33, 1), (34, 1)) .AssertLinesCovered(BuildConfiguration.Release, (33, 1)); - } else { TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AutoProps.cs") - .AssertLinesCovered(BuildConfiguration.Debug, (29, 3), (31, 1), (32, 1), (33, 1), (34, 1)) - .AssertLinesCovered(BuildConfiguration.Release, (29, 3), (31, 1), (33, 1)); + .AssertLinesCovered(BuildConfiguration.Debug, (29, 1), (31, 1), (32, 1), (33, 1), (34, 1)) + .AssertLinesCovered(BuildConfiguration.Release, (29, 1), (31, 1), (33, 1)); + } + } + finally + { + File.Delete(path); + } + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void SkipInheritingRecordsWithProperties(bool skipAutoProps) + { + string path = Path.GetTempFileName(); + try + { + FunctionExecutor.Run(async (string[] parameters) => + { + CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run(instance => + { + return Task.CompletedTask; + }, + persistPrepareResultToFile: parameters[0], skipAutoProps: bool.Parse(parameters[1])); + + return 0; + }, new string[] { path, skipAutoProps.ToString() }); + + if (skipAutoProps) + { + TestInstrumentationHelper.GetCoverageResult(path) + .Document("Instrumentation.AutoProps.cs") + .AssertNonInstrumentedLines(BuildConfiguration.Debug, 39, 39) + .AssertNonInstrumentedLines(BuildConfiguration.Release, 39, 39) + .AssertLinesCovered(BuildConfiguration.Debug, (41, 1), (44, 1), (45, 1), (46, 1)) + .AssertLinesCovered(BuildConfiguration.Release, (45, 1)); + + } + else + { + TestInstrumentationHelper.GetCoverageResult(path) + .Document("Instrumentation.AutoProps.cs") + .AssertLinesCovered(BuildConfiguration.Debug, (39, 1), (41, 1), (44, 1), (45, 1), (46, 1)) + .AssertLinesCovered(BuildConfiguration.Release, (39, 1), (41, 1), (45, 1)); } } finally diff --git a/test/coverlet.core.tests/Samples/Instrumentation.AutoProps.cs b/test/coverlet.core.tests/Samples/Instrumentation.AutoProps.cs index 852fcb182..abf2321ac 100644 --- a/test/coverlet.core.tests/Samples/Instrumentation.AutoProps.cs +++ b/test/coverlet.core.tests/Samples/Instrumentation.AutoProps.cs @@ -24,13 +24,27 @@ public RecordWithPropertyInit() public string RecordAutoPropsInit { get; set; } = string.Empty; } - public class ClassWithAutoRecordProperties + public class ClassWithRecordsAutoProperties { - record AutoRecordWithProperties(string Prop1, string Prop2); + record RecordWithPrimaryConstructor(string Prop1, string Prop2); - public ClassWithAutoRecordProperties() + public ClassWithRecordsAutoProperties() { - var record = new AutoRecordWithProperties(string.Empty, string.Empty); + var record = new RecordWithPrimaryConstructor(string.Empty, string.Empty); } } + + public class ClassWithInheritingRecordsAndAutoProperties + { + record BaseRecord(int A); + + record InheritedRecord(int A) : BaseRecord(A); + + public ClassWithInheritingRecordsAndAutoProperties() + { + var record = new InheritedRecord(1); + } + } + + }