Skip to content

Commit

Permalink
* Shifted around test defaults (coverage directory, test results dire…
Browse files Browse the repository at this point in the history
…ctory, cleaning etc.) into extension methods

* Added net8.0 runtime for report generator and gitversion
* Ensure runsettings overwrites correctly.
  • Loading branch information
david-driscoll committed Nov 16, 2023
1 parent 26aec8c commit 3d4a9e2
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 175 deletions.
8 changes: 6 additions & 2 deletions src/Nuke/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ namespace Rocket.Surgery.Nuke;
internal static class Constants
{
public const string ReportGeneratorFramework =
#if NET7_0_OR_GREATER
#if NET8_0_OR_GREATER
"net8.0"
#elif NET7_0_OR_GREATER
"net7.0"
#elif NET6_0_OR_GREATER
"net6.0"
Expand All @@ -15,7 +17,9 @@ internal static class Constants
;

public const string GitVersionFramework =
#if NET7_0_OR_GREATER
#if NET8_0_OR_GREATER
"net8.0"
#elif NET7_0_OR_GREATER
"net7.0"
#elif NET6_0_OR_GREATER
"net6.0"
Expand Down
97 changes: 29 additions & 68 deletions src/Nuke/DotNetCore/ICanTestWithDotNetCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,74 +38,35 @@ public interface ICanTestWithDotNetCore : IHaveCollectCoverage,
.EnableNoRestore()
)
)
.CreateOrCleanDirectory(TestResultsDirectory)
.CleanCoverageDirectory(CoverageDirectory)
.EnsureRunSettingsExists(RunSettings)
.Executes(
() =>
{
TestResultsDirectory.CreateOrCleanDirectory();
CoverageDirectory.GlobFiles("*.cobertura.xml", "*.opencover.xml", "*.json", "*.info")
.Where(x => Guid.TryParse(Path.GetFileName(x).Split('.')[0], out var _))
.ForEach(AbsolutePathExtensions.DeleteFile);
}
() => DotNetTasks.DotNetTest(
s => s
.SetProjectFile(Solution)
.SetDefaultLoggers(LogsDirectory / "test.log")
.SetGitVersionEnvironment(GitVersion)
.SetConfiguration("Debug")
.EnableNoRestore()
.EnableNoBuild()
.SetLoggers("trx")
// DeterministicSourcePaths being true breaks coverlet!
.SetProperty("DeterministicSourcePaths", "false")
.SetResultsDirectory(TestResultsDirectory)
.When(
!CollectCoverage,
x => x.SetProperty((string)"CollectCoverage", "true")
.SetProperty("CoverageDirectory", CoverageDirectory)
)
.When(
CollectCoverage,
x => x
.SetProperty("CollectCoverage", "false")
.SetDataCollector("XPlat Code Coverage")
.SetSettingsFile(RunSettings)
)
)
)
.Executes(
async () =>
{
// ReSharper disable once IdentifierTypo
// ReSharper disable once StringLiteralTypo
var runsettings = TestsDirectory / "coverlet.runsettings";
if (!runsettings.FileExists())
{
// ReSharper disable once StringLiteralTypo
runsettings = NukeBuild.TemporaryDirectory / "default.runsettings";
await using var tempFile = File.Open(runsettings, runsettings.Exists() ? FileMode.Truncate : FileMode.CreateNew);
await typeof(ICanTestWithDotNetCore).Assembly
// ReSharper disable once NullableWarningSuppressionIsUsed
.GetManifestResourceStream("Rocket.Surgery.Nuke.default.runsettings")!
.CopyToAsync(tempFile)
.ConfigureAwait(false);
}

DotNetTasks.DotNetTest(
s => s
.SetProjectFile(Solution)
.SetDefaultLoggers(LogsDirectory / "test.log")
.SetGitVersionEnvironment(GitVersion)
.SetConfiguration("Debug")
.EnableNoRestore()
.EnableNoBuild()
.SetLoggers("trx")
// DeterministicSourcePaths being true breaks coverlet!
.SetProperty("DeterministicSourcePaths", "false")
.SetResultsDirectory(TestResultsDirectory)
.When(
!CollectCoverage,
x => x.SetProperty((string)"CollectCoverage", "true")
.SetProperty("CoverageDirectory", CoverageDirectory)
)
.When(
CollectCoverage,
x => x
.SetProperty("CollectCoverage", "false")
.SetDataCollector("XPlat Code Coverage")
.SetSettingsFile(runsettings)
)
);

// Ensure anything that has been dropped in the test results from a collector is
// into the coverage directory
foreach (var file in TestResultsDirectory
.GlobFiles("**/*.cobertura.xml")
.Where(x => Guid.TryParse(Path.GetFileName(x.Parent), out var _))
.SelectMany(coverage => coverage.Parent.GlobFiles("*.*")))
{
var folderName = Path.GetFileName(file.Parent);
var extensionPart = string.Join(".", Path.GetFileName(file).Split('.').Skip(1));
CopyFile(
file,
CoverageDirectory / $"{folderName}.{extensionPart}",
FileExistsPolicy.OverwriteIfNewer
);
}
}
);
.CollectCoverage(TestResultsDirectory, CoverageDirectory);
}
93 changes: 28 additions & 65 deletions src/Nuke/DotNetCore/ICanTestWithDotNetCoreBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,71 +41,34 @@ public interface ICanTestWithDotNetCoreBuild : IHaveCollectCoverage,
.SetPackageVersion(GitVersion?.NuGetVersionV2)
)
)
.CreateOrCleanDirectory(TestResultsDirectory)
.CleanCoverageDirectory(CoverageDirectory)
.EnsureRunSettingsExists(RunSettings)
.Executes(
() =>
{
TestResultsDirectory.CreateOrCleanDirectory();
CoverageDirectory.GlobFiles("*.cobertura.xml", "*.opencover.xml", "*.json", "*.info")
.Where(x => Guid.TryParse(Path.GetFileName(x).Split('.')[0], out var _))
.ForEach(DeleteFile);
}
() => DotNetTasks.DotNetTest(
s => s.SetProjectFile(Solution)
.SetDefaultLoggers(LogsDirectory / "test.log")
.SetGitVersionEnvironment(GitVersion)
.EnableNoRestore()
.SetLoggers("trx")
.SetConfiguration(Configuration)
.EnableNoBuild()
// DeterministicSourcePaths being true breaks coverlet!
.SetProperty("DeterministicSourcePaths", "false")
.SetResultsDirectory(TestResultsDirectory)
.When(
!CollectCoverage,
x => x.SetProperty((string)"CollectCoverage", "true")
.SetProperty("CoverageDirectory", CoverageDirectory)
)
.When(
CollectCoverage,
x => x
.SetProperty("CollectCoverage", "false")
.SetDataCollector("XPlat Code Coverage")
.SetSettingsFile(RunSettings)
)
)
)
.Executes(
() =>
{
var runSettings = TestsDirectory / "coverlet.runsettings";
if (!runSettings.FileExists())
{
runSettings = NukeBuild.TemporaryDirectory / "default.runsettings";
if (!runSettings.FileExists())
{
using var tempFile = File.Open(runSettings, FileMode.CreateNew);
typeof(ICanTestWithDotNetCore)
.Assembly
.GetManifestResourceStream("Rocket.Surgery.Nuke.default.runsettings")!.CopyTo(tempFile);
}
}

DotNetTasks.DotNetTest(
s => s.SetProjectFile(Solution)
.SetDefaultLoggers(LogsDirectory / "test.log")
.SetGitVersionEnvironment(GitVersion)
.EnableNoRestore()
.SetLoggers("trx")
.SetConfiguration(Configuration)
.EnableNoBuild()
// DeterministicSourcePaths being true breaks coverlet!
.SetProperty("DeterministicSourcePaths", "false")
.SetResultsDirectory(TestResultsDirectory)
.When(
!CollectCoverage,
x => x.SetProperty((string)"CollectCoverage", "true")
.SetProperty("CoverageDirectory", CoverageDirectory)
)
.When(
CollectCoverage,
x => x
.SetProperty("CollectCoverage", "false")
.SetDataCollector("XPlat Code Coverage")
.SetSettingsFile(runSettings)
)
);

// Ensure anything that has been dropped in the test results from a collector is
// into the coverage directory
foreach (var file in TestResultsDirectory
.GlobFiles("**/*.cobertura.xml")
.Where(x => Guid.TryParse(Path.GetFileName(x.Parent), out var _))
.SelectMany(coverage => coverage.Parent.GlobFiles("*.*")))
{
var folderName = Path.GetFileName(file.Parent);
var extensionPart = string.Join(".", Path.GetFileName(file).Split('.').Skip(1));
CopyFile(
file,
CoverageDirectory / $"{folderName}.{extensionPart}",
FileExistsPolicy.OverwriteIfNewer
);
}
}
);
.CollectCoverage(TestResultsDirectory, CoverageDirectory);
}
2 changes: 1 addition & 1 deletion src/Nuke/DotNetCore/IHaveMsBuildCoverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Rocket.Surgery.Nuke.DotNetCore;

