This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[jest] refactor config check (elastic#135960)
- Loading branch information
Spencer
committed
Jul 11, 2022
1 parent
a9f1f89
commit 4d47a9d
Showing
10 changed files
with
201 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import Fs from 'fs'; | ||
import Path from 'path'; | ||
|
||
import execa from 'execa'; | ||
import minimatch from 'minimatch'; | ||
import { REPO_ROOT } from '@kbn/utils'; | ||
|
||
// @ts-expect-error jest-preset is necessarily a JS file | ||
import { testMatch } from '../../../jest-preset'; | ||
|
||
const UNIT_CONFIG_NAME = 'jest.config.js'; | ||
const INTEGRATION_CONFIG_NAME = 'jest.integration.config.js'; | ||
|
||
export async function getAllJestPaths() { | ||
const proc = await execa('git', ['ls-files', '-comt', '--exclude-standard'], { | ||
cwd: REPO_ROOT, | ||
stdio: ['ignore', 'pipe', 'pipe'], | ||
buffer: true, | ||
}); | ||
|
||
const testsRe = (testMatch as string[]).map((p) => minimatch.makeRe(p)); | ||
const classify = (rel: string) => { | ||
if (testsRe.some((re) => re.test(rel))) { | ||
return 'test' as const; | ||
} | ||
|
||
const basename = Path.basename(rel); | ||
return basename === UNIT_CONFIG_NAME || basename === INTEGRATION_CONFIG_NAME | ||
? ('config' as const) | ||
: undefined; | ||
}; | ||
|
||
const tests = new Set<string>(); | ||
const configs = new Set<string>(); | ||
|
||
for (const line of proc.stdout.split('\n').map((l) => l.trim())) { | ||
if (!line) { | ||
continue; | ||
} | ||
|
||
const rel = line.slice(2); // trim the single char status from the line | ||
const type = classify(rel); | ||
|
||
if (!type) { | ||
continue; | ||
} | ||
|
||
const set = type === 'test' ? tests : configs; | ||
const abs = Path.resolve(REPO_ROOT, rel); | ||
|
||
if (line.startsWith('C ')) { | ||
// this line indicates that the previous path is changed in the working tree, so we need to determine if | ||
// it was deleted, and if so, remove it from the set we added it to | ||
if (!Fs.existsSync(abs)) { | ||
set.delete(abs); | ||
} | ||
} else { | ||
set.add(abs); | ||
} | ||
} | ||
|
||
return { | ||
tests, | ||
configs, | ||
}; | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/kbn-test/src/jest/configs/get_tests_for_config_paths.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { readConfig } from 'jest-config'; | ||
import { createContext } from 'jest-runtime'; | ||
import { SearchSource } from 'jest'; | ||
import { asyncMapWithLimit } from '@kbn/std'; | ||
|
||
const EMPTY_ARGV = { | ||
$0: '', | ||
_: [], | ||
}; | ||
|
||
const NO_WARNINGS_CONSOLE = { | ||
...console, | ||
warn() { | ||
// ignore haste-map warnings | ||
}, | ||
}; | ||
|
||
export interface TestsForConfigPath { | ||
path: string; | ||
testPaths: Set<string>; | ||
} | ||
|
||
export async function getTestsForConfigPaths( | ||
configPaths: Iterable<string> | ||
): Promise<TestsForConfigPath[]> { | ||
return await asyncMapWithLimit(configPaths, 60, async (path) => { | ||
const config = await readConfig(EMPTY_ARGV, path); | ||
const searchSource = new SearchSource( | ||
await createContext(config.projectConfig, { | ||
maxWorkers: 1, | ||
watchman: false, | ||
watch: false, | ||
console: NO_WARNINGS_CONSOLE, | ||
}) | ||
); | ||
|
||
const results = await searchSource.getTestPaths(config.globalConfig, undefined, undefined); | ||
|
||
return { | ||
path, | ||
testPaths: new Set(results.tests.map((t) => t.path)), | ||
}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters