Skip to content

Commit

Permalink
fix: use command value instead of getters for dryRun and interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherPHolder committed Mar 6, 2024
1 parent 9bc03fe commit 5d75b2a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 37 deletions.
6 changes: 3 additions & 3 deletions packages/cli/src/lib/commands/collect/command-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { run } from '../../core/processing/behaviors';
import { collectRcJson } from '../init/processes/collect-rc-json';
import { startServerIfNeededAndExecute } from './utils/serve-command';
import { collectReports } from './processes/collect-reports';
import { CollectOptions } from './options';
import { CollectCommandOptions } from './options';

export async function runCollectCommand(argv: CollectOptions): Promise<void> {
export async function runCollectCommand(argv: CollectCommandOptions): Promise<void> {
const cfg = getCollectCommandOptionsFromArgv(argv);
logVerbose('Collect options: ', cfg);
await run([
collectRcJson,
(cfg: RcJson) =>
startServerIfNeededAndExecute(
() => collectReports(cfg, argv.openReport),
() => collectReports(cfg, argv),
cfg.collect
)
])(cfg);
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/lib/commands/collect/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { awaitServeStdout } from './awaitServeStdout';
import { dryRun } from './dryRun';
import { assertOptions } from '../../assert/options';
import { config } from './config';
import { GlobalCliOptions } from '../../../global/options';

export const persistOptions = {
outPath,
Expand All @@ -29,3 +30,4 @@ export const collectOptions = {
...assertOptions
} satisfies Record<string, Options>;
export type CollectOptions = InferredOptionTypes<typeof collectOptions>;
export type CollectCommandOptions = CollectOptions & GlobalCliOptions;
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { collectFlow, loadFlow } from '../utils/user-flow';
import { persistFlow } from '../utils/persist/persist-flow';
import { handleOpenFlowReports } from '../utils/persist/open-report';
import { RcJson } from '../../../types';
import { CollectCommandOptions } from '../options';

export async function collectReports(cfg: RcJson, openReport?: boolean): Promise<RcJson> {
export async function collectReports(cfg: RcJson, argv: CollectCommandOptions): Promise<RcJson> {

const { collect, persist, assert } = cfg;

Expand All @@ -16,7 +17,7 @@ export async function collectReports(cfg: RcJson, openReport?: boolean): Promise
...collect, ...persist, ...assert, dryRun: dryRun()
}, { ...provider, path })
.then((flow) => persistFlow(flow, { ...persist, ...collect }))
.then(handleOpenFlowReports(openReport))
.then(handleOpenFlowReports(argv))
.then(_ => cfg);
})
)(cfg);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as openReport from 'open';
import { get as dryRun } from '../../../../commands/collect/options/dryRun';
import { get as interactive } from '../../../../global/options/interactive';
import { logVerbose } from '../../../../core/loggin';
import { CollectCommandOptions } from '../../options';

export async function openFlowReports(fileNames: string[]): Promise<void> {
const htmlReport = fileNames.find(i => i.includes('.html'));
Expand All @@ -27,8 +26,8 @@ export async function openFlowReports(fileNames: string[]): Promise<void> {
return Promise.resolve(void 0);
}

export function handleOpenFlowReports(openReport?: boolean): typeof openFlowReports | undefined {
if (dryRun() || !openReport || !interactive()) {
export function handleOpenFlowReports({ dryRun, openReport, interactive}: CollectCommandOptions) {
if (dryRun || !openReport || !interactive) {
return;
}
return openFlowReports;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
import * as openReport from 'open';
import { handleOpenFlowReports, openFlowReports } from './open-report';
import { logVerbose } from '../../../../core/loggin';
import * as openReport from 'open';

import * as dryRun from '../../../../commands/collect/options/dryRun';
import * as interactive from '../../../../global/options/interactive';

jest.mock('../../../../commands/collect/options/dryRun');
jest.mock('../../../../global/options/interactive');
import { CollectCommandOptions } from '../../options';

describe('handleOpenFlowReport', () => {
beforeEach(() => {
jest.clearAllMocks();
})

it('should return the openFlowReport function if openReport, interactive and not dryRun', async () => {
const openReportsProcess = handleOpenFlowReports({
openReport: true,
interactive: true,
dryRun: false,
} as CollectCommandOptions);
expect(typeof openReportsProcess).toEqual("function");
});

it('should return undefined if openReport is false', () => {
const openReportsProcess = handleOpenFlowReports(false);
const openReportsProcess = handleOpenFlowReports({
openReport: false,
interactive: true,
dryRun: false,
} as CollectCommandOptions);
expect(openReportsProcess).toEqual(undefined);
});

it('should return undefined if dryRun is true', () => {
jest.spyOn(dryRun, 'get').mockReturnValue(true);
const openReportsProcess = handleOpenFlowReports(true);
const openReportsProcess = handleOpenFlowReports({
openReport: true,
interactive: true,
dryRun: true,
} as CollectCommandOptions);
expect(openReportsProcess).toEqual(undefined);
});

it('should return undefined if interactive is false', () => {
jest.spyOn(interactive, 'get').mockReturnValue(false);
const openReportsProcess = handleOpenFlowReports(true);
const openReportsProcess = handleOpenFlowReports({
openReport: true,
interactive: false,
dryRun: false,
} as CollectCommandOptions);
expect(openReportsProcess).toEqual(undefined);
});

it('should return the openFlowReport function if openReport, interactive and not dryRun', async () => {
jest.spyOn(interactive, 'get').mockReturnValue(true);
jest.spyOn(dryRun, 'get').mockReturnValue(false);
const openReportsProcess = handleOpenFlowReports(true);
expect(typeof openReportsProcess).toEqual("function");
});

});

jest.mock('open');
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/lib/global/options/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { InferredOptionTypes, Options } from 'yargs';

import { verbose, get as getVerbose } from './verbose';
import { rcPath, get as getRcPath } from '../rc-json/options/rc';
import { interactive, get as getInteractive } from './interactive';
import { Options } from 'yargs';

export const GLOBAL_OPTIONS_YARGS_CFG = {
verbose,
rcPath,
interactive
} satisfies Record<string, Options>;
export type GlobalCliOptions = InferredOptionTypes<typeof GLOBAL_OPTIONS_YARGS_CFG>;

export const globalOptions = {
getVerbose,
Expand Down
12 changes: 3 additions & 9 deletions packages/cli/src/lib/global/options/interactive.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { argv, Options } from 'yargs';
import { getEnvPreset } from '../../pre-set';

function getDefaultByCliMode(): boolean {
return getEnvPreset().interactive as boolean;
}
export const interactive = {
alias: 'i',
type: 'boolean',
description: 'When false questions are skipped with the values from the suggestions. This is useful for CI integrations.',
default: getDefaultByCliMode()
default: getEnvPreset().interactive,
} satisfies Options;

const defaultValue = interactive.default;

// We don't rely on yargs option normalization features as this can happen before cli bootstrap
export function get(): boolean {
const { interactive, i } = argv as any as {interactive?: boolean, i?: boolean};
return interactive !== undefined ? Boolean(interactive) : i !== undefined ? Boolean(i) : defaultValue;
const { interactive } = argv as any as { interactive: boolean };
return interactive;
}

0 comments on commit 5d75b2a

Please sign in to comment.