Skip to content

Commit 02193a7

Browse files
Merge branch 'master' into baseline-command
2 parents ca9b354 + b32cef2 commit 02193a7

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

src/Stryker.Core/Stryker.Core.UnitTest/Initialisation/InitialBuildProcessTests.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ namespace Stryker.Core.UnitTest.Initialisation
1111
{
1212
public class InitialBuildProcessTests : TestBase
1313
{
14+
private string _cProjectsExampleCsproj;
15+
16+
public InitialBuildProcessTests()
17+
{
18+
_cProjectsExampleCsproj = Environment.OSVersion.Platform == PlatformID.Win32NT ? @"C:\Projects\Example.csproj" : "/usr/projects/Example.csproj";
19+
}
20+
1421
[Fact]
1522
public void InitialBuildProcess_ShouldThrowStrykerInputExceptionOnFail()
1623
{
@@ -20,8 +27,8 @@ public void InitialBuildProcess_ShouldThrowStrykerInputExceptionOnFail()
2027

2128
var target = new InitialBuildProcess(processMock.Object);
2229

23-
Should.Throw<InputException>(() => target.InitialBuild(false, @"C:\Projects\Example.csproj", null))
24-
.Details.ShouldBe("Initial build of targeted project failed. Please make sure the targeted project is buildable. You can reproduce this error yourself using: \"dotnet build \"" + @"C:\Projects\Example.csproj" + "\"\"");
30+
Should.Throw<InputException>(() => target.InitialBuild(false, _cProjectsExampleCsproj, null))
31+
.Details.ShouldBe("Initial build of targeted project failed. Please make sure the targeted project is buildable. You can reproduce this error yourself using: \"dotnet build \"" + @"Example.csproj" + "\"\"");
2532
}
2633

2734
[SkippableFact]
@@ -35,9 +42,28 @@ public void InitialBuildProcess_WithPathAsBuildCommand_ShouldThrowStrykerInputEx
3542

3643
var target = new InitialBuildProcess(processMock.Object);
3744

38-
Should.Throw<InputException>(() => target.InitialBuild(true, null, @"C:\Projects\Example.csproj", @"C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe"))
45+
Should.Throw<InputException>(() => target.InitialBuild(true, null, _cProjectsExampleCsproj, @"C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe"))
3946
.Details.ShouldBe("Initial build of targeted project failed. Please make sure the targeted project is buildable. You can reproduce this error yourself using: \"\"" +
40-
@"C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe" + "\" \"" + @"C:\Projects\Example.csproj" + "\"\"");
47+
@"C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe" + "\" \"" + _cProjectsExampleCsproj + "\"\"");
48+
}
49+
50+
[SkippableFact]
51+
public void InitialBuildProcess_WithPathAsBuildCommand_TriesWithMsBuildIfDotnetFails()
52+
{
53+
Skip.IfNot(Environment.OSVersion.Platform == PlatformID.Win32NT, "MSBuild is only available on Windows");
54+
55+
var processMock = new Mock<IProcessExecutor>(MockBehavior.Strict);
56+
57+
processMock.SetupProcessMockToReturn("", 2);
58+
59+
var target = new InitialBuildProcess(processMock.Object);
60+
61+
Should.Throw<InputException>(() => target.InitialBuild(false, null, _cProjectsExampleCsproj, @"C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe"))
62+
.Details.ShouldBe("Initial build of targeted project failed. Please make sure the targeted project is buildable. You can reproduce this error yourself using: \"\"" +
63+
@"C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe" + "\" \"" + _cProjectsExampleCsproj + "\"\"");
64+
65+
processMock.Verify(x =>x.Start(It.IsAny<string>(), It.Is<string>(app => app.Contains("dotnet")), It.IsAny<string>(), It.IsAny<IEnumerable<KeyValuePair<string, string>>>(), 0), Times.Once());
66+
processMock.Verify(x =>x.Start(It.IsAny<string>(), It.Is<string>(app => app.Contains("MSBuild.exe")), It.IsAny<string>(), It.IsAny<IEnumerable<KeyValuePair<string, string>>>(), 0), Times.Once());
4167
}
4268

