From ba563c3565212a2546088ea60f3856ab1f4f1745 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Mon, 16 Dec 2024 00:23:01 +0300 Subject: [PATCH] test(cli): replace `nc` with `node:net` --- test/cli.test.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/test/cli.test.js b/test/cli.test.js index d096f2e17a..06c057b84b 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 net from 'node:net' import getPort from 'get-port' import '../build/globals.js' import { isMain, normalizeExt } from '../build/cli.js' @@ -23,6 +24,19 @@ const __filename = fileURLToPath(import.meta.url) const spawn = $.spawn const nodeMajor = +process.versions?.node?.split('.')[0] const test22 = nodeMajor >= 22 ? test : test.skip +const getServer = (resp = [], log = console.log) => { + const server = net.createServer() + server.on('connection', (conn) => { + conn.on('data', (d) => { + conn.write(resp.pop() || 'pong') + }) + }) + server.start = (port) => + new Promise((resolve) => server.listen(port, () => resolve(server))) + server.stop = () => + new Promise((resolve) => server.close(() => resolve(server))) + return server +} describe('cli', () => { // Helps detect unresolved ProcessPromise. @@ -125,21 +139,21 @@ describe('cli', () => { }) test('scripts from https', async () => { + const resp = await fs.readFile(path.resolve('test/fixtures/echo.http')) const port = await getPort() - const server = $`cat ${path.resolve('test/fixtures/echo.http')} | nc -l ${port}` + const server = await getServer([resp]).start(port) const out = - await $`sleep 10 && node build/cli.js --verbose http://127.0.0.1:${port}/echo.mjs` + await $`node build/cli.js --verbose http://127.0.0.1:${port}/echo.mjs` assert.match(out.stderr, /test/) - await server.kill() + await server.stop() }) test('scripts from https not ok', async () => { 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:${port}`.nothrow() + const server = await getServer(['HTTP/1.1 500\n\n']).listen(port) + const out = await $`node build/cli.js http://127.0.0.1:${port}`.nothrow() assert.match(out.stderr, /Error: Can't get/) - await server.kill() + await server.stop() }) test('scripts with no extension', async () => {