Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added since on line level changes #3086

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Stryker.Core/Stryker.Core/DiffProviders/DiffResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace Stryker.Core.DiffProviders;

public class DiffResult
{
public ICollection<string> ChangedTestFiles { get; set; }
public ICollection<string> ChangedSourceFiles { get; set; }
public IDictionary<string, List<int>> ChangedTestFiles { get; set; }
public IDictionary<string, List<int>> ChangedSourceFiles { get; set; }
}
44 changes: 38 additions & 6 deletions src/Stryker.Core/Stryker.Core/DiffProviders/GitDiffProvider.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -27,8 +28,8 @@ public DiffResult ScanDiff()
{
var diffResult = new DiffResult()
{
ChangedSourceFiles = new Collection<string>(),
ChangedTestFiles = new Collection<string>()
ChangedSourceFiles = new Dictionary<string, List<int>>(),
ChangedTestFiles = new Dictionary<string, List<int>>(),
};

// A git repository has been detected, calculate the diff to filter
Expand Down Expand Up @@ -58,13 +59,44 @@ public DiffResult ScanDiff()

if (testPaths.Any(testPath => diffPath.StartsWith(testPath)))
{
diffResult.ChangedTestFiles.Add(diffPath);
for (var i = 0; i < patchChanges.AddedLines.Count; i++)
{
if (!diffResult.ChangedTestFiles.ContainsKey(diffPath))
{
diffResult.ChangedTestFiles.Add(diffPath, [patchChanges.AddedLines[i].LineNumber]);

}
else

{
var entry = diffResult.ChangedTestFiles[diffPath];
entry.Add(patchChanges.AddedLines[i].LineNumber);
}
}
}
else
{
diffResult.ChangedSourceFiles.Add(diffPath);
if (patchChanges.AddedLines.Count > 0)
{
for (var i = 0; i < patchChanges.AddedLines.Count; i++)
{
if (!diffResult.ChangedSourceFiles.ContainsKey(diffPath))
{
diffResult.ChangedSourceFiles.Add(diffPath, [patchChanges.AddedLines[i].LineNumber]);

}
else

{
var entry = diffResult.ChangedSourceFiles[diffPath];
entry.Add(patchChanges.AddedLines[i].LineNumber);
}
}

}
}
}

RemoveFilteredOutFiles(diffResult);

