Skip to content
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

feat: add support for --config option in push command #908

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions __tests__/push/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ describe('Push', () => {
return cli.stderr();
}

async function fakeProjectSetup(settings, monitor) {
async function fakeProjectSetup(
settings,
monitor,
filename = 'synthetics.config.ts'
) {
await writeFile(
join(PROJECT_DIR, 'synthetics.config.ts'),
join(PROJECT_DIR, filename),
`export default { monitor: ${JSON.stringify(
monitor
)}, project: ${JSON.stringify(settings)} }`
Expand Down Expand Up @@ -266,13 +270,38 @@ heartbeat.monitors:
journey('journey 2', () => monitor.use({ id: 'j2' }));`
);
const output = await runPush();
expect(output).toContain('Pushing monitors for project: test-project');
expect(output).toContain(
"Pushing monitors for 'test-project' project in kibana 'dummy' space"
);
expect(output).toContain('bundling 2 monitors');
expect(output).toContain('creating or updating 2 monitors');
expect(output).toContain(deleteProgress);
expect(output).toContain('✓ Pushed:');
await rm(testJourney, { force: true });
});

it('push journeys with --config', async () => {
const testJourney = join(PROJECT_DIR, 'test.journey.ts');
await writeFile(
testJourney,
`import {journey, monitor} from '../../../';
journey('journey 1', () => monitor.use({ id: 'j1' }));`
);
await fakeProjectSetup(
{ id: 'bar', space: 'dummy', url: server.PREFIX },
{ locations: ['test-loc'], schedule: 3 },
'synthetics.config.test.ts'
);
const output = await runPush([
...DEFAULT_ARGS,
'--config',
join(PROJECT_DIR, 'synthetics.config.test.ts'),
]);
expect(output).toContain(
"Pushing monitors for 'bar' project in kibana 'dummy' space"
);
await rm(testJourney, { force: true });
});
});
});
});
19 changes: 8 additions & 11 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,13 @@ import { installTransform } from './core/transform';
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const { name, version } = require('../package.json');

const { params, pattern, playwrightOpts, auth, authMandatory } =
const { params, pattern, playwrightOpts, auth, authMandatory, configOpt } =
getCommonCommandOpts();

program
.name(`npx ${name}`)
.usage('[options] [dir] [files] file')
.option(
'-c, --config <path>',
'configuration path (default: synthetics.config.js)'
)
.addOption(configOpt)
.addOption(pattern)
.addOption(params)
.option('--tags <name...>', 'run tests with a tag that matches the glob')
Expand Down Expand Up @@ -189,16 +186,17 @@ program
.addOption(pattern)
.addOption(params)
.addOption(playwrightOpts)
.action(async (cmdOpts: PushOptions) => {
.addOption(configOpt)
.action(async cmdOpts => {
cmdOpts = { ...cmdOpts, ...program.opts() };
const workDir = cwd();
const tearDown = await globalSetup({ inline: false, ...program.opts() }, [
const tearDown = await globalSetup({ inline: false, ...cmdOpts }, [
workDir,
]);
try {
const settings = await loadSettings();
const settings = await loadSettings(cmdOpts.config);
const options = (await normalizeOptions(
{
...program.opts(),
...settings,
...cmdOpts,
},
Expand Down Expand Up @@ -244,8 +242,7 @@ program
.addOption(auth)
.action(async (cmdOpts: LocationCmdOptions) => {
const revert = installTransform();
const url = cmdOpts.url ?? (await loadSettings(true))?.url;

const url = cmdOpts.url ?? (await loadSettings(null, true))?.url;
try {
if (url && cmdOpts.auth) {
const allLocations = await getLocations({
Expand Down
2 changes: 1 addition & 1 deletion src/common_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ type BaseArgs = {
params?: Params;
screenshots?: ScreenshotOptions;
dryRun?: boolean;
config?: string;
pattern?: string;
match?: string;
tags?: Array<string>;
Expand All @@ -219,7 +220,6 @@ type BaseArgs = {
};

export type CliArgs = BaseArgs & {
config?: string;
reporter?: BuiltInReporterName;
inline?: boolean;
require?: Array<string>;
Expand Down
6 changes: 6 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,17 @@ export function getCommonCommandOpts() {
.env('SYNTHETICS_API_KEY')
.makeOptionMandatory(true);

const configOpt = createOption(
'-c, --config <path>',
'configuration path (default: synthetics.config.js)'
);

return {
auth,
authMandatory,
params,
playwrightOpts,
pattern,
configOpt,
};
}
21 changes: 15 additions & 6 deletions src/push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export async function push(monitors: Monitor[], options: PushOptions) {
if (duplicates.size > 0) {
throw error(formatDuplicateError(duplicates));
}
progress(`Pushing monitors for project: ${options.id}`);
progress(
`Pushing monitors for '${options.id}' project in kibana '${options.space}' space`
);

/**
* Legacy API for kibana which does not support bulk operations
Expand Down Expand Up @@ -160,9 +162,12 @@ export function formatDuplicateError(monitors: Set<Monitor>) {

const INSTALLATION_HELP = `Run 'npx @elastic/synthetics init' to create project with default settings.`;

export async function loadSettings(ignoreMissing = false) {
export async function loadSettings(configPath, ignoreMissing = false) {
try {
const config = await readConfig(process.env['NODE_ENV'] || 'development');
const config = await readConfig(
process.env['NODE_ENV'] || 'development',
configPath
);
// Missing config file, fake throw to capture as missing file
if (Object.keys(config).length === 0) {
throw '';
Expand Down Expand Up @@ -216,9 +221,13 @@ ${reason}
${INSTALLATION_HELP}`);
}

async function overrideSettings(oldValue: string, newValue: string) {
async function overrideSettings(
configPath,
oldValue: string,
newValue: string
) {
const cwd = process.cwd();
const configPath = await findSyntheticsConfig(cwd, cwd);
configPath = configPath ?? (await findSyntheticsConfig(cwd, cwd));
if (!configPath) {
throw warn(`Unable to find synthetics config file: ${configPath}`);
}
Expand Down Expand Up @@ -256,7 +265,7 @@ export async function catchIncorrectSettings(
}
}
if (override) {
await overrideSettings(settings.id, options.id);
await overrideSettings(options.config, settings.id, options.id);
}
}

Expand Down
Loading