Skip to content

Commit 196f214

Browse files
fix(ci): use a local test server for fetch test (#207)
1 parent c6e1a48 commit 196f214

File tree

5 files changed

+78
-22
lines changed

5 files changed

+78
-22
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/componentize.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '@bytecodealliance/jco';
99
import { spawnSync } from 'node:child_process';
1010
import { tmpdir } from 'node:os';
11-
import { resolve, join, dirname } from 'node:path';
11+
import { join, dirname } from 'node:path';
1212
import { readFile, writeFile, mkdir, rm } from 'node:fs/promises';
1313
import { rmSync, existsSync } from 'node:fs';
1414
import { createHash } from 'node:crypto';
@@ -17,26 +17,13 @@ import {
1717
stubWasi,
1818
} from '../lib/spidermonkey-embedding-splicer.js';
1919
import { fileURLToPath } from 'node:url';
20-
import { cwd, stdout, platform } from 'node:process';
20+
import { cwd, stdout } from 'node:process';
21+
22+
import { maybeWindowsPath } from './platform.js';
23+
2124
export const { version } = JSON.parse(
2225
await readFile(new URL('../package.json', import.meta.url), 'utf8'),
2326
);
24-
const isWindows = platform === 'win32';
25-
26-
function maybeWindowsPath(path) {
27-
if (!path) return path;
28-
const resolvedPath = resolve(path);
29-
if (!isWindows) return resolvedPath;
30-
31-
// Strip any existing UNC prefix check both the format we add as well as what
32-
// the windows API returns when using path.resolve
33-
let cleanPath = resolvedPath;
34-
while (cleanPath.startsWith('\\\\?\\') || cleanPath.startsWith('//?/')) {
35-
cleanPath = cleanPath.substring(4);
36-
}
37-
38-
return '//?/' + cleanPath.replace(/\\/g, '/');
39-
}
4027

4128
/**
4229
* Clean up the given input string by removing the given patterns if

src/platform.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { resolve } from 'node:path';
2+
import { platform } from 'node:process';
3+
4+
export const isWindows = platform === 'win32';
5+
6+
export function maybeWindowsPath(path) {
7+
if (!path) return path;
8+
const resolvedPath = resolve(path);
9+
if (!isWindows) return resolvedPath;
10+
11+
// Strip any existing UNC prefix check both the format we add as well as what
12+
// the windows API returns when using path.resolve
13+
let cleanPath = resolvedPath;
14+
while (cleanPath.startsWith('\\\\?\\') || cleanPath.startsWith('//?/')) {
15+
cleanPath = cleanPath.substring(4);
16+
}
17+
18+
return '//?/' + cleanPath.replace(/\\/g, '/');
19+
}

test/builtins/fetch.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
import { URL, fileURLToPath } from 'node:url';
2+
import { createServer } from 'node:http';
3+
14
import { strictEqual, ok } from 'node:assert';
25

6+
import { maybeWindowsPath } from '../../src/platform.js';
7+
import { getRandomPort } from '../util.js';
8+
9+
const FETCH_URL = 'http://localhost';
10+
11+
const port = await getRandomPort();
12+
313
export const source = `
414
export async function run () {
5-
const res = await fetch('https://httpbin.org/anything');
15+
const res = await fetch('${FETCH_URL}:${port}');
616
const source = await res.json();
717
console.log(source.url);
818
}
@@ -14,7 +24,34 @@ export const source = `
1424
export const enableFeatures = ['http'];
1525

1626
export async function test(run) {
27+
const url = `${FETCH_URL}:${port}`;
28+
29+
// Run a local server on some port
30+
const server = createServer(async (req, res) => {
31+
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
32+
res.write(
33+
JSON.stringify({
34+
status: 'ok',
35+
url,
36+
}),
37+
);
38+
res.end();
39+
}).listen(port);
40+
41+
// Wait until the server is ready
42+
let ready = false;
43+
while (!ready) {
44+
try {
45+
const res = await fetch(url);
46+
ready = true;
47+
} catch (err) {
48+
await new Promise((resolve) => setTimeout(resolve, 250));
49+
}
50+
}
51+
1752
const { stdout, stderr } = await run();
1853
strictEqual(stderr, '');
19-
strictEqual(stdout.trim(), 'https://httpbin.org/anything');
54+
strictEqual(stdout.trim(), url);
55+
56+
server.close();
2057
}

test/util.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createServer } from 'node:net';
2+
3+
// Utility function for getting a random port
4+
export async function getRandomPort() {
5+
return await new Promise((resolve) => {
6+
const server = createServer();
7+
server.listen(0, function () {
8+
const port = this.address().port;
9+
server.on('close', () => resolve(port));
10+
server.close();
11+
});
12+
});
13+
}

0 commit comments

Comments
 (0)