Add Rust binary coverage function

This commit is contained in:
Caelan Sayler
2023-12-24 13:05:48 +00:00
parent 36d6bd57e6
commit bdd78d2655

View File

@@ -3,10 +3,12 @@ using System;
using System.Data.Common;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.Versioning;
using System.Text;
using System.Xml.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.Win32;
using NuGet.Packaging;
using Squirrel.Compression;
@@ -226,7 +228,7 @@ public class WindowsPackTests
var setupPath1 = Path.Combine(tmpReleaseDir, $"{id}-Setup-[win-x64].exe");
Assert.True(File.Exists(setupPath1));
RunProcessNoCoverage(setupPath1, new[] { "--nocolor", "--silent", "--installto", tmpInstallDir }, Environment.CurrentDirectory, logger);
RunNoCoverage(setupPath1, new[] { "--nocolor", "--silent", "--installto", tmpInstallDir }, Environment.CurrentDirectory, logger);
var updatePath = Path.Combine(tmpInstallDir, "Update.exe");
Assert.True(File.Exists(updatePath));
@@ -253,7 +255,7 @@ public class WindowsPackTests
var date = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
Assert.Equal(date, installDate.Trim('\0'));
RunProcessNoCoverage(updatePath, new string[] { "--nocolor", "--silent", "--uninstall" }, Environment.CurrentDirectory, logger);
RunNoCoverage(updatePath, new string[] { "--nocolor", "--silent", "--uninstall" }, Environment.CurrentDirectory, logger);
Assert.False(File.Exists(shortcutPath));
Assert.False(File.Exists(appPath));
@@ -279,7 +281,7 @@ public class WindowsPackTests
// install app
var setupPath1 = Path.Combine(releaseDir, $"{id}-Setup-[win-x64].exe");
RunProcessNoCoverage(setupPath1, new string[] { "--nocolor", "--silent", "--installto", installDir }, Environment.GetFolderPath(Environment.SpecialFolder.Desktop), logger);
RunNoCoverage(setupPath1, new string[] { "--nocolor", "--silent", "--installto", installDir }, Environment.GetFolderPath(Environment.SpecialFolder.Desktop), logger);
// check app installed correctly
var appPath = Path.Combine(installDir, "current", "TestApp.exe");
@@ -291,11 +293,11 @@ public class WindowsPackTests
logger.Info("TEST: v1 installed");
// check app output
var chk1test = RunProcess(appPath, new string[] { "test" }, installDir, logger);
var chk1test = RunCoveredDotnet(appPath, new string[] { "test" }, installDir, logger);
Assert.EndsWith(Environment.NewLine + "version 1 test", chk1test);
var chk1version = RunProcess(appPath, new string[] { "version" }, installDir, logger);
var chk1version = RunCoveredDotnet(appPath, new string[] { "version" }, installDir, logger);
Assert.EndsWith(Environment.NewLine + "1.0.0", chk1version);
var chk1check = RunProcess(appPath, new string[] { "check", releaseDir }, installDir, logger);
var chk1check = RunCoveredDotnet(appPath, new string[] { "check", releaseDir }, installDir, logger);
Assert.EndsWith(Environment.NewLine + "no updates", chk1check);
logger.Info("TEST: v1 output verified");
@@ -303,7 +305,7 @@ public class WindowsPackTests
PackTestApp(id, "2.0.0", "version 2 test", releaseDir, logger);
// check can find v2 update
var chk2check = RunProcess(appPath, new string[] { "check", releaseDir }, installDir, logger);
var chk2check = RunCoveredDotnet(appPath, new string[] { "check", releaseDir }, installDir, logger);
Assert.EndsWith(Environment.NewLine + "update: 2.0.0", chk2check);
logger.Info("TEST: found v2 update");
@@ -312,17 +314,17 @@ public class WindowsPackTests
// perform full update, check that we get v3
// apply should fail if there's not an update downloaded
RunProcess(appPath, new string[] { "apply", releaseDir }, installDir, logger, exitCode: -1);
RunProcess(appPath, new string[] { "download", releaseDir }, installDir, logger);
RunProcess(appPath, new string[] { "apply", releaseDir }, installDir, logger, exitCode: null);
RunCoveredDotnet(appPath, new string[] { "apply", releaseDir }, installDir, logger, exitCode: -1);
RunCoveredDotnet(appPath, new string[] { "download", releaseDir }, installDir, logger);
RunCoveredDotnet(appPath, new string[] { "apply", releaseDir }, installDir, logger, exitCode: null);
logger.Info("TEST: v3 applied");
// check app output
var chk3test = RunProcess(appPath, new string[] { "test" }, installDir, logger);
var chk3test = RunCoveredDotnet(appPath, new string[] { "test" }, installDir, logger);
Assert.EndsWith(Environment.NewLine + "version 3 test", chk3test);
var chk3version = RunProcess(appPath, new string[] { "version" }, installDir, logger);
var chk3version = RunCoveredDotnet(appPath, new string[] { "version" }, installDir, logger);
Assert.EndsWith(Environment.NewLine + "3.0.0", chk3version);
var ch3check2 = RunProcess(appPath, new string[] { "check", releaseDir }, installDir, logger);
var ch3check2 = RunCoveredDotnet(appPath, new string[] { "check", releaseDir }, installDir, logger);
Assert.EndsWith(Environment.NewLine + "no updates", ch3check2);
logger.Info("TEST: v3 output verified");
@@ -340,28 +342,39 @@ public class WindowsPackTests
// uninstall
var updatePath = Path.Combine(installDir, "Update.exe");
RunProcessNoCoverage(updatePath, new string[] { "--nocolor", "--silent", "--uninstall" }, Environment.CurrentDirectory, logger);
RunNoCoverage(updatePath, new string[] { "--nocolor", "--silent", "--uninstall" }, Environment.CurrentDirectory, logger);
logger.Info("TEST: uninstalled / complete");
}
private string RunProcess(string exe, string[] args, string workingDir, ILogger logger, int? exitCode = 0)
private string RunCoveredRust(string binName, string[] args, string workingDir, ILogger logger, int? exitCode = 0)
{
var outputfile = GetPath($"coverage.runprocess.{RandomString(8)}.xml");
var outputfile = GetPath($"coverage.runrust.{RandomString(8)}.xml");
var manifestFile = GetPath("..", "src", "Rust", "Cargo.toml");
var psi = new ProcessStartInfo("dotnet-coverage");
psi.WorkingDirectory = workingDir;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = workingDir;
psi.ArgumentList.Add("collect");
psi.ArgumentList.Add("-o");
psi.ArgumentList.Add("llvm-cov");
psi.ArgumentList.Add("run");
psi.ArgumentList.Add("--cobertura");
psi.ArgumentList.Add("--manifest-path");
psi.ArgumentList.Add(manifestFile);
psi.ArgumentList.Add("--output");
psi.ArgumentList.Add(outputfile);
psi.ArgumentList.Add("-f");
psi.ArgumentList.Add("cobertura");
psi.ArgumentList.Add(exe);
psi.ArgumentList.Add("--bin");
psi.ArgumentList.Add(binName);
psi.ArgumentList.Add("--");
psi.ArgumentList.AddRange(args);
return RunImpl(psi, logger, exitCode);
}
private string RunImpl(ProcessStartInfo psi, ILogger logger, int? exitCode = 0)
{
logger.Info($"TEST: Running {psi.FileName} {psi.ArgumentList.Aggregate((a, b) => $"{a} {b}")}");
var p = Process.Start(psi);
StringBuilder sb = new StringBuilder();
@@ -382,6 +395,27 @@ public class WindowsPackTests
).Trim();
}
private string RunCoveredDotnet(string exe, string[] args, string workingDir, ILogger logger, int? exitCode = 0)
{
var outputfile = GetPath($"coverage.rundotnet.{RandomString(8)}.xml");
var psi = new ProcessStartInfo("dotnet-coverage");
psi.WorkingDirectory = workingDir;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.ArgumentList.Add("collect");
psi.ArgumentList.Add("-o");
psi.ArgumentList.Add(outputfile);
psi.ArgumentList.Add("-f");
psi.ArgumentList.Add("cobertura");
psi.ArgumentList.Add(exe);
psi.ArgumentList.AddRange(args);
return RunImpl(psi, logger, exitCode);
}
private static Random _random = new Random();
private static string RandomString(int length)
{
@@ -390,7 +424,7 @@ public class WindowsPackTests
.Select(s => s[_random.Next(s.Length)]).ToArray());
}
private string RunProcessNoCoverage(string exe, string[] args, string workingDir, ILogger logger, int? exitCode = 0)
private string RunNoCoverage(string exe, string[] args, string workingDir, ILogger logger, int? exitCode = 0)
{
var psi = new ProcessStartInfo(exe);
psi.WorkingDirectory = workingDir;
@@ -398,19 +432,7 @@ public class WindowsPackTests
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.ArgumentList.AddRange(args);
var p = Process.Start(psi);
StringBuilder sb = new StringBuilder();
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.OutputDataReceived += (s, e) => { sb.AppendLine(e.Data); logger.Debug(e.Data); };
p.ErrorDataReceived += (s, e) => { sb.AppendLine(e.Data); logger.Debug(e.Data); };
p.WaitForExit();
if (exitCode != null)
Assert.Equal(exitCode, p.ExitCode);
return sb.ToString().Trim();
return RunImpl(psi, logger, exitCode);
}
private void PackTestApp(string id, string version, string testString, string releaseDir, ILogger logger)
@@ -421,7 +443,7 @@ public class WindowsPackTests
var oldText = File.ReadAllText(testStringFile);
File.WriteAllText(testStringFile, $"class Const {{ public const string TEST_STRING = \"{testString}\"; }}");
var args = new string[] { "publish", "--no-self-contained", "-c", "Release", "-r", "win-x64", "-o", "publish" };
RunProcessNoCoverage("dotnet", args, projDir, logger);
RunNoCoverage("dotnet", args, projDir, logger);
File.WriteAllText(testStringFile, oldText);
var options = new WindowsPackOptions {