Add tests for VelopackApp verifier

This commit is contained in:
Caelan Sayler
2024-01-25 15:17:37 +00:00
parent c51bf9b6bb
commit 29a5db626b
6 changed files with 129 additions and 6 deletions

View File

@@ -38,7 +38,7 @@ public class WindowsPackCommandRunner : PackageBuilder<WindowsPackOptions>
"Please publish your application to a folder without ClickOnce.");
}
DotnetUtil.VerifyVelopackAppBuilder(MainExePath, Log);
DotnetUtil.VerifyVelopackApp(MainExePath, Log);
// copy files to temp dir, so we can modify them
var dir = TempDir.CreateSubdirectory("PreprocessPackDirWin");

View File

@@ -1,6 +1,4 @@
using System.Diagnostics;
using System.IO;
using AsmResolver.DotNet;
using AsmResolver.DotNet;
using AsmResolver.DotNet.Bundles;
using AsmResolver.DotNet.Serialized;
using AsmResolver.PE;
@@ -14,7 +12,7 @@ namespace Velopack.Packaging.Windows
{
public class DotnetUtil
{
public static void VerifyVelopackAppBuilder(string exeFile, ILogger log)
public static NuGetVersion VerifyVelopackApp(string exeFile, ILogger log)
{
try {
NuGetVersion velopackVersion = null;
@@ -25,7 +23,7 @@ namespace Velopack.Packaging.Windows
if (mainAssy == null) {
// not a dotnet binary
return;
return null;
}
if (velopackDll != null) {
@@ -57,8 +55,10 @@ namespace Velopack.Packaging.Windows
$"This may cause compatibility issues. Expected {VelopackRuntimeInfo.VelopackProductVersion}, " +
$"but found {velopackVersion}.");
}
return velopackVersion;
} else {
log.Warn("VelopackApp verified at entry point, but ProductVersion could not be checked.");
return null;
}
}
}
@@ -68,9 +68,12 @@ namespace Velopack.Packaging.Windows
// if we've iterated the whole main method and not found the call, then the velopack builder is missing
throw new UserInfoException($"Unable to verify VelopackApp, in application main method '{entryPoint.FullName}'. " +
"Please ensure that 'VelopackApp.Build().Run()' is present in your Program.Main().");
} catch (Exception ex) when (ex is not UserInfoException) {
log.Error("Unable to verify VelopackApp: " + ex.Message);
}
return null;
}
private static AssemblyDefinition LoadFullFramework(string exeFile, ref IPEImage velopackDll)

View File

@@ -9,6 +9,12 @@ public static class PathHelper
public static string GetProjectDir()
=> Path.Combine(GetTestRoot(), "..");
public static string GetAvaloniaSample()
=> Path.Combine(GetProjectDir(), "examples", "AvaloniaCrossPlat");
public static string GetWpfSample()
=> Path.Combine(GetProjectDir(), "examples", "VeloWpfSample");
public static string GetVendorLibDir()
=> Path.Combine(GetProjectDir(), "vendor");

View File

