Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
chore(tests): Use verdaccio as node process instead of docker image#20336
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
081f6b24ea7a8126022b30eb9fe0ff682516e0e4fc68acf7789607f5544631140904f064cbb91d6f2e8dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,140 @@ | ||
| /* eslint-disable no-console */ | ||
| import * as childProcess from 'child_process'; | ||
| import { TEST_REGISTRY_CONTAINER_NAME, VERDACCIO_VERSION } from './lib/constants'; | ||
| import { spawn, spawnSync, type ChildProcess } from 'child_process'; | ||
| import * as fs from 'fs'; | ||
| import * as http from 'http'; | ||
| import * as path from 'path'; | ||
| import { publishPackages } from './lib/publishPackages'; | ||
| // https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines | ||
| function groupCIOutput(groupTitle: string, fn: () => void): void { | ||
| const VERDACCIO_PORT = 4873; | ||
| let verdaccioChild: ChildProcess | undefined; | ||
| export interface RegistrySetupOptions { | ||
| /** | ||
| * When true, Verdaccio is spawned detached with stdio disconnected from the parent, then | ||
| * the child is unref'd after a successful setup so the parent can exit while the registry | ||
| * keeps running (e.g. `yarn test:prepare` then installs against 127.0.0.1:4873). | ||
| */ | ||
| daemonize?: boolean; | ||
| } | ||
| /** Stops any Verdaccio runner from a previous prepare/run so port 4873 is free. */ | ||
| function killStrayVerdaccioRunner(): void { | ||
| spawnSync('pkill', ['-f', 'verdaccio-runner.mjs'], { stdio: 'ignore' }); | ||
| } | ||
| async function groupCIOutput(groupTitle: string, fn: () => void | Promise<void>): Promise<void> { | ||
| if (process.env.CI) { | ||
| console.log(`::group::${groupTitle}`); | ||
| fn(); | ||
| console.log('::endgroup::'); | ||
| try { | ||
| await Promise.resolve(fn()); | ||
| } finally { | ||
| console.log('::endgroup::'); | ||
| } | ||
| } else { | ||
| fn(); | ||
| await Promise.resolve(fn()); | ||
| } | ||
| } | ||
| export function registrySetup(): void { | ||
| groupCIOutput('Test Registry Setup', () => { | ||
| // Stop test registry container (Verdaccio) if it was already running | ||
| childProcess.spawnSync('docker', ['stop', TEST_REGISTRY_CONTAINER_NAME], { stdio: 'ignore' }); | ||
| console.log('Stopped previously running test registry'); | ||
| // Start test registry (Verdaccio) | ||
| const startRegistryProcessResult = childProcess.spawnSync( | ||
| 'docker', | ||
| [ | ||
| 'run', | ||
| '--detach', | ||
| '--rm', | ||
| '--name', | ||
| TEST_REGISTRY_CONTAINER_NAME, | ||
| '-p', | ||
| '4873:4873', | ||
| '-v', | ||
| `${__dirname}/verdaccio-config:/verdaccio/conf`, | ||
| `verdaccio/verdaccio:${VERDACCIO_VERSION}`, | ||
| ], | ||
| { encoding: 'utf8', stdio: 'inherit' }, | ||
| ); | ||
| if (startRegistryProcessResult.status !== 0) { | ||
| throw new Error('Start Registry Process failed.'); | ||
| function waitUntilVerdaccioResponds(maxRetries: number = 60): Promise<void> { | ||
| const pingUrl = `http://127.0.0.1:${VERDACCIO_PORT}/-/ping`; | ||
| function tryOnce(): Promise<boolean> { | ||
| return new Promise(resolve => { | ||
| const req = http.get(pingUrl, res => { | ||
| res.resume(); | ||
| resolve((res.statusCode ?? 0) > 0 && (res.statusCode ?? 500) < 500); | ||
| }); | ||
| req.on('error', () => resolve(false)); | ||
| req.setTimeout(2000, () => { | ||
| req.destroy(); | ||
| resolve(false); | ||
| }); | ||
| }); | ||
| } | ||
| return (async () => { | ||
| for (let i = 0; i < maxRetries; i++) { | ||
| if (await tryOnce()) { | ||
| return; | ||
| } | ||
| await new Promise(r => setTimeout(r, 1000)); | ||
| } | ||
| throw new Error('Verdaccio did not start in time.'); | ||
| })(); | ||
| } | ||
| function startVerdaccioChild(configPath: string, port: number, daemonize: boolean): ChildProcess { | ||
| const runnerPath = path.join(__dirname, 'verdaccio-runner.mjs'); | ||
| const verbose = process.env.E2E_VERDACCIO_VERBOSE === '1'; | ||
| return spawn(process.execPath, [runnerPath, configPath, String(port)], { | ||
| detached: daemonize, | ||
| stdio: daemonize && !verbose ? 'ignore' : 'inherit', | ||
| }); | ||
| } | ||
| async function stopVerdaccioChild(): Promise<void> { | ||
| const child = verdaccioChild; | ||
| verdaccioChild = undefined; | ||
| if (!child || child.killed) { | ||
| return; | ||
| } | ||
| child.kill('SIGTERM'); | ||
| await new Promise<void>(resolve => { | ||
| const timeoutId = setTimeout(resolve, 5000); | ||
| child.once('exit', () => { | ||
| clearTimeout(timeoutId); | ||
| resolve(); | ||
| }); | ||
| }); | ||
sentry[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| /** Drop the child handle so the parent process can exit; Verdaccio keeps running. */ | ||
| function detachVerdaccioRunner(): void { | ||
| const child = verdaccioChild; | ||
| verdaccioChild = undefined; | ||
| if (child && !child.killed) { | ||
| child.unref(); | ||
| } | ||
| } | ||
| export async function registrySetup(options: RegistrySetupOptions = {}): Promise<void> { | ||
| const { daemonize = false } = options; | ||
| await groupCIOutput('Test Registry Setup', async () => { | ||
| killStrayVerdaccioRunner(); | ||
| const configPath = path.join(__dirname, 'verdaccio-config', 'config.yaml'); | ||
| const storagePath = path.join(__dirname, 'verdaccio-config', 'storage'); | ||
| // Clear previous registry storage to ensure a fresh state | ||
| fs.rmSync(storagePath, { recursive: true, force: true }); | ||
| publishPackages(); | ||
| // Verdaccio runs in a child process so tarball uploads are not starved by the | ||
| // same Node event loop as ts-node (in-process runServer + npm publish could hang). | ||
| console.log('Starting Verdaccio...'); | ||
| verdaccioChild = startVerdaccioChild(configPath, VERDACCIO_PORT, daemonize); | ||
| try { | ||
| await waitUntilVerdaccioResponds(60); | ||
| console.log('Verdaccio is ready'); | ||
| await publishPackages(); | ||
| } catch (error) { | ||
| await stopVerdaccioChild(); | ||
| throw error; | ||
| } | ||
| }); | ||
| if (daemonize) { | ||
| detachVerdaccioRunner(); | ||
| } | ||
| console.log(''); | ||
| console.log(''); | ||
| } | ||
| export async function registryCleanup(): Promise<void> { | ||
| await stopVerdaccioChild(); | ||
| killStrayVerdaccioRunner(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* eslint-disable no-console */ | ||
| import { createRequire } from 'node:module'; | ||
| const require = createRequire(import.meta.url); | ||
| const { runServer } = require('verdaccio'); | ||
| const configPath = process.argv[2]; | ||
| const port = parseInt(process.argv[3], 10); | ||
| if (!configPath || !Number.isFinite(port)) { | ||
| console.error('verdaccio-runner: expected <configPath> <port> argv'); | ||
| process.exit(1); | ||
| } | ||
| try { | ||
| // runServer resolves to the Express app; binding errors are emitted on the | ||
| // http.Server returned by app.listen(), not on the app itself. | ||
| const app = await runServer(configPath, { listenArg: String(port) }); | ||
| await new Promise((resolve, reject) => { | ||
| const httpServer = app.listen(port, '127.0.0.1', () => resolve()); | ||
| httpServer.once('error', reject); | ||
| }); | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } catch (err) { | ||
| console.error(err); | ||
| process.exit(1); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leftover I noticed I forgot to bump, not really related but this is non-breaking for us.