Skip to content

Commit

Permalink
Improve error message when all discovered projects are test projects
Browse files Browse the repository at this point in the history
Fixes #3065

Update error handling to provide a clear message when only test projects are found.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/stryker-mutator/stryker-net/issues/3065?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
rouke-broersma committed Oct 28, 2024
1 parent 79d9f40 commit 9290f63
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
24 changes: 24 additions & 0 deletions src/Stryker.Abstractions/Exceptions/InvalidProjectsException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace Stryker.Abstractions.Exceptions;

/// <summary>
/// Represents error when no test projects are found in the solution or configured for stryker.
/// </summary>
public class InvalidProjectsException : Exception
{
public InvalidProjectsException(string message)
: base(message)
{
}

public static InvalidProjectsException NoTestProjectsFound()
{
return new InvalidProjectsException("No test projects found. Please add a test project to your solution or fix your stryker config.");
}

public static InvalidProjectsException OnlyTestProjectsFound()
{
return new InvalidProjectsException("Only test projects found. Please ensure that your solution contains non-test projects.");
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion src/Stryker.CLI/Stryker.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static int Main(string[] args)
var app = new StrykerCli();
return app.Run(args);
}
catch (NoTestProjectsException exception)
catch (InvalidProjectsException exception)
{
AnsiConsole.WriteLine(exception.Message);
return ExitCodes.Success;
Expand Down
45 changes: 44 additions & 1 deletion src/Stryker.Core/Stryker.Core.UnitTest/StrykerRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,50 @@ public void ShouldThrow_WhenNoProjectsFound()

var target = new StrykerRunner(reporterFactory: reporterFactoryMock.Object);

Should.Throw<NoTestProjectsException>(() => target.RunMutationTest(inputsMock.Object, new LoggerFactory(), projectOrchestratorMock.Object));
Should.Throw<InvalidProjectsException>(() => target.RunMutationTest(inputsMock.Object, new LoggerFactory(), projectOrchestratorMock.Object));

reporterMock.Verify(x => x.OnStartMutantTestRun(It.IsAny<IList<IMutant>>()), Times.Never);
reporterMock.Verify(x => x.OnMutantTested(It.IsAny<IMutant>()), Times.Never);
reporterMock.Verify(x => x.OnAllMutantsTested(It.IsAny<IReadOnlyProjectComponent>(), It.IsAny<TestProjectsInfo>()), Times.Never);
}

[TestMethod]
public void ShouldThrow_WhenOnlyTestProjectsFound()
{
var projectOrchestratorMock = new Mock<IProjectOrchestrator>(MockBehavior.Strict);
var reporterFactoryMock = new Mock<IReporterFactory>(MockBehavior.Strict);
var reporterMock = new Mock<IReporter>(MockBehavior.Strict);
var inputsMock = new Mock<IStrykerInputs>(MockBehavior.Strict);
var fileSystemMock = new MockFileSystem();

var folder = new CsharpFolderComposite();
folder.Add(new CsharpFileLeaf
{
Mutants = new Collection<IMutant>() { new Mutant() { Id = 1, ResultStatus = MutantStatus.Ignored } }
});
var mutationTestInput = new MutationTestInput()
{
SourceProjectInfo = new SourceProjectInfo()
{
ProjectContents = folder
}
};

inputsMock.Setup(x => x.ValidateAll()).Returns(new StrykerOptions
{
ProjectPath = "C:/test",
OptimizationMode = OptimizationModes.None,
LogOptions = new LogOptions()
});

projectOrchestratorMock.Setup(x => x.MutateProjects(It.IsAny<StrykerOptions>(), It.IsAny<IReporter>(), It.IsAny<ITestRunner>()))
.Returns(new List<IMutationTestProcess>() { });

reporterFactoryMock.Setup(x => x.Create(It.IsAny<StrykerOptions>(), It.IsAny<IGitInfoProvider>())).Returns(reporterMock.Object);

var target = new StrykerRunner(reporterFactory: reporterFactoryMock.Object);

Should.Throw<InvalidProjectsException>(() => target.RunMutationTest(inputsMock.Object, new LoggerFactory(), projectOrchestratorMock.Object));

reporterMock.Verify(x => x.OnStartMutantTestRun(It.IsAny<IList<IMutant>>()), Times.Never);
reporterMock.Verify(x => x.OnMutantTested(It.IsAny<IMutant>()), Times.Never);
Expand Down
7 changes: 6 additions & 1 deletion src/Stryker.Core/Stryker.Core/StrykerRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ private IReadOnlyProjectComponent AddRootFolderIfMultiProject(IEnumerable<IReadO
{
if (!projectComponents.Any())
{
throw new NoTestProjectsException();
throw InvalidProjectsException.NoTestProjectsFound();
}

if (projectComponents.All(pc => pc is TestProject))
{
throw InvalidProjectsException.OnlyTestProjectsFound();
}

if (projectComponents.Count() > 1)
Expand Down

0 comments on commit 9290f63

Please sign in to comment.