@@ -7,6 +7,7 @@ try {
bool shouldExit = false;
bool shouldAutoUpdate = args.Any(a => a.Equals("--autoupdate", StringComparison.OrdinalIgnoreCase));
#if !NO_VELO_BUILDER
VelopackApp.Build()
.SetAutoApplyOnStartup(shouldAutoUpdate)
.WithFirstRun((v) => {
@@ -33,6 +34,7 @@ try {
if (shouldExit) {
return 0;
}
#endif
if (args.Length == 1 && args[0] == "version") {
var locator = VelopackLocator.GetDefault(new ConsoleLogger());

View File

@@ -6,6 +6,10 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup Condition=" $(NoVelopackApp) != '' ">
<DefineConstants>NO_VELO_BUILDER</DefineConstants>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Velopack\Velopack.csproj" />
</ItemGroup>

View File

@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Velopack.Packaging.Exceptions;
using Velopack.Packaging.Windows;
using Velopack.Packaging.Windows.Commands;
namespace Velopack.Packaging.Tests
{
public class DotnetUtilTests
{
private readonly ITestOutputHelper _output;
public DotnetUtilTests(ITestOutputHelper output)
{
_output = output;
}
[SkippableFact]
public void NonDotnetBinaryPasses()
{
Skip.IfNot(VelopackRuntimeInfo.IsWindows);
using var logger = _output.BuildLoggerFor<DotnetUtilTests>();
Assert.Null(DotnetUtil.VerifyVelopackApp(PathHelper.GetRustAsset("testapp.exe"), logger));
}
[SkippableFact]
public void PublishSingleFilePasses()
{
Skip.IfNot(VelopackRuntimeInfo.IsWindows);
using var logger = _output.BuildLoggerFor<DotnetUtilTests>();
using var _1 = Utility.GetTempDirectory(out var dir);
var sample = PathHelper.GetAvaloniaSample();
Exe.InvokeAndThrowIfNonZero(
"dotnet",
new string[] { "publish", "--no-self-contained", "-r", "win-x64", "-o", dir,
"-p:UseLocalVelopack=true", "-p:PublishSingleFile=true" },
sample);
var path = Path.Combine(dir, "AvaloniaCrossPlat.exe");
Assert.Equal(VelopackRuntimeInfo.VelopackProductVersion, DotnetUtil.VerifyVelopackApp(path, logger));
var newPath = Path.Combine(dir, "AvaloniaCrossPlat-asd2.exe");
File.Move(path, newPath);
Assert.Equal(VelopackRuntimeInfo.VelopackProductVersion, DotnetUtil.VerifyVelopackApp(newPath, logger));
}
[SkippableFact]
public void PublishDotnet6Passes()
{
Skip.IfNot(VelopackRuntimeInfo.IsWindows);
using var logger = _output.BuildLoggerFor<DotnetUtilTests>();
using var _1 = Utility.GetTempDirectory(out var dir);
var sample = PathHelper.GetAvaloniaSample();
Exe.InvokeAndThrowIfNonZero(
"dotnet",
new string[] { "publish", "--no-self-contained", "-r", "win-x64", "-o", dir,
"-p:UseLocalVelopack=true" },
sample);
var path = Path.Combine(dir, "AvaloniaCrossPlat.exe");
Assert.Equal(VelopackRuntimeInfo.VelopackProductVersion, DotnetUtil.VerifyVelopackApp(path, logger));
var newPath = Path.Combine(dir, "AvaloniaCrossPlat-asd2.exe");
File.Move(path, newPath);
Assert.Equal(VelopackRuntimeInfo.VelopackProductVersion, DotnetUtil.VerifyVelopackApp(newPath, logger));
}
[SkippableFact]
public void PublishNet48Passes()
{
Skip.IfNot(VelopackRuntimeInfo.IsWindows);
using var logger = _output.BuildLoggerFor<DotnetUtilTests>();
using var _1 = Utility.GetTempDirectory(out var dir);
var sample = PathHelper.GetWpfSample();
Exe.InvokeAndThrowIfNonZero(
"dotnet",
new string[] { "publish", "-o", dir },
sample);
var path = Path.Combine(dir, "VeloWpfSample.exe");
Assert.NotNull(DotnetUtil.VerifyVelopackApp(path, logger));
var newPath = Path.Combine(dir, "VeloWpfSample-asd2.exe");
File.Move(path, newPath);
Assert.NotNull(DotnetUtil.VerifyVelopackApp(newPath, logger));
}
[SkippableFact]
public void UnawareDotnetAppFails()
{
Skip.IfNot(VelopackRuntimeInfo.IsWindows);
using var logger = _output.BuildLoggerFor<DotnetUtilTests>();
using var _1 = Utility.GetTempDirectory(out var dir);
var sample = PathHelper.GetTestRootPath("TestApp");
Exe.InvokeAndThrowIfNonZero(
"dotnet",
new string[] { "publish", "--no-self-contained", "-r", "win-x64", "-o", dir,
"-p:NoVelopackApp=true" },
sample);
var path = Path.Combine(dir, "TestApp.exe");
Assert.Throws<UserInfoException>(() => DotnetUtil.VerifyVelopackApp(path, logger));
}
}
}