From 30b332793531240ab79d9517054e1afe502b8ab9 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sun, 15 Dec 2024 23:04:32 +0300 Subject: [PATCH] test: get free port to avoid net collisions --- package-lock.json | 14 ++++++++++++++ package.json | 1 + test/cli.test.js | 11 +++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 969c60360c..88f44f4e22 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,6 +33,7 @@ "esbuild-plugin-transform-hook": "^0.1.1", "esbuild-plugin-utils": "^0.1.0", "fs-extra": "^11.2.0", + "get-port": "^7.1.0", "globby": "^14.0.2", "jsr": "^0.13.2", "madge": "^8.0.0", @@ -2584,6 +2585,19 @@ "dev": true, "license": "ISC" }, + "node_modules/get-port": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/get-port/-/get-port-7.1.0.tgz", + "integrity": "sha512-QB9NKEeDg3xxVwCCwJQ9+xycaz6pBB6iQ76wiWMl1927n0Kir6alPiP+yuiICLLU4jpMe08dXfpebuQppFA2zw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=16" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/get-tsconfig": { "version": "4.8.1", "resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.8.1.tgz", diff --git a/package.json b/package.json index b127f22bc8..84613595c0 100644 --- a/package.json +++ b/package.json @@ -114,6 +114,7 @@ "esbuild-plugin-transform-hook": "^0.1.1", "esbuild-plugin-utils": "^0.1.0", "fs-extra": "^11.2.0", + "get-port": "^7.1.0", "globby": "^14.0.2", "jsr": "^0.13.2", "madge": "^8.0.0", diff --git a/test/cli.test.js b/test/cli.test.js index c96b4f01ef..d096f2e17a 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -15,6 +15,7 @@ import assert from 'node:assert' import { test, describe, before, after } from 'node:test' import { fileURLToPath } from 'node:url' +import getPort from 'get-port' import '../build/globals.js' import { isMain, normalizeExt } from '../build/cli.js' @@ -124,17 +125,19 @@ describe('cli', () => { }) test('scripts from https', async () => { - const server = $`cat ${path.resolve('test/fixtures/echo.http')} | nc -l 8080` + const port = await getPort() + const server = $`cat ${path.resolve('test/fixtures/echo.http')} | nc -l ${port}` const out = - await $`sleep 10 && node build/cli.js --verbose http://127.0.0.1:8080/echo.mjs` + await $`sleep 10 && node build/cli.js --verbose http://127.0.0.1:${port}/echo.mjs` assert.match(out.stderr, /test/) await server.kill() }) test('scripts from https not ok', async () => { - const server = $`echo $'HTTP/1.1 500\n\n' | nc -l 8081` + const port = await getPort() + const server = $`echo $'HTTP/1.1 500\n\n' | nc -l ${port}` const out = - await $`sleep 10 && node build/cli.js http://127.0.0.1:8081`.nothrow() + await $`sleep 10 && node build/cli.js http://127.0.0.1:${port}`.nothrow() assert.match(out.stderr, /Error: Can't get/) await server.kill() })