Skip to content
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

Fix property attribute detection #477

Merged
merged 3 commits into from
Jul 3, 2019
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
10 changes: 9 additions & 1 deletion src/coverlet.core/Instrumentation/Instrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,18 @@ private void InstrumentType(TypeDefinition type)
foreach (var method in methods)
{
MethodDefinition actualMethod = method;
IEnumerable<CustomAttribute> customAttributes = method.CustomAttributes;
if (InstrumentationHelper.IsLocalMethod(method.Name))
actualMethod = methods.FirstOrDefault(m => m.Name == method.Name.Split('>')[0].Substring(1)) ?? method;

if (!actualMethod.CustomAttributes.Any(IsExcludeAttribute))
if (actualMethod.IsGetter || actualMethod.IsSetter)
{
PropertyDefinition prop = type.Properties.FirstOrDefault(p => p.GetMethod.FullName.Equals(actualMethod.FullName));
if (prop?.HasCustomAttributes == true)
customAttributes = customAttributes.Union(prop.CustomAttributes);
}

if (!customAttributes.Any(IsExcludeAttribute))
InstrumentMethod(method);
}

Expand Down
42 changes: 41 additions & 1 deletion test/coverlet.core.tests/Instrumentation/InstrumenterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public void TestInstrument_ClassesWithExcludeAttributeAreExcluded(Type excludedT
instrumenterTest.Directory.Delete(true);
}


[Theory]
[InlineData(nameof(ObsoleteAttribute))]
[InlineData("Obsolete")]
Expand All @@ -103,6 +102,47 @@ public void TestInstrument_ClassesWithCustomExcludeAttributeAreExcluded(string e
instrumenterTest.Directory.Delete(true);
}

[Theory]
[InlineData(nameof(ObsoleteAttribute))]
[InlineData("Obsolete")]
public void TestInstrument_ClassesWithMethodWithCustomExcludeAttributeAreExcluded(string excludedAttribute)
{
var instrumenterTest = CreateInstrumentor(attributesToIgnore: new string[] { excludedAttribute });
var result = instrumenterTest.Instrumenter.Instrument();

var doc = result.Documents.Values.FirstOrDefault(d => Path.GetFileName(d.Path) == "Samples.cs");
Assert.NotNull(doc);
#pragma warning disable CS0612 // Type or member is obsolete
var found = doc.Lines.Values.Any(l => l.Method.Equals("System.String Coverlet.Core.Samples.Tests.ClassWithMethodExcludedByObsoleteAttr::Method(System.String)"));
#pragma warning restore CS0612 // Type or member is obsolete
Assert.False(found, "Method decorated with with exclude attribute should be excluded");

instrumenterTest.Directory.Delete(true);
}

[Theory]
[InlineData(nameof(ObsoleteAttribute))]
[InlineData("Obsolete")]
public void TestInstrument_ClassesWithPropertyWithCustomExcludeAttributeAreExcluded(string excludedAttribute)
{
var instrumenterTest = CreateInstrumentor(attributesToIgnore: new string[] { excludedAttribute });
var result = instrumenterTest.Instrumenter.Instrument();

var doc = result.Documents.Values.FirstOrDefault(d => Path.GetFileName(d.Path) == "Samples.cs");
Assert.NotNull(doc);
#pragma warning disable CS0612 // Type or member is obsolete
var getFound = doc.Lines.Values.Any(l => l.Method.Equals("System.String Coverlet.Core.Samples.Tests.ClassWithPropertyExcludedByObsoleteAttr::get_Property()"));
#pragma warning restore CS0612 // Type or member is obsolete
Assert.False(getFound, "Property getter decorated with with exclude attribute should be excluded");

#pragma warning disable CS0612 // Type or member is obsolete
var setFound = doc.Lines.Values.Any(l => l.Method.Equals("System.String Coverlet.Core.Samples.Tests.ClassWithPropertyExcludedByObsoleteAttr::set_Property()"));
#pragma warning restore CS0612 // Type or member is obsolete
Assert.False(setFound, "Property setter decorated with with exclude attribute should be excluded");

instrumenterTest.Directory.Delete(true);
}

private InstrumenterTest CreateInstrumentor(bool fakeCoreLibModule = false, string[] attributesToIgnore = null)
{
string module = GetType().Assembly.Location;
Expand Down
18 changes: 17 additions & 1 deletion test/coverlet.core.tests/Samples/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ public string Method(string input)
[ExcludeFromCodeCoverage]
public class ClassExcludedByCodeAnalysisCodeCoverageAttr
{

public string Method(string input)
{
if (string.IsNullOrEmpty(input))
Expand All @@ -201,7 +200,18 @@ public string Method(string input)
[Obsolete]
public class ClassExcludedByObsoleteAttr
{
public string Method(string input)
{
if (string.IsNullOrEmpty(input))
throw new ArgumentException("Cannot be empty", nameof(input));

return input;
}
}

public class ClassWithMethodExcludedByObsoleteAttr
{
[Obsolete]
public string Method(string input)
{
if (string.IsNullOrEmpty(input))
Expand All @@ -211,6 +221,12 @@ public string Method(string input)
}
}

public class ClassWithPropertyExcludedByObsoleteAttr
{
[Obsolete]
public string Property { get; set; }
}

public class ExceptionFilter
{
public void Test()
Expand Down