-
Notifications
You must be signed in to change notification settings - Fork 50
fix(ci): use a local test server for fetch test #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tschneidereit
merged 4 commits into
bytecodealliance:main
from
vados-cosmonic:fix(ci)=allow-retries-for-fetch-test
Apr 25, 2025
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7aa0182
refactor(tests): use ephemeral local http server for fetch test
vados-cosmonic e891103
refactor: reusable windows path fixing
vados-cosmonic b45cd1b
fix(tests): windows path resolution
vados-cosmonic 18bb608
revert(tests): test state machinery
vados-cosmonic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { resolve } from 'node:path'; | ||
| import { platform } from 'node:process'; | ||
|
|
||
| export const isWindows = platform === 'win32'; | ||
|
|
||
| export function maybeWindowsPath(path) { | ||
| if (!path) return path; | ||
| const resolvedPath = resolve(path); | ||
| if (!isWindows) return resolvedPath; | ||
|
|
||
| // Strip any existing UNC prefix check both the format we add as well as what | ||
| // the windows API returns when using path.resolve | ||
| let cleanPath = resolvedPath; | ||
| while (cleanPath.startsWith('\\\\?\\') || cleanPath.startsWith('//?/')) { | ||
| cleanPath = cleanPath.substring(4); | ||
| } | ||
|
|
||
| return '//?/' + cleanPath.replace(/\\/g, '/'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,70 @@ | ||
| import { URL, fileURLToPath } from 'node:url'; | ||
| import { createServer } from 'node:http'; | ||
|
|
||
| import { strictEqual, ok } from 'node:assert'; | ||
|
|
||
| export const source = ` | ||
| import { maybeWindowsPath } from '../../src/platform.js'; | ||
| import { getRandomPort } from '../util.js'; | ||
|
|
||
| const FETCH_URL = 'http://localhost'; | ||
|
|
||
| export const state = async () => { | ||
| const port = await getRandomPort(); | ||
| return { port }; | ||
| }; | ||
|
|
||
| export const source = (testState) => { | ||
| let port = testState?.port ? ':' + testState.port : ''; | ||
| const url = FETCH_URL + port; | ||
| return ` | ||
| export async function run () { | ||
| const res = await fetch('https://httpbin.org/anything'); | ||
| const res = await fetch('${url}'); | ||
| const source = await res.json(); | ||
| console.log(source.url); | ||
| } | ||
| export function ready () { | ||
| return true; | ||
| } | ||
| `; | ||
| }; | ||
|
|
||
| export const enableFeatures = ['http']; | ||
|
|
||
| export async function test(run) { | ||
| export async function test(run, testState) { | ||
| // Get the randomly generated port | ||
| const port = testState.port; | ||
| if (!port) { | ||
| throw new Error('missing port on test state'); | ||
| } | ||
|
|
||
| const url = FETCH_URL + (port ? ':' + port : ''); | ||
|
|
||
| // Run a local server on some port | ||
| const server = createServer(async (req, res) => { | ||
| res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' }); | ||
| res.write( | ||
| JSON.stringify({ | ||
| status: 'ok', | ||
| url, | ||
| }), | ||
| ); | ||
| res.end(); | ||
| }).listen(port); | ||
|
|
||
| // Wait until the server is ready | ||
| let ready = false; | ||
| while (!ready) { | ||
| try { | ||
| const res = await fetch(url); | ||
| ready = true; | ||
| } catch (err) { | ||
| await new Promise((resolve) => setTimeout(resolve, 250)); | ||
| } | ||
| } | ||
|
|
||
| const { stdout, stderr } = await run(); | ||
| strictEqual(stderr, ''); | ||
| strictEqual(stdout.trim(), 'https://httpbin.org/anything'); | ||
| strictEqual(stdout.trim(), url); | ||
|
|
||
| server.close(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { createServer } from 'node:net'; | ||
|
|
||
| // Utility function for getting a random port | ||
| export async function getRandomPort() { | ||
| return await new Promise((resolve) => { | ||
| const server = createServer(); | ||
| server.listen(0, function () { | ||
| const port = this.address().port; | ||
| server.on('close', () => resolve(port)); | ||
| server.close(); | ||
| }); | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.