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 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
79 changes: 73 additions & 6 deletions __tests__/core/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,83 @@ 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 and tag globs', async () => {
runner.addJourney(new Journey({ name: 'j1' }, noop));
runner.addJourney(new Journey({ name: 'tagj2', tags: ['j2'] }, noop));
expect(
await runner.run({
wsEndpoint,
outfd: fs.openSync(dest, 'w'),
match: 'j*',
})
).toEqual({
j1: { status: 'succeeded' },
tagj2: { status: 'succeeded' },
});
});

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

it('run api - support multiple tags', async () => {
runner.addJourney(new Journey({ name: 'j1', tags: ['hello:foo'] }, noop));
runner.addJourney(new Journey({ name: 'j2', tags: ['hello:bar'] }, noop));
runner.addJourney(new Journey({ name: 'j3', tags: ['hello:baz'] }, noop));
expect(
await runner.run({
wsEndpoint,
outfd: fs.openSync(dest, 'w'),
tags: ['hello:b*'],
})
).toEqual({
j2: { status: 'succeeded' },
j3: { status: 'succeeded' },
});
});

it('run api - support negation tags', async () => {
runner.addJourney(new Journey({ name: 'j1', tags: ['hello:foo'] }, noop));
runner.addJourney(new Journey({ name: 'j2', tags: ['hello:bar'] }, noop));
runner.addJourney(new Journey({ name: 'j3', tags: ['hello:baz'] }, noop));
expect(
await runner.run({
wsEndpoint,
outfd: fs.openSync(dest, 'w'),
tags: ['!hello:b*'],
})
).toEqual({
j1: { status: 'succeeded' },
});
});

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', tags: ['tag1', 'tag2'] };
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
23 changes: 23 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 micromatch, { isMatch } from 'micromatch';
import { Step } from './step';
import { VoidCallback, HooksCallback, Params } from '../common_types';

export type JourneyOptions = {
name: string;
id?: string;
tags?: string[];
};

type HookType = 'before' | 'after';
Expand All @@ -45,13 +47,15 @@ export type JourneyCallback = (options: {
export class Journey {
name: string;
id?: string;
tags?: string[];
callback: JourneyCallback;
steps: Step[] = [];
hooks: Hooks = { before: [], after: [] };

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

Expand All @@ -64,4 +68,23 @@ export class Journey {
addHook(type: HookType, callback: HooksCallback) {
this.hooks[type].push(callback);
}
/**
* Matches journeys based on the provided args. Proitize tags over match
* - tags pattern that matches only tags
* - match pattern that matches both name and tags
*/
isMatch(matchPattern: string, tagsPattern: Array<string>) {
if (tagsPattern) {
return this.tagsMatch(tagsPattern);
}
if (matchPattern) {
return isMatch(this.name, matchPattern) || this.tagsMatch(matchPattern);
}
return true;
}

tagsMatch(pattern) {
const matchess = micromatch(this.tags || ['*'], pattern);
return matchess.length > 0;
}
}
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 or tags 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,
tags: journey.tags,
status: type === 'journey/end' ? status : undefined,
};
}
Expand Down