Skip to content

Commit

Permalink
Add Integration Test for WASM (#2532)
Browse files Browse the repository at this point in the history
* Write failing wasm test

* Use local dotnet installation to install workload

* Add v8 to path

* Add wasm-tools workload installation to build project, thanks @ilonatommy

---------

Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
Co-authored-by: Tim Cassell <35501420+timcassell@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 5, 2024
1 parent f17d40e commit 4435799
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ jobs:
platform: x64
- name: Set up zlib-static
run: sudo apt-get install -y libkrb5-dev
- name: Set up node
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Set up v8
run: npm install jsvu -g && jsvu --os=linux64 --engines=v8 && echo "$HOME/.jsvu/bin" >> $GITHUB_PATH
- name: Install wasm-tools workload
run: ./build.cmd install-wasm-tools
- name: Run task 'build'
run: ./build.cmd build
- name: Run task 'unit-tests'
Expand All @@ -79,6 +87,14 @@ jobs:
runs-on: macos-13
steps:
- uses: actions/checkout@v3
- name: Set up node
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Set up v8
run: npm install jsvu -g && jsvu --os=mac64 --engines=v8 && echo "$HOME/.jsvu/bin" >> $GITHUB_PATH
- name: Install wasm-tools workload
run: ./build.cmd install-wasm-tools
- name: Run task 'build'
run: ./build.cmd build
- name: Run task 'unit-tests'
Expand Down
20 changes: 20 additions & 0 deletions build/BenchmarkDotNet.Build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ public HelpInfo GetHelp()
}
}

[TaskName(Name)]
[TaskDescription("Install wasm-tools workload")]
public class InstallWasmToolsWorkload : FrostingTask<BuildContext>, IHelpProvider
{
private const string Name = "install-wasm-tools";

public override void Run(BuildContext context) => context.BuildRunner.InstallWorkload("wasm-tools");

public HelpInfo GetHelp()
{
return new HelpInfo
{
Examples = new[]
{
new Example(Name)
}
};
}
}

[TaskName(Name)]
[TaskDescription("Run unit tests (fast)")]
[IsDependentOn(typeof(BuildTask))]
Expand Down
11 changes: 11 additions & 0 deletions build/BenchmarkDotNet.Build/Runners/BuildRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Cake.Common.Tools.DotNet.Build;
using Cake.Common.Tools.DotNet.Pack;
using Cake.Common.Tools.DotNet.Restore;
using Cake.Common.Tools.DotNet.Workload.Install;
using Cake.Core;
using Cake.Core.IO;

Expand All @@ -28,6 +29,16 @@ public void Restore()
});
}

public void InstallWorkload(string workloadId)
{
context.DotNetWorkloadInstall(workloadId,
new DotNetWorkloadInstallSettings
{
IncludePreviews = true,
NoCache = true
});
}

public void Build()
{
context.Information("BuildSystemProvider: " + context.BuildSystem().Provider);
Expand Down
9 changes: 9 additions & 0 deletions tests/BenchmarkDotNet.IntegrationTests/AppBundle/test-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { dotnet } from './_framework/dotnet.js'

await dotnet
.withDiagnosticTracing(false)
.withApplicationArguments(...arguments)
.run()
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="AppBundle\**" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
Expand Down
51 changes: 51 additions & 0 deletions tests/BenchmarkDotNet.IntegrationTests/WasmTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.IO;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Tests.Loggers;
using BenchmarkDotNet.Tests.XUnit;
using BenchmarkDotNet.Toolchains.DotNetCli;
using BenchmarkDotNet.Toolchains.MonoWasm;
using Xunit.Abstractions;

namespace BenchmarkDotNet.IntegrationTests
{
public class WasmTests : BenchmarkTestExecutor
{
public WasmTests(ITestOutputHelper output) : base(output) { }

[FactEnvSpecific("WASM is only supported on Unix", EnvRequirement.NonWindows)]
public void WasmIsSupported()
{
var dotnetVersion = "net8.0";
var logger = new OutputLogger(Output);
var netCoreAppSettings = new NetCoreAppSettings(dotnetVersion, null, "Wasm");
var mainJsPath = Path.Combine(AppContext.BaseDirectory, "AppBundle", "test-main.js");

var config = ManualConfig.CreateEmpty()
.AddLogger(logger)
.AddJob(Job.Dry
.WithArguments([new MsBuildArgument($"/p:WasmMainJSPath={mainJsPath}")])
.WithRuntime(new WasmRuntime(dotnetVersion, moniker: RuntimeMoniker.WasmNet80, javaScriptEngineArguments: "--expose_wasm --module"))
.WithToolchain(WasmToolchain.From(netCoreAppSettings)))
.WithOption(ConfigOptions.GenerateMSBuildBinLog, true);

CanExecute<WasmBenchmark>(config);
}

public class WasmBenchmark
{
[Benchmark]
public void Check()
{
if (!RuntimeInformation.IsWasm)
{
throw new Exception("Incorrect runtime detection");
}
}
}
}
}

0 comments on commit 4435799

Please sign in to comment.