Add test coverage for GitHub and fix JSON error

This commit is contained in:
Caelan Sayler
2024-01-08 10:58:49 +00:00
parent fe77e3cd28
commit 65bd274a7d
11 changed files with 239 additions and 35 deletions

View File

@@ -15,6 +15,9 @@ public static class PathHelper
public static string GetFixture(params string[] names)
=> Path.Combine(new string[] { GetTestRoot(), "fixtures" }.Concat(names).ToArray());
public static string GetTestRootPath(params string[] names)
=> Path.Combine(new string[] { GetTestRoot() }.Concat(names).ToArray());
public static string GetRustSrcDir()
=> Path.Combine(GetProjectDir(), "src", "Rust");

View File

@@ -0,0 +1,159 @@
using System.Diagnostics;
using Velopack.Deployment;
using Velopack.Packaging.OSX.Commands;
using Velopack.Packaging.Windows.Commands;
using Velopack.Sources;
using Octokit;
namespace Velopack.Packaging.Tests
{
public class DeploymentTests
{
public readonly string GITHUB_TOKEN = Environment.GetEnvironmentVariable("VELOPACK_GITHUB_TEST_TOKEN");
public readonly string GITHUB_REPOURL = "https://github.com/caesay/VelopackGithubUpdateTest";
private readonly ITestOutputHelper _output;
public DeploymentTests(ITestOutputHelper output)
{
_output = output;
}
private Release GetGithubRelease(GitHubClient client, string releaseName)
{
var (repoOwner, repoName) = GitHubRepository.GetOwnerAndRepo(GITHUB_REPOURL);
var existingReleases = client.Repository.Release.GetAll(repoOwner, repoName).GetAwaiterResult();
return existingReleases.SingleOrDefault(s => s.Name == releaseName);
}
[Fact]
public void CanDeployAndUpdateFromGithub()
{
using var logger = _output.BuildLoggerFor<DeploymentTests>();
var id = "GithubUpdateTest";
var ci = !String.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI"));
using var _1 = Utility.GetTempDirectory(out var releaseDir);
var uniqueSuffix = ci ? "ci-" : "local-" + VelopackRuntimeInfo.SystemOs.GetOsShortName();
var releaseName = $"{VelopackRuntimeInfo.VelopackDisplayVersion}-{uniqueSuffix}";
// delete release if already exists
var client = new GitHubClient(new ProductHeaderValue("Velopack")) {
Credentials = new Credentials(GITHUB_TOKEN)
};
var (repoOwner, repoName) = GitHubRepository.GetOwnerAndRepo(GITHUB_REPOURL);
var existingRelease = client.Repository.Release.GetAll(repoOwner, repoName).GetAwaiterResult().SingleOrDefault(s => s.Name == releaseName);
if (existingRelease != null) {
client.Repository.Release.Delete(repoOwner, repoName, existingRelease.Id).GetAwaiterResult();
logger.Info($"Deleted existing release '{releaseName}'");
}
// create releases
var notesPath = Path.Combine(releaseDir, "NOTES");
var notesContent = $"""
# Release {releaseName}
CI: {ci}
This is just a _test_!
""";
File.WriteAllText(notesPath, notesContent);
if (String.IsNullOrEmpty(GITHUB_TOKEN))
throw new Exception("VELOPACK_GITHUB_TEST_TOKEN is not set.");
PackTestApp(id, $"1.0.0-{uniqueSuffix}", "t1", releaseDir, logger, notesPath);
PackTestApp(id, $"2.0.0-{uniqueSuffix}", "t2", releaseDir, logger, notesPath);
// deploy
var gh = new GitHubRepository(logger);
var options = new GitHubUploadOptions {
ReleaseName = releaseName,
ReleaseDir = new DirectoryInfo(releaseDir),
RepoUrl = GITHUB_REPOURL,
Token = GITHUB_TOKEN,
Prerelease = false,
Publish = true,
};
gh.UploadMissingAssetsAsync(options).GetAwaiterResult();
// check
var newRelease = client.Repository.Release.GetAll(repoOwner, repoName).GetAwaiterResult().Single(s => s.Name == releaseName);
Assert.False(newRelease.Draft);
Assert.Equal(notesContent.Trim().ReplaceLineEndings("\n"), newRelease.Body.Trim());
// update
var source = new GithubSource(GITHUB_REPOURL, GITHUB_TOKEN, false, logger: logger);
var releases = source.GetReleaseFeed().GetAwaiterResult();
var ghrel = releases.Select(r => (GithubReleaseEntry) r).ToArray();
Assert.Equal(2, ghrel.Length);
foreach (var r in ghrel) {
Assert.Equal(releaseName, r.Release.Name);
Assert.Equal(id, r.PackageId);
Assert.Equal($"2.0.0-{uniqueSuffix}", r.Version.ToNormalizedString());
}
using var _2 = Utility.GetTempDirectory(out var releaseDirNew);
gh.DownloadLatestFullPackageAsync(new GitHubDownloadOptions {
Token = GITHUB_TOKEN,
RepoUrl = GITHUB_REPOURL,
ReleaseDir = new DirectoryInfo(releaseDirNew),
}).GetAwaiterResult();
var filename = $"{id}-2.0.0-{uniqueSuffix}-{VelopackRuntimeInfo.SystemOs.GetOsShortName()}-full.nupkg";
Assert.True(File.Exists(Path.Combine(releaseDirNew, filename)));
}
private void PackTestApp(string id, string version, string testString, string releaseDir, ILogger logger, string releaseNotes)
{
var projDir = PathHelper.GetTestRootPath("TestApp");
var testStringFile = Path.Combine(projDir, "Const.cs");
var oldText = File.ReadAllText(testStringFile);
try {
File.WriteAllText(testStringFile, $"class Const {{ public const string TEST_STRING = \"{testString}\"; }}");
var args = new string[] { "publish", "--no-self-contained", "-c", "Release", "-r", VelopackRuntimeInfo.SystemRid, "-o", "publish" };
var psi = new ProcessStartInfo("dotnet");
psi.WorkingDirectory = projDir;
psi.AppendArgumentListSafe(args, out var debug);
logger.Info($"TEST: Running {psi.FileName} {debug}");
using var p = Process.Start(psi);
p.WaitForExit();
if (p.ExitCode != 0)
throw new Exception($"dotnet publish failed with exit code {p.ExitCode}");
if (VelopackRuntimeInfo.IsWindows) {
var options = new WindowsPackOptions {
EntryExecutableName = "TestApp.exe",
ReleaseDir = new DirectoryInfo(releaseDir),
PackId = id,
TargetRuntime = RID.Parse(VelopackRuntimeInfo.SystemOs.GetOsShortName()),
PackVersion = version,
PackDirectory = Path.Combine(projDir, "publish"),
ReleaseNotes = releaseNotes,
};
var runner = new WindowsPackCommandRunner(logger);
runner.Pack(options);
} else if (VelopackRuntimeInfo.IsOSX) {
var options = new OsxPackOptions {
EntryExecutableName = "TestApp",
ReleaseDir = new DirectoryInfo(releaseDir),
PackId = id,
PackVersion = version,
PackDirectory = Path.Combine(projDir, "publish"),
ReleaseNotes = releaseNotes,
};
var runner = new OsxPackCommandRunner(logger);
runner.Pack(options);
} else {
throw new PlatformNotSupportedException();
}
} finally {
File.WriteAllText(testStringFile, oldText);
}
}
}
}

