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 filtering by tags and names #300

Merged
merged 5 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
47 changes: 41 additions & 6 deletions __tests__/core/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,19 +265,54 @@ describe('runner', () => {
});
});

it('run api - only runs specified journeyName', async () => {
it('run api - match journey name explict', async () => {
runner.addJourney(new Journey({ name: 'j1' }, noop));
runner.addJourney(new Journey({ name: 'j2' }, noop));
const result = await runner.run({
wsEndpoint,
outfd: fs.openSync(dest, 'w'),
journeyName: 'j2',
expect(
await runner.run({
wsEndpoint,
outfd: fs.openSync(dest, 'w'),
match: 'j2',
})
).toEqual({
j2: { status: 'succeeded' },
});
expect(result).toEqual({
});

it('run api - match journey name globs', async () => {
runner.addJourney(new Journey({ name: 'j1' }, noop));
runner.addJourney(new Journey({ name: 'j2' }, noop));
expect(
await runner.run({
wsEndpoint,
outfd: fs.openSync(dest, 'w'),
match: 'j*',
})
).toEqual({
j1: { status: 'succeeded' },
j2: { status: 'succeeded' },
});
});

it('run api - match journey tags globs', async () => {
runner.addJourney(new Journey({ name: 'j1', tag: 'foo' }, noop));
runner.addJourney(new Journey({ name: 'j2', tag: 'bar' }, noop));
runner.addJourney(new Journey({ name: 'j3', tag: 'foo:test' }, noop));
runner.addJourney(new Journey({ name: 'j4', tag: 'baz' }, noop));
runner.addJourney(new Journey({ name: 'h1', tag: 'foo' }, noop));
expect(
await runner.run({
wsEndpoint,
outfd: fs.openSync(dest, 'w'),
tags: ['foo*'],
match: 'j*',
})
).toEqual({
j1: { status: 'succeeded' },
j3: { status: 'succeeded' },
});
});

it('run api - accumulate failed journeys', async () => {
runner.addJourney(new Journey({ name: 'j1' }, noop));
const j2 = new Journey({ name: 'j2' }, noop);
Expand Down
2 changes: 1 addition & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('Run', () => {
filmstrips: false,
trace: false,
dryRun: true,
journeyName: 'There and Back Again',
match: 'check*',
network: true,
pauseOnError: true,
reporter: 'json',
Expand Down
15 changes: 15 additions & 0 deletions __tests__/reporters/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ describe('json reporter', () => {
expect(journeyEnd.error).toEqual(helpers.formatError(myErr));
});

it('writes full journey info if present', async () => {
const journeyOpts = { name: 'name', id: 'id', tag: 'tag' };
runner.emit('journey:end', {
journey: journey(journeyOpts, () => {}),
start: 0,
end: 1,
status: 'skipped',
});

const journeyEnd = (await readAndCloseStreamJson()).find(
json => json.type == 'journey/end'
);
expect(journeyEnd.journey).toEqual({ ...journeyOpts, status: 'skipped' });
});

it('captures number of journeys as metadata event', async () => {
runner.emit('start', {
numJourneys: 10,
Expand Down
25 changes: 17 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"commander": "^7.0.0",
"http-proxy": "^1.18.1",
"kleur": "^4.1.3",
"micromatch": "^4.0.4",
"playwright-chromium": "=1.11.0",
"sharp": "^0.28.3",
"snakecase-keys": "^3.2.1",
Expand All @@ -51,6 +52,7 @@
},
"devDependencies": {
"@types/jest": "^26.0.19",
"@types/micromatch": "^4.0.1",
"@types/node": "^14.14.14",
"@types/sharp": "^0.28.2",
"@typescript-eslint/eslint-plugin": "^3.10.1",
Expand Down
3 changes: 2 additions & 1 deletion src/common_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ export type CliArgs = {
filmstrips?: boolean;
trace?: boolean;
dryRun?: boolean;
journeyName?: string;
network?: boolean;
pauseOnError?: boolean;
reporter?: Reporters;
Expand All @@ -134,6 +133,8 @@ export type CliArgs = {
json?: boolean;
pattern?: string;
inline: boolean;
match?: string;
tags?: Array<string>;
require: Array<string>;
debug?: boolean;
suiteParams?: string;
Expand Down
9 changes: 4 additions & 5 deletions src/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/

import { once, EventEmitter } from 'events';
import { join } from 'path';
import { mkdirSync, rmdirSync, writeFileSync } from 'fs';
import { Journey } from '../dsl/journey';
import { Step } from '../dsl/step';
import { reporters, Reporter } from '../reporters';
Expand All @@ -46,8 +48,6 @@ import { PluginManager } from '../plugins';
import { PerformanceManager, Metrics } from '../plugins';
import { Driver, Gatherer } from './gatherer';
import { log } from './logger';
import { mkdirSync, rmdirSync, writeFileSync } from 'fs';
import { join } from 'path';

export type RunOptions = Omit<
CliArgs,
Expand Down Expand Up @@ -369,17 +369,16 @@ export default class Runner extends EventEmitter {
env: options.environment,
params: options.params,
});
const { dryRun, match, tags } = options;
for (const journey of this.journeys) {
const { dryRun, journeyName } = options;
/**
* Used by heartbeat to gather all registered journeys
*/
if (dryRun) {
this.emit('journey:register', { journey });
continue;
}
// TODO: Replace functionality with journey.only
if (journeyName && journey.name != journeyName) {
if (!journey.isMatch(match, tags)) {
continue;
}
const journeyResult = await this.runJourney(journey, options);
Expand Down
10 changes: 10 additions & 0 deletions src/dsl/journey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
*/

import { Browser, Page, BrowserContext, CDPSession } from 'playwright-chromium';
import { contains, isMatch } from 'micromatch';
import { Step } from './step';
import { VoidCallback, HooksCallback, Params } from '../common_types';

export type JourneyOptions = {
name: string;
id?: string;
tag?: string;
};

type HookType = 'before' | 'after';
Expand All @@ -45,13 +47,15 @@ export type JourneyCallback = (options: {
export class Journey {
name: string;
id?: string;
tag?: string;
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
callback: JourneyCallback;
steps: Step[] = [];
hooks: Hooks = { before: [], after: [] };

constructor(options: JourneyOptions, callback: JourneyCallback) {
this.name = options.name;
this.id = options.id;
this.tag = options.tag;
this.callback = callback;
}

Expand All @@ -64,4 +68,10 @@ export class Journey {
addHook(type: HookType, callback: HooksCallback) {
this.hooks[type].push(callback);
}

isMatch(namePattern = '**', tagsPattern = ['**']) {
return (
contains(this.name, namePattern) && isMatch(this.tag || '**', tagsPattern)
);
}
}
9 changes: 8 additions & 1 deletion src/parse_args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ program
'--dry-run',
"don't actually execute anything, report only registered journeys"
)
.option('--journey-name <name>', 'only run the journey with the given name')
.option(
'--match <name>',
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
'run only journeys with a name that matches the glob'
)
.option(
'--tags <name...>',
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
'run only journeys with the given tag(s), or globs'
)
.option(
'--outfd <fd>',
'specify a file descriptor for logs. Default is stdout',
Expand Down
1 change: 1 addition & 0 deletions src/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ function journeyInfo(
return {
name: journey.name,
id: journey.id,
tag: journey.tag,
status: type === 'journey/end' ? status : undefined,
};
}
Expand Down