From 337c9cffc9af9dd913ad4c70e0640208f29fb296 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 14 Sep 2024 00:30:04 +0300 Subject: [PATCH] test(build-env): add unit tests for parseRegistryData --- .../internal/verdaccio/verdaccio-registry.ts | 50 ++++++++--------- .../verdaccio/verdaccio-registry.unit-test.ts | 53 +++++++++++++++++++ 2 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 tooling/build-env/src/internal/verdaccio/verdaccio-registry.unit-test.ts diff --git a/tooling/build-env/src/internal/verdaccio/verdaccio-registry.ts b/tooling/build-env/src/internal/verdaccio/verdaccio-registry.ts index 0f781256..31e48329 100644 --- a/tooling/build-env/src/internal/verdaccio/verdaccio-registry.ts +++ b/tooling/build-env/src/internal/verdaccio/verdaccio-registry.ts @@ -1,19 +1,13 @@ -import { gray, bold, red } from 'ansis'; +import { bold, gray, red } from 'ansis'; import { join } from 'node:path'; -import { formatError, formatInfo } from '../utils/logging'; import { logger } from '@nx/devkit'; import { objectToCliArgs } from '../utils/terminal'; import { executeProcess } from '../utils/execute-process'; import { uniquePort } from '../utils/unique-port'; import { getEnvironmentsRoot } from '../../shared/setup'; +import { formatError, formatInfo } from '../utils/logging'; -export function logInfo(msg: string) { - formatInfo(msg, 'Verdaccio: '); -} - -export function logError(msg: string) { - formatError(msg, 'Verdaccio: '); -} +const VERDACCIO_TOKEN = 'Verdaccio: '; export type VerdaccioProcessResult = { protocol: string; @@ -31,10 +25,8 @@ export type RegistryResult = { }; export function parseRegistryData(stdout: string): VerdaccioProcessResult { - const output = stdout.toString(); - // Extract protocol, host, and port - const match = output.match( + const match = stdout.match( /(?https?):\/\/(?[^:]+):(?\d+)/ ); @@ -52,9 +44,7 @@ export function parseRegistryData(stdout: string): VerdaccioProcessResult { if (!host) { throw new Error(`Invalid host ${String(host)}.`); } - const port = !Number.isNaN(Number(match.groups['port'])) - ? Number(match.groups['port']) - : undefined; + const port = match.groups['port'] ? Number(match.groups['port']) : undefined; if (!port) { throw new Error(`Invalid port ${String(port)}.`); } @@ -135,20 +125,26 @@ export async function startVerdaccioServer({ try { childProcess?.kill(); } catch { - logError( - `Can't kill Verdaccio process with id: ${childProcess?.pid}` + logger.error( + formatError( + `Can't kill Verdaccio process with id: ${childProcess?.pid}`, + VERDACCIO_TOKEN + ) ); } }, }; - logInfo( - `Registry started on URL: ${bold( - result.registry.url - )}, ProcessID: ${bold(String(childProcess?.pid))}` + logger.info( + formatInfo( + `Registry started on URL: ${bold( + result.registry.url + )}, ProcessID: ${bold(String(childProcess?.pid))}`, + VERDACCIO_TOKEN + ) ); if (verbose) { - logInfo(''); + logger.info(formatInfo('', VERDACCIO_TOKEN)); console.table(result); } @@ -157,18 +153,16 @@ export async function startVerdaccioServer({ }, onStderr: (stderr: string) => { if (verbose) { - process.stdout.write( - `${red('>')} ${red(bold('Verdaccio'))} ${stderr}` - ); + process.stdout.write(formatInfo(stderr, VERDACCIO_TOKEN)); } }, }, }).catch((error) => { - logger.error(error); + logger.error(formatError(error, VERDACCIO_TOKEN)); reject(error); }); - }).catch((error: unknown) => { - logger.error(error); + }).catch((error) => { + logger.error(formatError(error, VERDACCIO_TOKEN)); throw error; }); } diff --git a/tooling/build-env/src/internal/verdaccio/verdaccio-registry.unit-test.ts b/tooling/build-env/src/internal/verdaccio/verdaccio-registry.unit-test.ts new file mode 100644 index 00000000..7ded88b2 --- /dev/null +++ b/tooling/build-env/src/internal/verdaccio/verdaccio-registry.unit-test.ts @@ -0,0 +1,53 @@ +import { describe, it, expect } from 'vitest'; +import { parseRegistryData } from './verdaccio-registry'; // Adjust import path + +describe('parseRegistryData', () => { + it('should correctly parse protocol host and port from stdout', () => { + const stdout = + 'warn --- http address - http://localhost:4873/ - verdaccio/5.31.1'; + const result = parseRegistryData(stdout); + + expect(result).toEqual({ + protocol: 'http', + host: 'localhost', + port: 4873, + url: 'http://localhost:4873', + }); + }); + + it('should correctly parse https protocol', () => { + const stdout = + 'warn --- http address - https://localhost:4873/ - verdaccio/5.31.1'; + const result = parseRegistryData(stdout); + + expect(result.protocol).toEqual('https'); + }); + + it('should throw an error if the protocol is invalid', () => { + const stdout = 'ftp://localhost:4873'; // Invalid protocol + expect(() => parseRegistryData(stdout)).toThrowError( + 'Could not parse registry data from stdout' + ); + }); + + it('should throw an error if the host is missing', () => { + const stdout = 'http://:4873'; // Missing host + expect(() => parseRegistryData(stdout)).toThrowError( + 'Could not parse registry data from stdout' + ); + }); + + it('should throw an error if the stdout is empty', () => { + const stdout = ''; // Empty output + expect(() => parseRegistryData(stdout)).toThrowError( + 'Could not parse registry data from stdout' + ); + }); + + it('should throw an error if the port is missing', () => { + const stdout = 'http://localhost:'; // Missing port + expect(() => parseRegistryData(stdout)).toThrowError( + 'Could not parse registry data from stdout' + ); + }); +});