View File

@@ -5,7 +5,10 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Velopack.Deployment\Velopack.Deployment.csproj" />
<ProjectReference Include="..\..\src\Velopack.Packaging.OSX\Velopack.Packaging.OSX.csproj" />
<ProjectReference Include="..\..\src\Velopack.Packaging.Windows\Velopack.Packaging.Windows.csproj" />
<PackageReference Include="Octokit" Version="9.1.0" />
</ItemGroup>
</Project>

View File

@@ -623,7 +623,7 @@ public class WindowsPackTests
//logger.Info($"TEST: Running {psi.FileName} {psi.ArgumentList.Aggregate((a, b) => $"{a} {b}")}");
//using var p = Process.Start(psi);
var outputfile = GetPath($"run.{RandomString(8)}.log");
var outputfile = PathHelper.GetTestRootPath($"run.{RandomString(8)}.log");
try {
// this is a huge hack, but WaitForProcess hangs in the test runner when the output is redirected
@@ -684,7 +684,7 @@ public class WindowsPackTests
private string RunCoveredDotnet(string exe, string[] args, string workingDir, ILogger logger, int? exitCode = 0)
{
var outputfile = GetPath($"coverage.rundotnet.{RandomString(8)}.xml");
var outputfile = PathHelper.GetTestRootPath($"coverage.rundotnet.{RandomString(8)}.xml");
if (!File.Exists(exe))
throw new Exception($"File {exe} does not exist.");
@@ -730,7 +730,7 @@ public class WindowsPackTests
private void PackTestApp(string id, string version, string testString, string releaseDir, ILogger logger)
{
var projDir = GetPath("TestApp");
var projDir = PathHelper.GetTestRootPath("TestApp");
var testStringFile = Path.Combine(projDir, "Const.cs");
var oldText = File.ReadAllText(testStringFile);
@@ -767,17 +767,4 @@ public class WindowsPackTests
File.WriteAllText(testStringFile, oldText);
}
}
private static string GetPath(params string[] paths)
{
var ret = GetIntegrationTestRootDirectory();
return (new FileInfo(paths.Aggregate(ret, Path.Combine))).FullName;
}
private static string GetIntegrationTestRootDirectory()
{
var st = new StackFrame(true);
var di = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(st.GetFileName()), ".."));
return di.FullName;
}
}