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

ExcludeFromCodeCoverage not working for autogen getters and setters #269

Closed
pape77 opened this issue Dec 19, 2018 · 14 comments · Fixed by #1114
Closed

ExcludeFromCodeCoverage not working for autogen getters and setters #269

pape77 opened this issue Dec 19, 2018 · 14 comments · Fixed by #1114
Labels
bug Something isn't working tenet-coverage Issue related to possible incorrect coverage

Comments

@pape77
Copy link
Contributor

pape77 commented Dec 19, 2018

Noticed that when trying to apply the excludeFromCoverage attribute to getters and setters from a
class property, they are not taken into account

   public class Key
    {
       [ExcludeFromCodeCoverage]
        public int Id { get; set; }

        public Guid Uuid { get; set; }

        public Guid Kid { get; set; }
    }
"/build/src/Keys.Data/Entities/Key.cs": {
      "Keys.Data.Entities.Key": {
        "System.Int32 Keys.Data.Entities.Key::get_Id()": {
          "Lines": {
            "10": 0
          },

This can easily be seen in action in existing tests within coverlet. In InstrumenterTests.cs, in the TestInstrument_ClassesWithExcludeAttributeAreExcluded
Just remove the [ExcludeFromCodeCoverage] attribute from one of the classes that this method uses in Samples.cs and add a property like this Id one.

For example in Samples.cs change this class as follows:

    public class ClassExcludedByCoverletCodeCoverageAttr
    {

        public string Method(string input)
        {
            if(string.IsNullOrEmpty(input))
                throw new ArgumentException("Cannot be empty", nameof(input));

            return input;
        }

        [ExcludeFromCodeCoverage]
        public int Id { get; set; }
    }

Then you will that in method InstrumentType from Instrumenter.cs the attributes don't show up, because the method name is changed to get_Id() (autogenerated) and so it gets instrumented anyway.

Can we fix this somehow?

@pape77 pape77 changed the title ExcludeFromCodeCoverage not working for getters and setters ExcludeFromCodeCoverage not working for autogen getters and setters Dec 19, 2018
@pape77
Copy link
Contributor Author

pape77 commented Dec 19, 2018

Similar thing happens with async methods which are read like this (with MoveNext stuff):

"Keys.Data.Repositories.KeyRepository/<FindOrCreateAsync>d__6": {
        "System.Void Keys.Data.Repositories.KeyRepository/<FindOrCreateAsync>d__6::MoveNext()":
"Lines": {
            "55": 2,
            "56": 2,
            "58": 2,
  ...
}
}

and then all attributes are lost when Instrumenter.cs 's method is evauluating

@SlowLogicBoy
Copy link
Contributor

try:

public int MyProperty { [ExcludeFromCodeCoverage] get; [ExcludeFromCodeCoverage] set; }

@consultwithmike
Copy link

consultwithmike commented Dec 19, 2018

@SlowLogicBoy that workaround does work, but I'd say we need to get something in place that iterates the Properties collection in the Iterator as well so that we don't have to decorate the get and set, but rather the property itself.

Honestly, I don't even think we'd have to support properties, we could just add the get and set accessors to the methods listing if we see the property is excluded.

@pape77
Copy link
Contributor Author

pape77 commented Dec 19, 2018

Thank's for the hack @SlowLogicBoy
Indeed @consultwithmike that's what I expected to happen.
I'm guessing for the async things, a similar thing happens, cuz they are autogenerated with that name or something.
I looked into the Instrumenter.cs a bit, but got no clear solution for it

@MarcoRossignoli
Copy link
Collaborator

@pape77 is this issue still here?

@MarcoRossignoli
Copy link
Collaborator

Now we can use p:ExcludeByAttribute=\"CompilerGeneratedAttribute\" to skip autogen prop

@StingyJack
Copy link
Contributor

@SlowLogicBoy @MarcoRossignoli - this is still a problem for properties

image
image

Should this issue really be closed?

@MarcoRossignoli
Copy link
Collaborator

This issue is old can you open new one?
Which version are you using?

@StingyJack
Copy link
Contributor

I was going to but found that a similar issue was opened after this and was closed as a duplicate of this.

@StingyJack
Copy link
Contributor

@MarcoRossignoli As far as I can tell this was never actually fixed so closing this was not legitimate action. There is no PR or commit linked to this, just a comment for what looks like an msbuild property.

Besides like I said, other issues were opened for this same problem and were closed as duplicates of this.

@MarcoRossignoli
Copy link
Collaborator

MarcoRossignoli commented Mar 5, 2021

@StingyJack which version are you using?can you provide a repro?

Can you try also the new feature to skip autoprop?https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md#skip-auto-implemented-properties

@StingyJack
Copy link
Contributor

StingyJack commented Mar 6, 2021

Both versions 1.3.0 and 3.0.3 had the same problem.

I'm not invoking this by command line, another tool is doing the invocation that I don't have immediate control of.

The repro is to put a property in a class, run coverage, see that it's uncovered, then apply the excluded attribute on the property and rerun coverage. It still shows up as uncovered. The workaround of applying the attribute to each the get and set does work, but leaves messy code

EDIT: Perhaps an example can explain better.

//when using this, it reports the property as "not covered"
public class User 
{
    [ExcludeFromCodeCoverage] 
    public object Id { get;  set; }
...
}

//when using this, it reports the property as "covered", but seems like it should not be necessary.
public class User 
{
    public object Id { [ExcludeFromCodeCoverage] get; [ExcludeFromCodeCoverage] set; }
...
}

@StingyJack
Copy link
Contributor

Also I dont want to skip auto properties in the first place. Any property that is not listed as covered by a unit test is something that may be unused by the code and can be removed (YAGNI), or something I should have an assertion for in a unit test (and thus make covered). There are just some fields that are handled by an ORM or DB driver that I wouldn't unit test for, or temporary cases where the properties are needed by someone else's code that will be covering them later, but my PR needs to meet or exceed a coverage threshold to be merged.

Usages of [ExcludedFromCodeCoverage] for us need to be followed by a justification comment or it could block the changes from being merged into main. I think we may have to disable that if we cant follow the attribute with a justification comment, but I have to see if it will accept a /**/ style comment

@MarcoRossignoli
Copy link
Collaborator

Ok reopening for further investigation

@MarcoRossignoli MarcoRossignoli added tenet-coverage Issue related to possible incorrect coverage bug Something isn't working and removed needs more info More details are needed labels Mar 6, 2021
@MarcoRossignoli MarcoRossignoli pinned this issue Mar 6, 2021
@MarcoRossignoli MarcoRossignoli unpinned this issue Mar 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tenet-coverage Issue related to possible incorrect coverage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants