Skip to content

Commit

Permalink
Unit tested file exclusions with in-proc and out-of-proc analyzers.
Browse files Browse the repository at this point in the history
  • Loading branch information
realvizu committed Feb 2, 2019
1 parent 3fd86a8 commit 1a6b693
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace A
{
using B;

class MyClass3
{
private MyEnum e;
}
}
16 changes: 16 additions & 0 deletions source/NsDepCop.MsBuildTask.Test/ExcludedFiles/ExcludedByName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace A
{
using B;

class MyClass1
{
private MyEnum e;
}
}

namespace B
{
enum MyEnum
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace A
{
using B;

class MyClass2
{
private MyEnum e;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace A
{
using B;

class MyClass4
{
private MyEnum e;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace A
{
using B;

class MyClass5
{
private MyEnum e;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<NsDepCopConfig ExcludedFiles="ExcludedByName.cs, **\*.g.cs"/>
18 changes: 18 additions & 0 deletions source/NsDepCop.MsBuildTask.Test/NsDepCop.MsBuildTask.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,22 @@
<Content Include="Serialization\Serialization.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="ExcludedFiles\Subfolder1\ExcludedByExtensions2.g.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="ExcludedFiles\Subfolder1\Sub folder 2\ExcludedByExtensions3.g.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="ExcludedFiles\ExcludedByExtensions1.g.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Compile Include="MsBuildLoggerGatewayTests.cs" />
<Content Include="ExcludedFiles\ExcludedByName.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="ExcludedFiles\IncludedFile.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Compile Include="NsDepCopTaskTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Content Include="DependencyViolation_ReportWarning\DependencyViolation_ReportWarning.cs">
Expand Down Expand Up @@ -293,6 +308,9 @@
<Content Include="TaskInfoImportanceIsLow\config.nsdepcop">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="ExcludedFiles\config.nsdepcop">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
Expand Down
27 changes: 25 additions & 2 deletions source/NsDepCop.MsBuildTask.Test/NsDepCopTaskTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,33 @@ public void MultipleSourceFiles()
var nsDepCopTask = CreateNsDepCopTask();
nsDepCopTask.Compile = new ITaskItem[]
{
new TestTaskItem(Path.Combine(nsDepCopTask.BaseDirectory.ItemSpec, "SourceFile1.cs")),
new TestTaskItem(Path.Combine(nsDepCopTask.BaseDirectory.ItemSpec, "SourceFile2.cs"))
CreateCompiledTaskItem(nsDepCopTask, "SourceFile1.cs"),
CreateCompiledTaskItem(nsDepCopTask, "SourceFile2.cs")
};
nsDepCopTask.Execute().Should().BeTrue();

VerifyTaskExceptionLogged(Times.Never());
VerifyDependencyIssueLogged(Times.Exactly(2));
}

[Fact]
public void ExcludedFiles()
{
var nsDepCopTask = CreateNsDepCopTask();
nsDepCopTask.Compile = new ITaskItem[]
{
CreateCompiledTaskItem(nsDepCopTask, "IncludedFile.cs"),
CreateCompiledTaskItem(nsDepCopTask, "ExcludedByName.cs"),
CreateCompiledTaskItem(nsDepCopTask, "ExcludedByExtensions1.g.cs"),
CreateCompiledTaskItem(nsDepCopTask, @"Subfolder1\ExcludedByExtensions2.g.cs"),
CreateCompiledTaskItem(nsDepCopTask, @"Subfolder1\Sub folder 2\ExcludedByExtensions3.g.cs"),
};
nsDepCopTask.Execute().Should().BeTrue();

VerifyTaskExceptionLogged(Times.Never());
VerifyDependencyIssueLogged(Times.Exactly(1));
}

[Fact]
public void NoConfigFile()
{
Expand Down Expand Up @@ -196,6 +214,11 @@ private NsDepCopTask CreateNsDepCopTask([CallerMemberName] string taskName = nul
};
}

private static TestTaskItem CreateCompiledTaskItem(NsDepCopTask nsDepCopTask, string filename)
{
return new TestTaskItem(Path.Combine(nsDepCopTask.BaseDirectory.ItemSpec, filename));
}

private static string GetTestFileFullPath(string testName)
{
return Path.Combine(GetBinFilePath($@"{testName}\{testName}.cs"));
Expand Down
12 changes: 12 additions & 0 deletions source/NsDepCop.SourceTest.Roslyn2x/AnalyzerFeatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public void AnalyzerFeature_DisallowedDependency()
.Execute();
}

[Fact]
public void AnalyzerFeature_ExcludedFiles()
{
SourceTestSpecification.Create().Execute();
}

[Fact]
public void AnalyzerFeature_ExcludedFiles_WithWildcard()
{
SourceTestSpecification.Create().Execute();
}

[Fact]
public void AnalyzerFeature_SameNamespaceAlwaysAllowed()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace A
{
public class MyClass
{
private B.MyEnum field1;
}
}

namespace B
{
public enum MyEnum
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<NsDepCopConfig ExcludedFiles="AnalyzerFeature_ExcludedFiles.cs">
</NsDepCopConfig>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace A
{
public class MyClass
{
private B.MyEnum field1;
}
}

namespace B
{
public enum MyEnum
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<NsDepCopConfig ExcludedFiles="*.cs">
</NsDepCopConfig>
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@
<Content Include="Cs7_3_AttributeOnPropertyBackingField\Cs7_3_AttributeOnPropertyBackingField.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="AnalyzerFeature_ExcludedFiles\AnalyzerFeature_ExcludedFiles.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="AnalyzerFeature_ExcludedFiles_WithWildcard\AnalyzerFeature_ExcludedFiles_WithWildcard.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Compile Include="Cs7_3_Tests.cs" />
<Compile Include="Cs7_2_Tests.cs" />
<Compile Include="Cs7_1_Tests.cs" />
Expand Down Expand Up @@ -303,6 +309,12 @@
<Content Include="AnalyzerFeature_DisallowedDependency\config.nsdepcop">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="AnalyzerFeature_ExcludedFiles\config.nsdepcop">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="AnalyzerFeature_ExcludedFiles_WithWildcard\config.nsdepcop">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="app.config">
<SubType>Designer</SubType>
</None>
Expand Down

0 comments on commit 1a6b693

Please sign in to comment.