Skip to content

Commit

Permalink
Add ExcludeFromCodeCoverage to extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov authored and NikolayPianikov committed Jan 21, 2022
1 parent 9368e46 commit b14d836
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions Immutype.Tests/Integration/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Immutype.Tests.Integration
{
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

public static class TestExtensions
Expand Down
14 changes: 14 additions & 0 deletions Immutype/Core/ExtensionsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public IEnumerable<Source> Create(GenerationContext<TypeDeclarationSyntax> conte
.AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword), SyntaxFactory.Token(SyntaxKind.PartialKeyword))
.AddMembers(_methodsFactory.Create(context, typeSyntax, parameters).ToArray());

extensionsClass = TryAddAttribute(context.SemanticModel, extensionsClass, "System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage");

var code = CreateRootNode(typeDeclarationSyntax, AdditionalUsings, extensionsClass).NormalizeWhitespace().ToString();
var fileName = string.Join(".", ns.Select(i => i.Name.ToString()).Concat(new []{typeDeclarationSyntax.Identifier.Text}));
yield return new Source(fileName, SourceText.From(code, Encoding.UTF8));
Expand Down Expand Up @@ -82,5 +84,17 @@ private static UsingDirectiveSyntax[] GetUsings(IEnumerable<UsingDirectiveSyntax
var currentUsins = usings.Select(i => i.Name.ToString()).ToImmutableHashSet();
return additionalUsings.Where(i => !currentUsins.Contains(i.Name.ToString())).ToArray();
}

private static ClassDeclarationSyntax TryAddAttribute(SemanticModel semanticModel, ClassDeclarationSyntax classDeclarationSyntax, string attributeClassName)
{
var excludeFromCodeCoverageType = semanticModel.Compilation.GetTypeByMetadataName(attributeClassName + "Attribute");
if (excludeFromCodeCoverageType != default)
{
classDeclarationSyntax = classDeclarationSyntax
.AddAttributeLists(SyntaxFactory.AttributeList().AddAttributes(SyntaxFactory.Attribute(SyntaxFactory.IdentifierName(attributeClassName))));
}

return classDeclarationSyntax;
}
}
}

0 comments on commit b14d836

Please sign in to comment.