Skip to content

Commit

Permalink
Improve failure message when no analyzers found (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-marichal authored May 16, 2024
1 parent 5ada8bd commit 85c56ea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
2 changes: 2 additions & 0 deletions RoslynPluginGenerator/AnalyzerPluginGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public bool Generate(ProcessedArgs args)
if (!args.RecurseDependencies)
{
logger.LogWarning(UIResources.APG_NoAnalyzersInTargetSuggestRecurse);
logger.LogError(UIResources.APG_PluginNotGenerated);
return false;
}
}
Expand All @@ -112,6 +113,7 @@ public bool Generate(ProcessedArgs args)

if (!analyzersByPackage.Any())
{
logger.LogError(UIResources.APG_PluginNotGenerated);
return false;
}
}
Expand Down
10 changes: 9 additions & 1 deletion RoslynPluginGenerator/UIResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions RoslynPluginGenerator/UIResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@
<data name="APG_NoAnalyzersInTargetSuggestRecurse" xml:space="preserve">
<value>Re-run this generator with /recurse if plugins should be generated for the dependencies of this package.</value>
</data>
<data name="APG_PluginNotGenerated" xml:space="preserve">
<value>Plugin was not generated: No Roslyn analyzers were found. Check NuGet content and the minimal compatible Roslyn version.</value>
</data>
<data name="APG_PluginGenerated" xml:space="preserve">
<value>Plugin generated: {0}</value>
</data>
Expand Down
53 changes: 40 additions & 13 deletions Tests/RoslynPluginGeneratorTests/AnalyzerPluginGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public class AnalyzerPluginGeneratorTests
{
public TestContext TestContext { get; set; }

private enum Node { Root, Child1, Child2, Grandchild1_1, Grandchild2_1, Grandchild2_2 };

[TestMethod]
public void Generate_NoAnalyzersFoundInPackage_GenerateFails()
{
Expand Down Expand Up @@ -274,7 +272,7 @@ public void Generate_LicenseAcceptanceNotRequestedIfNoAnalysers()
logger.AssertSingleWarningExists(String.Format(UIResources.APG_NoAnalyzersFound, "non-analyzer.requireAccept.id"));
logger.AssertSingleWarningExists(UIResources.APG_NoAnalyzersInTargetSuggestRecurse);
logger.AssertWarningsLogged(2);
logger.AssertErrorsLogged(0);
logger.AssertErrorsLogged(1);
}

[TestMethod]
Expand Down Expand Up @@ -309,7 +307,7 @@ public void Generate_LicenseAcceptanceNotRequired_NoAnalyzersInTarget()
logger.AssertSingleWarningExists(String.Format(UIResources.APG_NoAnalyzersFound, "parent.id"));
logger.AssertSingleWarningExists(UIResources.APG_NoAnalyzersInTargetSuggestRecurse);
logger.AssertWarningsLogged(2);
logger.AssertErrorsLogged(0);
logger.AssertErrorsLogged(1);

// 1. b) Target package and dependencies. Acceptance not required -> succeeds if generate dependencies = true
logger.Reset();
Expand Down Expand Up @@ -358,7 +356,7 @@ public void Generate_LicenseAcceptanceRequired_NoAnalysersInTarget()
logger.AssertSingleWarningExists(String.Format(UIResources.APG_NoAnalyzersFound, "non-analyzer.parent.requireAccept.id"));
logger.AssertSingleWarningExists(UIResources.APG_NoAnalyzersInTargetSuggestRecurse);
logger.AssertWarningsLogged(2);
logger.AssertErrorsLogged(0);
logger.AssertErrorsLogged(1);

// 1. b) Target package and dependencies. User does not accept.
// No analyzers in the target package, but analyzers in the dependencies -> fails with error
Expand Down Expand Up @@ -395,10 +393,10 @@ public void Generate_LicenseAcceptanceRequired_NoAnalysersInTarget()
public void Generate_RulesFileNotSpecified_TemplateFileCreated()
{
// Arrange
string outputDir = TestUtils.CreateTestDirectory(this.TestContext, ".out");
string outputDir = TestUtils.CreateTestDirectory(TestContext, ".out");

var logger = new TestLogger();
var remoteRepoBuilder = new RemoteRepoBuilder(this.TestContext);
var remoteRepoBuilder = new RemoteRepoBuilder(TestContext);
var child1 = CreatePackageWithAnalyzer(remoteRepoBuilder, "child1.requiredAccept.id", "2.1", License.NotRequired);
var child2 = CreatePackageWithAnalyzer(remoteRepoBuilder, "child2.id", "2.2", License.NotRequired);
var parent = CreatePackageWithAnalyzer(remoteRepoBuilder, "parent.id", "1.0", License.NotRequired, child1, child2);
Expand Down Expand Up @@ -434,9 +432,9 @@ public void Generate_RulesFileNotSpecified_TemplateFileCreated()
public void Generate_ValidRuleFileSpecified_TemplateFileNotCreated()
{
// Arrange
var outputDir = TestUtils.CreateTestDirectory(this.TestContext, ".out");
var outputDir = TestUtils.CreateTestDirectory(TestContext, ".out");

var remoteRepoBuilder = new RemoteRepoBuilder(this.TestContext);
var remoteRepoBuilder = new RemoteRepoBuilder(TestContext);
var apg = CreateTestSubjectWithFakeRemoteRepo(remoteRepoBuilder);

CreatePackageInFakeRemoteRepo(remoteRepoBuilder, "dummy.id", "1.1");
Expand All @@ -460,14 +458,14 @@ public void Generate_ValidRuleFileSpecified_TemplateFileNotCreated()
}

[TestMethod]
public void Generate_InvalidRuleFileSpecified_GeneratorError()
public void Generate_InvalidRuleFileSpecified_GeneratesError()
{
// Arrange
var outputDir = TestUtils.CreateTestDirectory(this.TestContext, ".out");
var outputDir = TestUtils.CreateTestDirectory(TestContext, ".out");

var logger = new TestLogger();

var remoteRepoBuilder = new RemoteRepoBuilder(this.TestContext);
var remoteRepoBuilder = new RemoteRepoBuilder(TestContext);
CreatePackageInFakeRemoteRepo(remoteRepoBuilder, "dummy.id", "1.1");

var nuGetHandler = new NuGetPackageHandler(remoteRepoBuilder.FakeRemoteRepo, GetLocalNuGetDownloadDir(), logger);
Expand All @@ -493,6 +491,35 @@ public void Generate_InvalidRuleFileSpecified_GeneratorError()
logger.AssertSingleErrorExists("invalidRule.xml"); // expecting an error containing the invalid rule file name
}

[DataTestMethod]
[DataRow(false)]
[DataRow(true)]
public void Generate_NoAnalyzers_GeneratesError(bool recurseDependencies)
{
var outputDir = TestUtils.CreateTestDirectory(TestContext, $"recurse-{recurseDependencies}", ".out");
var logger = new TestLogger();
var remoteRepoBuilder = new RemoteRepoBuilder(TestContext);
// Create a package that does not contain analyzers
remoteRepoBuilder.CreatePackage("dummy.id", "1.1", typeof(AnalyzerPluginGenerator).Assembly.Location, License.NotRequired);

var nuGetHandler = new NuGetPackageHandler(remoteRepoBuilder.FakeRemoteRepo, GetLocalNuGetDownloadDir(), logger);

AnalyzerPluginGenerator apg = new AnalyzerPluginGenerator(nuGetHandler, logger);

var args = new ProcessedArgsBuilder("dummy.id", outputDir)
.SetLanguage("cs")
.SetPackageVersion("1.1")
.SetRecurseDependencies(recurseDependencies)
.Build();

bool result = apg.Generate(args);

result.Should().BeFalse();
AssertRuleTemplateDoesNotExist(outputDir);
AssertJarsGenerated(outputDir, 0);
logger.AssertSingleErrorExists("Plugin was not generated: No Roslyn analyzers were found. Check NuGet content and the minimal compatible Roslyn version.");
}

[TestMethod]
public void CreatePluginManifest_AllProperties()
{
Expand Down Expand Up @@ -688,7 +715,7 @@ private void AssertRuleTemplateFileExistsForPackage(TestLogger logger, string ou
string expectedFilePath = GetExpectedRuleTemplateFilePath(outputDir, package);

File.Exists(expectedFilePath).Should().BeTrue();
this.TestContext.AddResultFile(expectedFilePath);
TestContext.AddResultFile(expectedFilePath);
logger.AssertSingleInfoMessageExists(expectedFilePath); // should be a message about the generated file
}

Expand Down

0 comments on commit 85c56ea

Please sign in to comment.