Skip to content

Commit

Permalink
only filter by esVersion once
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Jan 19, 2022
1 parent 06d770d commit de23de1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Test from 'mocha/lib/test';
import { filterSuites } from './filter_suites';
import { EsVersion } from '../es_version';

function setup({ include, exclude, esVersion = '8.0.0' }) {
function setup({ include, exclude, esVersion }) {
return new Promise((resolve) => {
const history = [];

Expand Down Expand Up @@ -73,7 +73,7 @@ function setup({ include, exclude, esVersion = '8.0.0' }) {
mocha,
include,
exclude,
esVersion: new EsVersion(esVersion),
esVersion,
});

mocha.run();
Expand All @@ -88,7 +88,6 @@ it('only runs hooks of parents and tests in level1a', async () => {

expect(history).toMatchInlineSnapshot(`
Array [
"info: Only running suites which are compatible with ES version 8.0.0",
"info: Only running suites (and their sub-suites) if they include the tag(s): [ 'level1a' ]",
"suite: ",
"suite: level 1",
Expand All @@ -108,7 +107,6 @@ it('only runs hooks of parents and tests in level1b', async () => {

expect(history).toMatchInlineSnapshot(`
Array [
"info: Only running suites which are compatible with ES version 8.0.0",
"info: Only running suites (and their sub-suites) if they include the tag(s): [ 'level1b' ]",
"suite: ",
"suite: level 1",
Expand All @@ -128,7 +126,6 @@ it('only runs hooks of parents and tests in level1a and level1b', async () => {

expect(history).toMatchInlineSnapshot(`
Array [
"info: Only running suites which are compatible with ES version 8.0.0",
"info: Only running suites (and their sub-suites) if they include the tag(s): [ 'level1a', 'level1b' ]",
"suite: ",
"suite: level 1",
Expand All @@ -152,7 +149,6 @@ it('only runs level1a if including level1 and excluding level1b', async () => {

expect(history).toMatchInlineSnapshot(`
Array [
"info: Only running suites which are compatible with ES version 8.0.0",
"info: Only running suites (and their sub-suites) if they include the tag(s): [ 'level1' ]",
"info: Filtering out any suites that include the tag(s): [ 'level1b' ]",
"suite: ",
Expand All @@ -173,7 +169,6 @@ it('only runs level1b if including level1 and excluding level1a', async () => {

expect(history).toMatchInlineSnapshot(`
Array [
"info: Only running suites which are compatible with ES version 8.0.0",
"info: Only running suites (and their sub-suites) if they include the tag(s): [ 'level1' ]",
"info: Filtering out any suites that include the tag(s): [ 'level1a' ]",
"suite: ",
Expand All @@ -194,7 +189,6 @@ it('only runs level2 if excluding level1', async () => {

expect(history).toMatchInlineSnapshot(`
Array [
"info: Only running suites which are compatible with ES version 8.0.0",
"info: Filtering out any suites that include the tag(s): [ 'level1' ]",
"suite: ",
"suite: level 2",
Expand All @@ -213,7 +207,6 @@ it('does nothing if everything excluded', async () => {

expect(history).toMatchInlineSnapshot(`
Array [
"info: Only running suites which are compatible with ES version 8.0.0",
"info: Filtering out any suites that include the tag(s): [ 'level1', 'level2a' ]",
]
`);
Expand All @@ -223,7 +216,7 @@ it(`excludes tests which don't meet the esVersionRequirement`, async () => {
const { history } = await setup({
include: [],
exclude: [],
esVersion: '9.0.0',
esVersion: new EsVersion('9.0.0'),
});

expect(history).toMatchInlineSnapshot(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface Options {
mocha: any;
include: string[];
exclude: string[];
esVersion: EsVersion;
esVersion?: EsVersion;
}

/**
Expand All @@ -42,24 +42,28 @@ export function filterSuites({ log, mocha, include, exclude, esVersion }: Option
const collectTests = (suite: SuiteInternal): Test[] =>
suite.suites.reduce((acc, s) => acc.concat(collectTests(s)), suite.tests);

// traverse the test graph and exclude any tests which don't meet their esVersionRequirement
log.info('Only running suites which are compatible with ES version', esVersion.toString());
(function recurse(parentSuite: SuiteInternal) {
const children = parentSuite.suites;
parentSuite.suites = [];

const meetsEsVersionRequirement = (suite: SuiteInternal) =>
!suite._esVersionRequirement || esVersion.matchRange(suite._esVersionRequirement);

for (const child of children) {
if (meetsEsVersionRequirement(child)) {
parentSuite.suites.push(child);
recurse(child);
} else {
mocha.testsExcludedByEsVersion = mocha.testsExcludedByEsVersion.concat(collectTests(child));
if (esVersion) {
// traverse the test graph and exclude any tests which don't meet their esVersionRequirement
log.info('Only running suites which are compatible with ES version', esVersion.toString());
(function recurse(parentSuite: SuiteInternal) {
const children = parentSuite.suites;
parentSuite.suites = [];

const meetsEsVersionRequirement = (suite: SuiteInternal) =>
!suite._esVersionRequirement || esVersion.matchRange(suite._esVersionRequirement);

for (const child of children) {
if (meetsEsVersionRequirement(child)) {
parentSuite.suites.push(child);
recurse(child);
} else {
mocha.testsExcludedByEsVersion = mocha.testsExcludedByEsVersion.concat(
collectTests(child)
);
}
}
}
})(mocha.suite);
})(mocha.suite);
}

// if include tags were provided, filter the tree once to
// only include branches that are included at some point
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ export async function setupMocha(lifecycle, log, config, providers, esVersion) {
// valiate that there aren't any tests in multiple ciGroups
validateCiGroupTags(log, mocha);

filterSuites({
log,
mocha,
include: [],
exclude: [],
esVersion,
});

// Each suite has a tag that is the path relative to the root of the repo
// So we just need to take input paths, make them relative to the root, and use them as tags
// Also, this is a separate filterSuitesByTags() call so that the test suites will be filtered first by
Expand All @@ -60,15 +68,13 @@ export async function setupMocha(lifecycle, log, config, providers, esVersion) {
mocha,
include: config.get('suiteFiles.include').map((file) => relative(REPO_ROOT, file)),
exclude: config.get('suiteFiles.exclude').map((file) => relative(REPO_ROOT, file)),
esVersion,
});

filterSuites({
log,
mocha,
include: config.get('suiteTags.include').map((tag) => tag.replace(/-\d+$/, '')),
exclude: config.get('suiteTags.exclude').map((tag) => tag.replace(/-\d+$/, '')),
esVersion,
});

return mocha;
Expand Down

0 comments on commit de23de1

Please sign in to comment.