/// <summary>
/// Defines `CollectCoverage` as flase to utilize the coverlet msbuild collector
/// Defines `CollectCoverage` as false to utilize the coverlet msbuild collector
/// </summary>
public interface IHaveMsBuildCoverage : IHaveCollectCoverage
{
Expand Down
29 changes: 29 additions & 0 deletions src/Nuke/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
using Nuke.Common.IO;
using Nuke.Common.Tooling;
using Nuke.Common.Tools.ReportGenerator;
using Nuke.Common.Utilities.Collections;
using Rocket.Surgery.Nuke.DotNetCore;
using static Nuke.Common.IO.FileSystemTasks;

namespace Rocket.Surgery.Nuke;

#pragma warning disable CA1724
Expand All @@ -16,4 +23,26 @@ public static T As<T>(this T value)
{
return value;
}

// ReSharper disable once CommentTypo
/// <summary>
/// A method that ensures the given directory exists or is cleaned
/// </summary>
/// <param name="target"></param>
/// <param name="testResultsDirectory"></param>
/// <returns></returns>
public static ITargetDefinition CreateOrCleanDirectory(this ITargetDefinition target, AbsolutePath testResultsDirectory)
{
return target.Executes(testResultsDirectory.CreateOrCleanDirectory);
}

/// <summary>
/// <p><em>Sets <see cref="ReportGeneratorSettings.Reports"/> to a new list</em></p>
/// <p>The coverage reports that should be parsed (separated by semicolon). Wildcards are allowed.</p>
/// </summary>
[Pure]
public static T SetReports<T>(this T toolSettings, IEnumerable<AbsolutePath> reports) where T : ReportGeneratorSettings
{
return toolSettings.SetReports(reports.Select(z => z.ToString()).ToArray());
}
}
33 changes: 33 additions & 0 deletions src/Nuke/FilePathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,38 @@ public static AbsolutePath PickDirectory(params AbsolutePath[] paths)
return paths.First();
}

