-
-
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
Open
mydea
wants to merge
12
commits into
develop
Choose a base branch
from
fn/node-verdaccio
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all 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 was deleted.
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
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,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.
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** 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(); | ||
| } | ||
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,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.
|
||
| } catch (err) { | ||
| console.error(err); | ||
| 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
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.