Skip to content

Commit

Permalink
moved package tests into test project
Browse files Browse the repository at this point in the history
  • Loading branch information
adamralph committed Feb 6, 2021
1 parent bb062b5 commit 4cb283e
Show file tree
Hide file tree
Showing 49 changed files with 1,227 additions and 493 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"gpgsign",
"nologo",
"nupkg",
"Sdks",
"shas",
"Versioner",
"Xbehave",
Expand Down
5 changes: 5 additions & 0 deletions .github/linters/.jscpd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ignore": [
"MinVerTests.Packages/**"
]
}
12 changes: 12 additions & 0 deletions MinVer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MinVer", "MinVer\MinVer.csp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "minver-cli", "minver-cli\minver-cli.csproj", "{34A5ECB1-400C-462A-B2B8-2B7136BE162A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MinVerTests.Infra", "MinVerTests.Infra\MinVerTests.Infra.csproj", "{AA98F752-F8EF-447D-BC85-4F277CAF4B82}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MinVerTests.Packages", "MinVerTests.Packages\MinVerTests.Packages.csproj", "{694ECAC7-FD05-4550-A4FB-8F497D29ECB0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,6 +43,14 @@ Global
{34A5ECB1-400C-462A-B2B8-2B7136BE162A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34A5ECB1-400C-462A-B2B8-2B7136BE162A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34A5ECB1-400C-462A-B2B8-2B7136BE162A}.Release|Any CPU.Build.0 = Release|Any CPU
{AA98F752-F8EF-447D-BC85-4F277CAF4B82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AA98F752-F8EF-447D-BC85-4F277CAF4B82}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA98F752-F8EF-447D-BC85-4F277CAF4B82}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA98F752-F8EF-447D-BC85-4F277CAF4B82}.Release|Any CPU.Build.0 = Release|Any CPU
{694ECAC7-FD05-4550-A4FB-8F497D29ECB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{694ECAC7-FD05-4550-A4FB-8F497D29ECB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{694ECAC7-FD05-4550-A4FB-8F497D29ECB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{694ECAC7-FD05-4550-A4FB-8F497D29ECB0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions MinVerTests.Infra/AssemblyVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#if !NETCOREAPP2_1
namespace MinVerTests.Infra
{
public record AssemblyVersion(int Major, int Minor, int Build, int Revision);
}
#endif
64 changes: 64 additions & 0 deletions MinVerTests.Infra/CommandExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CliWrap;
using CliWrap.Buffered;

namespace MinVerTests.Infra
{
internal static class CommandExtensions
{
private static int index;

public static async Task<BufferedCommandResult> ExecuteBufferedLoggedAsync(this Command command)
{
var validation = command.Validation;

var result = await command.WithValidation(CommandResultValidation.None).ExecuteBufferedAsync();

var index = Interlocked.Increment(ref CommandExtensions.index);

var log =
$@"
# Command {index}
## Target file path
`{command.TargetFilePath}`
## Arguments
`{command.Arguments}`
## Environment variables
```text
{string.Join(Environment.NewLine, command.EnvironmentVariables.Select(pair => $"{pair.Key}={pair.Value}"))}
```
## Exit code
`{result.ExitCode}`
## Standard error
```text
{result.StandardError}
```
## Standard output
```text
{result.StandardOutput}
```
";

await File.WriteAllTextAsync(Path.Combine(command.WorkingDirPath, $"command-{index:D2}.md"), log);

return result.ExitCode == 0 || validation == CommandResultValidation.None ? result : throw new Exception(log);
}
}
}
12 changes: 12 additions & 0 deletions MinVerTests.Infra/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace MinVerTests.Infra
{
internal static class Configuration
{
public const string Current =
#if DEBUG
"Debug";
#elif RELEASE
"Release";
#endif
}
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,33 @@
using System.IO;
using System.Threading;

namespace MinVerTests.Lib.Infra
namespace MinVerTests.Infra
{
// The spin waits are required. System.IO and the file system race. ¯\_(ツ)_/¯
public static class FileSystem
{
private static readonly int millisecondsTimeout = 50;

public static string GetScenarioDirectory(string scenarioName) => Path.Combine(Path.GetTempPath(), "minver-tests", scenarioName);

public static void EnsureEmptyDirectory(string path)
{
EnsureDirectoryDeleted(path);
EnsureDirectoryCreated(path);
}

private static void EnsureDirectoryCreated(string path)
{
if (SpinWait.SpinUntil(() => !Directory.Exists(path), millisecondsTimeout))
{
CreateDirectory(path);
}
}

private static void EnsureDirectoryDeleted(string path)
{
if (SpinWait.SpinUntil(() => Directory.Exists(path), millisecondsTimeout))
{
DeleteDirectory(path);
}
}

private static void CreateDirectory(string path)
{
Directory.CreateDirectory(path);

if (!SpinWait.SpinUntil(() => Directory.Exists(path), millisecondsTimeout))
{
throw new IOException($"Failed to create directory '{path}'.");
}
CreateDirectory(path);
}

private static void DeleteDirectory(string path)
{
// Directory.Delete fails if anything in the tree has the read-only attribute set. ¯\_(ツ)_/¯
ResetAttributes(new DirectoryInfo(path));

#if !NETCOREAPP2_1
static void ResetAttributes(DirectoryInfo directory)
#else
void ResetAttributes(DirectoryInfo directory)
#endif
{
foreach (var childDirectory in directory.GetDirectories())
{
Expand All @@ -69,5 +49,15 @@ void ResetAttributes(DirectoryInfo directory)
throw new IOException($"Failed to delete directory '{path}'.");
}
}

private static void CreateDirectory(string path)
{
_ = Directory.CreateDirectory(path);

if (!SpinWait.SpinUntil(() => Directory.Exists(path), millisecondsTimeout))
{
throw new IOException($"Failed to create directory '{path}'.");
}
}
}
}
6 changes: 6 additions & 0 deletions MinVerTests.Infra/FileVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#if !NETCOREAPP2_1
namespace MinVerTests.Infra
{
public record FileVersion(int FileMajorPart, int FileMinorPart, int FileBuildPart, int FilePrivatePart, string ProductVersion);
}
#endif
55 changes: 55 additions & 0 deletions MinVerTests.Infra/Git.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CliWrap;
using CliWrap.Buffered;

namespace MinVerTests.Infra
{
public static class Git
{
public static async Task EnsureEmptyRepositoryAndCommit(string path)
{
await EnsureEmptyRepository(path);
await Commit(path);
}

public static Task Commit(string path) =>
Cli.Wrap("git").WithArguments("commit -m '.' --allow-empty").WithWorkingDirectory(path).ExecuteAsync();

public static async Task EnsureEmptyRepository(string path)
{
FileSystem.EnsureEmptyDirectory(path);
await Init(path);
}

public static async Task Init(string path)
{
_ = await Cli.Wrap("git").WithArguments("init").WithWorkingDirectory(path).ExecuteAsync();
_ = await Cli.Wrap("git").WithArguments("config user.email [email protected]").WithWorkingDirectory(path).ExecuteAsync();
_ = await Cli.Wrap("git").WithArguments("config user.name John Doe").WithWorkingDirectory(path).ExecuteAsync();
_ = await Cli.Wrap("git").WithArguments("config commit.gpgsign false").WithWorkingDirectory(path).ExecuteAsync();
}

public static async Task<string> GetGraph(string path) =>
(await Cli.Wrap("git").WithArguments("log --graph --pretty=format:'%d'").WithWorkingDirectory(path).ExecuteBufferedAsync())
.StandardOutput;

public static Task Tag(string path, string tag) =>
Cli.Wrap("git").WithArguments($"tag {tag}").WithWorkingDirectory(path).ExecuteAsync();

public static Task Tag(string path, string tagName, string sha) =>
Cli.Wrap("git").WithArguments($"tag {tagName} {sha}").WithWorkingDirectory(path).ExecuteAsync();

public static Task AnnotatedTag(string path, string tag, string message) =>
Cli.Wrap("git").WithArguments($"tag {tag} -a -m '{message}'").WithWorkingDirectory(path).ExecuteAsync();

public static async Task<IEnumerable<string>> GetCommitShas(string path) =>
(await Cli.Wrap("git").WithArguments("log --pretty=format:\"%H\"").WithWorkingDirectory(path).ExecuteBufferedAsync())
.StandardOutput
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

public static Task Checkout(string path, string sha) =>
Cli.Wrap("git").WithArguments($"checkout {sha}").WithWorkingDirectory(path).ExecuteAsync();
}
}
26 changes: 26 additions & 0 deletions MinVerTests.Infra/MinVerCli.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using CliWrap;

namespace MinVerTests.Infra
{
public static class MinVerCli
{
public static async Task<(string, string)> Run(string workingDirectory, string configuration = Configuration.Current, params (string, string)[] envVars)
{
var environmentVariables = envVars.ToDictionary(envVar => envVar.Item1, envVar => envVar.Item2, StringComparer.OrdinalIgnoreCase);
_ = environmentVariables.TryAdd("MinVerVerbosity".ToAltCase(), "trace");

var result = await Cli.Wrap("dotnet")
.WithArguments($"exec {GetPath(configuration)}")
.WithEnvironmentVariables(environmentVariables)
.WithWorkingDirectory(workingDirectory).ExecuteBufferedLoggedAsync();

return (result.StandardOutput.Trim(), result.StandardError);
}

public static string GetPath(string configuration) =>
Solution.GetFullPath($"minver-cli/bin/{configuration}/netcoreapp2.1/minver-cli.dll");
}
}
11 changes: 11 additions & 0 deletions MinVerTests.Infra/MinVerTests.Infra.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CliWrap" Version="3.3.0" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions MinVerTests.Infra/Package.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#if !NETCOREAPP2_1
using System.Collections.Generic;
using System.Linq;

namespace MinVerTests.Infra
{
public record Package(string Version, AssemblyVersion AssemblyVersion, FileVersion FileVersion)
{
public static Package WithVersion(int Major, int Minor, int Patch, IEnumerable<string> PreReleaseIdentifiers = null, int Height = 0, string BuildMetadata = null)
{
var version = $"{Major}.{Minor}.{Patch}{(!(PreReleaseIdentifiers?.Any() ?? false) ? "" : $"-{string.Join(".", PreReleaseIdentifiers)}")}{(Height == 0 ? "" : $".{Height}")}{(string.IsNullOrEmpty(BuildMetadata) ? "" : $"+{BuildMetadata}")}";

return new Package(version, new AssemblyVersion(Major, 0, 0, 0), new FileVersion(Major, Minor, Patch, 0, version));
}
}
}
#endif
Loading

0 comments on commit 4cb283e

Please sign in to comment.