/// <summary>
/// Returns the first directory that exists on disk
/// </summary>
/// <remarks>
/// Caches the result for faster lookups later
/// </remarks>
/// <param name="paths"></param>
/// <returns></returns>
public static AbsolutePath PickFile(params AbsolutePath[] paths)
{
foreach (var path in paths)
{
if (Cache.TryGetValue(path, out var _))
{
return path;
}

if (!path.FileExists())
{
continue;
}

foreach (var p in paths)
{
Cache.TryAdd(p, path);
}

return path;
}

return paths.First();
}

private static readonly ConcurrentDictionary<AbsolutePath, AbsolutePath> Cache = new();
}
10 changes: 6 additions & 4 deletions src/Nuke/IComprehendTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ public interface IComprehendTests : IComprehend
/// <summary>
/// The directory where tests will be placed
/// </summary>
public AbsolutePath TestsDirectory => FilePathExtensions.PickDirectory(
NukeBuild.RootDirectory / "test",
NukeBuild.RootDirectory / "tests"
);
public AbsolutePath TestsDirectory => FilePathExtensions.PickDirectory(NukeBuild.RootDirectory / "test", NukeBuild.RootDirectory / "tests");

/// <summary>
/// The default path to look for user (eg. commited to the repo) test runsettings
/// </summary>
public AbsolutePath RunSettings => FilePathExtensions.PickFile(TestsDirectory / "settings.runsettings", TestsDirectory / "tests.runsettings", TestsDirectory / "coverlet.runsettings");
}
1 change: 1 addition & 0 deletions src/Nuke/IHaveSolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public interface IHaveSolution : IHave
/// The solution currently being build
/// </summary>
[Solution]
// ReSharper disable once NullableWarningSuppressionIsUsed
public Solution Solution => TryGetValue(() => Solution)!;
}
Loading

0 comments on commit 3d4a9e2

Please sign in to comment.