Skip to content

Commit 81fe6c8

Browse files
PackageToJS: Fix browser tests with non-.wasm product
1 parent 53d1a47 commit 81fe6c8

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

Diff for: Plugins/PackageToJS/Sources/PackageToJSPlugin.swift

+14-4
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ struct PackageToJSPlugin: CommandPlugin {
119119
)
120120
let planner = PackagingPlanner(
121121
options: buildOptions.packageOptions, context: context, selfPackage: selfPackage,
122-
outputDir: outputDir, wasmProductArtifact: productArtifact)
122+
outputDir: outputDir, wasmProductArtifact: productArtifact,
123+
wasmFilename: productArtifact.lastPathComponent
124+
)
123125
let rootTask = try planner.planBuild(
124126
make: &make, buildOptions: buildOptions)
125127
cleanIfBuildGraphChanged(root: rootTask, make: make, context: context)
@@ -193,7 +195,14 @@ struct PackageToJSPlugin: CommandPlugin {
193195
)
194196
let planner = PackagingPlanner(
195197
options: testOptions.packageOptions, context: context, selfPackage: selfPackage,
196-
outputDir: outputDir, wasmProductArtifact: productArtifact)
198+
outputDir: outputDir, wasmProductArtifact: productArtifact,
199+
// If the product artifact doesn't have a .wasm extension, add it
200+
// to deliver it with the correct MIME type when serving the test
201+
// files for browser tests.
202+
wasmFilename: productArtifact.lastPathComponent.hasSuffix(".wasm")
203+
? productArtifact.lastPathComponent
204+
: productArtifact.lastPathComponent + ".wasm"
205+
)
197206
let (rootTask, binDir) = try planner.planTestBuild(
198207
make: &make)
199208
cleanIfBuildGraphChanged(root: rootTask, make: make, context: context)
@@ -486,7 +495,8 @@ extension PackagingPlanner {
486495
context: PluginContext,
487496
selfPackage: Package,
488497
outputDir: URL,
489-
wasmProductArtifact: URL
498+
wasmProductArtifact: URL,
499+
wasmFilename: String
490500
) {
491501
let outputBaseName = outputDir.lastPathComponent
492502
let (configuration, triple) = PackageToJS.deriveBuildConfiguration(wasmProductArtifact: wasmProductArtifact)
@@ -498,7 +508,7 @@ extension PackagingPlanner {
498508
selfPackageDir: BuildPath(absolute: selfPackage.directoryURL.path),
499509
outputDir: BuildPath(absolute: outputDir.path),
500510
wasmProductArtifact: BuildPath(absolute: wasmProductArtifact.path),
501-
wasmFilename: wasmProductArtifact.lastPathComponent,
511+
wasmFilename: wasmFilename,
502512
configuration: configuration,
503513
triple: triple,
504514
system: system

Diff for: Plugins/PackageToJS/Templates/test.browser.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
<body>
55
<script type="module">
66
import { testBrowserInPage } from "./test.js";
7+
import { MODULE_PATH } from "./instantiate.js";
78
import { defaultBrowserSetup /* #if USE_SHARED_MEMORY */, createDefaultWorkerFactory /* #endif */} from './platforms/browser.js';
89

910
const logElement = document.createElement("pre");
1011
document.body.appendChild(logElement);
1112

1213
const processInfo = await fetch("/process-info.json").then((response) => response.json());
1314
const options = await defaultBrowserSetup({
14-
module: await fetch("./main.wasm"),
15+
module: await fetch(new URL(MODULE_PATH, import.meta.url)),
1516
args: processInfo.args,
1617
onStdoutLine: (line) => {
1718
console.log(line);

Diff for: Plugins/PackageToJS/Tests/ExampleTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ extension Trait where Self == ConditionTrait {
182182
let process = Process()
183183
process.executableURL = llvmCov
184184
let profdata = packageDir.appending(path: ".build/plugins/PackageToJS/outputs/PackageTests/default.profdata")
185-
let wasm = packageDir.appending(path: ".build/plugins/PackageToJS/outputs/PackageTests/main.wasm")
185+
let wasm = packageDir.appending(path: ".build/plugins/PackageToJS/outputs/PackageTests/TestingPackageTests.wasm")
186186
process.arguments = ["report", "-instr-profile", profdata.path, wasm.path]
187187
process.standardOutput = FileHandle.nullDevice
188188
try process.run()

0 commit comments

Comments
 (0)