-
-
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
081f6b2
chore(tests): Use verdaccio as node process instead of docker image
mydea 4ea7a81
fix storage path
mydea 26022b3
fix running both ways
mydea 0eb9fe0
fix lint
mydea ff68251
bump deps
mydea 6e0e4fc
bump deps
mydea 68acf77
dedupe deps
mydea 89607f5
remove unneeded pid thing
mydea 5446311
cleanup
mydea 40904f0
remove unused file
mydea 64cbb91
small fix
mydea d6f2e8d
fix listener
mydea 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
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
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,4 +1,3 @@ | ||
| export const TEST_REGISTRY_CONTAINER_NAME = 'verdaccio-e2e-test-registry'; | ||
| export const DEFAULT_BUILD_TIMEOUT_SECONDS = 60 * 5; | ||
| export const DEFAULT_TEST_TIMEOUT_SECONDS = 60 * 2; | ||
| export const VERDACCIO_VERSION = '5.22.1'; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
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
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,18 +1,17 @@ | ||
| /* eslint-disable no-console */ | ||
| import * as dotenv from 'dotenv'; | ||
| import { registrySetup } from './registrySetup'; | ||
| import { registryRelease, registrySetup } from './registrySetup'; | ||
|
|
||
| async function run(): Promise<void> { | ||
| // Load environment variables from .env file locally | ||
| dotenv.config(); | ||
|
|
||
| try { | ||
| registrySetup(); | ||
| } catch (error) { | ||
| console.error(error); | ||
| process.exit(1); | ||
| } | ||
| await registrySetup({ daemonize: true }); | ||
| // Leave Verdaccio running for later CI steps (e.g. pnpm install). Detached stdio so this process can exit cleanly. | ||
| registryRelease(); | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
| run(); | ||
| run().catch(error => { | ||
| console.error(error); | ||
| process.exit(1); | ||
| }); |
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,50 +1,142 @@ | ||
| /* 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. | ||
| * Use for `prepare.ts` so `yarn test:prepare` can exit while the registry keeps running. | ||
| * (Inherited stdio otherwise keeps the parent process tied to the child on many systems.) | ||
| */ | ||
| 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', | ||
| }); | ||
| } | ||
|
|
||
| publishPackages(); | ||
| async function stopVerdaccioChild(): Promise<void> { | ||
| const child = verdaccioChild; | ||
| verdaccioChild = undefined; | ||
| if (!child || child.killed) { | ||
| return; | ||
| } | ||
| child.kill('SIGTERM'); | ||
| await new Promise<void>(resolve => { | ||
| child.once('exit', () => resolve()); | ||
| setTimeout(resolve, 5000); | ||
| }); | ||
|
sentry[bot] marked this conversation as resolved.
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| 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 }); | ||
|
|
||
| // 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; | ||
| } | ||
| }); | ||
|
|
||
| console.log(''); | ||
| console.log(''); | ||
| } | ||
|
|
||
| /** | ||
| * Detach the Verdaccio child so `ts-node` can exit while the registry keeps running | ||
| * (e.g. CI: `yarn test:prepare` then `pnpm test:build` in later steps). | ||
| */ | ||
| export function registryRelease(): void { | ||
| const child = verdaccioChild; | ||
| verdaccioChild = undefined; | ||
| if (child && !child.killed) { | ||
| child.unref(); | ||
| } | ||
| } | ||
|
|
||
| export async function registryCleanup(): Promise<void> { | ||
| await stopVerdaccioChild(); | ||
| killStrayVerdaccioRunner(); | ||
| const pidFile = path.join(__dirname, 'verdaccio-config', '.verdaccio.pid'); | ||
| try { | ||
| fs.unlinkSync(pidFile); | ||
| } catch { | ||
| // File may not exist | ||
| } | ||
| } | ||
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
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,24 @@ | ||
| /* 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 { | ||
| const server = await runServer(configPath, { listenArg: String(port) }); | ||
| await new Promise((resolve, reject) => { | ||
| server.once('error', reject); | ||
| server.listen(port, '127.0.0.1', () => resolve()); | ||
| }); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } catch (err) { | ||
| console.error(err); | ||
| process.exit(1); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
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.