Skip to content

#if'ed the definition of ExcludeFromCodeCoverageAttribute depending o… #2332

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 1 commit into from
Jun 26, 2020

Conversation

odalet
Copy link
Contributor

@odalet odalet commented Jun 20, 2020

…n the target framework

The ExcludeFromCodeCoverageAttribute is not supported on some older .NET framework targets:

  • Classic framework < v4.0
  • .NET Core < v2.0
  • .NET Standard < 2.0

In order for projects targeting some of these frameworks to be able to use GitVersion, ExcludeFromCodeCoverageAttribute needs to be defined conditionally when targeting these frameworks.

This is exactly what this PR does.

This PR fixes #2330.

I used a side-project to ensure my changes were correct (see https://github.com/odalet/GitVersionTests). Unfortunately I wasn't able to enforce these tests with proper unit tests (I think it is not a trivial task to be able to ensure correct compilation of projects across all framework x language combinations).

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@odalet
Copy link
Contributor Author

odalet commented Jun 20, 2020

There's still this dotnet format issue, but this time, it complains about AddFormats/GitVersionInformation.cs which is the template that generates properties. It says there are whitespace issues in locations that either look ok or are not whitespaces at all...

Would the fact that this is not a real C# file bother the format tool?

Maybe the templates should be excluded from the analysis?

What's weird too is that dotnet format does not complain on my machine (Windows)

@asbjornu
Copy link
Member

Maybe the templates should be excluded from the analysis?

Yep, that sounds like a good idea. I don't know dotnet format well enough to fix that just yet, though.

#define SHOULD_DEFINE_EXCLUDE_FROM_CODE_COVERAGE
#elif NETSTANDARD1_0 || NETSTANDARD1_1 || NETSTANDARD1_2 || NETSTANDARD1_3 || NETSTANDARD1_4 || NETSTANDARD1_5 || NETSTANDARD1_6
#define SHOULD_DEFINE_EXCLUDE_FROM_CODE_COVERAGE
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why all these #elif's? Couldn't one big #if cover all targets?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just an attempt to split the very long #if line... I did something similar in the VB file, but couldn't do it in F#.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

@@ -8,6 +8,22 @@
// </auto-generated>
//------------------------------------------------------------------------------

#if NET20 || NET35 || NETCOREAPP1_0 || NETCOREAPP1_1 || NETSTANDARD1_0 || NETSTANDARD1_1 || NETSTANDARD1_2 || NETSTANDARD1_3 || NETSTANDARD1_4 || NETSTANDARD1_5 || NETSTANDARD1_6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't SHOULD_DEFINE_EXCLUDE_FROM_CODE_COVERAGE be defined in F# as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no preprocessor instruction in F# that allows defining a constant (when you need one you have to pass it to the compiler with a -d argument). And the SHOULD_DEFINE_EXCLUDE_FROM_CODE_COVERAGE constant has no special meaning, it's just used to split the #if as I said above.

I agree this is not consistant. If you prefer, I can have everything look more like the F# version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer consistency. Hopefully, we can replace these #if statements with a check before the files are generated in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully, we can replace these #if statements with a check before the files are generated in the first place.

I suppose you are referring to a solution similar to what was suggested in the issue: having the support of the attribute detected by MSBuild? That would probably work but:

  • I'm not that familiar (yet) with how MSBuild code works in GitVersion,
  • Will you ever want to support generation of GitVersionInformation class from the command-line utility? If so, I think we can't rely on constants defined in a MSBuild file that would not exist in this case.

Anyway, I don't have clear ideas to how we can reliably get rid of this ugly #if, but in the meantime and if you're ok with this. I'll simply have everything look more like the F# version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose you are referring to a solution similar to what was suggested in the issue: having the support of the attribute detected by MSBuild?

Correct. I agree that the solution is difficult, though, which is why I think we should go ahead with this #if-based solution first.

Anyway, I don't have clear ideas to how we can reliably get rid of this ugly #if, but in the meantime and if you're ok with this. I'll simply have everything look more like the F# version.

👍

#define SHOULD_DEFINE_EXCLUDE_FROM_CODE_COVERAGE
#elif NETSTANDARD1_0 || NETSTANDARD1_1 || NETSTANDARD1_2 || NETSTANDARD1_3 || NETSTANDARD1_4 || NETSTANDARD1_5 || NETSTANDARD1_6
#define SHOULD_DEFINE_EXCLUDE_FROM_CODE_COVERAGE
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

@@ -8,6 +8,22 @@
// </auto-generated>
//------------------------------------------------------------------------------

#if NET20 || NET35 || NETCOREAPP1_0 || NETCOREAPP1_1 || NETSTANDARD1_0 || NETSTANDARD1_1 || NETSTANDARD1_2 || NETSTANDARD1_3 || NETSTANDARD1_4 || NETSTANDARD1_5 || NETSTANDARD1_6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer consistency. Hopefully, we can replace these #if statements with a check before the files are generated in the first place.

@odalet odalet force-pushed the hotfix/fix-2330 branch from f1b8fcb to 6a72535 Compare June 26, 2020 14:50
@odalet odalet force-pushed the hotfix/fix-2330 branch from 6a72535 to 2246945 Compare June 26, 2020 15:08
@asbjornu asbjornu merged commit 59b5567 into GitTools:master Jun 26, 2020
@asbjornu
Copy link
Member

Brilliant, thank you so much @odalet! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] GitVersionInformation.g.cs(12,42): error CS0234 'ExcludeFromCodeCoverageAttribute' does not exist
2 participants