Skip to content

Commit

Permalink
Allow running all target frameworks for a unit test project
Browse files Browse the repository at this point in the history
  • Loading branch information
GeertvanHorrik committed Sep 20, 2024
1 parent 48fb657 commit d45b05d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 63 deletions.
35 changes: 25 additions & 10 deletions deployment/cake/lib-generic.cake
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,7 @@ private static bool IsDotNetCoreProject(BuildContext buildContext, string projec
var lowerCase = line.ToLower();
if (lowerCase.Contains("targetframework"))
{
if (lowerCase.Contains("netcore"))
{
isDotNetCore = true;
break;
}

if (lowerCase.Contains("net5") ||
lowerCase.Contains("net6") ||
lowerCase.Contains("net7") ||
lowerCase.Contains("net8"))
if (IsDotNetCoreTargetFramework(buildContext, lowerCase))
{
isDotNetCore = true;
break;
Expand All @@ -542,6 +533,30 @@ private static bool IsDotNetCoreProject(BuildContext buildContext, string projec

//-------------------------------------------------------------

private static bool IsDotNetCoreTargetFramework(BuildContext buildContext, string targetFramework)
{
var lowerCase = targetFramework.ToLower();

if (lowerCase.Contains("netcore"))
{
return true;
}

if (lowerCase.Contains("net5") ||
lowerCase.Contains("net6") ||
lowerCase.Contains("net7") ||
lowerCase.Contains("net8") ||
lowerCase.Contains("net9") ||
lowerCase.Contains("net10"))
{
return true;
}

return false;
}

//-------------------------------------------------------------

private static bool ShouldProcessProject(BuildContext buildContext, string projectName,
bool checkDeployment = true)
{
Expand Down
2 changes: 2 additions & 0 deletions deployment/cake/tests-variables.cake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class TestsContext : BuildContextWithItemsBase

public string Framework { get; set; }
public string TargetFramework { get; set; }
public string OperatingSystem { get; set; }
public string ProcessBit { get; set; }

protected override void ValidateContext()
Expand Down Expand Up @@ -47,6 +48,7 @@ private TestsContext InitializeTestsContext(BuildContext buildContext, IBuildCon

Framework = buildContext.BuildServer.GetVariable("TestFramework", "nunit", showValue: true),
TargetFramework = buildContext.BuildServer.GetVariable("TestTargetFramework", "", showValue: true),
OperatingSystem = buildContext.BuildServer.GetVariable("TestOperatingSystem", "win", showValue: true),
ProcessBit = buildContext.BuildServer.GetVariable("TestProcessBit", "X64", showValue: true)
};

Expand Down
115 changes: 62 additions & 53 deletions deployment/cake/tests.cake
Original file line number Diff line number Diff line change
Expand Up @@ -137,63 +137,70 @@ private static void RunUnitTests(BuildContext buildContext, string projectName)

var ranTests = false;
var failed = false;
var testTargetFramework = GetTestTargetFramework(buildContext, projectName);
var testTargetFrameworks = GetTestTargetFrameworks(buildContext, projectName);

try
{
if (IsDotNetCoreProject(buildContext, projectName))
foreach (var testTargetFramework in testTargetFrameworks)
{
buildContext.CakeContext.Information($"Project '{projectName}' is a .NET core project, using 'dotnet test' to run the unit tests");

var projectFileName = GetProjectFileName(buildContext, projectName);

var dotNetTestSettings = new DotNetTestSettings
{
Configuration = buildContext.General.Solution.ConfigurationName,
// Loggers = new []
// {
// "nunit;LogFilePath=test-result.xml"
// },
NoBuild = true,
NoLogo = true,
NoRestore = true,
OutputDirectory = System.IO.Path.Combine(GetProjectOutputDirectory(buildContext, projectName), testTargetFramework),
ResultsDirectory = testResultsDirectory
};

if (IsNUnitTestProject(buildContext, projectName))
{
dotNetTestSettings.ArgumentCustomization = args => args
.Append($"-- NUnit.TestOutputXml={testResultsDirectory}");
}
LogSeparator(buildContext.CakeContext, "Running tests for target framework {0}", testTargetFramework);

if (IsXUnitTestProject(buildContext, projectName))
if (IsDotNetCoreTargetFramework(buildContext, testTargetFramework))
{
var outputFileName = System.IO.Path.Combine(testResultsDirectory, $"{projectName}.xml");
buildContext.CakeContext.Information($"Project '{projectName}' is a .NET core project, using 'dotnet test' to run the unit tests");

var projectFileName = GetProjectFileName(buildContext, projectName);

var dotNetTestSettings = new DotNetTestSettings
{
Configuration = buildContext.General.Solution.ConfigurationName,
// Loggers = new []
// {
// "nunit;LogFilePath=test-result.xml"
// },
NoBuild = true,
NoLogo = true,
NoRestore = true,
OutputDirectory = System.IO.Path.Combine(GetProjectOutputDirectory(buildContext, projectName), testTargetFramework),
ResultsDirectory = testResultsDirectory
};

if (IsNUnitTestProject(buildContext, projectName))
{
dotNetTestSettings.ArgumentCustomization = args => args
.Append($"-- NUnit.TestOutputXml={testResultsDirectory}");
}

if (IsXUnitTestProject(buildContext, projectName))
{
var outputFileName = System.IO.Path.Combine(testResultsDirectory, $"{projectName}.xml");

dotNetTestSettings.ArgumentCustomization = args => args
.Append($"-l:trx;LogFileName={outputFileName}");
}

var processBit = buildContext.Tests.ProcessBit.ToLower();
if (!string.IsNullOrWhiteSpace(processBit))
{
dotNetTestSettings.Runtime = $"{buildContext.Tests.OperatingSystem}-{processBit}";
}

buildContext.CakeContext.Information($"Runtime: '{dotNetTestSettings.Runtime}'");

buildContext.CakeContext.DotNetTest(projectFileName, dotNetTestSettings);

dotNetTestSettings.ArgumentCustomization = args => args
.Append($"-l:trx;LogFileName={outputFileName}");
ranTests = true;
}

var processBit = buildContext.Tests.ProcessBit.ToLower();
if (!string.IsNullOrWhiteSpace(processBit))
else
{
dotNetTestSettings.Runtime = $"win-{processBit}";
}

buildContext.CakeContext.DotNetTest(projectFileName, dotNetTestSettings);

ranTests = true;
}
else
{
buildContext.CakeContext.Information($"Project '{projectName}' is a .NET project, using '{buildContext.Tests.Framework} runner' to run the unit tests");
buildContext.CakeContext.Information($"Project '{projectName}' is a .NET project, using '{buildContext.Tests.Framework} runner' to run the unit tests");

if (IsNUnitTestProject(buildContext, projectName))
{
RunTestsUsingNUnit(buildContext, projectName, testTargetFramework, testResultsDirectory);
if (IsNUnitTestProject(buildContext, projectName))
{
RunTestsUsingNUnit(buildContext, projectName, testTargetFramework, testResultsDirectory);

ranTests = true;
ranTests = true;
}
}
}
}
Expand Down Expand Up @@ -273,28 +280,30 @@ private static bool IsXUnitTestProject(BuildContext buildContext, string project

//-------------------------------------------------------------

private static string GetTestTargetFramework(BuildContext buildContext, string projectName)
private static IReadOnlyList<string> GetTestTargetFrameworks(BuildContext buildContext, string projectName)
{
// Step 1: if defined, use defined value
var testTargetFramework = buildContext.Tests.TargetFramework;
if (!string.IsNullOrWhiteSpace(testTargetFramework))
{
buildContext.CakeContext.Information("Using test target framework '{0}', specified via the configuration", testTargetFramework);

return testTargetFramework;
return new []
{
testTargetFramework
};
}

buildContext.CakeContext.Information("Test target framework not specified, auto detecting test target framework");
buildContext.CakeContext.Information("Test target framework not specified, auto detecting test target frameworks");

var targetFrameworks = GetTargetFrameworks(buildContext, projectName);
testTargetFramework = targetFrameworks.FirstOrDefault();

buildContext.CakeContext.Information("Auto detected test target framework '{0}'", testTargetFramework);
buildContext.CakeContext.Information("Auto detected test target frameworks '{0}'", string.Join(", ", targetFrameworks));

if (string.IsNullOrWhiteSpace(testTargetFramework))
if (targetFrameworks.Length == 0)
{
throw new Exception(string.Format("Test target framework could not automatically be detected for project '{0]'", projectName));
}

return testTargetFramework;
return targetFrameworks;
}

0 comments on commit d45b05d

Please sign in to comment.