4369
[Fact]

src/Stryker.Core/Stryker.Core/Initialisation/InitialBuildProcess.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,57 @@ public void InitialBuild(bool fullFramework, string projectPath, string solution
2828
_logger.LogDebug("Started initial build using {0}", fullFramework ? "msbuild.exe" : "dotnet build");
2929

3030
ProcessResult result;
31+
string buildCommand;
32+
string buildPath;
3133
if (fullFramework)
3234
{
3335
if (string.IsNullOrEmpty(solutionPath))
3436
{
3537
throw new InputException("Stryker could not build your project as no solution file was presented. Please pass the solution path to stryker.");
3638
}
37-
solutionPath = Path.GetFullPath(solutionPath);
38-
var solutionDir = Path.GetDirectoryName(solutionPath);
39-
msbuildPath ??= new MsBuildHelper().GetMsBuildPath(_processExecutor);
40-
41-
// Build project with MSBuild.exe
42-
result = _processExecutor.Start(solutionDir, msbuildPath, $"\"{solutionPath}\"");
43-
CheckBuildResult(result, msbuildPath, $"\"{solutionPath}\"");
39+
result = BuildSolutionWithMsBuild(ref solutionPath, ref msbuildPath);
40+
buildCommand = msbuildPath;
41+
buildPath = solutionPath;
4442
}
4543
else
4644
{
47-
var buildPath = !string.IsNullOrEmpty(solutionPath) ? solutionPath : Path.GetFileName(projectPath);
45+
buildPath = !string.IsNullOrEmpty(solutionPath) ? solutionPath : Path.GetFileName(projectPath);
46+
buildCommand = "dotnet build";
4847

4948
_logger.LogDebug("Initial build using path: {buildPath}", buildPath);
5049
// Build with dotnet build
5150
result = _processExecutor.Start(projectPath, "dotnet", $"build \"{buildPath}\"");
51+
if (result.ExitCode!=ExitCodes.Success && !string.IsNullOrEmpty(solutionPath))
52+
{
53+
_logger.LogWarning("Dotnet build failed, trying with MsBuild.");
54+
buildCommand = msbuildPath;
55+
result = BuildSolutionWithMsBuild(ref solutionPath, ref msbuildPath);
56+
}
5257

53-
CheckBuildResult(result, "dotnet build", $"\"{projectPath}\"");
5458
}
59+
CheckBuildResult(result, buildCommand, $"\"{buildPath}\"");
60+
}
61+
62+
private ProcessResult BuildSolutionWithMsBuild(ref string solutionPath, ref string msbuildPath)
63+
{
64+
solutionPath = Path.GetFullPath(solutionPath);
65+
var solutionDir = Path.GetDirectoryName(solutionPath);
66+
msbuildPath ??= new MsBuildHelper().GetMsBuildPath(_processExecutor);
67+
68+
// Build project with MSBuild.exe
69+
var result = _processExecutor.Start(solutionDir, msbuildPath, $"\"{solutionPath}\"");
70+
return result;
5571
}
5672

5773
private void CheckBuildResult(ProcessResult result, string buildCommand, string buildPath)
5874
{
59-
_logger.LogTrace("Initial build output {0}", result.Output);
6075
if (result.ExitCode != ExitCodes.Success)
6176
{
77+
_logger.LogError("Initial build failed: {0}", result.Output);
6278
// Initial build failed
6379
throw new InputException(result.Output, FormatBuildResultErrorString(buildCommand, buildPath));
6480
}
81+
_logger.LogTrace("Initial build output {0}", result.Output);
6582
_logger.LogDebug("Initial build successful");
6683
}
6784

0 commit comments

Comments
 (0)