Skip to content

Commit

Permalink
[wasm] Wasm.Build.Tests - build projects for net8.0 by default (#79254)
Browse files Browse the repository at this point in the history
* [wasm] Wasm.Build.Tests - build projects for net8.0 by default
* [wasm] Fix templates to use the correct tfm
* InstallWorkloadFromArtifacts: Share the nuget cache for installing all the workload combinations
* WBT: fix re-runs
* WBT: fix non-wasm console project tests
* Fix tfm used for blazor projects
* WBT: Update to use 7.0.0 version of the workload
* WBT: Update tests to remove workarounds for 7.0 that aren't needed anymore
* Fix BrowserBuildAndRun for 7.0
* WBT: Run a blazor template referencing a native lib
* WBT: Update SkiaSharp reference
* WBT: Use warnAsError for blazor builds
* WBT: Disable tests failing to run 8.0-sdk+tfm=net7 - #79313
  • Loading branch information
radical committed Dec 9, 2022
1 parent add75d0 commit 1857c91
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 89 deletions.
3 changes: 1 addition & 2 deletions eng/Versions.props
Expand Up @@ -8,8 +8,7 @@
<PatchVersion>0</PatchVersion>
<SdkBandVersion>8.0.100</SdkBandVersion>
<PackageVersionNet6>6.0.10</PackageVersionNet6>
<!-- problematic until NetCoreAppCurrent is net8.0 <PackageVersionNet7>7.0.0</PackageVersionNet7> -->
<PackageVersionForTemplates7>7.0.0-rtm.22476.8</PackageVersionForTemplates7>
<PackageVersionNet7>7.0.0</PackageVersionNet7>
<PreReleaseVersionLabel>alpha</PreReleaseVersionLabel>
<PreReleaseVersionIteration>1</PreReleaseVersionIteration>
<!-- Set assembly version to align with major and minor version,
Expand Down
6 changes: 3 additions & 3 deletions eng/testing/workloads-testing.targets
Expand Up @@ -8,7 +8,7 @@
<PropertyGroup Condition="'$(InstallWorkloadForTesting)' == 'true'">
<_SdkForWorkloadTestingBasePath>$(ArtifactsBinDir)</_SdkForWorkloadTestingBasePath>
<_SdkWithNoWorkloadPath>$([MSBuild]::NormalizeDirectory($(_SdkForWorkloadTestingBasePath), 'dotnet-none'))</_SdkWithNoWorkloadPath>
<_SdkWithNoWorkloadStampPath>$([MSBuild]::NormalizePath($(_SdkWithNoWorkloadPath), '.version-$(SdkVersionForWorkloadTesting).stamp'))</_SdkWithNoWorkloadStampPath>
<_SdkWithNoWorkloadStampPath>$([MSBuild]::NormalizePath($(_SdkWithNoWorkloadPath), '.version-for-none-$(SdkVersionForWorkloadTesting).stamp'))</_SdkWithNoWorkloadStampPath>
<InstallWorkloadUsingArtifactsDependsOn>
$(InstallWorkloadUsingArtifactsDependsOn);
_SetPackageVersionForWorkloadsTesting;
Expand Down Expand Up @@ -96,7 +96,7 @@
<_SdkWithWorkloadToInstall Include="@(WorkloadCombinationsToInstall)" />
<_SdkWithWorkloadToInstall InstallPath="$(_SdkForWorkloadTestingBasePath)\dotnet-%(Identity)" />
<!--<_SdkWithWorkloadToInstall StampPath="%(InstallPath)\version-%(Version).stamp" Condition="'%(Version)' != ''" />-->
<_SdkWithWorkloadToInstall StampPath="%(InstallPath)\version.stamp" />
<_SdkWithWorkloadToInstall StampPath="%(InstallPath)\.workload-installed.stamp" />
</ItemGroup>

<Error Text="Cannot find any nupkg files in $(LibrariesShippingPackagesDir), needed for workloads" Condition="@(AvailableNuGetsInArtifacts->Count()) == 0" />
Expand Down Expand Up @@ -224,6 +224,6 @@
SdkWithNoWorkloadInstalledPath="$(_SdkWithNoWorkloadPath)"
/>

<WriteLinesToFile Lines="" File="$(_SdkWithNoWorkloadStampPath)" />
<Touch Files="$(_SdkWithNoWorkloadStampPath)" AlwaysCreate="true" />
</Target>
</Project>
2 changes: 0 additions & 2 deletions src/libraries/Directory.Build.props
Expand Up @@ -136,8 +136,6 @@
<SdkWithNoWorkloadStampPath>$(SdkWithNoWorkloadForTestingPath)version-$(SdkVersionForWorkloadTesting).stamp</SdkWithNoWorkloadStampPath>
<SdkWithNoWorkload_WorkloadStampPath>$(SdkWithNoWorkloadForTestingPath)workload.stamp</SdkWithNoWorkload_WorkloadStampPath>

<!-- FIXME: Using net7 workload here, since we are building the test projects with net7.0
right now. -->
<SdkWithWorkloadForTestingPath>$(ArtifactsBinDir)dotnet-net7+latest\</SdkWithWorkloadForTestingPath>
<SdkWithWorkloadForTestingPath>$([MSBuild]::NormalizeDirectory($(SdkWithWorkloadForTestingPath)))</SdkWithWorkloadForTestingPath>

Expand Down
Expand Up @@ -38,11 +38,6 @@
<_WorkloadManifestValues Include="WorkloadVersion" Value="$(PackageVersion)" />
<_WorkloadManifestValues Include="PackageVersionNet7" Value="$(PackageVersionNet7)" Condition="'$(PackageVersionNet7)' != ''" />
<_WorkloadManifestValues Include="PackageVersionNet7" Value="$(PackageVersion)" Condition="'$(PackageVersionNet7)' == ''" />

<!-- We need to use a different version for net7 templates, to differentiate from net8 ones -->
<_WorkloadManifestValues Include="PackageVersionForTemplates7" Value="$(PackageVersionForTemplates7)" Condition="'$(PackageVersionNet7)' == ''" />
<_WorkloadManifestValues Include="PackageVersionForTemplates7" Value="$(PackageVersionNet7)" Condition="'$(PackageVersionNet7)' != ''" />

<_WorkloadManifestValues Include="EmscriptenVersion" Value="$(MicrosoftNETRuntimeEmscriptenVersion)" />
<_WorkloadManifestValues Include="NetCoreAppCurrent" Value="$(NetCoreAppCurrent)" />
</ItemGroup>
Expand Down
Expand Up @@ -170,7 +170,7 @@
},
"Microsoft.NET.Runtime.WebAssembly.Templates.net7": {
"kind": "template",
"version": "${PackageVersionForTemplates7}",
"version": "${PackageVersionNet7}",
"alias-to": {
"any": "Microsoft.NET.Runtime.WebAssembly.Templates"
}
Expand Down
16 changes: 14 additions & 2 deletions src/mono/wasm/Wasm.Build.Tests/Blazor/BuildPublishTests.cs
Expand Up @@ -5,9 +5,11 @@
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
using Microsoft.Playwright;

#nullable enable

Expand Down Expand Up @@ -82,7 +84,7 @@ public void DefaultTemplate_NoAOT_WithWorkload(string config)
[Theory]
[InlineData("Debug")]
[InlineData("Release")]
public void WithDllImportInMainAssembly(string config)
public async Task WithDllImportInMainAssembly(string config)
{
// Based on https://github.com/dotnet/runtime/issues/59255
string id = $"blz_dllimp_{config}";
Expand Down Expand Up @@ -111,18 +113,28 @@ public static class MyDllImports
File.WriteAllText(Path.Combine(_projectDir!, "Pages", "MyDllImport.cs"), myDllImportCs);

AddItemsPropertiesToProject(projectFile, extraItems: @"<NativeFileReference Include=""mylib.cpp"" />");
BlazorAddRazorButton("cpp_add", """
var result = MyDllImports.cpp_add(10, 12);
outputText = $"{result}";
""");

BlazorBuild(new BlazorBuildOptions(id, config, NativeFilesType.Relinked));
CheckNativeFileLinked(forPublish: false);

BlazorPublish(new BlazorBuildOptions(id, config, NativeFilesType.Relinked));
CheckNativeFileLinked(forPublish: true);

await BlazorRun(config, async (page) => {
await page.Locator("text=\"cpp_add\"").ClickAsync();
var txt = await page.Locator("p[role='test']").InnerHTMLAsync();
Assert.Equal("Output: 22", txt);
});

void CheckNativeFileLinked(bool forPublish)
{
// very crude way to check that the native file was linked in
// needed because we don't run the blazor app yet
string objBuildDir = Path.Combine(_projectDir!, "obj", config, DefaultTargetFramework, "wasm", forPublish ? "for-publish" : "for-build");
string objBuildDir = Path.Combine(_projectDir!, "obj", config, DefaultTargetFrameworkForBlazor, "wasm", forPublish ? "for-publish" : "for-build");
string pinvokeTableHPath = Path.Combine(objBuildDir, "pinvoke-table.h");
Assert.True(File.Exists(pinvokeTableHPath), $"Could not find {pinvokeTableHPath}");

Expand Down
66 changes: 59 additions & 7 deletions src/mono/wasm/Wasm.Build.Tests/BuildTestBase.cs
Expand Up @@ -18,6 +18,7 @@
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
using Microsoft.Playwright;

#nullable enable

Expand All @@ -27,7 +28,7 @@ namespace Wasm.Build.Tests
{
public abstract class BuildTestBase : IClassFixture<SharedBuildPerTestClassFixture>, IDisposable
{
public const string DefaultTargetFramework = "net7.0";
public const string DefaultTargetFramework = "net8.0";
public const string DefaultTargetFrameworkForBlazor = "net7.0";
protected static readonly bool s_skipProjectCleanup;
protected static readonly string s_xharnessRunnerCommand;
Expand Down Expand Up @@ -503,6 +504,9 @@ public string CreateBlazorWasmTemplateProject(string id)

protected (CommandResult, string) BlazorBuild(BlazorBuildOptions options, params string[] extraArgs)
{
if (options.WarnAsError)
extraArgs = extraArgs.Append("/warnaserror").ToArray();

var res = BuildInternal(options.Id, options.Config, publish: false, setWasmDevel: false, extraArgs);
_testOutput.WriteLine($"BlazorBuild, options.tfm: {options.TargetFramework}");
AssertDotNetNativeFiles(options.ExpectedFileType, options.Config, forPublish: false, targetFramework: options.TargetFramework);
Expand Down Expand Up @@ -671,7 +675,8 @@ protected static void AssertFilesDontExist(string dir, string[] filenames, strin
protected static void AssertFilesExist(string dir, string[] filenames, string? label = null, bool expectToExist=true)
{
string prefix = label != null ? $"{label}: " : string.Empty;
Assert.True(Directory.Exists(dir), $"[{label}] {dir} not found");
if (!Directory.Exists(dir))
throw new XunitException($"[{label}] {dir} not found");
foreach (string filename in filenames)
{
string path = Path.Combine(dir, filename);
Expand Down Expand Up @@ -791,10 +796,7 @@ protected string CreateProjectWithNativeReference(string id)
CreateBlazorWasmTemplateProject(id);

string extraItems = @$"
<PackageReference Include=""SkiaSharp"" Version=""2.88.1-preview.63"" />
<PackageReference Include=""SkiaSharp.NativeAssets.WebAssembly"" Version=""2.88.1-preview.63"" />
<NativeFileReference Include=""$(SkiaSharpStaticLibraryPath)\3.1.7\*.a"" />
{GetSkiaSharpReferenceItems()}
<WasmFilesToIncludeInFileSystem Include=""{Path.Combine(BuildEnvironment.TestAssetsPath, "mono.png")}"" />
";
string projectFile = Path.Combine(_projectDir!, $"{id}.csproj");
Expand All @@ -803,6 +805,50 @@ protected string CreateProjectWithNativeReference(string id)
return projectFile;
}

public void BlazorAddRazorButton(string buttonText, string customCode, string methodName="test", string razorPage="Pages/Counter.razor")
{
string additionalCode = $$"""
<p role="{{methodName}}">Output: @outputText</p>
<button class="btn btn-primary" @onclick="{{methodName}}">{{buttonText}}</button>

@code {
private string outputText = string.Empty;
public void {{methodName}}()
{
{{customCode}}
}
}
""";

// find blazor's Counter.razor
string counterRazorPath = Path.Combine(_projectDir!, razorPage);
if (!File.Exists(counterRazorPath))
throw new FileNotFoundException($"Could not find {counterRazorPath}");

string oldContent = File.ReadAllText(counterRazorPath);
File.WriteAllText(counterRazorPath, oldContent + additionalCode);
}

public async Task BlazorRun(string config, Func<IPage, Task>? test=null, string extraArgs="--no-build")
{
using var runCommand = new RunCommand(s_buildEnv, _testOutput)
.WithWorkingDirectory(_projectDir!);

await using var runner = new BrowserRunner(_testOutput);
var page = await runner.RunAsync(runCommand, $"run -c {config} {extraArgs}");

await page.Locator("text=Counter").ClickAsync();
var txt = await page.Locator("p[role='status']").InnerHTMLAsync();
Assert.Equal("Current count: 0", txt);

await page.Locator("text=\"Click me\"").ClickAsync();
txt = await page.Locator("p[role='status']").InnerHTMLAsync();
Assert.Equal("Current count: 1", txt);

if (test is not null)
await test(page);
}

public static (int exitCode, string buildOutput) RunProcess(string path,
ITestOutputHelper _testOutput,
string args = "",
Expand Down Expand Up @@ -1005,6 +1051,11 @@ internal BuildPaths GetBuildPaths(BuildArgs buildArgs, bool forPublish=true)
return table;
}

protected static string GetSkiaSharpReferenceItems()
=> @"<PackageReference Include=""SkiaSharp"" Version=""2.88.3"" />
<PackageReference Include=""SkiaSharp.NativeAssets.WebAssembly"" Version=""2.88.3"" />
<NativeFileReference Include=""$(SkiaSharpStaticLibraryPath)\3.1.12\st\*.a"" />";

protected static string s_mainReturns42 = @"
public class TestClass {
public static int Main()
Expand Down Expand Up @@ -1047,7 +1098,8 @@ public record BlazorBuildOptions
string Id,
string Config,
NativeFilesType ExpectedFileType,
string TargetFramework = BuildTestBase.DefaultTargetFrameworkForBlazor
string TargetFramework = BuildTestBase.DefaultTargetFrameworkForBlazor,
bool WarnAsError = true
);

public enum NativeFilesType { FromRuntimePack, Relinked, AOT };
Expand Down
6 changes: 1 addition & 5 deletions src/mono/wasm/Wasm.Build.Tests/NativeLibraryTests.cs
Expand Up @@ -59,11 +59,7 @@ public void ProjectUsingSkiaSharp(BuildArgs buildArgs, RunHost host, string id)
// FIXME: temporary, till `main` is either completely on 3.1.7, or 3.1.12
extraProperties: "<EmccExtraLDFlags>-s ERROR_ON_UNDEFINED_SYMBOLS=0</EmccExtraLDFlags>",
extraItems: @$"
<PackageReference Include=""SkiaSharp"" Version=""2.88.1-preview.63"" />
<PackageReference Include=""SkiaSharp.NativeAssets.WebAssembly"" Version=""2.88.1-preview.63"" />
<NativeFileReference Include=""$(SkiaSharpStaticLibraryPath)\3.1.7\*.a"" />
{GetSkiaSharpReferenceItems()}
<WasmFilesToIncludeInFileSystem Include=""{Path.Combine(BuildEnvironment.TestAssetsPath, "mono.png")}"" />
");

Expand Down
14 changes: 7 additions & 7 deletions src/mono/wasm/Wasm.Build.Tests/NonWasmTemplateBuildTests.cs
Expand Up @@ -30,20 +30,20 @@ public NonWasmTemplateBuildTests(ITestOutputHelper output, SharedBuildPerTestCla
private static string s_directoryBuildTargetsForPreviousTFM =
$$"""
<Project>
<Target Name="_FixupVersions" BeforeTargets="ProcessFrameworkReferences">
<Target Name="_FixupVersions" BeforeTargets="ProcessFrameworkReferences" Condition="'{{s_latestTargetFramework}}' != '{{DefaultTargetFramework}}'">
<ItemGroup>
<!-- Get net8.0 entry -->
<!-- Get {{s_latestTargetFramework}} entry -->
<_KnownFrameworkReferenceToCopyFrom
Include="@(KnownFrameworkReference)"
Condition="'%(Identity)' == 'Microsoft.NETCore.App' and '%(TargetFramework)' == '{{s_latestTargetFramework}}'" />
<!-- patch it's TFM=net7.0 -->
<!-- patch it's TFM={{DefaultTargetFramework}} -->
<_KnownFrameworkReferenceToCopyFrom Update="@(_KnownFrameworkReferenceToCopyFrom)" TargetFramework="{{DefaultTargetFramework}}" />

<!-- remove the existing net7.0 entry -->
<!-- remove the existing {{DefaultTargetFramework}} entry -->
<KnownFrameworkReference
Remove="@(KnownFrameworkReference)"
Condition="'%(Identity)' == 'Microsoft.NETCore.App' and '%(TargetFramework)' == '{{DefaultTargetFramework}}'" />
<!-- add the new patched up net7.0 entry -->
<!-- add the new patched up {{DefaultTargetFramework}} entry -->
<KnownFrameworkReference Include="@(_KnownFrameworkReferenceToCopyFrom)" />
</ItemGroup>
</Target>
Expand Down Expand Up @@ -80,7 +80,7 @@ public void NonWasmConsoleBuild_WithoutWorkload(string config, string extraBuild
extraBuildArgs,
targetFramework,
// net6 is sdk would be needed to run the app
shouldRun: targetFramework != "net6.0");
shouldRun: targetFramework == s_latestTargetFramework);


[Theory]
Expand All @@ -90,7 +90,7 @@ public void NonWasmConsoleBuild_WithWorkload(string config, string extraBuildArg
extraBuildArgs,
targetFramework,
// net6 is sdk would be needed to run the app
shouldRun: targetFramework != "net6.0");
shouldRun: targetFramework == s_latestTargetFramework);

private void NonWasmConsoleBuild(string config,
string extraBuildArgs,
Expand Down

0 comments on commit 1857c91

Please sign in to comment.