Skip to content

Commit c4c8fef

Browse files
fix(jco): fix test file generation
1 parent 015e75d commit c4c8fef

3 files changed

Lines changed: 32 additions & 10 deletions

File tree

crates/xtask/src/build/wast_fixtures.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ fn convert_wast_file(
2424
})?;
2525

2626
// TODO: write JS preamble
27+
writeln!(
28+
output_js,
29+
r#"
30+
import {{ }} from "../";
31+
"#
32+
)?;
2733

2834
for directive in parsed.directives {
2935
match directive {
@@ -40,6 +46,7 @@ fn convert_wast_file(
4046
input_wast_path.display()
4147
)
4248
})?;
49+
output_wasm.flush()?;
4350
}
4451
wast::WastDirective::ModuleDefinition(_) => {
4552
todo!("unsupported directive ModuleDefinition")
@@ -76,7 +83,7 @@ fn convert_wast_file(
7683
}
7784
}
7885

79-
// TODO: output JS file
86+
output_js.flush()?;
8087
Ok(())
8188
}
8289

@@ -96,18 +103,27 @@ pub(crate) fn run(wast_path: &Path) -> Result<()> {
96103
ensure!(input_wat.metadata()?.is_file(), "wast path must be a file");
97104

98105
let mut output_wasm_path = wast_path.clone();
99-
output_wasm_path.add_extension(".wasm");
106+
output_wasm_path.add_extension("wasm");
100107
let mut output_wasm = OpenOptions::new()
101108
.write(true)
109+
.create_new(true)
102110
.truncate(true)
103-
.open(output_wasm_path)?;
111+
.open(&output_wasm_path)
112+
.with_context(|| {
113+
format!(
114+
"failed to open output WASM file @ [{}]",
115+
output_wasm_path.display()
116+
)
117+
})?;
104118

105119
let mut output_js_path = wast_path.clone();
106-
output_js_path.add_extension(".js");
120+
output_js_path.add_extension("js");
107121
let mut output_js = OpenOptions::new()
108122
.write(true)
123+
.create_new(true)
109124
.truncate(true)
110-
.open(output_js_path)?;
125+
.open(output_js_path)
126+
.with_context(|| format!("failed to open output JS file @ [{}]", wast_path.display()))?;
111127

112128
convert_wast_file(&mut input_wat, &wast_path, &mut output_wasm, &mut output_js)?;
113129

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.wast.wasm
2+
*.wast.js

packages/jco/test/p3/ported/component-model/wast.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ import { spawn } from "node:child_process";
44

55
import { suite, test, assert, beforeAll } from "vitest";
66

7-
import { fileExists, COMPONENT_MODEL_FIXTURES_WAST_DIR } from "../../../common.js";
7+
import { COMPONENT_MODEL_FIXTURES_WAST_DIR } from "../../../common.js";
8+
import { fileExists } from "../../../helpers.js";
89

910
// These tests are ported from the component-model repo
1011
//
1112
// see: https://github.com/WebAssembly/component-model/tree/main/test
1213
//
1314
suite("component-model", async () => {
15+
let metadata = [];
1416
const walker = await opendir(COMPONENT_MODEL_FIXTURES_WAST_DIR, { recursive: true });
15-
const metadata = [];
17+
1618
for await (const dirent of walker) {
17-
if (!dirent.isFile) {
19+
if (!dirent.isFile()) {
1820
continue;
1921
}
2022
const wastPath = join(dirent.parentPath, dirent.name);
@@ -29,11 +31,13 @@ suite("component-model", async () => {
2931
});
3032
}
3133

34+
metadata = metadata.slice(0,1);
35+
3236
beforeAll(async () => {
3337
for (const { wastPath } of metadata) {
3438
const fixtureBuild = spawn("cargo", ["xtask", "build-wast-fixture", wastPath], {
3539
detached: false,
36-
stdio: "pipe",
40+
stdio: "inherit",
3741
shell: true,
3842
});
3943
await new Promise(resolve => fixtureBuild.on("exit", resolve));
@@ -42,8 +46,8 @@ suite("component-model", async () => {
4246

4347
for (const { wastRelPath, wasmPath, scriptPath } of metadata) {
4448
test.concurrent(wastRelPath, async () => {
45-
assert(await fileExists(scriptPath), `missing generated script @ [${scriptPath}]`);
4649
assert(await fileExists(wasmPath), `missing generated wasm component @ [${wasmPath}]`);
50+
assert(await fileExists(scriptPath), `missing generated script @ [${scriptPath}]`);
4751
// TODO: convert WAST test to WAST + executable JS
4852
assert.strictEqual(true, true);
4953
});

0 commit comments

Comments
 (0)