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 1 commit
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
11 changes: 6 additions & 5 deletions src/Stryker.Core/Stryker.Core/DiffProviders/DiffResult.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Collections.Generic;

namespace Stryker.Core.DiffProviders;

public class DiffResult
namespace Stryker.Core.DiffProviders
{
public ICollection<string> ChangedTestFiles { get; set; }
public ICollection<string> ChangedSourceFiles { get; set; }
public class DiffResult
{
public IDictionary<string, List<int>> ChangedTestFiles { get; set; }
public IDictionary<string, List<int>> ChangedSourceFiles { get; set; }
}
}
137 changes: 86 additions & 51 deletions src/Stryker.Core/Stryker.Core/DiffProviders/GitDiffProvider.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,116 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using LibGit2Sharp;
using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Extensions;
using Spectre.Console;
using Stryker.Abstractions.Exceptions;
using Stryker.Abstractions.Options;
using Stryker.Core.Baseline.Providers;
using Stryker.Core.Mutants;
using Stryker.Utilities;

namespace Stryker.Core.DiffProviders;

public class GitDiffProvider : IDiffProvider
namespace Stryker.Core.DiffProviders
{
public TestSet Tests { get; }
private readonly IStrykerOptions _options;
private readonly IGitInfoProvider _gitInfoProvider;

public GitDiffProvider(IStrykerOptions options, TestSet tests, IGitInfoProvider gitInfoProvider = null)
public class GitDiffProvider : IDiffProvider
{
Tests = tests;
_options = options;
_gitInfoProvider = gitInfoProvider ?? new GitInfoProvider(options);
}

public DiffResult ScanDiff()
{
var diffResult = new DiffResult()
{
ChangedSourceFiles = new Collection<string>(),
ChangedTestFiles = new Collection<string>()
};

// A git repository has been detected, calculate the diff to filter
var repository = _gitInfoProvider.Repository;
var commit = _gitInfoProvider.DetermineCommit();
public TestSet Tests { get; }

Choose a reason for hiding this comment

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

Why did we move these inside class?

Copy link
Author

Choose a reason for hiding this comment

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

@inishantmishra We did not change anything there it was like that only

private readonly IStrykerOptions _options;
private readonly IGitInfoProvider _gitInfoProvider;

if (commit == null)
public GitDiffProvider(IStrykerOptions options, TestSet tests, IGitInfoProvider gitInfoProvider = null)
{
throw new InputException("Could not find a commit to diff. Please check you have provided the correct committish for 'since'.");
Tests = tests;
_options = options;
_gitInfoProvider = gitInfoProvider ?? new GitInfoProvider(options);
}

var testProjects = _options.TestProjects.ToList();
if (!testProjects.Any())
public DiffResult ScanDiff()
{
testProjects.Add(_options.ProjectPath);
}
var diffResult = new DiffResult()
{
ChangedSourceFiles = new Dictionary<string, List<int>>(),
ChangedTestFiles = new Dictionary<string, List<int>>(),
};

var testPaths = testProjects
.Select(testProject => testProject.EndsWith(Path.DirectorySeparatorChar)
? testProject
: testProject + Path.DirectorySeparatorChar)
.ToArray();
// A git repository has been detected, calculate the diff to filter
var repository = _gitInfoProvider.Repository;
var commit = _gitInfoProvider.DetermineCommit();

foreach (var patchChanges in repository.Diff.Compare<Patch>(commit.Tree, DiffTargets.WorkingDirectory))
{
var diffPath = FilePathUtils.NormalizePathSeparators(Path.Combine(_gitInfoProvider.RepositoryPath, patchChanges.Path));
if (commit == null)
{
throw new InputException("Could not find a commit to diff. Please check you have provided the correct committish for 'since'.");
}

if (testPaths.Any(testPath => diffPath.StartsWith(testPath)))
var testProjects = _options.TestProjects.ToList();
if (!testProjects.Any())
{
diffResult.ChangedTestFiles.Add(diffPath);
testProjects.Add(_options.ProjectPath);
}
else

var testPaths = testProjects
.Select(testProject => testProject.EndsWith(Path.DirectorySeparatorChar)
? testProject
: testProject + Path.DirectorySeparatorChar)
.ToArray();

foreach (var patchChanges in repository.Diff.Compare<Patch>(commit.Tree, DiffTargets.WorkingDirectory))
{
diffResult.ChangedSourceFiles.Add(diffPath);
var diffPath = FilePathUtils.NormalizePathSeparators(Path.Combine(_gitInfoProvider.RepositoryPath, patchChanges.Path));

if (testPaths.Any(testPath => diffPath.StartsWith(testPath)))
{
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
{
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;
}
RemoveFilteredOutFiles(diffResult);

private void RemoveFilteredOutFiles(DiffResult diffResult)
{
foreach (var glob in _options.DiffIgnoreChanges.Select(d => d.Glob))
return diffResult;
}

private void RemoveFilteredOutFiles(DiffResult diffResult)
{
diffResult.ChangedSourceFiles = diffResult.ChangedSourceFiles.Where(diffResultFile => !glob.IsMatch(diffResultFile)).ToList();
diffResult.ChangedTestFiles = diffResult.ChangedTestFiles.Where(diffResultFile => !glob.IsMatch(diffResultFile)).ToList();
foreach (var glob in _options.DiffIgnoreChanges.Select(d => d.Glob))
{
diffResult.ChangedSourceFiles = diffResult.ChangedSourceFiles.Where(diffResultFile => !glob.IsMatch(diffResultFile.Key)).ToDictionary();
diffResult.ChangedTestFiles = diffResult.ChangedTestFiles.Where(diffResultFile => !glob.IsMatch(diffResultFile.Key)).ToDictionary();
}
}
}
}
Loading
Loading