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

@@ -2,6 +2,9 @@ name: Build
on: [ push, pull_request ] on: [ push, pull_request ]
env:
VELOPACK_GITHUB_TEST_TOKEN: ${{ secrets.VELOPACK_GITHUB_TEST_TOKEN }}
jobs: jobs:
build: build:
strategy: strategy:

View File

@@ -6,19 +6,16 @@ using Velopack.Sources;
namespace Velopack.Deployment; namespace Velopack.Deployment;
public class GitHubOptions : RepositoryOptions public class GitHubDownloadOptions : RepositoryOptions
{ {
public bool Prerelease { get; set; }
public string RepoUrl { get; set; } public string RepoUrl { get; set; }
public string Token { get; set; } public string Token { get; set; }
} }
public class GitHubDownloadOptions : GitHubOptions public class GitHubUploadOptions : GitHubDownloadOptions
{
public bool Pre { get; set; }
}
public class GitHubUploadOptions : GitHubOptions
{ {
public bool Publish { get; set; } public bool Publish { get; set; }
@@ -33,18 +30,24 @@ public class GitHubRepository : SourceRepository<GitHubDownloadOptions, GithubSo
public override GithubSource CreateSource(GitHubDownloadOptions options) public override GithubSource CreateSource(GitHubDownloadOptions options)
{ {
return new GithubSource(options.RepoUrl, options.Token, options.Pre, options.Channel, logger: Log); return new GithubSource(options.RepoUrl, options.Token, options.Prerelease, options.Channel, logger: Log);
} }
public async Task UploadMissingAssetsAsync(GitHubUploadOptions options) public static (string owner, string repo) GetOwnerAndRepo(string repoUrl)
{ {
var repoUri = new Uri(options.RepoUrl); var repoUri = new Uri(repoUrl);
var repoParts = repoUri.AbsolutePath.Trim('/').Split('/'); var repoParts = repoUri.AbsolutePath.Trim('/').Split('/');
if (repoParts.Length != 2) if (repoParts.Length != 2)
throw new Exception($"Invalid GitHub URL, '{repoUri.AbsolutePath}' should be in the format 'owner/repo'"); throw new Exception($"Invalid GitHub URL, '{repoUri.AbsolutePath}' should be in the format 'owner/repo'");
var repoOwner = repoParts[0]; var repoOwner = repoParts[0];
var repoName = repoParts[1]; var repoName = repoParts[1];
return (repoOwner, repoName);
}
public async Task UploadMissingAssetsAsync(GitHubUploadOptions options)
{
var (repoOwner, repoName) = GetOwnerAndRepo(options.RepoUrl);
var helper = new ReleaseEntryHelper(options.ReleaseDir.FullName, Log); var helper = new ReleaseEntryHelper(options.ReleaseDir.FullName, Log);
var assets = helper.GetUploadAssets(options.Channel, ReleaseEntryHelper.AssetsMode.OnlyLatest); var assets = helper.GetUploadAssets(options.Channel, ReleaseEntryHelper.AssetsMode.OnlyLatest);
@@ -62,7 +65,7 @@ public class GitHubRepository : SourceRepository<GitHubDownloadOptions, GithubSo
var newReleaseReq = new NewRelease(semVer.ToString()) { var newReleaseReq = new NewRelease(semVer.ToString()) {
Body = releaseNotes, Body = releaseNotes,
Draft = true, Draft = true,
Prerelease = semVer.HasMetadata || semVer.IsPrerelease, Prerelease = options.Prerelease,
Name = string.IsNullOrWhiteSpace(options.ReleaseName) ? semVer.ToString() : options.ReleaseName, Name = string.IsNullOrWhiteSpace(options.ReleaseName) ? semVer.ToString() : options.ReleaseName,
}; };
@@ -86,6 +89,14 @@ public class GitHubRepository : SourceRepository<GitHubDownloadOptions, GithubSo
var releasesBytes = releasesFileToUpload.ToArray(); var releasesBytes = releasesFileToUpload.ToArray();
var data = new ReleaseAssetUpload(assets.ReleasesFileName, "application/octet-stream", new MemoryStream(releasesBytes), TimeSpan.FromMinutes(1)); var data = new ReleaseAssetUpload(assets.ReleasesFileName, "application/octet-stream", new MemoryStream(releasesBytes), TimeSpan.FromMinutes(1));
await client.Repository.Release.UploadAsset(release, data, CancellationToken.None); await client.Repository.Release.UploadAsset(release, data, CancellationToken.None);
// convert draft to full release
if (options.Publish) {
Log.Info("Converting draft to full published release.");
var upd = release.ToUpdate();
upd.Draft = false;
release = await client.Repository.Release.Edit(repoOwner, repoName, release.Id, upd);
}
} }
private async Task UploadFileAsAsset(GitHubClient client, Release release, string filePath) private async Task UploadFileAsAsset(GitHubClient client, Release release, string filePath)

View File

@@ -12,7 +12,7 @@ public class OsxPackCommandRunner
_logger = logger; _logger = logger;
} }
public void Releasify(OsxPackOptions options) public void Pack(OsxPackOptions options)
{ {
if (options.TargetRuntime.BaseRID != RuntimeOs.OSX) if (options.TargetRuntime.BaseRID != RuntimeOs.OSX)
throw new ArgumentException("Target runtime must be OSX.", nameof(options.TargetRuntime)); throw new ArgumentException("Target runtime must be OSX.", nameof(options.TargetRuntime));

View File

@@ -34,6 +34,7 @@ namespace Velopack.Packaging
public void ValidateChannelForPackaging(SemanticVersion version, string channel, RID rid) public void ValidateChannelForPackaging(SemanticVersion version, string channel, RID rid)
{ {
channel ??= GetDefaultChannel(VelopackRuntimeInfo.SystemOs);
if (!_releases.ContainsKey(channel) || !_releases[channel].Any()) if (!_releases.ContainsKey(channel) || !_releases[channel].Any())
return; return;
@@ -49,6 +50,7 @@ namespace Velopack.Packaging
public ReleasePackageBuilder GetPreviousFullRelease(SemanticVersion version, string channel) public ReleasePackageBuilder GetPreviousFullRelease(SemanticVersion version, string channel)
{ {
channel ??= GetDefaultChannel(VelopackRuntimeInfo.SystemOs);
var releases = _releases.ContainsKey(channel) ? _releases[channel] : null; var releases = _releases.ContainsKey(channel) ? _releases[channel] : null;
if (releases == null || !releases.Any()) return null; if (releases == null || !releases.Any()) return null;
var entry = releases var entry = releases
@@ -63,6 +65,7 @@ namespace Velopack.Packaging
public ReleaseEntry GetLatestFullRelease(string channel) public ReleaseEntry GetLatestFullRelease(string channel)
{ {
channel ??= GetDefaultChannel(VelopackRuntimeInfo.SystemOs);
var releases = _releases.ContainsKey(channel) ? _releases[channel] : null; var releases = _releases.ContainsKey(channel) ? _releases[channel] : null;
if (releases == null || !releases.Any()) return null; if (releases == null || !releases.Any()) return null;
return releases.Where(z => !z.IsDelta).MaxBy(z => z.Version).First(); return releases.Where(z => !z.IsDelta).MaxBy(z => z.Version).First();
@@ -70,6 +73,7 @@ namespace Velopack.Packaging
public void AddRemoteReleaseEntries(IEnumerable<ReleaseEntry> entries, string channel) public void AddRemoteReleaseEntries(IEnumerable<ReleaseEntry> entries, string channel)
{ {
channel ??= GetDefaultChannel(VelopackRuntimeInfo.SystemOs);
if (!_releases.ContainsKey(channel)) if (!_releases.ContainsKey(channel))
_releases.Add(channel, new List<ReleaseEntry>()); _releases.Add(channel, new List<ReleaseEntry>());
var newEntries = entries.Where(x => !_releases[channel].Any(y => y.Version == x.Version && y.IsDelta == x.IsDelta)); var newEntries = entries.Where(x => !_releases[channel].Any(y => y.Version == x.Version && y.IsDelta == x.IsDelta));
@@ -78,6 +82,7 @@ namespace Velopack.Packaging
public void AddNewRelease(string nupkgPath, string channel) public void AddNewRelease(string nupkgPath, string channel)
{ {
channel ??= GetDefaultChannel(VelopackRuntimeInfo.SystemOs);
if (!File.Exists(nupkgPath)) if (!File.Exists(nupkgPath))
throw new FileNotFoundException("Could not find nupkg file", nupkgPath); throw new FileNotFoundException("Could not find nupkg file", nupkgPath);

View File

@@ -6,11 +6,16 @@ public class GitHubUploadCommand : GitHubBaseCommand
public string ReleaseName { get; private set; } public string ReleaseName { get; private set; }
public bool Pre { get; private set; }
public GitHubUploadCommand() public GitHubUploadCommand()
: base("github", "Upload releases to a GitHub repository.") : base("github", "Upload releases to a GitHub repository.")
{ {
AddOption<bool>((v) => Publish = v, "--publish") AddOption<bool>((v) => Publish = v, "--publish")
.SetDescription("Publish release instead of creating draft."); .SetDescription("Create and publish instead of leaving as draft.");
AddOption<bool>((v) => Pre = v, "--pre")
.SetDescription("Create as pre-release instead of stable.");
AddOption<string>((v) => ReleaseName = v, "--releaseName") AddOption<string>((v) => ReleaseName = v, "--releaseName")
.SetDescription("A custom name for created release.") .SetDescription("A custom name for created release.")

View File

@@ -62,7 +62,7 @@ public class EmbeddedRunner : ICommandRunner
SigningEntitlements = command.SigningEntitlements, SigningEntitlements = command.SigningEntitlements,
SigningInstallIdentity = command.SigningInstallIdentity, SigningInstallIdentity = command.SigningInstallIdentity,
}; };
new OsxPackCommandRunner(_logger).Releasify(options); new OsxPackCommandRunner(_logger).Pack(options);
return Task.CompletedTask; return Task.CompletedTask;
} }
@@ -118,7 +118,7 @@ public class EmbeddedRunner : ICommandRunner
public virtual Task ExecuteGithubDownload(GitHubDownloadCommand command) public virtual Task ExecuteGithubDownload(GitHubDownloadCommand command)
{ {
var options = new GitHubDownloadOptions { var options = new GitHubDownloadOptions {
Pre = command.Pre, Prerelease = command.Pre,
ReleaseDir = command.GetReleaseDirectory(), ReleaseDir = command.GetReleaseDirectory(),
RepoUrl = command.RepoUrl, RepoUrl = command.RepoUrl,
Token = command.Token, Token = command.Token,
@@ -130,6 +130,7 @@ public class EmbeddedRunner : ICommandRunner
public virtual Task ExecuteGithubUpload(GitHubUploadCommand command) public virtual Task ExecuteGithubUpload(GitHubUploadCommand command)
{ {
var options = new GitHubUploadOptions { var options = new GitHubUploadOptions {
Prerelease = command.Pre,
ReleaseDir = command.GetReleaseDirectory(), ReleaseDir = command.GetReleaseDirectory(),
RepoUrl = command.RepoUrl, RepoUrl = command.RepoUrl,
Token = command.Token, Token = command.Token,

View File

@@ -67,11 +67,15 @@ using System.Reflection;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text; using System.Text;
#if !NET5_0_OR_GREATER #if NET7_0_OR_GREATER
using System.Text.Json.Serialization.Metadata;
using System.Text.Json;
using System.Linq;
#else
using Velopack.Json.Reflection; using Velopack.Json.Reflection;
#endif #endif
#if NET5_0_OR_GREATER #if NET7_0_OR_GREATER
namespace Velopack.Json namespace Velopack.Json
{ {
@@ -80,12 +84,35 @@ namespace Velopack.Json
{ {
public static T DeserializeObject<T>(string json) public static T DeserializeObject<T>(string json)
{ {
return System.Text.Json.JsonSerializer.Deserialize<T>(json); var options = new JsonSerializerOptions {
TypeInfoResolver = new DefaultJsonTypeInfoResolver {
Modifiers = { DataMemberNameAttribute }
}
};
return JsonSerializer.Deserialize<T>(json, options);
}
static void DataMemberNameAttribute(JsonTypeInfo typeInfo)
{
// https://github.com/dotnet/runtime/issues/29975
if (typeInfo.Kind != JsonTypeInfoKind.Object)
return;
foreach (JsonPropertyInfo propertyInfo in typeInfo.Properties) {
if (propertyInfo.AttributeProvider is ICustomAttributeProvider provider) {
var attr = provider.GetCustomAttributes(typeof(DataMemberAttribute), inherit: true).Cast<DataMemberAttribute>().ToArray();
if (attr.Length > 0) {
var a = attr[0];
if (a.IsNameSetExplicitly) {
propertyInfo.Name = a.Name;
}
}
}
}
} }
} }
} }
#else #else
// ReSharper disable LoopCanBeConvertedToQuery // ReSharper disable LoopCanBeConvertedToQuery
// ReSharper disable RedundantExplicitArrayCreation // ReSharper disable RedundantExplicitArrayCreation

View File

@@ -15,6 +15,9 @@ public static class PathHelper
public static string GetFixture(params string[] names) public static string GetFixture(params string[] names)
=> Path.Combine(new string[] { GetTestRoot(), "fixtures" }.Concat(names).ToArray()); => 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() public static string GetRustSrcDir()
=> Path.Combine(GetProjectDir(), "src", "Rust"); => 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> </PropertyGroup>
<ItemGroup> <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" /> <ProjectReference Include="..\..\src\Velopack.Packaging.Windows\Velopack.Packaging.Windows.csproj" />
<PackageReference Include="Octokit" Version="9.1.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -623,7 +623,7 @@ public class WindowsPackTests
//logger.Info($"TEST: Running {psi.FileName} {psi.ArgumentList.Aggregate((a, b) => $"{a} {b}")}"); //logger.Info($"TEST: Running {psi.FileName} {psi.ArgumentList.Aggregate((a, b) => $"{a} {b}")}");
//using var p = Process.Start(psi); //using var p = Process.Start(psi);
var outputfile = GetPath($"run.{RandomString(8)}.log"); var outputfile = PathHelper.GetTestRootPath($"run.{RandomString(8)}.log");
try { try {
// this is a huge hack, but WaitForProcess hangs in the test runner when the output is redirected // 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) 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)) if (!File.Exists(exe))
throw new Exception($"File {exe} does not exist."); 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) 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 testStringFile = Path.Combine(projDir, "Const.cs");
var oldText = File.ReadAllText(testStringFile); var oldText = File.ReadAllText(testStringFile);
@@ -767,17 +767,4 @@ public class WindowsPackTests
File.WriteAllText(testStringFile, oldText); 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;
}
} }