Skip to content

CHANGE @W-18396029@ Running tests against only affected packages #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2025

Conversation

jfeingold35
Copy link
Contributor

@jfeingold35 jfeingold35 commented May 1, 2025

This PR makes the following changes:

  • The verify-pr.yml job can now identify which specific packages are affected by the changes in the PR, and will only run the tests in those packages.

See this Github Action run from an experimental branch to see it only running tests for code-analyzer-regex-engine and code-analyzer-flow-engine, and see the checks for this PR itself for an example of it running on all packages because no specific package was changed.

@@ -7,6 +7,9 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[package.json]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got sick of fighting with my IDE about formatting on package.json files, so I just updated the editor config file about it.

@@ -72,6 +72,9 @@ jobs:
- name: Support long paths
run: git config --global core.longpaths true
- uses: actions/checkout@v4
with:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're still checking out the same ref, but we need to do it like the validate-packages job does where the history is pulled too.

Comment on lines +95 to +96
npm run build `cat workspace-args.txt`
npm run test `cat workspace-args.txt`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried assigning cat workspace-args.txt to a variable and then using that, but for whatever reason that didn't want to work, so I'm doing this instead. It's a little cumbersome but it'll work.

node ./.github/workflows/verify-pr/identify-affected-workspaces.mjs "$CHANGED_FILES"
npm run build `cat workspace-args.txt`
npm run test `cat workspace-args.txt`
npm run lint
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LInting is fast, so there's no reason not to lint all the files.

@@ -83,4 +86,12 @@ jobs:
with:
python-version: 3.12
- run: npm install
- run: npm run all
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically we're losing the invocation of npm package that is contained by npm run all. If we want it, then I can put it back in.

const changedPackages = identifyChangedPackages(changedFiles);
if (changedPackages.length === 0) {
console.log(`No package-level changes. Using empty workspace arg to test all packages.`);
fs.writeFileSync(tmpFilePath, '');
Copy link
Contributor Author

@jfeingold35 jfeingold35 May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case (and the similar case above), the file will be empty, meaning that the GHA will run npm run test , causing all packages to be tested.
I had to choose between "changing only top-level files causes no packages to be tested" and "changing only top-level files causes all packages to be tested", and I chose the latter because it was easier to implement.
Also, corner case: if you change top-level files and also one package-level file, then only that package and its dependents will be tested. Are we okay with that?

const affectedPackages = [...(new Set([...changedPackages, ...dependentPackages]).keys())];
displayList('BASED ON THE ABOVE, THE FOLLOWING PACKAGES ARE AFFECTED BY CHANGES, AND WILL REQUIRE TESTING:', affectedPackages);

const correspondingWorkspaceArgs = affectedPackages.map(name => `--workspace ${name}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When changes can be tied to specific packages (and the ones that depend on those packages), we can construct a bunch of --workspace blah arguments and write them to a file, then we can use that file to turn npm run test into npm run test --workspace blah1 --workspace blah2 and thereby only run the tests in the affected workspaces.

@jfeingold35 jfeingold35 marked this pull request as ready for review May 1, 2025 17:31
@jfeingold35 jfeingold35 requested review from randi274 and stephen-carter-at-sf and removed request for randi274 May 1, 2025 17:33
BASE_SHA=${{ github.event.pull_request.base.sha }}
HEAD_SHA=${{ github.event.pull_request.head.sha }}
CHANGED_FILES=`git diff --name-only $HEAD_SHA $BASE_SHA`
node ./.github/workflows/verify-pr/identify-affected-workspaces.mjs "$CHANGED_FILES"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we have any issues with clipping if the number of changed files is massive?
Would it be better to output this to a file and then read it from the script instead?

@@ -52,6 +52,10 @@ function displayList(header, list) {
console.log('');
}

function readChangedFilesFile(changedFilesFileName) {
return fs.readFileSync(path.join(__dirname, '..', '..', '..', changedFilesFileName), 'utf-8').split('\n').map(s => s.trim());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just use pathToRoot here instead of using __dirname, .., .., .. again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good call. Thanks.

@jfeingold35 jfeingold35 merged commit 9f57613 into dev May 2, 2025
7 checks passed
@stephen-carter-at-sf stephen-carter-at-sf deleted the d/W-18396029-b branch May 14, 2025 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants