Skip to content

Commit 18bb608

Browse files
revert(tests): test state machinery
Signed-off-by: Victor Adossi <vadossi@cosmonic.com>
1 parent b45cd1b commit 18bb608

File tree

2 files changed

+39
-68
lines changed

2 files changed

+39
-68
lines changed

test/builtins/fetch.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,23 @@ import { getRandomPort } from '../util.js';
88

99
const FETCH_URL = 'http://localhost';
1010

11-
export const state = async () => {
12-
const port = await getRandomPort();
13-
return { port };
14-
};
11+
const port = await getRandomPort();
1512

16-
export const source = (testState) => {
17-
let port = testState?.port ? ':' + testState.port : '';
18-
const url = FETCH_URL + port;
19-
return `
13+
export const source = `
2014
export async function run () {
21-
const res = await fetch('${url}');
15+
const res = await fetch('${FETCH_URL}:${port}');
2216
const source = await res.json();
2317
console.log(source.url);
2418
}
2519
export function ready () {
2620
return true;
2721
}
2822
`;
29-
};
3023

3124
export const enableFeatures = ['http'];
3225

33-
export async function test(run, testState) {
34-
// Get the randomly generated port
35-
const port = testState.port;
36-
if (!port) {
37-
throw new Error('missing port on test state');
38-
}
39-
40-
const url = FETCH_URL + (port ? ':' + port : '');
26+
export async function test(run) {
27+
const url = `${FETCH_URL}:${port}`;
4128

4229
// Run a local server on some port
4330
const server = createServer(async (req, res) => {

test/test.js

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const LOG_DEBUGGING = false;
1111
const enableAot = process.env.WEVAL_TEST == '1';
1212
const debugBuild = process.env.DEBUG_TEST == '1';
1313

14-
const noOp = async () => {};
15-
1614
function maybeLogging(disableFeatures) {
1715
if (!LOG_DEBUGGING) return disableFeatures;
1816
if (disableFeatures && disableFeatures.includes('stdio')) {
@@ -26,24 +24,12 @@ suite('Builtins', () => {
2624
for (const filename of builtinsCases) {
2725
const name = filename.slice(0, -3);
2826
test(name, async () => {
29-
const testModule = await import(`./builtins/${filename}`);
3027
const {
31-
state,
28+
source,
3229
test: runTest,
3330
disableFeatures,
3431
enableFeatures,
35-
} = testModule;
36-
37-
// If an args object was provided, generate the arguments to feed to both
38-
// source generation (if necessary) and the test run itself
39-
let stateFn = state ?? noOp;
40-
const stateObj = await stateFn();
41-
42-
// If the source is a function then invoke it to generate the source string, possibly with arguments
43-
let source = testModule.source;
44-
if (typeof source === 'function') {
45-
source = source(stateObj);
46-
}
32+
} = await import(`./builtins/${filename}`);
4733

4834
const { component } = await componentize(
4935
source,
@@ -60,7 +46,7 @@ suite('Builtins', () => {
6046
enableFeatures,
6147
disableFeatures: maybeLogging(disableFeatures),
6248
enableAot,
63-
},
49+
}
6450
);
6551

6652
const { files } = await transpile(component, {
@@ -75,13 +61,13 @@ suite('Builtins', () => {
7561

7662
await writeFile(
7763
new URL(`./output/${name}.component.wasm`, import.meta.url),
78-
component,
64+
component
7965
);
8066

8167
for (const file of Object.keys(files)) {
8268
await writeFile(
8369
new URL(`./output/${name}/${file}`, import.meta.url),
84-
files[file],
70+
files[file]
8571
);
8672
}
8773

@@ -90,12 +76,11 @@ suite('Builtins', () => {
9076
`
9177
import { run } from './${name}.js';
9278
run();
93-
`,
79+
`
9480
);
9581

9682
try {
97-
// Build a run function to pass to the test
98-
const runFn = async function run() {
83+
await runTest(async function run() {
9984
let stdout = '',
10085
stderr = '',
10186
timeout;
@@ -105,10 +90,10 @@ suite('Builtins', () => {
10590
process.argv[0],
10691
[
10792
fileURLToPath(
108-
new URL(`./output/${name}/run.js`, import.meta.url),
93+
new URL(`./output/${name}/run.js`, import.meta.url)
10994
),
11095
],
111-
{ stdio: 'pipe' },
96+
{ stdio: 'pipe' }
11297
);
11398
cp.stdout.on('data', (chunk) => {
11499
stdout += chunk;
@@ -118,16 +103,16 @@ suite('Builtins', () => {
118103
});
119104
cp.on('error', reject);
120105
cp.on('exit', (code) =>
121-
code === 0 ? resolve() : reject(new Error(stderr || stdout)),
106+
code === 0 ? resolve() : reject(new Error(stderr || stdout))
122107
);
123108
timeout = setTimeout(() => {
124109
reject(
125110
new Error(
126111
'test timed out with output:\n' +
127-
stdout +
128-
'\n\nstderr:\n' +
129-
stderr,
130-
),
112+
stdout +
113+
'\n\nstderr:\n' +
114+
stderr
115+
)
131116
);
132117
}, 10_000);
133118
});
@@ -138,10 +123,7 @@ suite('Builtins', () => {
138123
}
139124

140125
return { stdout, stderr };
141-
};
142-
143-
// Run the actual test
144-
await runTest(runFn, stateObj);
126+
});
145127
} catch (err) {
146128
if (err.stderr) console.error(err.stderr);
147129
throw err.err || err;
@@ -156,7 +138,7 @@ suite('Bindings', () => {
156138
test(name, async () => {
157139
const source = await readFile(
158140
new URL(`./cases/${name}/source.js`, import.meta.url),
159-
'utf8',
141+
'utf8'
160142
);
161143

162144
let witWorld,
@@ -166,14 +148,14 @@ suite('Bindings', () => {
166148
try {
167149
witWorld = await readFile(
168150
new URL(`./cases/${name}/world.wit`, import.meta.url),
169-
'utf8',
151+
'utf8'
170152
);
171153
} catch (e) {
172154
if (e?.code == 'ENOENT') {
173155
try {
174156
isWasiTarget = true;
175157
witPath = fileURLToPath(
176-
new URL(`./cases/${name}/wit`, import.meta.url),
158+
new URL(`./cases/${name}/wit`, import.meta.url)
177159
);
178160
await readdir(witPath);
179161
} catch (e) {
@@ -247,14 +229,14 @@ suite('Bindings', () => {
247229

248230
await writeFile(
249231
new URL(`./output/${name}.component.wasm`, import.meta.url),
250-
component,
232+
component
251233
);
252234

253235
for (const file of Object.keys(files)) {
254236
let source = files[file];
255237
await writeFile(
256238
new URL(`./output/${name}/${file}`, import.meta.url),
257-
source,
239+
source
258240
);
259241
}
260242

@@ -292,12 +274,12 @@ suite('WASI', () => {
292274
worldName: 'test1',
293275
enableAot,
294276
debugBuild,
295-
},
277+
}
296278
);
297279

298280
await writeFile(
299281
new URL(`./output/wasi.component.wasm`, import.meta.url),
300-
component,
282+
component
301283
);
302284

303285
const { files } = await transpile(component, { tracing: DEBUG_TRACING });
@@ -309,7 +291,7 @@ suite('WASI', () => {
309291
for (const file of Object.keys(files)) {
310292
await writeFile(
311293
new URL(`./output/wasi/${file}`, import.meta.url),
312-
files[file],
294+
files[file]
313295
);
314296
}
315297

@@ -321,17 +303,19 @@ suite('WASI', () => {
321303
});
322304

323305
test('basic app (OriginalSourceFile API)', async () => {
324-
const { component } = await componentize({
325-
sourcePath: './test/api/index.js',
326-
witPath: fileURLToPath(new URL('./wit', import.meta.url)),
327-
worldName: 'test1',
328-
enableAot,
329-
debugBuild,
330-
});
306+
const { component } = await componentize(
307+
{
308+
sourcePath: "./test/api/index.js",
309+
witPath: fileURLToPath(new URL('./wit', import.meta.url)),
310+
worldName: 'test1',
311+
enableAot,
312+
debugBuild,
313+
}
314+
);
331315

332316
await writeFile(
333317
new URL(`./output/wasi.component.wasm`, import.meta.url),
334-
component,
318+
component
335319
);
336320

337321
const { files } = await transpile(component, { tracing: DEBUG_TRACING });
@@ -343,7 +327,7 @@ suite('WASI', () => {
343327
for (const file of Object.keys(files)) {
344328
await writeFile(
345329
new URL(`./output/wasi/${file}`, import.meta.url),
346-
files[file],
330+
files[file]
347331
);
348332
}
349333

0 commit comments

Comments
 (0)