mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00

* feat: (AppRunner) Mechanism to configure cli args and derive componentsjs vars from them implemented * fix: (AppRunner) tidying * fix: (AppRunner) tidying up * fix: (AppRunner) runCli method made sync * fix; (VarResolver) refactored to multiple files, and other stylistic fixes. * chore: (AppRunner) Uses builder pattern for yargs base arguments setup to enable better typescript inference * fix(AppRunner): refactoring AppRunner and VarResolver * fix(AppRunner): refactoring AppRunner promise handling * fix(AppRunner): verror dependency removal * fix: Simplify CLI error handling * feat: Use same config for both CLI and app instantiation * fix: Update typings and imports * feat: Split VariableResolver behaviour to 2 classes * feat: Move default value behaviour from CLI to ValueComputers * test: Add unit tests for new CLI classes * feat: Integrate new CLI configuration with all default configurations * feat: Add createApp function to AppRunner * docs: Update comments in CLI-related classes * fix: Various fixes and refactors Co-authored-by: damooo <damodara@protonmail.com>
84 lines
3.7 KiB
TypeScript
84 lines
3.7 KiB
TypeScript
import type { YargsArgOptions } from '../../../../src/init/cli/YargsCliExtractor';
|
|
import { YargsCliExtractor } from '../../../../src/init/cli/YargsCliExtractor';
|
|
|
|
const error = jest.spyOn(console, 'error').mockImplementation(jest.fn());
|
|
const log = jest.spyOn(console, 'log').mockImplementation(jest.fn());
|
|
const exit = jest.spyOn(process, 'exit').mockImplementation(jest.fn() as any);
|
|
describe('A YargsCliExtractor', (): void => {
|
|
const parameters: YargsArgOptions = {
|
|
baseUrl: { alias: 'b', requiresArg: true, type: 'string' },
|
|
port: { alias: 'p', requiresArg: true, type: 'number' },
|
|
};
|
|
let extractor: YargsCliExtractor;
|
|
|
|
beforeEach(async(): Promise<void> => {
|
|
extractor = new YargsCliExtractor(parameters);
|
|
});
|
|
|
|
afterEach(async(): Promise<void> => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('returns parsed results.', async(): Promise<void> => {
|
|
const argv = [ 'node', 'script', '-b', 'http://localhost:3000/', '-p', '3000' ];
|
|
await expect(extractor.handle(argv)).resolves.toEqual(expect.objectContaining({
|
|
baseUrl: 'http://localhost:3000/',
|
|
port: 3000,
|
|
}));
|
|
});
|
|
|
|
it('accepts full flags.', async(): Promise<void> => {
|
|
const argv = [ 'node', 'script', '--baseUrl', 'http://localhost:3000/', '--port', '3000' ];
|
|
await expect(extractor.handle(argv)).resolves.toEqual(expect.objectContaining({
|
|
baseUrl: 'http://localhost:3000/',
|
|
port: 3000,
|
|
}));
|
|
});
|
|
|
|
it('defaults to no parameters if none are provided.', async(): Promise<void> => {
|
|
extractor = new YargsCliExtractor();
|
|
const argv = [ 'node', 'script', '-b', 'http://localhost:3000/', '-p', '3000' ];
|
|
await expect(extractor.handle(argv)).resolves.toEqual(expect.objectContaining({}));
|
|
});
|
|
|
|
it('prints usage if defined.', async(): Promise<void> => {
|
|
extractor = new YargsCliExtractor(parameters, { usage: 'node ./bin/server.js [args]' });
|
|
const argv = [ 'node', 'script', '--help' ];
|
|
await extractor.handle(argv);
|
|
expect(exit).toHaveBeenCalledTimes(1);
|
|
expect(log).toHaveBeenCalledTimes(1);
|
|
expect(log).toHaveBeenLastCalledWith(expect.stringMatching(/^node \.\/bin\/server\.js \[args\]/u));
|
|
});
|
|
|
|
it('can error on undefined parameters.', async(): Promise<void> => {
|
|
extractor = new YargsCliExtractor(parameters, { strictMode: true });
|
|
const argv = [ 'node', 'script', '--unsupported' ];
|
|
await extractor.handle(argv);
|
|
expect(exit).toHaveBeenCalledTimes(1);
|
|
expect(error).toHaveBeenCalledWith('Unknown argument: unsupported');
|
|
});
|
|
|
|
it('can parse environment variables.', async(): Promise<void> => {
|
|
// While the code below does go into the corresponding values,
|
|
// yargs does not see the new environment variable for some reason.
|
|
// It does see all the env variables that were already in there
|
|
// (which can be tested by setting envVarPrefix to '').
|
|
// This can probably be fixed by changing jest setup to already load the custom env before loading the tests,
|
|
// but does not seem worth it just for this test.
|
|
const { env } = process;
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
process.env = { ...env, TEST_ENV_PORT: '3333' };
|
|
extractor = new YargsCliExtractor(parameters, { loadFromEnv: true, envVarPrefix: 'TEST_ENV' });
|
|
const argv = [ 'node', 'script', '-b', 'http://localhost:3333/' ];
|
|
await expect(extractor.handle(argv)).resolves.toEqual(expect.objectContaining({
|
|
baseUrl: 'http://localhost:3333/',
|
|
}));
|
|
process.env = env;
|
|
|
|
// This part is here for the case of envVarPrefix being defined
|
|
// since it doesn't make much sense to test it if the above doesn't work
|
|
extractor = new YargsCliExtractor(parameters, { loadFromEnv: true });
|
|
await extractor.handle(argv);
|
|
});
|
|
});
|