diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 2b0e5901..e307114c 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -251,23 +251,101 @@ jobs: with: name: nuget_preview path: artifacts - - name: Build ComputeSharp.Sample.NuGet (.NET 6) - run: dotnet build samples\ComputeSharp.Sample.NuGet\ComputeSharp.Sample.NuGet.csproj -c Release -f net6.0 + - name: Build ComputeSharp.NuGet (.NET 6) + run: dotnet build tests\ComputeSharp.NuGet\ComputeSharp.NuGet.csproj -c Release -f net6.0 shell: cmd - - name: Run ComputeSharp.Sample.NuGet (.NET 6) - run: dotnet samples\ComputeSharp.Sample.NuGet\bin\Release\net6.0\ComputeSharp.Sample.NuGet.dll + - name: Run ComputeSharp.NuGet (.NET 6) + run: dotnet tests\ComputeSharp.NuGet\bin\Release\net6.0\ComputeSharp.NuGet.dll shell: cmd - - name: Build ComputeSharp.Sample.NuGet (.NET Core 3.1) - run: dotnet build samples\ComputeSharp.Sample.NuGet\ComputeSharp.Sample.NuGet.csproj -c Release -f netcoreapp3.1 -r win-x64 + - name: Build ComputeSharp.NuGet (.NET Core 3.1) + run: dotnet build tests\ComputeSharp.NuGet\ComputeSharp.NuGet.csproj -c Release -f netcoreapp3.1 -r win-x64 shell: cmd - - name: Run ComputeSharp.Sample.NuGet (.NET Core 3.1) - run: samples\ComputeSharp.Sample.NuGet\bin\Release\netcoreapp3.1\win-x64\ComputeSharp.Sample.NuGet.exe + - name: Run ComputeSharp.NuGet (.NET Core 3.1) + run: tests\ComputeSharp.NuGet\bin\Release\netcoreapp3.1\win-x64\ComputeSharp.NuGet.exe shell: cmd - - name: Build ComputeSharp.Sample.NuGet (.NET Framework 4.7.2) - run: dotnet build samples\ComputeSharp.Sample.NuGet\ComputeSharp.Sample.NuGet.csproj -c Release -f net472 /p:Platform=x64 + - name: Build ComputeSharp.NuGet (.NET Framework 4.7.2) + run: dotnet build tests\ComputeSharp.NuGet\ComputeSharp.NuGet.csproj -c Release -f net472 /p:Platform=x64 shell: cmd - - name: Run ComputeSharp.Sample.NuGet (.NET Framework 4.7.2) - run: samples\ComputeSharp.Sample.NuGet\bin\x64\Release\net472\ComputeSharp.Sample.NuGet.exe + - name: Run ComputeSharp.NuGet (.NET Framework 4.7.2) + run: tests\ComputeSharp.NuGet\bin\x64\Release\net472\ComputeSharp.NuGet.exe + shell: cmd + + # Download the NuGet packages and test the ComputeSharp.Dynamic dependency + verify-packages-dynamic: + if: success() + needs: [build-packages] + runs-on: windows-2022 + steps: + - name: Git checkout + uses: actions/checkout@v2 + - name: Setup .NET 6 SDK + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 6.0.x + - name: Create local NuGet feed + run: mkdir artifacts + shell: cmd + - name: Download package artifacts + uses: actions/download-artifact@v2 + with: + name: nuget_preview + path: artifacts + - name: Build ComputeSharp.Dynamic.NuGet (.NET 6) + run: dotnet build tests\ComputeSharp.Dynamic.NuGet\ComputeSharp.Dynamic.NuGet.csproj -c Release -f net6.0 + shell: cmd + - name: Run ComputeSharp.Dynamic.NuGet (.NET 6) + run: dotnet tests\ComputeSharp.Dynamic.NuGet\bin\Release\net6.0\ComputeSharp.Dynamic.NuGet.dll + shell: cmd + - name: Build ComputeSharp.Dynamic.NuGet (.NET Core 3.1) + run: dotnet build tests\ComputeSharp.Dynamic.NuGet\ComputeSharp.Dynamic.NuGet.csproj -c Release -f netcoreapp3.1 -r win-x64 + shell: cmd + - name: Run ComputeSharp.Dynamic.NuGet (.NET Core 3.1) + run: tests\ComputeSharp.Dynamic.NuGet\bin\Release\netcoreapp3.1\win-x64\ComputeSharp.Dynamic.NuGet.exe + shell: cmd + - name: Build ComputeSharp.Dynamic.NuGet (.NET Framework 4.7.2) + run: dotnet build tests\ComputeSharp.Dynamic.NuGet\ComputeSharp.Dynamic.NuGet.csproj -c Release -f net472 /p:Platform=x64 + shell: cmd + - name: Run ComputeSharp.Dynamic.NuGet (.NET Framework 4.7.2) + run: tests\ComputeSharp.Dynamic.NuGet\bin\x64\Release\net472\ComputeSharp.Dynamic.NuGet.exe + shell: cmd + + # Download the NuGet packages and test the ComputeSharp.Pix dependency + verify-packages-pix: + if: success() + needs: [build-packages] + runs-on: windows-2022 + steps: + - name: Git checkout + uses: actions/checkout@v2 + - name: Setup .NET 6 SDK + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 6.0.x + - name: Create local NuGet feed + run: mkdir artifacts + shell: cmd + - name: Download package artifacts + uses: actions/download-artifact@v2 + with: + name: nuget_preview + path: artifacts + - name: Build ComputeSharp.Pix.NuGet (.NET 6) + run: dotnet build tests\ComputeSharp.Pix.NuGet\ComputeSharp.Pix.NuGet.csproj -c Release -f net6.0 + shell: cmd + - name: Run ComputeSharp.Pix.NuGet (.NET 6) + run: dotnet tests\ComputeSharp.Pix.NuGet\bin\Release\net6.0\ComputeSharp.Pix.NuGet.dll + shell: cmd + - name: Build ComputeSharp.Pix.NuGet (.NET Core 3.1) + run: dotnet build tests\ComputeSharp.Pix.NuGet\ComputeSharp.Pix.NuGet.csproj -c Release -f netcoreapp3.1 -r win-x64 + shell: cmd + - name: Run ComputeSharp.Pix.NuGet (.NET Core 3.1) + run: tests\ComputeSharp.Pix.NuGet\bin\Release\netcoreapp3.1\win-x64\ComputeSharp.Pix.NuGet.exe + shell: cmd + - name: Build ComputeSharp.Pix.NuGet (.NET Framework 4.7.2) + run: dotnet build tests\ComputeSharp.Pix.NuGet\ComputeSharp.Pix.NuGet.csproj -c Release -f net472 /p:Platform=x64 + shell: cmd + - name: Run ComputeSharp.Pix.NuGet (.NET Framework 4.7.2) + run: tests\ComputeSharp.Pix.NuGet\bin\x64\Release\net472\ComputeSharp.Pix.NuGet.exe shell: cmd # Run the extra tests to validate a number of build and publishing configurations. @@ -290,7 +368,7 @@ jobs: # Publish the packages to GitHub packages publish-nightlies-github: - needs: [ run-dx12-tests, run-d2d1-tests, run-dx12-samples, verify-packages, verify-package-native-libs ] + needs: [run-dx12-tests, run-d2d1-tests, run-dx12-samples, verify-packages, verify-packages-dynamic, verify-packages-pix, verify-package-native-libs] runs-on: windows-2022 if: ${{github.event_name == 'push'}} steps: @@ -306,7 +384,7 @@ jobs: # Publish the packages to Azure DevOps publish-nightlies-azure-devops: - needs: [ run-dx12-tests, run-d2d1-tests, run-dx12-samples, verify-packages, verify-package-native-libs ] + needs: [run-dx12-tests, run-d2d1-tests, run-dx12-samples, verify-packages, verify-packages-dynamic, verify-packages-pix, verify-package-native-libs] runs-on: windows-2022 if: ${{github.event_name == 'push'}} steps: diff --git a/ComputeSharp.sln b/ComputeSharp.sln index ea5f3016..880d7110 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -28,7 +28,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_Solution EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.SourceGenerators", "src\ComputeSharp.SourceGenerators\ComputeSharp.SourceGenerators.csproj", "{EB52CB67-4162-4722-874B-44EA8DA5450E}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NuGet", "NuGet", "{FB17EF84-9208-47AC-921F-680CB1DAD8BA}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Packages", "Packages", "{FB17EF84-9208-47AC-921F-680CB1DAD8BA}" ProjectSection(SolutionItems) = preProject src\ComputeSharp.Core.Package\ComputeSharp.Core.Package.msbuildproj = src\ComputeSharp.Core.Package\ComputeSharp.Core.Package.msbuildproj src\ComputeSharp.D2D1.Package\ComputeSharp.D2D1.Package.msbuildproj = src\ComputeSharp.D2D1.Package\ComputeSharp.D2D1.Package.msbuildproj @@ -103,6 +103,27 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.D2D1.Tests.Ass EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Pix", "src\ComputeSharp.Pix\ComputeSharp.Pix.csproj", "{14EF12A3-90E6-4683-BD71-9896AF87026B}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ComputeSharp.Dynamic.NuGet", "ComputeSharp.Dynamic.NuGet", "{CBA545B4-31EF-4E9F-A1A8-2A1290B54046}" + ProjectSection(SolutionItems) = preProject + tests\ComputeSharp.Dynamic.NuGet\ComputeSharp.Dynamic.NuGet.csproj = tests\ComputeSharp.Dynamic.NuGet\ComputeSharp.Dynamic.NuGet.csproj + tests\ComputeSharp.Dynamic.NuGet\nuget.config = tests\ComputeSharp.Dynamic.NuGet\nuget.config + tests\ComputeSharp.Dynamic.NuGet\Program.cs = tests\ComputeSharp.Dynamic.NuGet\Program.cs + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ComputeSharp.Pix.NuGet", "ComputeSharp.Pix.NuGet", "{C0B903AA-F32C-4760-B4B6-199AC4FE1C5A}" + ProjectSection(SolutionItems) = preProject + tests\ComputeSharp.Pix.NuGet\ComputeSharp.Pix.NuGet.csproj = tests\ComputeSharp.Pix.NuGet\ComputeSharp.Pix.NuGet.csproj + tests\ComputeSharp.Pix.NuGet\nuget.config = tests\ComputeSharp.Pix.NuGet\nuget.config + tests\ComputeSharp.Pix.NuGet\Program.cs = tests\ComputeSharp.Pix.NuGet\Program.cs + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ComputeSharp.NuGet", "ComputeSharp.NuGet", "{E3403F19-A0D8-4317-8A54-322C7A13F2A2}" + ProjectSection(SolutionItems) = preProject + tests\ComputeSharp.NuGet\ComputeSharp.NuGet.csproj = tests\ComputeSharp.NuGet\ComputeSharp.NuGet.csproj + tests\ComputeSharp.NuGet\nuget.config = tests\ComputeSharp.NuGet\nuget.config + tests\ComputeSharp.NuGet\Program.cs = tests\ComputeSharp.NuGet\Program.cs + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -454,6 +475,9 @@ Global {3ACACF03-6B20-40BA-B8EC-11AC1624D2C9} = {8DCE343F-BA78-4FE1-B562-B57C603A8BAA} {209C95A3-FA53-431B-B688-3299CA6C29D2} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {94178D51-0097-4AD2-B3D0-827E40D62F2E} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} + {CBA545B4-31EF-4E9F-A1A8-2A1290B54046} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} + {C0B903AA-F32C-4760-B4B6-199AC4FE1C5A} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} + {E3403F19-A0D8-4317-8A54-322C7A13F2A2} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} diff --git a/samples/ComputeSharp.Sample.NuGet/ComputeSharp.Sample.NuGet.csproj b/tests/ComputeSharp.Dynamic.NuGet/ComputeSharp.Dynamic.NuGet.csproj similarity index 82% rename from samples/ComputeSharp.Sample.NuGet/ComputeSharp.Sample.NuGet.csproj rename to tests/ComputeSharp.Dynamic.NuGet/ComputeSharp.Dynamic.NuGet.csproj index f75aab80..37213757 100644 --- a/samples/ComputeSharp.Sample.NuGet/ComputeSharp.Sample.NuGet.csproj +++ b/tests/ComputeSharp.Dynamic.NuGet/ComputeSharp.Dynamic.NuGet.csproj @@ -12,6 +12,4 @@ - - diff --git a/tests/ComputeSharp.Dynamic.NuGet/Program.cs b/tests/ComputeSharp.Dynamic.NuGet/Program.cs new file mode 100644 index 00000000..31b654f0 --- /dev/null +++ b/tests/ComputeSharp.Dynamic.NuGet/Program.cs @@ -0,0 +1,35 @@ +using System.Diagnostics; +using System.Linq; +using ComputeSharp; + +float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray(); + +// Create the graphics buffer +using ReadWriteBuffer gpuBuffer = GraphicsDevice.Default.AllocateReadWriteBuffer(array); + +// Run the shader +GraphicsDevice.Default.For(100, new MultiplyByTwo(gpuBuffer)); + +// Get the data back +float[] result = gpuBuffer.ToArray(); + +// Validate results +for (int i = 0; i < array.Length; i++) +{ + Trace.Assert(result[i] == array[i] * 2); +} + +/// +/// A sample kernel that requires dynamic compilation, as it's not precompiled. +/// +[AutoConstructor] +internal readonly partial struct MultiplyByTwo : IComputeShader +{ + public readonly ReadWriteBuffer buffer; + + /// + public void Execute() + { + buffer[ThreadIds.X] *= 2; + } +} diff --git a/samples/ComputeSharp.Sample.NuGet/nuget.config b/tests/ComputeSharp.Dynamic.NuGet/nuget.config similarity index 100% rename from samples/ComputeSharp.Sample.NuGet/nuget.config rename to tests/ComputeSharp.Dynamic.NuGet/nuget.config diff --git a/tests/ComputeSharp.NuGet/ComputeSharp.NuGet.csproj b/tests/ComputeSharp.NuGet/ComputeSharp.NuGet.csproj new file mode 100644 index 00000000..7bdf5213 --- /dev/null +++ b/tests/ComputeSharp.NuGet/ComputeSharp.NuGet.csproj @@ -0,0 +1,14 @@ + + + + Exe + net472;netcoreapp3.1;net6.0 + AnyCPU;x64;ARM64 + + + + + + + + diff --git a/tests/ComputeSharp.NuGet/Program.cs b/tests/ComputeSharp.NuGet/Program.cs new file mode 100644 index 00000000..591d80de --- /dev/null +++ b/tests/ComputeSharp.NuGet/Program.cs @@ -0,0 +1,36 @@ +using System.Diagnostics; +using System.Linq; +using ComputeSharp; + +float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray(); + +// Create the graphics buffer +using ReadWriteBuffer gpuBuffer = GraphicsDevice.Default.AllocateReadWriteBuffer(array); + +// Run the shader +GraphicsDevice.Default.For(100, new MultiplyByTwo(gpuBuffer)); + +// Get the data back +float[] result = gpuBuffer.ToArray(); + +// Validate results +for (int i = 0; i < array.Length; i++) +{ + Trace.Assert(result[i] == array[i] * 2); +} + +/// +/// A sample kernel that is precompiled. +/// +[EmbeddedBytecode(DispatchAxis.X)] +[AutoConstructor] +internal readonly partial struct MultiplyByTwo : IComputeShader +{ + public readonly ReadWriteBuffer buffer; + + /// + public void Execute() + { + buffer[ThreadIds.X] *= 2; + } +} diff --git a/tests/ComputeSharp.NuGet/nuget.config b/tests/ComputeSharp.NuGet/nuget.config new file mode 100644 index 00000000..995d3a62 --- /dev/null +++ b/tests/ComputeSharp.NuGet/nuget.config @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/tests/ComputeSharp.Pix.NuGet/ComputeSharp.Pix.NuGet.csproj b/tests/ComputeSharp.Pix.NuGet/ComputeSharp.Pix.NuGet.csproj new file mode 100644 index 00000000..5f156d64 --- /dev/null +++ b/tests/ComputeSharp.Pix.NuGet/ComputeSharp.Pix.NuGet.csproj @@ -0,0 +1,15 @@ + + + + Exe + net472;netcoreapp3.1;net6.0 + AnyCPU;x64;ARM64 + + + + + + + + + diff --git a/tests/ComputeSharp.Pix.NuGet/Program.cs b/tests/ComputeSharp.Pix.NuGet/Program.cs new file mode 100644 index 00000000..c2119998 --- /dev/null +++ b/tests/ComputeSharp.Pix.NuGet/Program.cs @@ -0,0 +1,42 @@ +using System.Diagnostics; +using System.Linq; +using ComputeSharp; + +float[] array = Enumerable.Range(1, 100).Select(static i => (float)i).ToArray(); + +// Create the graphics buffer +using ReadWriteBuffer gpuBuffer = GraphicsDevice.Default.AllocateReadWriteBuffer(array); + +// Run the shader +using (var context = GraphicsDevice.Default.CreateComputeContext()) +{ + context.Log("Dispatching shader"); + context.BeginEvent("Dispatching event"); + context.For(100, new MultiplyByTwo(gpuBuffer)); + context.EndEvent(); +} + +// Get the data back +float[] result = gpuBuffer.ToArray(); + +// Validate results +for (int i = 0; i < array.Length; i++) +{ + Trace.Assert(result[i] == array[i] * 2); +} + +/// +/// A sample kernel that is precompiled. +/// +[EmbeddedBytecode(DispatchAxis.X)] +[AutoConstructor] +internal readonly partial struct MultiplyByTwo : IComputeShader +{ + public readonly ReadWriteBuffer buffer; + + /// + public void Execute() + { + buffer[ThreadIds.X] *= 2; + } +} diff --git a/tests/ComputeSharp.Pix.NuGet/nuget.config b/tests/ComputeSharp.Pix.NuGet/nuget.config new file mode 100644 index 00000000..995d3a62 --- /dev/null +++ b/tests/ComputeSharp.Pix.NuGet/nuget.config @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/ComputeSharp.Tests.NativeLibrariesResolver.csproj b/tests/ComputeSharp.Tests.NativeLibrariesResolver/ComputeSharp.Tests.NativeLibrariesResolver.csproj index cb3020de..21bca770 100644 --- a/tests/ComputeSharp.Tests.NativeLibrariesResolver/ComputeSharp.Tests.NativeLibrariesResolver.csproj +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/ComputeSharp.Tests.NativeLibrariesResolver.csproj @@ -11,4 +11,9 @@ + + + + + diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/DynamicNativeLibrariesResolverTests.cs b/tests/ComputeSharp.Tests.NativeLibrariesResolver/DynamicNativeLibrariesResolverTests.cs new file mode 100644 index 00000000..d990e218 --- /dev/null +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/DynamicNativeLibrariesResolverTests.cs @@ -0,0 +1,37 @@ +using System.Diagnostics; +using System.IO; +using ComputeSharp.Tests.NativeLibrariesResolver.Helpers; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.NativeLibrariesResolver; + +[TestClass] +public class DynamicNativeLibrariesResolverTests : NativeLibrariesResolverTestsBase +{ + /// + protected override string SampleProjectName => "ComputeSharp.Dynamic.NuGet"; + + /// + /// Performs static initialization for the assembly before any unit tests are run. + /// + /// The for the test runner instance in use. + [ClassInitialize] + public static void InitializeDynamicDependencies(TestContext _) + { + string path = Path.GetDirectoryName(typeof(DynamicNativeLibrariesResolverTests).Assembly.Location)!; + + while (Path.GetFileName(path) is not "ComputeSharp") + { + path = Path.GetDirectoryName(path)!; + } + + string corePackagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Core", "ComputeSharp.Core.msbuildproj"); + string packagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Package", "ComputeSharp.Package.msbuildproj"); + string dynamicPackagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Dynamic.Package", "ComputeSharp.Dynamic.Package.msbuildproj"); + + // Run dotnet pack and on the packaging projects, to ensure the local NuGet packages are available + Process.Start("dotnet", $"pack {corePackagingProjectPath} -c Release").WaitForExit(); + Process.Start("dotnet", $"pack {packagingProjectPath} -c Release").WaitForExit(); + Process.Start("dotnet", $"pack {dynamicPackagingProjectPath} -c Release").WaitForExit(); + } +} diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/Helpers/NativeLibrariesResolverTestsBase.cs b/tests/ComputeSharp.Tests.NativeLibrariesResolver/Helpers/NativeLibrariesResolverTestsBase.cs new file mode 100644 index 00000000..503b491a --- /dev/null +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/Helpers/NativeLibrariesResolverTestsBase.cs @@ -0,0 +1,285 @@ +using System.ComponentModel; +using System.Diagnostics; +using System.IO; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.NativeLibrariesResolver.Helpers; + +/// +/// A base class to implement native libraries tests. +/// +public abstract class NativeLibrariesResolverTestsBase +{ + /// + /// Gets the name of the test project. + /// + protected abstract string SampleProjectName { get; } + + /// + /// Gets the directory containing the current test project. + /// + private string SampleProjectDirectory + { + get + { + string path = Path.GetDirectoryName(typeof(NativeLibrariesResolverTestsBase).Assembly.Location)!; + + while (Path.GetFileName(path) is not "tests") + { + path = Path.GetDirectoryName(path)!; + } + + return Path.Combine(path, SampleProjectName); + } + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [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} -f net6.0 {ToOption(rid)}")); + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [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}", "net6.0", $"{ToDirectory(rid)}", $"{SampleProjectName}.dll"); + + Assert.AreEqual(0, Exec(SampleProjectDirectory, "dotnet", realtivePathToDll)); + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [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}", "net6.0", $"{ToDirectory(rid)}"); + + Assert.AreEqual(0, Exec(pathToDllDirectory, "dotnet", $"{SampleProjectName}.dll")); + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [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}", "net6.0", $"{ToDirectory(rid)}", $"{SampleProjectName}.exe"); + + Assert.AreEqual(0, Exec(SampleProjectDirectory, relativePathToAppHost, "")); + } + + [TestMethod] + [DataRow(Configuration.Debug, RID.None)] + [DataRow(Configuration.Debug, RID.Win_x64)] + [DataRow(Configuration.Release, RID.None)] + [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}", "net6.0", $"{ToDirectory(rid)}"); + + Assert.AreEqual(0, Exec(pathToAppHostDirectory, $"{SampleProjectName}.exe", "")); + } + + + [TestMethod] + [DataRow(PublishMode.SelfContained, DeploymentMode.Multiassembly, NativeLibrariesDeploymentMode.NotApplicable)] + [DataRow(PublishMode.FrameworkDependent, DeploymentMode.Multiassembly, NativeLibrariesDeploymentMode.NotApplicable)] + [DataRow(PublishMode.SelfContained, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.CopyToApplicationDirectory)] + [DataRow(PublishMode.SelfContained, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.ExtractToTemporaryDirectory)] + [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. + // Furthermore, only publishing in Release mode is tested. + CleanSampleProject(Configuration.Release, RID.Win_x64); + + Exec(SampleProjectDirectory, "dotnet", $"publish -c Release -f net6.0 -r win-x64 {ToOption(publishMode)} {ToOption(deploymentMode)} {ToOption(nativeLibsDeploymentMode)} /bl"); + + string pathToAppHost = Path.Combine("bin", $"Release", "net6.0", "win-x64", "publish", $"{SampleProjectName}.exe"); + + Assert.AreEqual(0, Exec(SampleProjectDirectory, pathToAppHost, "")); + } + + /// + /// 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 void CleanSampleProject(Configuration configuration, RID rid) + { + 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 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. + /// + /// The working directory to execute the process. + /// The target file path for the process to execute. + /// The arguments to invoke the process. + /// The exit code for the executed process. + private static int Exec(string workingDirectory, string filePath, string arguments) + { + ProcessStartInfo startInfo = new() + { + UseShellExecute = true, + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden, + FileName = filePath, + Arguments = arguments, + WorkingDirectory = workingDirectory + }; + + using Process process = Process.Start(startInfo)!; + + process.WaitForExit(); + + return process.ExitCode; + } + + /// + /// Gets a text representation of the command line argument for the given . + /// + /// The input value. + /// A text representation for . + private static string ToOption(RID rid) => rid switch + { + RID.None => "", + RID.Win_x64 => "-r win-x64", + _ => 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", + PublishMode.SelfContained => "--self-contained true", + _ => 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", + DeploymentMode.SingleFile => "/p:PublishSingleFile=true", + _ => 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 => "", + NativeLibrariesDeploymentMode.CopyToApplicationDirectory => "/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 . + /// + /// The input value. + /// A text representation for . + private static string ToDirectory(RID rid) => rid switch + { + RID.None => "", + RID.Win_x64 => "win-x64", + _ => throw new InvalidEnumArgumentException(nameof(rid), (int)rid, typeof(RID)) + }; +} + +/// +/// A build configuration. +/// +public enum Configuration +{ + Debug, + Release +} + +/// +/// A runtime identifier. +/// +public enum RID +{ + None, + Win_x64 +} + +/// +/// Indicates how should the application carry the framework with itself. +/// +public enum PublishMode +{ + FrameworkDependent, + SelfContained +} + +/// +/// Indicates how should the application be packaged. Notably, these tests employ .NET 6 style SingleFile, +/// aka SuperHost. It does not affect native libraries packaging in .NET 6, but may in the future. +/// +public enum DeploymentMode +{ + Multiassembly, + SingleFile +} + +/// +/// Indicates the deployment mode for application's native dependencies. +/// Not applicable to multiassembly deployment mode. +/// +public enum NativeLibrariesDeploymentMode +{ + NotApplicable, + ExtractToTemporaryDirectory, + CopyToApplicationDirectory +} \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs b/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs index c949a9f3..6e8afe8c 100644 --- a/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/NativeLibrariesResolverTests.cs @@ -1,291 +1,35 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.ComponentModel; -using System.Diagnostics; +using System.Diagnostics; using System.IO; - -[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.ClassLevel)] +using ComputeSharp.Tests.NativeLibrariesResolver.Helpers; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests.NativeLibrariesResolver; [TestClass] -public class NativeLibrariesResolverTests +public class NativeLibrariesResolverTests : NativeLibrariesResolverTestsBase { + /// + protected override string SampleProjectName => "ComputeSharp.NuGet"; + /// /// 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 _) + [ClassInitialize] + public static void InitializeDynamicDependencies(TestContext _) { - string path = Path.GetDirectoryName(typeof(NativeLibrariesResolverTests).Assembly.Location)!; + string path = Path.GetDirectoryName(typeof(DynamicNativeLibrariesResolverTests).Assembly.Location)!; while (Path.GetFileName(path) is not "ComputeSharp") { path = Path.GetDirectoryName(path)!; } - SampleProjectDirectory = Path.Combine(path, "samples", "ComputeSharp.Sample.NuGet"); - + string corePackagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Core", "ComputeSharp.Core.msbuildproj"); string packagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Package", "ComputeSharp.Package.msbuildproj"); - string dynamicPackagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Dynamic.Package", "ComputeSharp.Dynamic.Package.msbuildproj"); // Run dotnet pack and on the packaging projects, to ensure the local NuGet packages are available + Process.Start("dotnet", $"pack {corePackagingProjectPath} -c Release").WaitForExit(); Process.Start("dotnet", $"pack {packagingProjectPath} -c Release").WaitForExit(); - Process.Start("dotnet", $"pack {dynamicPackagingProjectPath} -c Release").WaitForExit(); - } - - /// - /// Gets the directory of the ComputeSharp.Sample.NuGet project. - /// - private static string? SampleProjectDirectory { get; set; } - - [TestMethod] - [DataRow(Configuration.Debug, RID.None)] - [DataRow(Configuration.Debug, RID.Win_x64)] - [DataRow(Configuration.Release, RID.None)] - [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} -f net6.0 {ToOption(rid)}")); - } - - [TestMethod] - [DataRow(Configuration.Debug, RID.None)] - [DataRow(Configuration.Debug, RID.Win_x64)] - [DataRow(Configuration.Release, RID.None)] - [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}", "net6.0", $"{ToDirectory(rid)}", "ComputeSharp.Sample.NuGet.dll"); - - Assert.AreEqual(0, Exec(SampleProjectDirectory!, "dotnet", realtivePathToDll)); - } - - [TestMethod] - [DataRow(Configuration.Debug, RID.None)] - [DataRow(Configuration.Debug, RID.Win_x64)] - [DataRow(Configuration.Release, RID.None)] - [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}", "net6.0", $"{ToDirectory(rid)}"); - - Assert.AreEqual(0, Exec(pathToDllDirectory, "dotnet", "ComputeSharp.Sample.NuGet.dll")); - } - - [TestMethod] - [DataRow(Configuration.Debug, RID.None)] - [DataRow(Configuration.Debug, RID.Win_x64)] - [DataRow(Configuration.Release, RID.None)] - [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}", "net6.0", $"{ToDirectory(rid)}", "ComputeSharp.Sample.NuGet.exe"); - - Assert.AreEqual(0, Exec(SampleProjectDirectory!, relativePathToAppHost, "")); - } - - [TestMethod] - [DataRow(Configuration.Debug, RID.None)] - [DataRow(Configuration.Debug, RID.Win_x64)] - [DataRow(Configuration.Release, RID.None)] - [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}", "net6.0", $"{ToDirectory(rid)}"); - - Assert.AreEqual(0, Exec(pathToAppHostDirectory, "ComputeSharp.Sample.NuGet.exe", "")); - } - - - [TestMethod] - [DataRow(PublishMode.SelfContained, DeploymentMode.Multiassembly, NativeLibrariesDeploymentMode.NotApplicable)] - [DataRow(PublishMode.FrameworkDependent, DeploymentMode.Multiassembly, NativeLibrariesDeploymentMode.NotApplicable)] - [DataRow(PublishMode.SelfContained, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.CopyToApplicationDirectory)] - [DataRow(PublishMode.SelfContained, DeploymentMode.SingleFile, NativeLibrariesDeploymentMode.ExtractToTemporaryDirectory)] - [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. - // Furthermore, only publishing in Release mode is tested. - CleanSampleProject(Configuration.Release, RID.Win_x64); - - Exec(SampleProjectDirectory!, "dotnet", $"publish -c Release -f net6.0 -r win-x64 {ToOption(publishMode)} {ToOption(deploymentMode)} {ToOption(nativeLibsDeploymentMode)} /bl"); - - string pathToAppHost = Path.Combine("bin", $"Release", "net6.0", "win-x64", "publish", "ComputeSharp.Sample.NuGet.exe"); - - Assert.AreEqual(0, Exec(SampleProjectDirectory!, pathToAppHost, "")); - } - - /// - /// 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)}"); - } - - /// - /// 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. - /// - /// The working directory to execute the process. - /// The target file path for the process to execute. - /// The arguments to invoke the process. - /// The exit code for the executed process. - private static int Exec(string workingDirectory, string filePath, string arguments) - { - ProcessStartInfo startInfo = new() - { - UseShellExecute = true, - CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden, - FileName = filePath, - Arguments = arguments, - WorkingDirectory = workingDirectory - }; - - using Process process = Process.Start(startInfo)!; - - process.WaitForExit(); - - return process.ExitCode; - } - - /// - /// Gets a text representation of the command line argument for the given . - /// - /// The input value. - /// A text representation for . - private static string ToOption(RID rid) => rid switch - { - RID.None => "", - RID.Win_x64 => "-r win-x64", - _ => 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", - PublishMode.SelfContained => "--self-contained true", - _ => 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", - DeploymentMode.SingleFile => "/p:PublishSingleFile=true", - _ => 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 => "", - NativeLibrariesDeploymentMode.CopyToApplicationDirectory => "/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 . - /// - /// The input value. - /// A text representation for . - private static string ToDirectory(RID rid) => rid switch - { - RID.None => "", - RID.Win_x64 => "win-x64", - _ => throw new InvalidEnumArgumentException(nameof(rid), (int)rid, typeof(RID)) - }; - - /// - /// A build configuration. - /// - public enum Configuration - { - Debug, - Release - } - - /// - /// A runtime identifier. - /// - public enum RID - { - None, - Win_x64 - } - - /// - /// Indicates how should the application carry the framework with itself. - /// - public enum PublishMode - { - FrameworkDependent, - SelfContained - } - - /// - /// Indicates how should the application be packaged. Notably, these tests employ .NET 6 style SingleFile, - /// aka SuperHost. It does not affect native libraries packaging in .NET 6, but may in the future. - /// - public enum DeploymentMode - { - Multiassembly, - SingleFile - } - - /// - /// Indicates the deployment mode for application's native dependencies. - /// Not applicable to multiassembly deployment mode. - /// - public enum NativeLibrariesDeploymentMode - { - NotApplicable, - ExtractToTemporaryDirectory, - CopyToApplicationDirectory } } diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/PixNativeLibrariesResolverTests.cs b/tests/ComputeSharp.Tests.NativeLibrariesResolver/PixNativeLibrariesResolverTests.cs new file mode 100644 index 00000000..9624c1c4 --- /dev/null +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/PixNativeLibrariesResolverTests.cs @@ -0,0 +1,37 @@ +using System.Diagnostics; +using System.IO; +using ComputeSharp.Tests.NativeLibrariesResolver.Helpers; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.NativeLibrariesResolver; + +[TestClass] +public class PixNativeLibrariesResolverTests : NativeLibrariesResolverTestsBase +{ + /// + protected override string SampleProjectName => "ComputeSharp.Pix.NuGet"; + + /// + /// Performs static initialization for the assembly before any unit tests are run. + /// + /// The for the test runner instance in use. + [ClassInitialize] + public static void InitializeDynamicDependencies(TestContext _) + { + string path = Path.GetDirectoryName(typeof(PixNativeLibrariesResolverTests).Assembly.Location)!; + + while (Path.GetFileName(path) is not "ComputeSharp") + { + path = Path.GetDirectoryName(path)!; + } + + string corePackagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Core", "ComputeSharp.Core.msbuildproj"); + string packagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Package", "ComputeSharp.Package.msbuildproj"); + string pixPackagingProjectPath = Path.Combine(path, "src", "ComputeSharp.Pix.Package", "ComputeSharp.Pix.Package.msbuildproj"); + + // Run dotnet pack and on the packaging projects, to ensure the local NuGet packages are available + Process.Start("dotnet", $"pack {corePackagingProjectPath} -c Release").WaitForExit(); + Process.Start("dotnet", $"pack {packagingProjectPath} -c Release").WaitForExit(); + Process.Start("dotnet", $"pack {pixPackagingProjectPath} -c Release").WaitForExit(); + } +} diff --git a/tests/ComputeSharp.Tests.NativeLibrariesResolver/Properties/AssemblyInfo.cs b/tests/ComputeSharp.Tests.NativeLibrariesResolver/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..6a076669 --- /dev/null +++ b/tests/ComputeSharp.Tests.NativeLibrariesResolver/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +[assembly: DoNotParallelize] \ No newline at end of file