Skip to content

Commit

Permalink
feat: disable coverage analysis optimisation when no coverage is repo…
Browse files Browse the repository at this point in the history
…rted
  • Loading branch information
dupdob committed Nov 25, 2024
1 parent ac908c6 commit 6ec42a3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<GenerateProgramFile>true</GenerateProgramFile>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void IdentifyNonCoveredMutants()
}

[TestMethod]
public void WorksWhenAllMutantsAreIgnoredPool()
public void ShouldIgnoreCoverageAnalysisWhenEmpty()
{
var options = new StrykerOptions
{
Expand All @@ -270,7 +270,7 @@ public void WorksWhenAllMutantsAreIgnoredPool()

var analyzer = new CoverageAnalyser(options);
analyzer.DetermineTestCoverage(SourceProjectInfo, runner, new[] { Mutant, OtherMutant }, TestGuidsList.NoTest());
Mutant.CoveringTests.Count.ShouldBe(0);
Mutant.CoveringTests.IsEveryTest.ShouldBeTrue();
}

[TestMethod]
Expand Down Expand Up @@ -666,8 +666,9 @@ public void DetectUnexpectedCase()

var mockVsTest = BuildVsTestRunnerPool(options, out var runner);

var testResult = BuildCoverageTestResult("T0", new[] { "0;", "" });
var buildCase = BuildCase("unexpected", TestFrameworks.NUnit);
SetupMockCoverageRun(mockVsTest, new[] { new VsTest.TestResult(buildCase) { Outcome = VsTest.TestOutcome.Passed } });
SetupMockCoverageRun(mockVsTest, new[] { new VsTest.TestResult(buildCase) { Outcome = VsTest.TestOutcome.Passed }, testResult });

var analyzer = new CoverageAnalyser(options);
analyzer.DetermineTestCoverage(SourceProjectInfo, runner, new[] { Mutant, OtherMutant }, TestGuidsList.NoTest());
Expand Down
22 changes: 17 additions & 5 deletions src/Stryker.Core/Stryker.Core/CoverageAnalysis/CoverageAnalyser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,33 @@ public void DetermineTestCoverage(IProjectAndTests project, ITestRunner runner,
if (!_options.OptimizationMode.HasFlag(OptimizationModes.SkipUncoveredMutants) &&
!_options.OptimizationMode.HasFlag(OptimizationModes.CoverageBasedTest))
{
foreach (var mutant in mutants)
{
mutant.CoveringTests = TestGuidsList.EveryTest();
mutant.AssessingTests = TestGuidsList.EveryTest();
}
AssumeAllTestsAreNeeded(mutants);

return;
}

ParseCoverage(runner.CaptureCoverage(project), mutants, new TestGuidsList(resultFailingTests.GetGuids()));
}

private static void AssumeAllTestsAreNeeded(IEnumerable<IMutant> mutants)
{
foreach (var mutant in mutants)
{
mutant.CoveringTests = TestGuidsList.EveryTest();
mutant.AssessingTests = TestGuidsList.EveryTest();
}
}

private void ParseCoverage(IEnumerable<CoverageRunResult> coverage, IEnumerable<IMutant> mutantsToScan,
TestGuidsList failedTests)
{
if (coverage.Sum(c => c.MutationsCovered.Count) == 0)
{
_logger.LogError("It looks like the test coverage capture failed. Disable coverage based optimisation.");
AssumeAllTestsAreNeeded(mutantsToScan);
return;
}

var dubiousTests = new HashSet<Guid>();
var trustedTests = new HashSet<Guid>();
var testIds = new HashSet<Guid>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private InitialTestRun InitialTest(IStrykerOptions options, SourceProjectInfo pr
}

throw new InputException(
"No test has been detected. Make sure your test project contains test and is compatible with VsTest." +
"No test result reported. Make sure your test project contains test and is compatible with VsTest." +
string.Join(Environment.NewLine, projectInfo.Warnings));
}

Expand Down

0 comments on commit 6ec42a3

Please sign in to comment.