From c2a2a7f73c9ec9c7e823e477c88f1dcbe08f186c Mon Sep 17 00:00:00 2001 From: SingleAccretion Date: Wed, 6 Jan 2021 02:48:51 +0300 Subject: [PATCH 1/3] Added resolver tests for single-file publishing scenarios --- .../NativeLibrariesResolverTests.cs | 79 ++++++++++++++----- 1 file changed, 61 insertions(+), 18 deletions(-) diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs b/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs index 6f1add7c..3b58593b 100644 --- a/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs @@ -3,12 +3,14 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; +[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.ClassLevel)] namespace ComputeSharp.Tests.NativeLibrariesResolver { [TestClass] public class NativeLibrariesResolverTests { - static NativeLibrariesResolverTests() + [AssemblyInitialize] + public static void Initialize(TestContext context) { string path = Path.GetDirectoryName(typeof(NativeLibrariesResolverTests).Assembly.Location); @@ -23,17 +25,14 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver sampleProjectPath = Path.Combine(SampleProjectDirectory, "ComputeSharp.Sample.NuGet.csproj"), packagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Package", "ComputeSharp.Package.msbuildproj"); - // Run dotnet pack and on the packaging project, to ensure the local NuGet package is - // available. Then run dotnet restore on the sample project. This is done so that - // other tests can skip the restore step to run faster, as there's lots of them. + // Run dotnet pack and on the packaging project, to ensure the local NuGet package is available. Process.Start("dotnet", $"pack {packagingProjectPath} -c Release").WaitForExit(); - Process.Start("dotnet", $"restore {sampleProjectPath}").WaitForExit(); } /// /// Gets the directory of the ComputeSharp.Sample.NuGet project. /// - private static string SampleProjectDirectory { get; } + private static string SampleProjectDirectory { get; set; } [TestMethod] [DataRow(Configuration.Debug, RID.None)] @@ -42,7 +41,6 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver [DataRow(Configuration.Release, RID.Win_x64)] public void DotnetRunWorks(Configuration configuration, RID rid) { - // "dotnet run" fails with "--no-restore", so in this case we restore packages as well Assert.AreEqual(0, Exec(SampleProjectDirectory, "dotnet", $"run -c {configuration} {ToOption(rid)}")); } @@ -102,16 +100,20 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver Assert.AreEqual(0, Exec(pathToAppHostDirectory, "ComputeSharp.Sample.NuGet.exe", "")); } + [TestMethod] - [DataRow(Configuration.Debug)] - [DataRow(Configuration.Release)] - public void DotnetPublishWorks(Configuration configuration) + [DataRow(PublishMode.SelfContained, DeploymentMode.Multiassembly, NativeLibrariesDeploymentMode.NotApplicable)] + [DataRow(PublishMode.FrameworkDependent, DeploymentMode.Multiassembly, NativeLibrariesDeploymentMode.NotApplicable)] + [DataRow(PublishMode.SelfContained, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.BundleWithApplication)] + [DataRow(PublishMode.SelfContained, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.ExtractToTemporaryDirectory)] + [DataRow(PublishMode.FrameworkDependent, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.BundleWithApplication)] + [DataRow(PublishMode.FrameworkDependent, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.ExtractToTemporaryDirectory)] + public void DotnetPublishWorks(PublishMode publishMode, DeploymentMode deploymentMode, NativeLibrariesDeploymentMode nativeLibsDeploymentMode) { - // Publishing without specifying an RID is not supported - Exec(SampleProjectDirectory, "dotnet", $"publish -c {configuration} -r win-x64 --no-restore"); - - string pathToAppHost = Path.Combine("bin", $"{configuration}", "net5.0", "win-x64", "publish", "ComputeSharp.Sample.NuGet.exe"); - + // Publishing without specifying a RID is not supported + // We do not test Debug builds as it was determined that they are not an important scenario + Exec(SampleProjectDirectory, "dotnet", $"publish -c Release -r win-x64 {ToOption(publishMode)} {ToOption(deploymentMode)} {ToOption(nativeLibsDeploymentMode)}"); + string pathToAppHost = Path.Combine("bin", $"Release", "net5.0", "win-x64", "publish", "ComputeSharp.Sample.NuGet.exe"); Assert.AreEqual(0, Exec(SampleProjectDirectory, pathToAppHost, "")); } @@ -122,7 +124,7 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver /// The RID to use to build the project. private static void BuildSampleProject(Configuration configuration, RID rid) { - Exec(SampleProjectDirectory, "dotnet", $"build -c {configuration} {ToOption(rid)} --no-restore"); + Exec(SampleProjectDirectory, "dotnet", $"build -c {configuration} {ToOption(rid)}"); } /// @@ -144,7 +146,7 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver WorkingDirectory = workingDirectory }; - Process process = Process.Start(startInfo); + using Process process = Process.Start(startInfo); process.WaitForExit(); @@ -163,6 +165,28 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver _ => throw new InvalidEnumArgumentException(nameof(rid), (int)rid, typeof(RID)) }; + private static string ToOption(PublishMode publishMode) => publishMode switch + { + PublishMode.FrameworkDependent => "--self-contained false", + PublishMode.SelfContained => "--self-contained true", + _ => throw new InvalidEnumArgumentException(nameof(publishMode), (int)publishMode, typeof(PublishMode)) + }; + + private static string ToOption(DeploymentMode deploymentMode) => deploymentMode switch + { + DeploymentMode.Multiassembly => "/p:PublishSingleFile=false", + DeploymentMode.SingleFile => "/p:PublishSingleFile=true", + _ => throw new InvalidEnumArgumentException(nameof(deploymentMode), (int)deploymentMode, typeof(DeploymentMode)) + }; + + private static string ToOption(NativeLibrariesDeploymentMode deploymentMode) => deploymentMode switch + { + NativeLibrariesDeploymentMode.NotApplicable => "", + NativeLibrariesDeploymentMode.BundleWithApplication => "/p:IncludeNativeLibrariesForSelfExtract=false", + NativeLibrariesDeploymentMode.ExtractToTemporaryDirectory => "/p:IncludeNativeLibrariesForSelfExtract=true", + _ => throw new InvalidEnumArgumentException(nameof(deploymentMode), (int)deploymentMode, typeof(NativeLibrariesDeploymentMode)) + }; + /// /// Gets a text representation of the build folder for the given . /// @@ -190,7 +214,26 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver public enum RID { None, - Win_x64, + Win_x64 + } + + public enum PublishMode + { + FrameworkDependent, + SelfContained + } + + public enum DeploymentMode + { + Multiassembly, + SingleFile + } + + public enum NativeLibrariesDeploymentMode + { + NotApplicable, + ExtractToTemporaryDirectory, + BundleWithApplication } } } From db9006758a2a08a77cf17dc3a35c105a22b4f15a Mon Sep 17 00:00:00 2001 From: SingleAccretion Date: Wed, 6 Jan 2021 16:55:16 +0300 Subject: [PATCH 2/3] Fixed failure of builds in resolver tests due to stale artifacts. --- .../NativeLibrariesResolverTests.cs | 46 +++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs b/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs index 3b58593b..6be4765f 100644 --- a/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs @@ -21,9 +21,7 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver SampleProjectDirectory = Path.Combine(path, "samples", "ComputeSharp.Sample.NuGet"); - string - sampleProjectPath = Path.Combine(SampleProjectDirectory, "ComputeSharp.Sample.NuGet.csproj"), - packagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Package", "ComputeSharp.Package.msbuildproj"); + string packagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Package", "ComputeSharp.Package.msbuildproj"); // Run dotnet pack and on the packaging project, to ensure the local NuGet package is available. Process.Start("dotnet", $"pack {packagingProjectPath} -c Release").WaitForExit(); @@ -41,6 +39,8 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver [DataRow(Configuration.Release, RID.Win_x64)] public void DotnetRunWorks(Configuration configuration, RID rid) { + CleanSampleProject(configuration, rid); + Assert.AreEqual(0, Exec(SampleProjectDirectory, "dotnet", $"run -c {configuration} {ToOption(rid)}")); } @@ -51,6 +51,7 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver [DataRow(Configuration.Release, RID.Win_x64)] public void DotnetBuildWithRunningDotnetHostFromProjectDirectoryWorks(Configuration configuration, RID rid) { + CleanSampleProject(configuration, rid); BuildSampleProject(configuration, rid); string realtivePathToDll = Path.Combine("bin", $"{configuration}", "net5.0", $"{ToDirectory(rid)}", "ComputeSharp.Sample.NuGet.dll"); @@ -65,6 +66,7 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver [DataRow(Configuration.Release, RID.Win_x64)] public void DotnetBuildWithRunningDotnetHostDirectlyWorks(Configuration configuration, RID rid) { + CleanSampleProject(configuration, rid); BuildSampleProject(configuration, rid); string pathToDllDirectory = Path.Combine(SampleProjectDirectory, "bin", $"{configuration}", "net5.0", $"{ToDirectory(rid)}"); @@ -79,6 +81,7 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver [DataRow(Configuration.Release, RID.Win_x64)] public void DotnetBuildWithRunningAppHostFromProjectDirectoryWorks(Configuration configuration, RID rid) { + CleanSampleProject(configuration, rid); BuildSampleProject(configuration, rid); string relativePathToAppHost = Path.Combine("bin", $"{configuration}", "net5.0", $"{ToDirectory(rid)}", "ComputeSharp.Sample.NuGet.exe"); @@ -93,6 +96,7 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver [DataRow(Configuration.Release, RID.Win_x64)] public void DotnetBuildWithRunningAppHostDirectlyWorks(Configuration configuration, RID rid) { + CleanSampleProject(configuration, rid); BuildSampleProject(configuration, rid); string pathToAppHostDirectory = Path.Combine(SampleProjectDirectory, "bin", $"{configuration}", "net5.0", $"{ToDirectory(rid)}"); @@ -104,15 +108,17 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver [TestMethod] [DataRow(PublishMode.SelfContained, DeploymentMode.Multiassembly, NativeLibrariesDeploymentMode.NotApplicable)] [DataRow(PublishMode.FrameworkDependent, DeploymentMode.Multiassembly, NativeLibrariesDeploymentMode.NotApplicable)] - [DataRow(PublishMode.SelfContained, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.BundleWithApplication)] + [DataRow(PublishMode.SelfContained, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.CopyToApplicationDirectory)] [DataRow(PublishMode.SelfContained, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.ExtractToTemporaryDirectory)] - [DataRow(PublishMode.FrameworkDependent, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.BundleWithApplication)] + [DataRow(PublishMode.FrameworkDependent, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.CopyToApplicationDirectory)] [DataRow(PublishMode.FrameworkDependent, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.ExtractToTemporaryDirectory)] public void DotnetPublishWorks(PublishMode publishMode, DeploymentMode deploymentMode, NativeLibrariesDeploymentMode nativeLibsDeploymentMode) { // Publishing without specifying a RID is not supported // We do not test Debug builds as it was determined that they are not an important scenario - Exec(SampleProjectDirectory, "dotnet", $"publish -c Release -r win-x64 {ToOption(publishMode)} {ToOption(deploymentMode)} {ToOption(nativeLibsDeploymentMode)}"); + CleanSampleProject(Configuration.Release, RID.Win_x64); + + Exec(SampleProjectDirectory, "dotnet", $"publish -c Release -r win-x64 {ToOption(publishMode)} {ToOption(deploymentMode)} {ToOption(nativeLibsDeploymentMode)} /bl"); string pathToAppHost = Path.Combine("bin", $"Release", "net5.0", "win-x64", "publish", "ComputeSharp.Sample.NuGet.exe"); Assert.AreEqual(0, Exec(SampleProjectDirectory, pathToAppHost, "")); } @@ -127,6 +133,18 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver Exec(SampleProjectDirectory, "dotnet", $"build -c {configuration} {ToOption(rid)}"); } + /// + /// Cleans the sample project's artifacts for a specific configuration and target runtime. + /// This method is called at the start of each test method to work around bugs around some up-to-date checks + /// Causing certain targets to not run and effectively corrupting the build output. + /// + /// The configuration for which the output is cleaned. + /// The RID for which the output is cleaned. + private static void CleanSampleProject(Configuration configuration, RID rid) + { + Exec(SampleProjectDirectory, "dotnet", $"clean -c {configuration} {ToOption(rid)}"); + } + /// /// Executes a specified process from the command line and returns the exit code. /// @@ -182,7 +200,7 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver private static string ToOption(NativeLibrariesDeploymentMode deploymentMode) => deploymentMode switch { NativeLibrariesDeploymentMode.NotApplicable => "", - NativeLibrariesDeploymentMode.BundleWithApplication => "/p:IncludeNativeLibrariesForSelfExtract=false", + NativeLibrariesDeploymentMode.CopyToApplicationDirectory => "/p:IncludeNativeLibrariesForSelfExtract=false", NativeLibrariesDeploymentMode.ExtractToTemporaryDirectory => "/p:IncludeNativeLibrariesForSelfExtract=true", _ => throw new InvalidEnumArgumentException(nameof(deploymentMode), (int)deploymentMode, typeof(NativeLibrariesDeploymentMode)) }; @@ -217,23 +235,35 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver Win_x64 } + /// + /// Publish mode: should the application carry the framework with itself. + /// public enum PublishMode { FrameworkDependent, SelfContained } + /// + /// Deployment mode: how should the application be packaged. + /// Notably, these tests employ .NET 5 style SingleFile, aka SuperHost. + /// It does not affect native libraries packaging in .NET 5, but may in the future. + /// public enum DeploymentMode { Multiassembly, SingleFile } + /// + /// Deployment mode for application's native dependencies. + /// Not applicable to multiassembly deployment mode. + /// public enum NativeLibrariesDeploymentMode { NotApplicable, ExtractToTemporaryDirectory, - BundleWithApplication + CopyToApplicationDirectory } } } From 49e31c220fb11fd1ea1016a85ae47a4ef00ccac6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 6 Jan 2021 15:39:08 +0100 Subject: [PATCH 3/3] Minor code style tweaks --- .../NativeLibrariesResolverTests.cs | 63 ++++++++++++------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs b/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs index 6be4765f..86e61ea8 100644 --- a/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs @@ -4,13 +4,18 @@ using System.Diagnostics; using System.IO; [assembly: Parallelize(Workers = 0, Scope = ExecutionScope.ClassLevel)] + namespace ComputeSharp.Tests.NativeLibrariesResolver { [TestClass] public class NativeLibrariesResolverTests { + /// + /// Performs static initialization for the assembly before any unit tests are run. + /// + /// The for the test runner instance in use. [AssemblyInitialize] - public static void Initialize(TestContext context) + public static void Initialize(TestContext _) { string path = Path.GetDirectoryName(typeof(NativeLibrariesResolverTests).Assembly.Location); @@ -23,7 +28,7 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver string packagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Package", "ComputeSharp.Package.msbuildproj"); - // Run dotnet pack and on the packaging project, to ensure the local NuGet package is available. + // Run dotnet pack and on the packaging project, to ensure the local NuGet package is available Process.Start("dotnet", $"pack {packagingProjectPath} -c Release").WaitForExit(); } @@ -114,23 +119,15 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver [DataRow(PublishMode.FrameworkDependent, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.ExtractToTemporaryDirectory)] public void DotnetPublishWorks(PublishMode publishMode, DeploymentMode deploymentMode, NativeLibrariesDeploymentMode nativeLibsDeploymentMode) { - // Publishing without specifying a RID is not supported - // We do not test Debug builds as it was determined that they are not an important scenario + // Publishing without specifying a RID is not supported. + // Furthermore, only publishing in Release mode is tested. CleanSampleProject(Configuration.Release, RID.Win_x64); Exec(SampleProjectDirectory, "dotnet", $"publish -c Release -r win-x64 {ToOption(publishMode)} {ToOption(deploymentMode)} {ToOption(nativeLibsDeploymentMode)} /bl"); - string pathToAppHost = Path.Combine("bin", $"Release", "net5.0", "win-x64", "publish", "ComputeSharp.Sample.NuGet.exe"); - Assert.AreEqual(0, Exec(SampleProjectDirectory, pathToAppHost, "")); - } - /// - /// Builds the sample project with a specific configuration and target runtime. - /// - /// The configuration to use to build the project. - /// The RID to use to build the project. - private static void BuildSampleProject(Configuration configuration, RID rid) - { - Exec(SampleProjectDirectory, "dotnet", $"build -c {configuration} {ToOption(rid)}"); + string pathToAppHost = Path.Combine("bin", $"Release", "net5.0", "win-x64", "publish", "ComputeSharp.Sample.NuGet.exe"); + + Assert.AreEqual(0, Exec(SampleProjectDirectory, pathToAppHost, "")); } /// @@ -145,6 +142,16 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver Exec(SampleProjectDirectory, "dotnet", $"clean -c {configuration} {ToOption(rid)}"); } + /// + /// Builds the sample project with a specific configuration and target runtime. + /// + /// The configuration to use to build the project. + /// The RID to use to build the project. + private static void BuildSampleProject(Configuration configuration, RID rid) + { + Exec(SampleProjectDirectory, "dotnet", $"build -c {configuration} {ToOption(rid)}"); + } + /// /// Executes a specified process from the command line and returns the exit code. /// @@ -172,7 +179,7 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver } /// - /// Gets a text representation of a cmd option for the dotnet tool, for the given . + /// Gets a text representation of the command line argument for the given . /// /// The input value. /// A text representation for . @@ -183,6 +190,11 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver _ => throw new InvalidEnumArgumentException(nameof(rid), (int)rid, typeof(RID)) }; + /// + /// Gets a text representation of the command line argument for the given . + /// + /// The input value. + /// A text representation for . private static string ToOption(PublishMode publishMode) => publishMode switch { PublishMode.FrameworkDependent => "--self-contained false", @@ -190,6 +202,11 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver _ => throw new InvalidEnumArgumentException(nameof(publishMode), (int)publishMode, typeof(PublishMode)) }; + /// + /// Gets a text representation of the command line argument for the given . + /// + /// The input value. + /// A text representation for . private static string ToOption(DeploymentMode deploymentMode) => deploymentMode switch { DeploymentMode.Multiassembly => "/p:PublishSingleFile=false", @@ -197,6 +214,11 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver _ => throw new InvalidEnumArgumentException(nameof(deploymentMode), (int)deploymentMode, typeof(DeploymentMode)) }; + /// + /// Gets a text representation of the command line argument for the given . + /// + /// The input value. + /// A text representation for . private static string ToOption(NativeLibrariesDeploymentMode deploymentMode) => deploymentMode switch { NativeLibrariesDeploymentMode.NotApplicable => "", @@ -236,7 +258,7 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver } /// - /// Publish mode: should the application carry the framework with itself. + /// Indicates how should the application carry the framework with itself. /// public enum PublishMode { @@ -245,9 +267,8 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver } /// - /// Deployment mode: how should the application be packaged. - /// Notably, these tests employ .NET 5 style SingleFile, aka SuperHost. - /// It does not affect native libraries packaging in .NET 5, but may in the future. + /// Indicates how should the application be packaged. Notably, these tests employ .NET 5 style SingleFile, + /// aka SuperHost. It does not affect native libraries packaging in .NET 5, but may in the future. /// public enum DeploymentMode { @@ -256,7 +277,7 @@ namespace ComputeSharp.Tests.NativeLibrariesResolver } /// - /// Deployment mode for application's native dependencies. + /// Indicates the deployment mode for application's native dependencies. /// Not applicable to multiassembly deployment mode. /// public enum NativeLibrariesDeploymentMode