return diffResult;
Expand All @@ -74,8 +106,8 @@ private void RemoveFilteredOutFiles(DiffResult diffResult)
{
foreach (var glob in _options.DiffIgnoreChanges.Select(d => d.Glob))
{
diffResult.ChangedSourceFiles = diffResult.ChangedSourceFiles.Where(diffResultFile => !glob.IsMatch(diffResultFile)).ToList();
diffResult.ChangedTestFiles = diffResult.ChangedTestFiles.Where(diffResultFile => !glob.IsMatch(diffResultFile)).ToList();
diffResult.ChangedSourceFiles = diffResult.ChangedSourceFiles.Where(diffResultFile => !glob.IsMatch(diffResultFile.Key)).ToDictionary();
diffResult.ChangedTestFiles = diffResult.ChangedTestFiles.Where(diffResultFile => !glob.IsMatch(diffResultFile.Key)).ToDictionary();
}
}
}
54 changes: 46 additions & 8 deletions src/Stryker.Core/Stryker.Core/MutantFilters/SinceMutantFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -55,17 +54,37 @@ public IEnumerable<IMutant> FilterMutants(IEnumerable<IMutant> mutants, IReadOnl
IEnumerable<IMutant> filteredMutants;

// A non-csharp file is flagged by the diff result as modified. We cannot determine which mutants will be affected by this, thus all mutants have to be tested.
if (_diffResult.ChangedTestFiles is { } && _diffResult.ChangedTestFiles.Any(x => !x.EndsWith(".cs")))
if (_diffResult.ChangedTestFiles is { } && _diffResult.ChangedTestFiles.Keys.Any(x => !x.EndsWith(".cs")))
{
_logger.LogDebug("Returning all mutants in {RelativePath} because a non-source file is modified", file.RelativePath);
return SetMutantStatusForNonCSharpFileChanged(mutants);
}

// If the diff result flags this file as modified, we want to run all mutants again
if (_diffResult.ChangedSourceFiles != null && _diffResult.ChangedSourceFiles.Contains(file.FullPath))
if (_diffResult.ChangedSourceFiles != null && _diffResult.ChangedSourceFiles.Keys.Contains(file.FullPath))
{
foreach (var mutant in mutants)
{
foreach (var line in _diffResult.ChangedSourceFiles[file.FullPath])
{

var actualLineSpan = mutant.Mutation.OriginalNode.GetLocation().GetMappedLineSpan();

_logger.LogDebug("Original line span is found to be {actualLineSpan}", actualLineSpan);
var start = actualLineSpan.Span.Start.Line;
var end = actualLineSpan.Span.Start.Line;
if (start <= line && line <= end)
{
if (mutant.ResultStatus != MutantStatus.NoCoverage)
{
mutant.ResultStatus = MutantStatus.Pending;
mutant.ResultStatusReason = "Mutant changed compared to target commit";
}
}
Comment on lines +78 to +82
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you not use SetMutantStatusForFileChanged?

}
}
_logger.LogDebug("Returning all mutants in {RelativePath} because the file is modified", file.RelativePath);
return SetMutantStatusForFileChanged(mutants);
return mutants;
}
else
{
Expand All @@ -74,12 +93,30 @@ public IEnumerable<IMutant> FilterMutants(IEnumerable<IMutant> mutants, IReadOnl

// If any of the tests have been changed, we want to return all mutants covered by these testfiles.
// Only check for changed c# files. Other files have already been handled.
if (_diffResult.ChangedTestFiles != null && _diffResult.ChangedTestFiles.Any(file => file.EndsWith(".cs")))
if (_diffResult.ChangedTestFiles != null && _diffResult.ChangedTestFiles.Keys.Any(file => file.EndsWith(".cs")))
{
filteredMutants = ResetMutantStatusForChangedTests(mutants);
foreach (var mutant in mutants)
{
foreach (var line in _diffResult.ChangedTestFiles[file.FullPath])
{
var actualLineSpan = mutant.Mutation.OriginalNode.GetLocation().GetMappedLineSpan();

_logger.LogDebug("Original line span is found to be {actualLineSpan}", actualLineSpan);
var start = actualLineSpan.Span.Start.Line;
var end = actualLineSpan.Span.Start.Line;
if (start <= line && line <= end)
{
if (mutant.ResultStatus != MutantStatus.NoCoverage)
{
mutant.ResultStatus = MutantStatus.Pending;
mutant.ResultStatusReason = "Mutant changed compared to target commit";
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The status reason is not correct, the mutant has not been changed but a covering test has been changed

Comment on lines +109 to +112
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you not use ResetMutantStatusForChangedTests?

}
}
}
}

return filteredMutants;
return mutants;
}

private static IEnumerable<IMutant> SetNotRunMutantsToIgnored(IEnumerable<IMutant> mutants)
Expand Down Expand Up @@ -128,7 +165,7 @@ private IEnumerable<IMutant> ResetMutantStatusForChangedTests(IEnumerable<IMutan
var coveringTests = _tests.Extract(mutant.CoveringTests.GetGuids());

if (coveringTests != null
&& coveringTests.Any(coveringTest => _diffResult.ChangedTestFiles.Any(changedTestFile => coveringTest.TestFilePath == changedTestFile
&& coveringTests.Any(coveringTest => _diffResult.ChangedTestFiles.Keys.Any(changedTestFile => coveringTest.TestFilePath == changedTestFile
|| string.IsNullOrEmpty(coveringTest.TestFilePath))))
{
mutant.ResultStatus = MutantStatus.Pending;
Expand All @@ -141,3 +178,4 @@ private IEnumerable<IMutant> ResetMutantStatusForChangedTests(IEnumerable<IMutan
return filteredMutants;
}
}

Loading