Skip to content

Commit

Permalink
feat: Try loading embedded resources from on-disk module before gener…
Browse files Browse the repository at this point in the history
…ating (#2433)

* feat: Try loading embedded resources from on-disk module instead of generating first

* Use stream.CopyTo

* Fix unit tests

* Exclude copied code from code coverage

* Improve

* Comment GenerateResourceName

* New line
  • Loading branch information
rouke-broersma committed Mar 17, 2023
1 parent 8b73ca6 commit e919891
Show file tree
Hide file tree
Showing 5 changed files with 535 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public int Subtract(int first, int second)
{
ProjectInfo = new ProjectInfo(new MockFileSystem())
{
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/project.csproj", properties: new Dictionary<string, string>()
{
{ "TargetDir", "" },
{ "AssemblyName", "AssemblyName"},
{ "TargetFileName", "TargetFileName.dll"},
}).Object,
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/testproject.csproj", properties: new Dictionary<string, string>()
{
{ "AssemblyName", "TargetFileName"},
}).Object
Expand Down Expand Up @@ -93,13 +93,13 @@ public int Subtract(string first, string second)
{
ProjectInfo = new ProjectInfo(new MockFileSystem())
{
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/project.csproj", properties: new Dictionary<string, string>()
{
{ "TargetDir", "" },
{ "AssemblyName", "AssemblyName"},
{ "TargetFileName", "TargetFileName.dll"},
}).Object,
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/testproject.csproj", properties: new Dictionary<string, string>()
{
{ "AssemblyName", "TargetFileName"},
}).Object
Expand Down Expand Up @@ -146,13 +146,13 @@ public int Subtract(int first, int second)
{
ProjectInfo = new ProjectInfo(new MockFileSystem())
{
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/project.csproj", properties: new Dictionary<string, string>()
{
{ "TargetDir", "" },
{ "AssemblyName", "AssemblyName"},
{ "TargetFileName", "TargetFileName.dll"},
}).Object,
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/testproject.csproj", properties: new Dictionary<string, string>()
{
{ "AssemblyName", "TargetFileName"},
}).Object
Expand Down Expand Up @@ -297,13 +297,14 @@ public int Subtract(int first, int second)
ProjectInfo = new ProjectInfo(new MockFileSystem())
{
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(
projectFilePath: "/c/project.csproj",
properties: new Dictionary<string, string>()
{
{ "TargetDir", "" },
{ "TargetFileName", "TargetFileName.dll" },
{ "AssemblyName", "AssemblyName"},
}).Object,
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/testproject.csproj", properties: new Dictionary<string, string>()
{
{ "TargetDir", "" },
{ "TargetFileName", "TargetFileName.dll" },
Expand Down Expand Up @@ -426,6 +427,7 @@ private static IEnumerable<Mutant> MutateAndCompileSource(string sourceFile)
ProjectInfo = new ProjectInfo(fileSystem)
{
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(
projectFilePath: "/c/project.csproj",
properties: new Dictionary<string, string>()
{
{ "TargetDir", "Project" },
Expand All @@ -434,7 +436,7 @@ private static IEnumerable<Mutant> MutateAndCompileSource(string sourceFile)
}).Object,
TestProjectAnalyzerResults = new List<IAnalyzerResult>
{
TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/testproject.csproj", properties: new Dictionary<string, string>()
{
{ "AssemblyName", "TargetFileName" },
{ "TargetDir", "Test" },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
Expand Down Expand Up @@ -61,14 +60,14 @@ public void ShouldCallMutantOrchestratorAndReporter()
{
ProjectInfo = new ProjectInfo(fileSystem)
{
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/project.csproj", properties: new Dictionary<string, string>()
{
{ "TargetDir", "/bin/Debug/netcoreapp2.1" },
{ "TargetFileName", "TestName.dll" },
{ "AssemblyName", "AssemblyName" },
{ "Language", "C#" }
}).Object,
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/testproject.csproj", properties: new Dictionary<string, string>()
{
{ "TargetDir", "/bin/Debug/netcoreapp2.1" },
{ "TargetFileName", "TestName.dll" },
Expand Down Expand Up @@ -121,7 +120,7 @@ public void FilterMutantsShouldCallMutantFilters()

var folder = new CsharpFolderComposite();
folder.Add(inputFile);

var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ Path.Combine(FilesystemRoot, "ExampleProject","Recursive.cs"), new MockFileData(SourceFile)},
Expand All @@ -133,14 +132,14 @@ public void FilterMutantsShouldCallMutantFilters()
{
ProjectInfo = new ProjectInfo(fileSystem)
{
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/project.csproj", properties: new Dictionary<string, string>()
{
{ "TargetDir", "/bin/Debug/netcoreapp2.1" },
{ "TargetFileName", "TestName.dll" },
{ "AssemblyName", "AssemblyName" },
{ "Language", "C#" }
}).Object,
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/testproject.csproj", properties: new Dictionary<string, string>()
{
{ "TargetDir", "/bin/Debug/netcoreapp2.1" },
{ "TargetFileName", "TestName.dll" },
Expand Down Expand Up @@ -216,14 +215,14 @@ public void MutateShouldWriteToDisk_IfCompilationIsSuccessful()
{
ProjectInfo = new ProjectInfo(fileSystem)
{
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/project.csproj", properties: new Dictionary<string, string>()
{
{ "TargetDir", Path.Combine(FilesystemRoot, "ProjectUnderTest", "bin", "Debug", "netcoreapp2.0") },
{ "TargetFileName", "ProjectUnderTest.dll" },
{ "AssemblyName", "ProjectUnderTest.dll" },
{ "Language", "C#" }
}).Object,
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary<string, string>()
TestProjectAnalyzerResults = new List<IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(projectFilePath: "/c/testproject.csproj", properties: new Dictionary<string, string>()
{
{ "TargetDir", Path.Combine(FilesystemRoot, "TestProject", "bin", "Debug", "netcoreapp2.0") },
{ "TargetFileName", "TestProject.dll" },
Expand Down Expand Up @@ -255,7 +254,7 @@ public void MutateShouldWriteToDisk_IfCompilationIsSuccessful()
target.Mutate();

// Verify the created assembly is written to disk on the right location
string expectedPath = Path.Combine(FilesystemRoot, "TestProject", "bin", "Debug", "netcoreapp2.0", "ProjectUnderTest.dll");
var expectedPath = Path.Combine(FilesystemRoot, "TestProject", "bin", "Debug", "netcoreapp2.0", "ProjectUnderTest.dll");
fileSystem.ShouldContainFile(expectedPath);
}

Expand Down Expand Up @@ -407,7 +406,7 @@ public void ShouldHandleCoverage()
ProjectContents = folder
},
AssemblyReferences = _assemblies,
InitialTestRun = new InitialTestRun(scenario.GetInitialRunResult(), new TimeoutValueCalculator(500))
InitialTestRun = new InitialTestRun(scenario.GetInitialRunResult(), new TimeoutValueCalculator(500))
};

var options = new StrykerOptions
Expand All @@ -420,8 +419,8 @@ public void ShouldHandleCoverage()
var target = new MutationTestProcess(input, options, null, executor);
// test mutants
target.GetCoverage();
target.Test(input.ProjectInfo.ProjectContents.Mutants.Where(m=> m.ResultStatus == MutantStatus.NotRun));

target.Test(input.ProjectInfo.ProjectContents.Mutants.Where(m => m.ResultStatus == MutantStatus.NotRun));
// first mutant should be killed by test 2
scenario.GetMutantStatus(1).ShouldBe(MutantStatus.Killed);
// other mutant survives
Expand Down Expand Up @@ -483,7 +482,7 @@ public void ShouldNotKillMutantIfOnlyKilledByFailingTest()

// test mutants
target.GetCoverage();

target.Test(input.ProjectInfo.ProjectContents.Mutants);
// first mutant should be marked as survived
scenario.GetMutantStatus(1).ShouldBe(MutantStatus.Survived);
Expand Down Expand Up @@ -541,7 +540,7 @@ public void ShouldNotKillMutantIfOnlyCoveredByFailingTest()
var target = new MutationTestProcess(input, options, null, executor);
// test mutants
target.GetCoverage();

// first mutant should be marked as survived without any test
scenario.GetMutantStatus(1).ShouldBe(MutantStatus.Survived);
}
Expand Down Expand Up @@ -599,7 +598,7 @@ public void ShouldKillMutantKilledByFailingTestAndNormalTest()

// test mutants
target.GetCoverage();

target.Test(input.ProjectInfo.ProjectContents.Mutants);
// first mutant should be killed by test 2
scenario.GetMutantStatus(1).ShouldBe(MutantStatus.Killed);
Expand Down Expand Up @@ -664,7 +663,7 @@ public void ShouldThrowExceptionWhenOtherStatusThanNotRunIsPassed(MutantStatus s
[Fact]
public void ShouldNotTest_WhenThereAreNoMutationsAtAll()
{
string basePath = Path.Combine(FilesystemRoot, "ExampleProject.Test");
var basePath = Path.Combine(FilesystemRoot, "ExampleProject.Test");
var scenario = new FullRunScenario();
var folder = new CsharpFolderComposite();
folder.Add(new CsharpFileLeaf()
Expand Down Expand Up @@ -708,7 +707,7 @@ public void ShouldNotTest_WhenThereAreNoMutationsAtAll()
[Fact]
public void ShouldNotTest_WhenThereAreNoTestableMutations()
{
string basePath = Path.Combine(FilesystemRoot, "ExampleProject.Test");
var basePath = Path.Combine(FilesystemRoot, "ExampleProject.Test");

var folder = new CsharpFolderComposite();
folder.Add(new CsharpFileLeaf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static IEnumerable<ResourceDescription> GetResources(this IAnalyzerResult
var rootNamespace = analyzerResult.GetRootNamespace();
var embeddedResources = analyzerResult.GetItem("EmbeddedResource").Select(x => x.ItemSpec);
return EmbeddedResourcesGenerator.GetManifestResources(
analyzerResult.GetAssemblyPath(),
analyzerResult.ProjectFilePath,
rootNamespace,
embeddedResources);
Expand Down
Loading

0 comments on commit e919891

Please sign in to comment.