diff --git a/eng/testing/scenarios/BuildWasmAppsJobsList.txt b/eng/testing/scenarios/BuildWasmAppsJobsList.txt index 11bfdab2c7414e..df0ef94f26facc 100644 --- a/eng/testing/scenarios/BuildWasmAppsJobsList.txt +++ b/eng/testing/scenarios/BuildWasmAppsJobsList.txt @@ -47,3 +47,4 @@ Wasm.Build.Tests.WasmTemplateTests Wasm.Build.Tests.WorkloadTests Wasm.Build.Tests.MT.Blazor.SimpleMultiThreadedTests Wasm.Build.Tests.TestAppScenarios.DebugLevelTests +Wasm.Build.Tests.DownlevelCheck diff --git a/src/mono/wasm/Wasm.Build.Tests/DownLevelCheck.cs b/src/mono/wasm/Wasm.Build.Tests/DownLevelCheck.cs new file mode 100644 index 00000000000000..5372d4dba5c141 --- /dev/null +++ b/src/mono/wasm/Wasm.Build.Tests/DownLevelCheck.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Xunit; +using Xunit.Abstractions; + +namespace Wasm.Build.Tests +{ + public class DownlevelCheck : BuildTestBase + { + public DownlevelCheck(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) + : base(new TestMainJsProjectProvider(output), output, buildContext) + { + } + + private const string ProjectTemplate = + """ + + + {TargetFramework} + Exe + + + + + + """; + + private string FormatTestMessage(string version) => $"TEST::TargetingPackVersion::{version}::TEST"; + + public static IEnumerable GetData() => new[] + { + new object[] { "net6.0", DownlevelVersions.PackageVersionNet6 }, + new object[] { "net7.0", DownlevelVersions.PackageVersionNet7 }, + new object[] { "net8.0", DownlevelVersions.PackageVersionNet8 }, + }; + + [Theory] + [MemberData(nameof(GetData))] + public void TestComputedVersions(string targetFramework, Version computedVersion) + { + string id = $"DownlevelCheck_{targetFramework}_{GetRandomId()}"; + string projectContent = ProjectTemplate + .Replace("{TargetFramework}", targetFramework) + .Replace("{TestMessage}", FormatTestMessage("@(ResolvedFrameworkReference->'%(TargetingPackVersion)')")); + + InitPaths(id); + InitProjectDir(_projectDir); + File.WriteAllText(Path.Combine(_projectDir, $"{id}.csproj"), projectContent); + File.WriteAllText(Path.Combine(_projectDir, $"Program.cs"), "System.Console.WriteLine(\"Hello, World!\");"); // no-op + + (var result, _) = BuildProjectWithoutAssert(id, "Release", new( + AssertAppBundle: false, + CreateProject: false, + Publish: false, + TargetFramework: targetFramework + )); + + var versionExtractor = new Regex(FormatTestMessage("([0-9]+.[0-9]+.[0-9]+)")); + var match = versionExtractor.Match(result.Output); + Assert.True(match.Success, $"Expected to find a version in the output, but got: {result.Output}"); + + if (!Version.TryParse(match.Groups[1].Value, out var actualVersion)) + Assert.Fail($"Unable to parse version from regex match '{match.Groups[1].Value}'"); + + Assert.True(actualVersion >= computedVersion, $"Actually used version is lower then computed version in repository: (actually used version) '{actualVersion}' >= (computed version in repository) '{computedVersion}'"); + } + } +} diff --git a/src/mono/wasm/Wasm.Build.Tests/DownlevelVersions.cs.in b/src/mono/wasm/Wasm.Build.Tests/DownlevelVersions.cs.in new file mode 100644 index 00000000000000..63a187a362f684 --- /dev/null +++ b/src/mono/wasm/Wasm.Build.Tests/DownlevelVersions.cs.in @@ -0,0 +1,8 @@ +using System; + +public static class DownlevelVersions +{ + public static readonly Version PackageVersionNet6 = Version.Parse("${PackageVersionNet6}"); + public static readonly Version PackageVersionNet7 = Version.Parse("${PackageVersionNet7}"); + public static readonly Version PackageVersionNet8 = Version.Parse("${PackageVersionNet8}"); +} \ No newline at end of file diff --git a/src/mono/wasm/Wasm.Build.Tests/Wasm.Build.Tests.csproj b/src/mono/wasm/Wasm.Build.Tests/Wasm.Build.Tests.csproj index 62601dd54d85bc..628f16034ca536 100644 --- a/src/mono/wasm/Wasm.Build.Tests/Wasm.Build.Tests.csproj +++ b/src/mono/wasm/Wasm.Build.Tests/Wasm.Build.Tests.csproj @@ -46,6 +46,7 @@ + @@ -55,6 +56,24 @@ + + + <_DownlevelVersionsOutputPath>$(IntermediateOutputPath)\DownlevelVersions.cs + + + <_DownlevelVersionsProperty Include="PackageVersionNet6" Value="$(PackageVersionNet6)" /> + <_DownlevelVersionsProperty Include="PackageVersionNet7" Value="$(PackageVersionNet7)" /> + <_DownlevelVersionsProperty Include="PackageVersionNet8" Value="$(PackageVersionNet8)" /> + + + + + + +