Skip to content

Commit

Permalink
test(build-env): add unit tests for parseRegistryData
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 13, 2024
1 parent 2567641 commit 337c9cf
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 28 deletions.
50 changes: 22 additions & 28 deletions tooling/build-env/src/internal/verdaccio/verdaccio-registry.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(
/(?<proto>https?):\/\/(?<host>[^:]+):(?<port>\d+)/
);

Expand All @@ -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)}.`);
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
});
}
Original file line number Diff line number Diff line change
@@ -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'
);
});
});

0 comments on commit 337c9cf

Please sign in to comment.