Skip to content

Commit 68a76a7

Browse files
authored
[FTR] Add support for --include and --exclude files via tags (#60123) (#60383)
1 parent 489d112 commit 68a76a7

File tree

9 files changed

+117
-35
lines changed

9 files changed

+117
-35
lines changed

packages/kbn-test/src/functional_test_runner/cli.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ export function runFtrCli() {
4848
kbnTestServer: {
4949
installDir: parseInstallDir(flags),
5050
},
51+
suiteFiles: {
52+
include: toArray(flags.include as string | string[]).map(makeAbsolutePath),
53+
exclude: toArray(flags.exclude as string | string[]).map(makeAbsolutePath),
54+
},
5155
suiteTags: {
5256
include: toArray(flags['include-tag'] as string | string[]),
5357
exclude: toArray(flags['exclude-tag'] as string | string[]),
5458
},
5559
updateBaselines: flags.updateBaselines,
56-
excludeTestFiles: flags.exclude || undefined,
5760
}
5861
);
5962

@@ -96,7 +99,15 @@ export function runFtrCli() {
9699
},
97100
{
98101
flags: {
99-
string: ['config', 'grep', 'exclude', 'include-tag', 'exclude-tag', 'kibana-install-dir'],
102+
string: [
103+
'config',
104+
'grep',
105+
'include',
106+
'exclude',
107+
'include-tag',
108+
'exclude-tag',
109+
'kibana-install-dir',
110+
],
100111
boolean: ['bail', 'invert', 'test-stats', 'updateBaselines'],
101112
default: {
102113
config: 'test/functional/config.js',
@@ -107,7 +118,8 @@ export function runFtrCli() {
107118
--bail stop tests after the first failure
108119
--grep <pattern> pattern used to select which tests to run
109120
--invert invert grep to exclude tests
110-
--exclude=file path to a test file that should not be loaded
121+
--include=file a test file to be included, pass multiple times for multiple files
122+
--exclude=file a test file to be excluded, pass multiple times for multiple files
111123
--include-tag=tag a tag to be included, pass multiple times for multiple tags
112124
--exclude-tag=tag a tag to be excluded, pass multiple times for multiple tags
113125
--test-stats print the number of tests (included and excluded) to STDERR

packages/kbn-test/src/functional_test_runner/lib/config/schema.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,16 @@ export const schema = Joi.object()
6464
testFiles: Joi.array().items(Joi.string()),
6565
testRunner: Joi.func(),
6666

67-
excludeTestFiles: Joi.array()
68-
.items(Joi.string())
69-
.default([]),
67+
suiteFiles: Joi.object()
68+
.keys({
69+
include: Joi.array()
70+
.items(Joi.string())
71+
.default([]),
72+
exclude: Joi.array()
73+
.items(Joi.string())
74+
.default([]),
75+
})
76+
.default(),
7077

7178
suiteTags: Joi.object()
7279
.keys({

packages/kbn-test/src/functional_test_runner/lib/mocha/decorate_mocha_ui.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
19+
import { relative } from 'path';
20+
import { REPO_ROOT } from '@kbn/dev-utils';
2021
import { createAssignmentProxy } from './assignment_proxy';
2122
import { wrapFunction } from './wrap_function';
2223
import { wrapRunnableArgs } from './wrap_runnable_args';
@@ -65,6 +66,10 @@ export function decorateMochaUi(lifecycle, context) {
6566
this._tags = [].concat(this._tags || [], tags);
6667
};
6768

69+
const relativeFilePath = relative(REPO_ROOT, this.file);
70+
this.tags(relativeFilePath);
71+
this.suiteTag = relativeFilePath; // The tag that uniquely targets this suite/file
72+
6873
provider.call(this);
6974

7075
after(async () => {

packages/kbn-test/src/functional_test_runner/lib/mocha/load_test_files.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,12 @@ import { decorateMochaUi } from './decorate_mocha_ui';
3131
* @param {String} path
3232
* @return {undefined} - mutates mocha, no return value
3333
*/
34-
export const loadTestFiles = ({
35-
mocha,
36-
log,
37-
lifecycle,
38-
providers,
39-
paths,
40-
excludePaths,
41-
updateBaselines,
42-
}) => {
43-
const pendingExcludes = new Set(excludePaths.slice(0));
44-
34+
export const loadTestFiles = ({ mocha, log, lifecycle, providers, paths, updateBaselines }) => {
4535
const innerLoadTestFile = path => {
4636
if (typeof path !== 'string' || !isAbsolute(path)) {
4737
throw new TypeError('loadTestFile() only accepts absolute paths');
4838
}
4939

50-
if (pendingExcludes.has(path)) {
51-
pendingExcludes.delete(path);
52-
log.warning('Skipping test file %s', path);
53-
return;
54-
}
55-
5640
loadTracer(path, `testFile[${path}]`, () => {
5741
log.verbose('Loading test file %s', path);
5842

@@ -94,13 +78,4 @@ export const loadTestFiles = ({
9478
};
9579

9680
paths.forEach(innerLoadTestFile);
97-
98-
if (pendingExcludes.size) {
99-
throw new Error(
100-
`After loading all test files some exclude paths were not consumed:${[
101-
'',
102-
...pendingExcludes,
103-
].join('\n -')}`
104-
);
105-
}
10681
};

packages/kbn-test/src/functional_test_runner/lib/mocha/setup_mocha.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919

2020
import Mocha from 'mocha';
21+
import { relative } from 'path';
22+
import { REPO_ROOT } from '@kbn/dev-utils';
2123

2224
import { loadTestFiles } from './load_test_files';
2325
import { filterSuitesByTags } from './filter_suites_by_tags';
@@ -50,10 +52,20 @@ export async function setupMocha(lifecycle, log, config, providers) {
5052
lifecycle,
5153
providers,
5254
paths: config.get('testFiles'),
53-
excludePaths: config.get('excludeTestFiles'),
5455
updateBaselines: config.get('updateBaselines'),
5556
});
5657

58+
// Each suite has a tag that is the path relative to the root of the repo
59+
// So we just need to take input paths, make them relative to the root, and use them as tags
60+
// Also, this is a separate filterSuitesByTags() call so that the test suites will be filtered first by
61+
// files, then by tags. This way, you can target tags (like smoke) in a specific file.
62+
filterSuitesByTags({
63+
log,
64+
mocha,
65+
include: config.get('suiteFiles.include').map(file => relative(REPO_ROOT, file)),
66+
exclude: config.get('suiteFiles.exclude').map(file => relative(REPO_ROOT, file)),
67+
});
68+
5769
filterSuitesByTags({
5870
log,
5971
mocha,

packages/kbn-test/src/functional_tests/cli/run_tests/__snapshots__/args.test.js.snap

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/kbn-test/src/functional_tests/cli/run_tests/__snapshots__/cli.test.js.snap

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/kbn-test/src/functional_tests/cli/run_tests/args.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ const options = {
4646
updateBaselines: {
4747
desc: 'Replace baseline screenshots with whatever is generated from the test.',
4848
},
49+
include: {
50+
arg: '<file>',
51+
desc: 'Files that must included to be run, can be included multiple times.',
52+
},
53+
exclude: {
54+
arg: '<file>',
55+
desc: 'Files that must NOT be included to be run, can be included multiple times.',
56+
},
4957
'include-tag': {
5058
arg: '<tag>',
5159
desc: 'Tags that suites must include to be run, can be included multiple times.',
@@ -115,6 +123,13 @@ export function processOptions(userOptions, defaultConfigPaths) {
115123
delete userOptions['kibana-install-dir'];
116124
}
117125

126+
userOptions.suiteFiles = {
127+
include: [].concat(userOptions.include || []),
128+
exclude: [].concat(userOptions.exclude || []),
129+
};
130+
delete userOptions.include;
131+
delete userOptions.exclude;
132+
118133
userOptions.suiteTags = {
119134
include: [].concat(userOptions['include-tag'] || []),
120135
exclude: [].concat(userOptions['exclude-tag'] || []),

packages/kbn-test/src/functional_tests/lib/run_ftr.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { CliError } from './run_cli';
2222

2323
async function createFtr({
2424
configPath,
25-
options: { installDir, log, bail, grep, updateBaselines, suiteTags },
25+
options: { installDir, log, bail, grep, updateBaselines, suiteFiles, suiteTags },
2626
}) {
2727
const config = await readConfigFile(log, configPath);
2828

@@ -37,6 +37,10 @@ async function createFtr({
3737
installDir,
3838
},
3939
updateBaselines,
40+
suiteFiles: {
41+
include: [...suiteFiles.include, ...config.get('suiteFiles.include')],
42+
exclude: [...suiteFiles.exclude, ...config.get('suiteFiles.exclude')],
43+
},
4044
suiteTags: {
4145
include: [...suiteTags.include, ...config.get('suiteTags.include')],
4246
exclude: [...suiteTags.exclude, ...config.get('suiteTags.exclude')],

0 commit comments

Comments
 (0)