-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add e2e tests for app version and for single package promotion
- Loading branch information
Showing
4 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Promote.NuGet\Promote.NuGet.csproj" /> | ||
<ProjectReference Include="..\Promote.NuGet.TestInfrastructure\Promote.NuGet.TestInfrastructure.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
using System.Diagnostics; | ||
using System.IO; | ||
|
||
namespace Promote.NuGet.Tests; | ||
|
||
public static class PromoteNugetProcessRunner | ||
{ | ||
public sealed class ProcessWrapper : IAsyncDisposable | ||
{ | ||
public Process Process { get; } | ||
|
||
public ProcessWrapper(Process process) | ||
{ | ||
Process = process; | ||
} | ||
|
||
public void WaitForExit() | ||
{ | ||
Process.WaitForExit(); | ||
} | ||
|
||
public bool WaitForExit(int milliseconds) | ||
{ | ||
return Process.WaitForExit(milliseconds); | ||
} | ||
|
||
public Task WaitForExitAsync(CancellationToken cancellationToken = default) | ||
{ | ||
return Process.WaitForExitAsync(cancellationToken); | ||
} | ||
|
||
public int ExitCode => Process.ExitCode; | ||
|
||
public StreamReader StandardError => Process.StandardError; | ||
|
||
public StreamReader StandardOutput => Process.StandardOutput; | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
if (Process.HasExited == false) | ||
{ | ||
TestContext.WriteLine("The process is still running. Dumping its output and killing the process."); | ||
TestContext.WriteLine("Error output:"); | ||
TestContext.WriteLine(await Process.StandardError.ReadToEndAsync()); | ||
TestContext.WriteLine("Standard output:"); | ||
TestContext.WriteLine(await Process.StandardOutput.ReadToEndAsync()); | ||
|
||
TestContext.WriteLine("Killing..."); | ||
Process.Kill(); | ||
await Process.WaitForExitAsync(); | ||
|
||
TestContext.WriteLine("The process is stopped."); | ||
} | ||
|
||
Process.Dispose(); | ||
} | ||
} | ||
|
||
public record ProcessRunResult(int ExitCode, IReadOnlyCollection<string> StdOutput, IReadOnlyCollection<string> StdError); | ||
|
||
public static async Task<ProcessRunResult> RunForResultAsync(params string[] arguments) | ||
{ | ||
await using var process = await RunToExitAsync(arguments); | ||
|
||
var stdOutput = new List<string>(); | ||
while (await process.StandardOutput.ReadLineAsync() is { } line) | ||
{ | ||
stdOutput.Add(line); | ||
} | ||
|
||
var stdError = new List<string>(); | ||
while (await process.StandardError.ReadLineAsync() is { } line) | ||
{ | ||
stdError.Add(line); | ||
} | ||
|
||
return new ProcessRunResult(process.ExitCode, stdOutput, stdError); | ||
} | ||
|
||
public static async Task<ProcessWrapper> RunToExitAsync(params string[] arguments) | ||
{ | ||
var cancellationToken = TestContext.CurrentContext.CancellationToken; | ||
|
||
var process = Run(arguments); | ||
|
||
await process.WaitForExitAsync(cancellationToken); | ||
|
||
return process; | ||
} | ||
|
||
public static ProcessWrapper Run(params string[] arguments) | ||
{ | ||
var processStartInfo = new ProcessStartInfo | ||
{ | ||
FileName = "dotnet", | ||
ArgumentList = { "Promote.NuGet.dll" }, | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true, | ||
}; | ||
|
||
foreach (var argument in arguments) | ||
{ | ||
processStartInfo.ArgumentList.Add(argument); | ||
} | ||
|
||
Process? process = null; | ||
try | ||
{ | ||
process = Process.Start(processStartInfo); | ||
|
||
if (process == null) | ||
{ | ||
Assert.Fail("Failed to run the process"); | ||
Environment.FailFast("UNREACHABLE"); | ||
} | ||
|
||
return new ProcessWrapper(process); | ||
} | ||
catch | ||
{ | ||
process?.Dispose(); | ||
throw; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using NuGet.Protocol.Core.Types; | ||
using NuGet.Versioning; | ||
using Promote.NuGet.Feeds; | ||
using Promote.NuGet.TestInfrastructure; | ||
|
||
namespace Promote.NuGet.Tests; | ||
|
||
[TestFixture] | ||
public class PromoteSinglePackageTests | ||
{ | ||
[Test, CancelAfter(30_000)] | ||
public async Task Promotes_a_package_with_its_dependencies_to_destination_feed() | ||
{ | ||
await using var destinationFeed = await LocalNugetFeed.Create(); | ||
|
||
// Act | ||
var result = await PromoteNugetProcessRunner.RunForResultAsync( | ||
"promote", | ||
"package", | ||
"System.Runtime", | ||
"--version", "4.3.0", | ||
"--destination", destinationFeed.FeedUrl, | ||
"--destination-api-key", destinationFeed.ApiKey | ||
); | ||
|
||
var destinationFeedDescriptor = new NuGetRepositoryDescriptor(destinationFeed.FeedUrl, destinationFeed.ApiKey); | ||
var destinationRepo = new NuGetRepository(destinationFeedDescriptor, NullSourceCacheContext.Instance, TestNuGetLogger.Instance); | ||
|
||
// Assert | ||
result.StdOutput.Should().StartWith( | ||
Check failure on line 30 in tests/Promote.NuGet.Tests/PromoteSinglePackageTests.cs GitHub Actions / .NET TestsPromote.NuGet.Tests.PromoteSinglePackageTests ► Promotes_a_package_with_its_dependencies_to_destination_feed
Raw output
|
||
new[] | ||
{ | ||
"Resolving packages to promote:", | ||
"└── System.Runtime 4.3.0" | ||
} | ||
); | ||
|
||
result.StdOutput.Should().ContainInConsecutiveOrder( | ||
"Found 3 package(s) to promote:", | ||
"├── Microsoft.NETCore.Platforms 1.1.0", | ||
"├── Microsoft.NETCore.Targets 1.1.0", | ||
"└── System.Runtime 4.3.0" | ||
); | ||
|
||
result.StdOutput.Should().ContainInOrder( | ||
"(1/3) Promote Microsoft.NETCore.Platforms 1.1.0", | ||
"(2/3) Promote Microsoft.NETCore.Targets 1.1.0", | ||
"(3/3) Promote System.Runtime 4.3.0", | ||
"3 package(s) promoted." | ||
); | ||
|
||
result.StdError.Should().BeEmpty(); | ||
result.ExitCode.Should().Be(0); | ||
|
||
await AssertContainsVersions(destinationRepo, "System.Runtime", new[] { new NuGetVersion(4, 3, 0) }); | ||
await AssertContainsVersions(destinationRepo, "Microsoft.NETCore.Platforms", new[] { new NuGetVersion(1, 1, 0) }); | ||
await AssertContainsVersions(destinationRepo, "Microsoft.NETCore.Targets", new[] { new NuGetVersion(1, 1, 0) }); | ||
} | ||
|
||
private static async Task AssertContainsVersions(INuGetRepository repo, string packageId, params NuGetVersion[] expectedVersions) | ||
{ | ||
var netCorePlatformsPackages = await repo.Packages.GetAllVersions(packageId); | ||
netCorePlatformsPackages.IsSuccess.Should().BeTrue(); | ||
netCorePlatformsPackages.Value.Should().Contain(expectedVersions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Diagnostics; | ||
|
||
namespace Promote.NuGet.Tests; | ||
|
||
[TestFixture] | ||
public class ToolVersionTests | ||
{ | ||
[Test, CancelAfter(10_000)] | ||
public async Task Returns_version_of_the_tool() | ||
{ | ||
var expectedVersion = FileVersionInfo.GetVersionInfo(typeof(Program).Assembly.Location).ProductVersion ?? string.Empty; | ||
var expectedVersionLines = expectedVersion.Chunk(80).Select(x => new string(x)).ToList(); | ||
|
||
// Act | ||
var result = await PromoteNugetProcessRunner.RunForResultAsync("--version"); | ||
|
||
// Assert | ||
result.StdOutput.Should().BeEquivalentTo(expectedVersionLines); | ||
result.StdError.Should().BeEmpty(); | ||
result.ExitCode.Should().Be(0); | ||
} | ||
} |