From de23de1c5c302c3423b456b73ebdfd1709f1d76c Mon Sep 17 00:00:00 2001 From: spalger Date: Tue, 18 Jan 2022 17:33:59 -0700 Subject: [PATCH] only filter by esVersion once --- .../lib/mocha/filter_suites.test.js | 13 ++---- .../lib/mocha/filter_suites.ts | 40 ++++++++++--------- .../lib/mocha/setup_mocha.js | 10 ++++- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/packages/kbn-test/src/functional_test_runner/lib/mocha/filter_suites.test.js b/packages/kbn-test/src/functional_test_runner/lib/mocha/filter_suites.test.js index d12a9ea485036..191503af123d0 100644 --- a/packages/kbn-test/src/functional_test_runner/lib/mocha/filter_suites.test.js +++ b/packages/kbn-test/src/functional_test_runner/lib/mocha/filter_suites.test.js @@ -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 = []; @@ -73,7 +73,7 @@ function setup({ include, exclude, esVersion = '8.0.0' }) { mocha, include, exclude, - esVersion: new EsVersion(esVersion), + esVersion, }); mocha.run(); @@ -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", @@ -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", @@ -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", @@ -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: ", @@ -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: ", @@ -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", @@ -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' ]", ] `); @@ -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(` diff --git a/packages/kbn-test/src/functional_test_runner/lib/mocha/filter_suites.ts b/packages/kbn-test/src/functional_test_runner/lib/mocha/filter_suites.ts index 148b074b82969..90bb3a894bc6c 100644 --- a/packages/kbn-test/src/functional_test_runner/lib/mocha/filter_suites.ts +++ b/packages/kbn-test/src/functional_test_runner/lib/mocha/filter_suites.ts @@ -21,7 +21,7 @@ interface Options { mocha: any; include: string[]; exclude: string[]; - esVersion: EsVersion; + esVersion?: EsVersion; } /** @@ -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 diff --git a/packages/kbn-test/src/functional_test_runner/lib/mocha/setup_mocha.js b/packages/kbn-test/src/functional_test_runner/lib/mocha/setup_mocha.js index 1e4841f9abd8f..8d88410cb2c1d 100644 --- a/packages/kbn-test/src/functional_test_runner/lib/mocha/setup_mocha.js +++ b/packages/kbn-test/src/functional_test_runner/lib/mocha/setup_mocha.js @@ -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 @@ -60,7 +68,6 @@ 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({ @@ -68,7 +75,6 @@ export async function setupMocha(lifecycle, log, config, providers, esVersion) { mocha, include: config.get('suiteTags.include').map((tag) => tag.replace(/-\d+$/, '')), exclude: config.get('suiteTags.exclude').map((tag) => tag.replace(/-\d+$/, '')), - esVersion, }); return mocha;