Skip to content

Commit c290df1

Browse files
authored
[browser] Add OutputType=Library tests (#110731)
* Browser library test * Wasi should test both sdks. * Test both sdks in browser lib mode. * Trnsform the test asset to sample-like-project. * Add running the lib to the test.
1 parent 2572ab8 commit c290df1

File tree

10 files changed

+101
-22
lines changed

10 files changed

+101
-22
lines changed

src/mono/nuget/Microsoft.NET.Runtime.WebAssembly.Sdk/Sdk/Sdk.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetArchitecture>wasm</TargetArchitecture>
44
<TargetOS>browser</TargetOS>
55
<UseMonoRuntime>true</UseMonoRuntime>
6-
<OutputType>Exe</OutputType>
6+
<OutputType Condition="'$(OutputType)' == ''">Exe</OutputType>
77
<PublishTrimmed Condition="'$(PublishTrimmed)' == ''">true</PublishTrimmed>
88
</PropertyGroup>
99
</Project>

src/mono/wasi/Wasi.Build.Tests/WasiLibraryModeTests.cs

+9-21
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,17 @@ public WasiLibraryModeTests(ITestOutputHelper output, SharedBuildPerTestClassFix
1919
{
2020
}
2121

22-
[Fact]
23-
public void ConsoleBuildLibraryMode()
22+
[Theory]
23+
[InlineData("Microsoft.NET.Sdk", false)]
24+
[InlineData("Microsoft.NET.Sdk.WebAssembly", true)]
25+
public void LibraryModeBuild(string sdk, bool hasWasmAppBundle)
2426
{
2527
string config = "Release";
2628
string id = $"{config}_{GetRandomId()}";
2729
string projectFile = CreateWasmTemplateProject(id, "wasiconsole");
28-
string code =
29-
"""
30-
using System;
31-
using System.Runtime.InteropServices;
32-
public unsafe class Test
33-
{
34-
[UnmanagedCallersOnly(EntryPoint = "MyCallback")]
35-
public static int MyCallback()
36-
{
37-
Console.WriteLine("WASM Library MyCallback is called");
38-
return 100;
39-
}
40-
}
41-
""";
42-
string csprojCode =
43-
"""
44-
<Project Sdk="Microsoft.NET.Sdk.WebAssembly">
30+
string csprojCode =
31+
$"""
32+
<Project Sdk="{sdk}">
4533
<PropertyGroup>
4634
<TargetFramework>net9.0</TargetFramework>
4735
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
@@ -51,6 +39,7 @@ public static int MyCallback()
5139
</PropertyGroup>
5240
</Project>
5341
""";
42+
string code = File.ReadAllText(Path.Combine(BuildEnvironment.TestAssetsPath, "LibraryMode.cs"));
5443
File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), code);
5544
File.WriteAllText(Path.Combine(_projectDir!, $"{id}.csproj"), csprojCode);
5645
string projectName = Path.GetFileNameWithoutExtension(projectFile);
@@ -62,9 +51,8 @@ public static int MyCallback()
6251
DotnetWasmFromRuntimePack: false,
6352
CreateProject: false,
6453
Publish: false,
54+
AssertAppBundle: hasWasmAppBundle,
6555
TargetFramework: BuildTestBase.DefaultTargetFramework
6656
));
67-
68-
Assert.Contains("Build succeeded.", output);
6957
}
7058
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
public unsafe class Test
4+
{
5+
[UnmanagedCallersOnly(EntryPoint = "MyCallback")]
6+
public static int MyCallback()
7+
{
8+
Console.WriteLine("TestOutput -> WASM Library MyCallback is called");
9+
return 100;
10+
}
11+
}

src/mono/wasm/Wasm.Build.Tests/BrowserStructures/TestAsset.cs

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ public class TestAsset
44
public string RunnableProjectSubPath { get; init; }
55
public static readonly TestAsset WasmBasicTestApp = new() { Name = "WasmBasicTestApp", RunnableProjectSubPath = "App" };
66
public static readonly TestAsset BlazorBasicTestApp = new() { Name = "BlazorBasicTestApp", RunnableProjectSubPath = "App" };
7+
public static readonly TestAsset LibraryModeTestApp = new() { Name = "LibraryMode" };
78
}

src/mono/wasm/Wasm.Build.Tests/Templates/WasmTemplateTests.cs

+22
Original file line numberDiff line numberDiff line change
@@ -284,5 +284,27 @@ void AssertFile(string suffix)
284284
Assert.True(copyOutputSymbolsToPublishDirectory == (fileName != null && File.Exists(fileName)), $"The {fileName} file {(copyOutputSymbolsToPublishDirectory ? "should" : "shouldn't")} exist in publish folder");
285285
}
286286
}
287+
288+
[Theory]
289+
[InlineData(false)]
290+
[InlineData(true)]
291+
public async void LibraryModeBuild(bool useWasmSdk)
292+
{
293+
var config = Configuration.Release;
294+
ProjectInfo info = CopyTestAsset(config, aot: false, TestAsset.LibraryModeTestApp, "libraryMode");
295+
if (!useWasmSdk)
296+
{
297+
UpdateFile($"{info.ProjectName}.csproj", new Dictionary<string, string>() {
298+
{ "Microsoft.NET.Sdk.WebAssembly", "Microsoft.NET.Sdk" }
299+
});
300+
}
301+
BuildProject(info, config, new BuildOptions(AssertAppBundle: useWasmSdk));
302+
if (useWasmSdk)
303+
{
304+
var result = await RunForBuildWithDotnetRun(new BrowserRunOptions(config, ExpectedExitCode: 100));
305+
Assert.Contains("WASM Library MyExport is called", result.TestOutput);
306+
}
307+
308+
}
287309
}
288310
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices.JavaScript;
6+
7+
namespace LibraryMode;
8+
9+
public partial class Test
10+
{
11+
[JSExport]
12+
public static int MyExport()
13+
{
14+
Console.WriteLine("TestOutput -> WASM Library MyExport is called");
15+
return 100;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk.WebAssembly">
2+
<PropertyGroup>
3+
<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
6+
<OutputType>Library</OutputType>
7+
</PropertyGroup>
8+
</Project>
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
[assembly:System.Runtime.Versioning.SupportedOSPlatform("browser")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<!-- Licensed to the .NET Foundation under one or more agreements. -->
3+
<!-- The .NET Foundation licenses this file to you under the MIT license. -->
4+
<html>
5+
6+
<head>
7+
<title>tmp</title>
8+
<meta charset="UTF-8">
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
10+
<script type='module' src="./main.js"></script>
11+
</head>
12+
13+
<body>
14+
</body>
15+
16+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
import { dotnet } from './_framework/dotnet.js'
5+
6+
const { getAssemblyExports, getConfig } = await dotnet.create();
7+
8+
const config = getConfig();
9+
const exports = await getAssemblyExports(config.mainAssemblyName);
10+
var code = exports.LibraryMode.Test.MyExport();
11+
console.log(`WASM EXIT ${code}`);

0 commit comments

Comments
 (0)