stryker-mutator / stryker-net

Mutation testing for .NET core and .NET framework!
https://stryker-mutator.io
Apache License 2.0
1.76k stars 175 forks source link

Unhandled exception when single file in multiple projects #2538

Closed CzBuCHi closed 1 year ago

CzBuCHi commented 1 year ago

Describe the bug I have file that is imported in every single project in solution (SharedAssemblyInfo.cs to be precise). When running stryker on whole solution it ends up with this exception:

Unhandled exception. System.ArgumentException: An item with the same key has already been added. Key: c:\projects\dotnet\Centaur\5.0\_build\SharedAssemblyInfo.cs
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at Stryker.Core.Reporters.Json.JsonReport.AddTestFiles(TestProjectsInfo testProjectsInfo) in /_/src/Stryker.Core/Stryker.Core/Reporters/Json/JsonReport.cs:line 69
   at Stryker.Core.Reporters.Json.JsonReport..ctor(StrykerOptions options, IReadOnlyProjectComponent mutationReport, TestProjectsInfo testProjectsInfo) in /_/src/Stryker.Core/Stryk                                                   er.Core/Reporters/Json/JsonReport.cs:line 29
   at Stryker.Core.Reporters.Json.JsonReport.Build(StrykerOptions options, IReadOnlyProjectComponent mutationReport, TestProjectsInfo testProjectsInfo) in /_/src/Stryker.Core/Stryk                                                   er.Core/Reporters/Json/JsonReport.cs:line 32
   at Stryker.Core.Reporters.Html.HtmlReporter.WriteHtmlReport(String filePath, IReadOnlyProjectComponent reportComponent, TestProjectsInfo testProjectsInfo) in /_/src/Stryker.Core                                                   /Stryker.Core/Reporters/Html/HtmlReporter.cs:line 110
   at Stryker.Core.Reporters.Html.HtmlReporter.OnMutantsCreated(IReadOnlyProjectComponent reportComponent, TestProjectsInfo testProjectsInfo) in /_/src/Stryker.Core/Stryker.Core/Re                                                   porters/Html/HtmlReporter.cs:line 74
   at Stryker.Core.Reporters.BroadcastReporter.OnMutantsCreated(IReadOnlyProjectComponent reportComponent, TestProjectsInfo testProjectsInfo) in /_/src/Stryker.Core/Stryker.Core/Re                                                   porters/BroadcastReporter.cs:line 26
   at Stryker.Core.StrykerRunner.RunMutationTest(IStrykerInputs inputs, ILoggerFactory loggerFactory, IProjectOrchestrator projectOrchestrator) in /_/src/Stryker.Core/Stryker.Core/                                                   StrykerRunner.cs:line 76
   at Stryker.CLI.StrykerCli.RunStryker(IStrykerInputs inputs) in /_/src/Stryker.CLI/Stryker.CLI/StrykerCLI.cs:line 93
   at Stryker.CLI.StrykerCli.<>c__DisplayClass10_0.<Run>b__0() in /_/src/Stryker.CLI/Stryker.CLI/StrykerCLI.cs:line 68
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.<>c__DisplayClass143_0.<OnExecute>b__0(CancellationToken _)
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.ExecuteAsync(String[] args, CancellationToken cancellationToken)
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Stryker.CLI.StrykerCli.Run(String[] args) in /_/src/Stryker.CLI/Stryker.CLI/StrykerCLI.cs:line 74
   at Stryker.CLI.Program.Main(String[] args) in /_/src/Stryker.CLI/Stryker.CLI/Program.cs:line 14

Expected behavior merge all reports for given file into single one (looking at JsonTestFile and it dont look to complicated to merge 2 into one ...)

Desktop (please complete the following information):

Additional context I would expect non zero return code when this happends ...

CzBuCHi commented 1 year ago

fixed localy by editing https://github.com/stryker-mutator/stryker-net/blob/005dca01ea52aa180f68bcd1526d4a6d970fd150/src/Stryker.Core/Stryker.Core/Reporters/Json/JsonReport.cs#LL69C5-L69C5 :

- TestFiles.Add(testFile.FilePath, new JsonTestFile(testFile));
+ TestFiles.TryAdd(testFile.FilePath, new JsonTestFile(testFile));
dupdob commented 1 year ago

Thanks for reporting this. The correct fix is a bit more complex: Stryker needs to merge both test files, as there may be differences due to compilation symbols: same source files, but with different detected tests. Keeping only the first one would lead to loss of information.

rouke-broersma commented 1 year ago

The initial implementation actually did contain a merge of test files to prevent data loss, but it was decided during review that it was probably unlikely to be needed... Guess not :)

Edit:

Actually no, the merging was done in TestFile and not in JsonTestFile.