-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add test mapping script (#63757)
### What? This adds a script that first gets a list of changed files compared to our base branch (`canary`) in PRs and identifies related test files by looking them up from a `related-tests-manifest.json`, then runs them. ### Why? We would like to be able to run related E2E tests when certain files change, so we can test deployment stability. NOTE: We still want to run all E2E tests on PRs, but this script can be used to also test deployments for hot paths. Since these tests can take up to [3-4 hours](https://github.com/vercel/next.js/actions/workflows/test_e2e_deploy.yml), we aim to run a subset of these tests first. Closes NEXT-2947
- Loading branch information
1 parent
2670e1c
commit b93ead0
Showing
2 changed files
with
57 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* This script reads the 'test/realted-tests-manifest.json' file, which contains a list of test globs that maps source code files to test files. | ||
* The script is used to find all related test cases for a given code change. | ||
* Usage: `node scripts/run-related-test.mjs <path1> <path2> ...` | ||
*/ | ||
|
||
import { promisify } from 'node:util' | ||
import { exec as execOrg } from 'node:child_process' | ||
import { readFile } from 'node:fs/promises' | ||
import { join } from 'node:path' | ||
|
||
const exec = promisify(execOrg) | ||
|
||
/** | ||
* Get all changed files from git under the packages directory | ||
* @returns {Promise<string[]>} - List of changed files | ||
*/ | ||
async function getChangedFilesFromPackages(baseBranch = 'canary') { | ||
await exec('git config --global --add safe.directory /work') | ||
await exec(`git remote set-branches --add origin ${baseBranch}`) | ||
await exec(`git fetch origin ${baseBranch} --depth=20`) | ||
const { stdout } = await exec(`git diff --name-only ${baseBranch}`) | ||
return stdout | ||
.trim() | ||
.split('\n') | ||
.filter((line) => line.startsWith('packages/')) | ||
} | ||
|
||
export async function getRelatedTests(args = []) { | ||
const relatedTestsManifest = join( | ||
process.cwd(), | ||
'test', | ||
'related-tests-manifest.json' | ||
) | ||
/** @type {Record<string, string[]>} */ | ||
const manifest = JSON.parse( | ||
await readFile(relatedTestsManifest, 'utf-8').catch(() => '{}') | ||
) | ||
const tests = [] | ||
const paths = args.length ? args : await getChangedFilesFromPackages() | ||
for (const path of paths) { | ||
const relatedTestsKey = Object.keys(manifest).find((key) => | ||
path.startsWith(key) | ||
) | ||
if (relatedTestsKey) { | ||
const lines = manifest[relatedTestsKey] | ||
tests.push(...lines) | ||
} | ||
} | ||
|
||
return Array.from(new Set(tests)) | ||
} | ||
|
||
// console.log(await getRelatedTests(process.argv.slice(2))) |
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,3 @@ | ||
{ | ||
"packages/next/src/server": ["e2e/app-dir/ppr-*"] | ||
} |