Skip to content

Commit

Permalink
test: Add new method in DockerFixture and modify tests
Browse files Browse the repository at this point in the history
A new method `StartWithInstalledHusky` has been added to `DockerFixture`, this method provides an easier way to get a container with Husky installed. Some code rearrangement and wording modifications were made in the `Extensions` and `Tests` classes to make test cases clearer and more understandable. The newly created TestProjectBase solution was also added.
  • Loading branch information
alirezanet committed Dec 17, 2023
1 parent f9ebb54 commit 037af15
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 37 deletions.
22 changes: 22 additions & 0 deletions tests/HuskyIntegrationTests/TestProjectBase/TestProjectBase.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProjectBase", "TestProjectBase.csproj", "{A53CD1DD-6DB6-4ADD-82E5-F75A15514523}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A53CD1DD-6DB6-4ADD-82E5-F75A15514523}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A53CD1DD-6DB6-4ADD-82E5-F75A15514523}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A53CD1DD-6DB6-4ADD-82E5-F75A15514523}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A53CD1DD-6DB6-4ADD-82E5-F75A15514523}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
61 changes: 26 additions & 35 deletions tests/HuskyIntegrationTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,36 @@

namespace HuskyIntegrationTests;

public class Tests : IClassFixture<DockerFixture>
public class Tests(DockerFixture docker, ITestOutputHelper output) : IClassFixture<DockerFixture>
{
private readonly DockerFixture _docker;
private readonly ITestOutputHelper _output;

public Tests(DockerFixture docker, ITestOutputHelper output)
{
_docker = docker;
_output = output;
// TestcontainersSettings.Logger = new DockerLogger(output);
}

[Fact]
public async Task IntegrationTestCopyFolder()
{
var c = await _docker.CopyAndStartAsync(nameof(TestProjectBase));
public async Task IntegrationTestCopyFolder()
{
var c = await docker.CopyAndStartAsync(nameof(TestProjectBase));

await c.BashAsync("git init");
await c.BashAsync("dotnet tool restore");
await c.BashAsync("dotnet husky install");
var result = await c.BashAsync(_output, "dotnet husky run");
await c.BashAsync("git init");
await c.BashAsync("dotnet tool restore");
await c.BashAsync("dotnet husky install");
var result = await c.BashAsync(output, "dotnet husky run");

result.Stdout.Should().Contain("✔ Successfully executed");
result.ExitCode.Should().Be(0);
}
result.Stdout.Should().Contain(Extensions.SuccessfullyExecuted);
result.ExitCode.Should().Be(0);
}

[Fact]
public async Task IntegrationTest()
{
var c = await _docker.StartAsync();

await c.BashAsync("dotnet new classlib");
await c.BashAsync("dotnet new tool-manifest");
await c.BashAsync("dotnet tool install Husky");
await c.BashAsync("git init");
await c.BashAsync("dotnet husky install");
var result = await c.BashAsync(_output, "dotnet husky run");

result.Stdout.Should().Contain("✔ Successfully executed");
result.ExitCode.Should().Be(0);
}
[Fact]
public async Task IntegrationTest()
{
var c = await docker.StartAsync();

await c.BashAsync("dotnet new classlib");
await c.BashAsync("dotnet new tool-manifest");
await c.BashAsync("dotnet tool install Husky");
await c.BashAsync("git init");
await c.BashAsync("dotnet husky install");
var result = await c.BashAsync(output, "dotnet husky run");

result.Stdout.Should().Contain(Extensions.SuccessfullyExecuted);
result.ExitCode.Should().Be(0);
}
}
14 changes: 14 additions & 0 deletions tests/HuskyIntegrationTests/Utilities/DockerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using DotNet.Testcontainers.Images;
using HuskyIntegrationTests;

public class DockerFixture : IAsyncDisposable
{
Expand Down Expand Up @@ -67,6 +68,19 @@ public async Task<IContainer> StartAsync([CallerMemberName] string name = null!)
return container;
}


public async Task<IContainer> StartWithInstalledHusky([CallerMemberName] string name = null!)
{
var c = await CopyAndStartAsync(nameof(TestProjectBase), name);
await c.BashAsync("git init");
await c.BashAsync("dotnet tool restore");
await c.BashAsync("dotnet husky install");
await c.BashAsync("git add .");
await c.BashAsync("git commit -m 'initial commit'");
return c;
}


private static string GenerateContainerName(string name)
{
return $"{name}-{Guid.NewGuid().ToString("N")[..4]}";
Expand Down
12 changes: 10 additions & 2 deletions tests/HuskyIntegrationTests/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace HuskyIntegrationTests;

public static class Extensions
{
public const string SuccessfullyExecuted = "✔ Successfully executed";

public static Task<ExecResult> BashAsync(this IContainer container, params string[] command)
{
return container.ExecAsync(["/bin/bash", "-c", ..command]);
Expand All @@ -12,12 +14,18 @@ public static Task<ExecResult> BashAsync(this IContainer container, params strin
public static async Task<ExecResult> BashAsync(this IContainer container, ITestOutputHelper output, params string[] command)
{
var result = await container.ExecAsync(["/bin/bash", "-c", ..command]);
if (!string.IsNullOrEmpty(result.Stderr))
output.WriteLine(result.Stderr);

if (!string.IsNullOrEmpty(result.Stdout))
output.WriteLine(result.Stdout);

if (!string.IsNullOrEmpty(result.Stderr))
output.WriteLine(result.Stderr);

return result;
}

public static Task<ExecResult> UpdateTaskRunner(this IContainer container, string content)
{
return container.BashAsync($"echo -e '{content}' > /test/.husky/task-runner.json");
}
}

0 comments on commit 037af15

Please sign in to comment.