Skip to content

Commit

Permalink
Fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-sz committed Jan 4, 2023
1 parent dcb5999 commit b522735
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Simply add a `PackageReference` to the [ReferenceTrimmer](https://www.nuget.org/

The package contains build logic to emit warnings when unused dependencies are detected.

Note: to get better effects, enable [`IDE0005`](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0005) unnecessary code rule. See also the note for why IDE0005 code analysis rule requires `<GenerateDocumentationFile>` property to be enabled.
Note: to get better effects, enable [`IDE0005`](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0005) unnecessary code rule. See also the note for why IDE0005 code analysis rule requires `<GenerateDocumentationFile>` property to be enabled. Documentation generation is also required for accuracy of used references detection (based on https://github.com/dotnet/roslyn/issues/66188).

## Configuration
`$(EnableReferenceTrimmer)` - Controls whether the build logic should run for a given project. Defaults to `true`.
Expand Down
2 changes: 1 addition & 1 deletion analyzer/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
RT0001 | References | Warning | Unused references should be removed
DOC001 | Documentation | Warning | See https://github.com/dotnet/roslyn/issues/66188
12 changes: 10 additions & 2 deletions analyzer/UsedAssemblyReferencesDumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace ReferenceTrimmer
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic, LanguageNames.FSharp)]
public class UsedAssemblyReferencesDumper : DiagnosticAnalyzer
{
internal static readonly string Title = "Unused references should be removed";
internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor("RT0001", Title, "'{0}'", "References", DiagnosticSeverity.Warning, true, customTags: WellKnownDiagnosticTags.Unnecessary);
private static readonly string Title = "Enable documentation generation for accuracy of used references detection";
private static readonly string Message = "Enable /doc parameter or in MSBuild set <GenerateDocumentationFile>true</GenerateDocumentationFile> for accuracy of used references detection";
private static DiagnosticDescriptor Rule = new DiagnosticDescriptor("DOC001", Title, Message, "Documentation", DiagnosticSeverity.Warning, true);

/// <summary>
/// The supported diagnosticts.
Expand All @@ -25,6 +26,13 @@ public override void Initialize(AnalysisContext context)
private static void DumpUsedReferences(CompilationAnalysisContext context)
{
Compilation compilation = context.Compilation;
if (compilation.SyntaxTrees.FirstOrDefault()?.Options.DocumentationMode == DocumentationMode.None)
{
string nameOrPath = compilation.Options.ModuleName;
Location location = Location.None;
context.ReportDiagnostic(Diagnostic.Create(Rule, location, nameOrPath));
}

if (compilation.Options.Errors.IsEmpty)
{
IEnumerable<MetadataReference> usedReferences = compilation.GetUsedAssemblyReferences();
Expand Down
14 changes: 14 additions & 0 deletions test/E2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public static void ClassInitialize(TestContext testContext)
$@"<Project>
<PropertyGroup>
<ReferenceTrimmerTaskAssembly>{testOutputDir}\ReferenceTrimmer\ReferenceTrimmer.dll</ReferenceTrimmerTaskAssembly>
<!-- Per https://github.com/dotnet/roslyn/issues/66188 /doc param is required for accurate results -->
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<Analyzer Include=""{testOutputDir}\ReferenceTrimmer\ReferenceTrimmerAnalyzer.dll""/>
Expand Down Expand Up @@ -116,6 +119,17 @@ public void UnusedPackageReference()
});
}

[TestMethod]
public void UnusedPackageReferenceDocDisabled()
{
RunMSBuild(
projectFile: @"Library\Library.csproj",
expectedWarnings: new[]
{
@"Enable /doc parameter or in MSBuild set <GenerateDocumentationFile>true</GenerateDocumentationFile> for accuracy of used references detection",
});
}

[TestMethod]
public void MissingReferenceSourceTarget()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Library
{
public static class Foo
{
public static string Bar() => "Baz";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net472</TargetFramework>
<!-- Assert the analyzer reports diagnostic -->
<GenerateDocumentationFile>false</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>

</Project>

0 comments on commit b522735

Please sign in to comment.