From 9532174a57ee6b4a47b68a358bd873c8ff640146 Mon Sep 17 00:00:00 2001 From: Cyrille DUPUYDAUBY Date: Mon, 4 Mar 2024 13:50:48 +0100 Subject: [PATCH] fix: ensure MutationControl class compiles without warning with 'nullable' enabled --- .../InjectedHelpers/InjectedHelperTests.cs | 45 ++++++++++++++++++- .../InjectedHelpers/MutantControl.cs | 12 ++--- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/Stryker.Core/Stryker.Core.UnitTest/InjectedHelpers/InjectedHelperTests.cs b/src/Stryker.Core/Stryker.Core.UnitTest/InjectedHelpers/InjectedHelperTests.cs index 61eea216da..143e5b511c 100644 --- a/src/Stryker.Core/Stryker.Core.UnitTest/InjectedHelpers/InjectedHelperTests.cs +++ b/src/Stryker.Core/Stryker.Core.UnitTest/InjectedHelpers/InjectedHelperTests.cs @@ -57,7 +57,50 @@ public void InjectHelpers_ShouldCompile_ForAllLanguageVersions(LanguageVersion v references: references); compilation.GetDiagnostics().ShouldNotContain(diag => diag.Severity == DiagnosticSeverity.Error, - $"errors :{string.Join(Environment.NewLine, compilation.GetDiagnostics().Where(x => x.Severity == DiagnosticSeverity.Error).Select(diag => $"'{diag.GetMessage()}' at {diag.Location.SourceTree.FilePath}, {diag.Location.GetLineSpan().StartLinePosition.Line}:{diag.Location.GetLineSpan().StartLinePosition.Character}"))}"); + $"errors :{string.Join(Environment.NewLine, compilation.GetDiagnostics().Where(x => x.Severity == DiagnosticSeverity.Error).Select(diag => $"{diag.Id}: '{diag.GetMessage()}' at {diag.Location.SourceTree.FilePath}, {diag.Location.GetLineSpan().StartLinePosition.Line+1}:{diag.Location.GetLineSpan().StartLinePosition.Character}"))}"); + } + + [Theory] + [InlineData(LanguageVersion.CSharp8)] + [InlineData(LanguageVersion.CSharp9)] + [InlineData(LanguageVersion.CSharp10)] + [InlineData(LanguageVersion.CSharp11)] + [InlineData(LanguageVersion.CSharp12)] + [InlineData(LanguageVersion.Default)] + [InlineData(LanguageVersion.Latest)] + [InlineData(LanguageVersion.LatestMajor)] + [InlineData(LanguageVersion.Preview)] + public void InjectHelpers_ShouldCompile_ForAllLanguageVersionsWithNullableOptions(LanguageVersion version) + { + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); + + var needed = new[] { ".CoreLib", ".Runtime", "System.IO.Pipes", ".Collections", ".Console" }; + var references = new List(); + var hack = new NamedPipeClientStream("test"); + foreach (var assembly in assemblies) + { + if (needed.Any(x => assembly.FullName.Contains(x))) + { + references.Add(MetadataReference.CreateFromFile(assembly.Location)); + } + } + + var syntaxes = new List(); + var codeInjection = new CodeInjection(); + + foreach (var helper in codeInjection.MutantHelpers) + { + syntaxes.Add(CSharpSyntaxTree.ParseText(helper.Value, new CSharpParseOptions(languageVersion: version), + helper.Key)); + } + + var compilation = CSharpCompilation.Create("dummy.dll", + syntaxes, + options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, nullableContextOptions: NullableContextOptions.Enable, generalDiagnosticOption: ReportDiagnostic.Error), + references: references); + + compilation.GetDiagnostics().ShouldNotContain(diag => diag.Severity == DiagnosticSeverity.Error, + $"errors :{string.Join(Environment.NewLine, compilation.GetDiagnostics().Where(x => x.Severity == DiagnosticSeverity.Error).Select(diag => $"{diag.Id}: '{diag.GetMessage()}' at {diag.Location.SourceTree.FilePath}, {diag.Location.GetLineSpan().StartLinePosition.Line+1}:{diag.Location.GetLineSpan().StartLinePosition.Character}"))}"); } } } diff --git a/src/Stryker.Core/Stryker.Core/InjectedHelpers/MutantControl.cs b/src/Stryker.Core/Stryker.Core/InjectedHelpers/MutantControl.cs index 5562dfcd1a..abd64b9cbe 100644 --- a/src/Stryker.Core/Stryker.Core/InjectedHelpers/MutantControl.cs +++ b/src/Stryker.Core/Stryker.Core/InjectedHelpers/MutantControl.cs @@ -2,9 +2,9 @@ namespace Stryker { public static class MutantControl { - private static System.Collections.Generic.List _coveredMutants; - private static System.Collections.Generic.List _coveredStaticdMutants; - private static string envName; + private static System.Collections.Generic.List _coveredMutants = new System.Collections.Generic.List(); + private static System.Collections.Generic.List _coveredStaticdMutants = new System.Collections.Generic.List(); + private static string envName = string.Empty; private static System.Object _coverageLock = new System.Object(); // this attribute will be set by the Stryker Data Collector before each test @@ -12,11 +12,6 @@ public static class MutantControl public static int ActiveMutant = -2; public const int ActiveMutantNotInitValue = -2; - static MutantControl() - { - InitCoverage(); - } - public static void InitCoverage() { ResetCoverage(); @@ -51,6 +46,7 @@ public static bool IsActive(int id) } if (ActiveMutant == ActiveMutantNotInitValue) { + #pragma warning disable CS8600 string environmentVariable = System.Environment.GetEnvironmentVariable("ActiveMutation"); if (string.IsNullOrEmpty(environmentVariable)) {