Skip to content

[wasm] Stop hardcoding TFM in Wasm.Build.Tests #112955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

<Target Name="SetNugetConfigContent">
<GetNugetConfigTask
InputFile="$(MonoProjectRoot)wasm/Wasm.Build.Tests/data/nuget10.config"
InputFile="$(MonoProjectRoot)wasm\Wasm.Build.Tests\data\nuget.config"
ArtifactsDir="$(ArtifactsDir)"
Configuration="$(Configuration)">
<Output TaskParameter="NugetConfigContent" PropertyName="NugetConfigContent"/>
Expand Down
21 changes: 12 additions & 9 deletions src/mono/wasi/Wasi.Build.Tests/BuildTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Wasm.Build.Tests
{
public abstract class BuildTestBase : IClassFixture<SharedBuildPerTestClassFixture>, IDisposable
{
public const string DefaultTargetFramework = "net10.0";
public static readonly string DefaultTargetFramework = $"net{Environment.Version.Major}.0";
protected static readonly bool s_skipProjectCleanup;
protected static readonly string s_xharnessRunnerCommand;
protected string? _projectDir;
Expand Down Expand Up @@ -55,7 +55,7 @@ public abstract class BuildTestBase : IClassFixture<SharedBuildPerTestClassFixtu
public static bool IsUsingWorkloads => s_buildEnv.IsWorkload;
public static bool IsNotUsingWorkloads => !s_buildEnv.IsWorkload;
public static string GetNuGetConfigPathFor(string targetFramework) =>
Path.Combine(BuildEnvironment.TestDataPath, "nuget10.config");
Path.Combine(BuildEnvironment.TestDataPath, "nuget.config");

static BuildTestBase()
{
Expand Down Expand Up @@ -143,8 +143,9 @@ protected void InitPaths(string id, string? projectParentDir = null)
Directory.CreateDirectory(_logPath);
}

protected void InitProjectDir(string dir, bool addNuGetSourceForLocalPackages = false, string targetFramework = DefaultTargetFramework)
protected void InitProjectDir(string dir, bool addNuGetSourceForLocalPackages = false, string? targetFramework = null)
{
targetFramework ??= DefaultTargetFramework;
Directory.CreateDirectory(dir);
File.WriteAllText(Path.Combine(dir, "Directory.Build.props"), s_buildEnv.DirectoryBuildPropsContents);
File.WriteAllText(Path.Combine(dir, "Directory.Build.targets"), s_buildEnv.DirectoryBuildTargetsContents);
Expand All @@ -164,7 +165,7 @@ protected void InitProjectDir(string dir, bool addNuGetSourceForLocalPackages =
}
}

protected const string SimpleProjectTemplate =
protected static readonly string SimpleProjectTemplate =
@$"<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>{DefaultTargetFramework}</TargetFramework>
Expand All @@ -178,8 +179,9 @@ protected void InitProjectDir(string dir, bool addNuGetSourceForLocalPackages =
##INSERT_AT_END##
</Project>";

protected static BuildArgs ExpandBuildArgs(BuildArgs buildArgs, string extraProperties="", string extraItems="", string insertAtEnd="", string projectTemplate=SimpleProjectTemplate)
protected static BuildArgs ExpandBuildArgs(BuildArgs buildArgs, string extraProperties="", string extraItems="", string insertAtEnd="")
{
string projectTemplate = SimpleProjectTemplate;
if (buildArgs.AOT)
{
extraProperties = $"{extraProperties}\n<RunAOTCompilation>true</RunAOTCompilation>";
Expand Down Expand Up @@ -217,8 +219,7 @@ protected static BuildArgs ExpandBuildArgs(BuildArgs buildArgs, string extraProp
options.InitProject?.Invoke();

File.WriteAllText(Path.Combine(_projectDir, $"{buildArgs.ProjectName}.csproj"), buildArgs.ProjectFileContents);
File.Copy(Path.Combine(AppContext.BaseDirectory,
options.TargetFramework == "net7.0" ? "data/test-main-7.0.js" : "test-main.js"),
File.Copy(Path.Combine(AppContext.BaseDirectory, "test-main.js"),
Path.Combine(_projectDir, "test-main.js"));
}
else if (_projectDir is null)
Expand Down Expand Up @@ -461,15 +462,17 @@ private string FindSubDirIgnoringCase(string parentDir, string dirName)
return first ?? Path.Combine(parentDir, dirName);
}

protected string GetBinDir(string config, string targetFramework=DefaultTargetFramework, string? baseDir=null)
protected string GetBinDir(string config, string? targetFramework = null, string? baseDir=null)
{
targetFramework ??= DefaultTargetFramework;
var dir = baseDir ?? _projectDir;
Assert.NotNull(dir);
return Path.Combine(dir!, "bin", config, targetFramework, BuildEnvironment.DefaultRuntimeIdentifier);
}

protected string GetObjDir(string config, string targetFramework=DefaultTargetFramework, string? baseDir=null)
protected string GetObjDir(string config, string? targetFramework = null, string? baseDir=null)
{
targetFramework ??= DefaultTargetFramework;
var dir = baseDir ?? _projectDir;
Assert.NotNull(dir);
return Path.Combine(dir!, "obj", config, targetFramework, BuildEnvironment.DefaultRuntimeIdentifier);
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasi/Wasi.Build.Tests/WasiLibraryModeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void LibraryModeBuild(string sdk, bool hasWasmAppBundle)
$"""
<Project Sdk="{sdk}">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<TargetFramework>{DefaultTargetFramework}</TargetFramework>
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
<WasmSingleFileBundle>true</WasmSingleFileBundle>
<OutputType>Library</OutputType>
Expand Down
7 changes: 4 additions & 3 deletions src/mono/wasm/Wasm.Build.Tests/Blazor/BlazorWasmTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ protected override void OnAfterRender(bool firstRender)
protected void UpdateHomePage() =>
UpdateFile(Path.Combine("Pages", "Home.razor"), blazorHomePageReplacements);

public void InitBlazorWasmProjectDir(string id, string targetFramework = DefaultTargetFrameworkForBlazor)
public void InitBlazorWasmProjectDir(string id, string? targetFramework = null)
{
targetFramework ??= DefaultTargetFrameworkForBlazor;
InitPaths(id);
if (Directory.Exists(_projectDir))
Directory.Delete(_projectDir, recursive: true);
Expand Down Expand Up @@ -220,6 +221,6 @@ public override async Task<RunResult> RunForPublishWithWebServer(RunOptions runO
return serverEnvironment;
}

public string GetBlazorBinFrameworkDir(Configuration config, bool forPublish, string framework = DefaultTargetFrameworkForBlazor, string? projectDir = null)
=> _provider.GetBinFrameworkDir(config: config, forPublish: forPublish, framework: framework, projectDir: projectDir);
public string GetBlazorBinFrameworkDir(Configuration config, bool forPublish, string? framework = null, string? projectDir = null)
=> _provider.GetBinFrameworkDir(config: config, forPublish: forPublish, framework: framework ?? DefaultTargetFrameworkForBlazor, projectDir: projectDir);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public BuildOptions(
bool IsPublish = false,
bool AOT = false,
NativeFilesType ExpectedFileType = NativeFilesType.FromRuntimePack,
string TargetFramework = BuildTestBase.DefaultTargetFramework,
string? TargetFramework = null,
GlobalizationMode GlobalizationMode = GlobalizationMode.Sharded,
string CustomIcuFile = "",
bool UseCache = true,
Expand All @@ -29,9 +29,9 @@ public BuildOptions(
bool FeaturePerfTracing = false
) : base(
IsPublish,
TargetFramework ?? BuildTestBase.DefaultTargetFramework,
AOT,
ExpectedFileType,
TargetFramework,
GlobalizationMode,
CustomIcuFile,
UseCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace Wasm.Build.Tests;
public abstract record MSBuildOptions
(
bool IsPublish,
string TargetFramework,
bool AOT = false,
NativeFilesType ExpectedFileType = NativeFilesType.FromRuntimePack,
string TargetFramework = BuildTestBase.DefaultTargetFramework,
GlobalizationMode GlobalizationMode = GlobalizationMode.Sharded,
string CustomIcuFile = "",
bool UseCache = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public PublishOptions(
bool IsPublish = true,
bool AOT = false,
NativeFilesType ExpectedFileType = NativeFilesType.FromRuntimePack,
string TargetFramework = BuildTestBase.DefaultTargetFramework,
string? TargetFramework = null,
GlobalizationMode GlobalizationMode = GlobalizationMode.Sharded,
string CustomIcuFile = "",
bool UseCache = true,
Expand All @@ -34,9 +34,9 @@ public PublishOptions(
bool FeaturePerfTracing = false
) : base(
IsPublish,
TargetFramework ?? BuildTestBase.DefaultTargetFramework,
AOT,
ExpectedFileType,
TargetFramework,
GlobalizationMode,
CustomIcuFile,
UseCache,
Expand Down
13 changes: 8 additions & 5 deletions src/mono/wasm/Wasm.Build.Tests/BuildTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ namespace Wasm.Build.Tests
{
public abstract class BuildTestBase : IClassFixture<SharedBuildPerTestClassFixture>, IDisposable
{
public const string DefaultTargetFramework = "net10.0";
public const string DefaultTargetFrameworkForBlazor = "net10.0";
public const string TargetFrameworkForTasks = "net10.0";
public static readonly string DefaultTargetFramework = $"net{Environment.Version.Major}.0";
public static readonly string PreviousTargetFramework = $"net{Environment.Version.Major - 1}.0";
public static readonly string Previous2TargetFramework = $"net{Environment.Version.Major - 2}.0";
public static readonly string DefaultTargetFrameworkForBlazor = $"net{Environment.Version.Major}.0";
public static readonly string TargetFrameworkForTasks = $"net{Environment.Version.Major}.0";
private const string DefaultEnvironmentLocale = "en-US";
protected static readonly string s_unicodeChars = "\u9FC0\u8712\u679B\u906B\u486B\u7149";
protected static readonly bool s_skipProjectCleanup;
Expand Down Expand Up @@ -62,7 +64,7 @@ public abstract class BuildTestBase : IClassFixture<SharedBuildPerTestClassFixtu
public static bool IsWorkloadWithMultiThreadingForDefaultFramework => s_buildEnv.IsWorkloadWithMultiThreadingForDefaultFramework;
public static bool UseWebcil => s_buildEnv.UseWebcil;
public static string GetNuGetConfigPathFor(string targetFramework)
=> Path.Combine(BuildEnvironment.TestDataPath, targetFramework == "net10.0" ? "nuget10.config" : "nuget9.config");
=> Path.Combine(BuildEnvironment.TestDataPath, "nuget.config");

public TProvider GetProvider<TProvider>() where TProvider : ProjectProviderBase
=> (TProvider)_providerOfBaseType;
Expand Down Expand Up @@ -230,8 +232,9 @@ private string GetBinlogMessageContext(TextNode node)
return (_logPath, _nugetPackagesDir);
}

protected void InitProjectDir(string dir, bool addNuGetSourceForLocalPackages = true, string targetFramework = DefaultTargetFramework)
protected void InitProjectDir(string dir, bool addNuGetSourceForLocalPackages = true, string? targetFramework = null)
{
targetFramework ??= DefaultTargetFramework;
if (Directory.Exists(dir))
Directory.Delete(dir, recursive: true);
Directory.CreateDirectory(dir);
Expand Down
6 changes: 3 additions & 3 deletions src/mono/wasm/Wasm.Build.Tests/Common/BuildEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,18 @@ public BuildEnvironment()
Directory.CreateDirectory(TmpPath);
}

public string GetRuntimePackVersion(string tfm = BuildTestBase.DefaultTargetFramework)
public string GetRuntimePackVersion(string tfm)
=> s_runtimePackVersions.TryGetValue(tfm, out string? version)
? version
: throw new ArgumentException($"No runtime pack version found for tfm={tfm} .");

public string GetRuntimePackDir(string tfm = BuildTestBase.DefaultTargetFramework, RuntimeVariant runtimeType = RuntimeVariant.SingleThreaded)
public string GetRuntimePackDir(string tfm, RuntimeVariant runtimeType = RuntimeVariant.SingleThreaded)
=> Path.Combine(WorkloadPacksDir,
runtimeType is RuntimeVariant.SingleThreaded
? $"Microsoft.NETCore.App.Runtime.Mono.{DefaultRuntimeIdentifier}"
: $"Microsoft.NETCore.App.Runtime.Mono.multithread.{DefaultRuntimeIdentifier}",
GetRuntimePackVersion(tfm));
public string GetRuntimeNativeDir(string tfm = BuildTestBase.DefaultTargetFramework, RuntimeVariant runtimeType = RuntimeVariant.SingleThreaded)
public string GetRuntimeNativeDir(string tfm, RuntimeVariant runtimeType = RuntimeVariant.SingleThreaded)
=> Path.Combine(GetRuntimePackDir(tfm, runtimeType), "runtimes", DefaultRuntimeIdentifier, "native");
public bool IsMultiThreadingRuntimePackAvailableFor(string tfm)
=> IsWorkload && File.Exists(Path.Combine(GetRuntimeNativeDir(tfm, RuntimeVariant.MultiThreaded), "dotnet.native.worker.mjs"));
Expand Down
12 changes: 5 additions & 7 deletions src/mono/wasm/Wasm.Build.Tests/NonWasmTemplateBuildTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -23,10 +24,10 @@ public NonWasmTemplateBuildTests(ITestOutputHelper output, SharedBuildPerTestCla
// So, copy the reference for latest TFM, and add that back with the
// TFM=DefaultTargetFramework
//
// This is useful for the case when we are on tfm=net10.0, but sdk, and packages
// are really 10.0 .
private const string s_latestTargetFramework = "net10.0";
private const string s_previousTargetFramework = "net9.0";
// This is useful for the case when we are on latest TFM, but sdk, and packages
// are really the previous version .
private static readonly string s_latestTargetFramework = $"net{Environment.Version.Major}.0";
private static readonly string s_previousTargetFramework = $"net{Environment.Version.Major - 1}.0";
private static string s_directoryBuildTargetsForPreviousTFM =
$$"""
<Project>
Expand Down Expand Up @@ -69,7 +70,6 @@ public NonWasmTemplateBuildTests(ITestOutputHelper output, SharedBuildPerTestCla
(
EnvironmentVariables.WorkloadsTestPreviousVersions
? [
"net6.0",
s_previousTargetFramework,
s_latestTargetFramework
]
Expand All @@ -83,7 +83,6 @@ public void NonWasmConsoleBuild_WithoutWorkload(Configuration config, string ext
=> NonWasmConsoleBuild(config,
extraBuildArgs,
targetFramework,
// net6 is sdk would be needed to run the app
shouldRun: targetFramework == s_latestTargetFramework);

[Theory]
Expand All @@ -92,7 +91,6 @@ public void NonWasmConsoleBuild_WithWorkload(Configuration config, string extraB
=> NonWasmConsoleBuild(config,
extraBuildArgs,
targetFramework,
// net6 is sdk would be needed to run the app
shouldRun: targetFramework == s_latestTargetFramework);

private void NonWasmConsoleBuild(Configuration config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ private async Task BrowserRunTwiceWithAndThenWithoutBuildAsync(Configuration con
public static IEnumerable<object?[]> BrowserBuildAndRunTestData()
{
yield return new object?[] { "", BuildTestBase.DefaultTargetFramework, DefaultRuntimeAssetsRelativePath };
yield return new object?[] { "-f net10.0", "net10.0", DefaultRuntimeAssetsRelativePath };
yield return new object?[] { $"-f {DefaultTargetFramework}", DefaultTargetFramework, DefaultRuntimeAssetsRelativePath };

if (EnvironmentVariables.WorkloadsTestPreviousVersions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little bit worried that we'll forget updating the earlier versions here.
Should we introduce a int MinimumTargetFramwork = 8 in BuildTestBase.cs and then here return all TFMs from MinimumTargetFramwork => DefaultTargetFramework?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are two old versions at max and in other places net7 and net6 were used. i've removed them and added Previous2 property. when it's time for net 11, it will be testing one STS version which may not be ideal in second half of the year (when it goes out of support) but won't break anything if nuget.config keeps dotnet9 for a while.

{
yield return new object?[] { "-f net9.0", "net9.0", DefaultRuntimeAssetsRelativePath };
yield return new object?[] { "-f net8.0", "net8.0", DefaultRuntimeAssetsRelativePath };
yield return new object?[] { $"-f {PreviousTargetFramework}", PreviousTargetFramework, DefaultRuntimeAssetsRelativePath };
yield return new object?[] { $"-f {Previous2TargetFramework}", Previous2TargetFramework, DefaultRuntimeAssetsRelativePath };
}

// ActiveIssue("https://github.com/dotnet/runtime/issues/90979")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,9 @@ protected void DeleteFile(string pathRelativeToProjectDir)
}
}

protected void UpdateBrowserMainJs(string targetFramework = DefaultTargetFramework, string runtimeAssetsRelativePath = DefaultRuntimeAssetsRelativePath)
protected void UpdateBrowserMainJs(string? targetFramework = null, string runtimeAssetsRelativePath = DefaultRuntimeAssetsRelativePath)
{
targetFramework ??= DefaultTargetFramework;
string mainJsPath = Path.Combine(_projectDir, "wwwroot", "main.js");
string mainJsContent = File.ReadAllText(mainJsPath);
Version targetFrameworkVersion = new Version(targetFramework.Replace("net", ""));
Expand Down Expand Up @@ -366,8 +367,8 @@ void OnErrorMessage(string msg)
}
}

public string GetBinFrameworkDir(Configuration config, bool forPublish, string framework = DefaultTargetFramework, string? projectDir = null) =>
_provider.GetBinFrameworkDir(config, forPublish, framework, projectDir);
public string GetBinFrameworkDir(Configuration config, bool forPublish, string? framework = null, string? projectDir = null) =>
_provider.GetBinFrameworkDir(config, forPublish, framework ?? DefaultTargetFramework, projectDir);

public BuildPaths GetBuildPaths(Configuration config, bool forPublish) =>
_provider.GetBuildPaths(config, forPublish);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<packageSources>
<clear />
<!-- TEST_RESTORE_SOURCES_INSERTION_LINE -->
<add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
<add key="dotnet9" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet9/nuget/v3/index.json" />
<add key="dotnet10" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet10/nuget/v3/index.json" />
<add key="dotnet-public" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json" />
Expand Down
17 changes: 0 additions & 17 deletions src/mono/wasm/Wasm.Build.Tests/data/nuget9.config

This file was deleted.

Loading
Loading