-
Notifications
You must be signed in to change notification settings - Fork 283
ci: optimize pr-check-secondary-examples workflow (#1949) #2292
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
Open
Mounil2005
wants to merge
8
commits into
hiero-ledger:main
Choose a base branch
from
Mounil2005:optimize-examples-workflow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
28e72b1
ci: optimize pr-check-secondary-examples workflow (#1949)
Mounil2005 55f9939
ci: add jest.config.js to scope test runner to __tests__/
Mounil2005 6e9de91
ci: address Copilot review feedback
Mounil2005 e4acdab
ci: revert .gitignore changes unrelated to this PR
Mounil2005 d73fba2
ci: fix shell injection in getChangedExamples via spawnSync
Mounil2005 4d76cca
ci: address CodeRabbit review feedback
Mounil2005 804deb5
ci: address CodeRabbit review feedback
Mounil2005 2e1d55d
ci: address CodeRabbit review feedback
Mounil2005 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
244 changes: 244 additions & 0 deletions
244
.github/scripts/__tests__/pr-check-secondary-examples.test.js
This file contains hidden or 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,244 @@ | ||
| jest.mock("child_process", () => ({ | ||
| execSync: jest.fn(), | ||
| spawnSync: jest.fn(), | ||
| })); | ||
|
|
||
| const { execSync, spawnSync } = require("child_process"); | ||
|
|
||
| const { | ||
| toModule, | ||
| getAllExamples, | ||
| getChangedExamples, | ||
| runExample, | ||
| runAll, | ||
| computeExecutionPlan, | ||
| } = require("../pr-check-secondary-examples"); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // toModule | ||
| // --------------------------------------------------------------------------- | ||
| describe("toModule", () => { | ||
| test("converts nested path to module", () => { | ||
| expect(toModule("examples/a/b.py")).toBe("examples.a.b"); | ||
| }); | ||
|
|
||
| test("converts root-level example path to module", () => { | ||
| expect(toModule("examples/foo.py")).toBe("examples.foo"); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // getAllExamples | ||
| // --------------------------------------------------------------------------- | ||
| describe("getAllExamples", () => { | ||
| beforeEach(() => jest.clearAllMocks()); | ||
|
|
||
| test("returns list of example files", () => { | ||
| execSync.mockReturnValueOnce("examples/a.py\nexamples/b/c.py\n"); | ||
|
|
||
| expect(getAllExamples()).toEqual(["examples/a.py", "examples/b/c.py"]); | ||
| }); | ||
|
|
||
| test("excludes __init__.py files", () => { | ||
| execSync.mockReturnValueOnce("examples/a.py\nexamples/__init__.py\nexamples/b.py\n"); | ||
|
|
||
| expect(getAllExamples()).toEqual(["examples/a.py", "examples/b.py"]); | ||
| }); | ||
|
|
||
| test("returns empty array when git ls-files produces no output", () => { | ||
| execSync.mockReturnValueOnce(""); | ||
|
|
||
| expect(getAllExamples()).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // getChangedExamples | ||
| // --------------------------------------------------------------------------- | ||
| describe("getChangedExamples", () => { | ||
| const ORIG_ENV = process.env; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| process.env = { ...ORIG_ENV }; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.env = ORIG_ENV; | ||
| }); | ||
|
|
||
| test("returns empty array when GITHUB_BASE_REF is not set", () => { | ||
| delete process.env.GITHUB_BASE_REF; | ||
|
|
||
| expect(getChangedExamples()).toEqual([]); | ||
| expect(spawnSync).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("returns only changed example .py files", () => { | ||
| process.env.GITHUB_BASE_REF = "main"; | ||
| spawnSync | ||
| .mockReturnValueOnce({ status: 0, stdout: "" }) // git fetch | ||
| .mockReturnValueOnce({ status: 0, stdout: "" }) // git rev-parse --verify | ||
| .mockReturnValueOnce({ status: 0, stdout: "examples/a.py\nsrc/foo.py\nexamples/b.py\nREADME.md\n" }); // git diff | ||
|
|
||
| expect(getChangedExamples()).toEqual(["examples/a.py", "examples/b.py"]); | ||
| }); | ||
|
|
||
| test("filters out non-.py files under examples/", () => { | ||
| process.env.GITHUB_BASE_REF = "main"; | ||
| spawnSync | ||
| .mockReturnValueOnce({ status: 0, stdout: "" }) | ||
| .mockReturnValueOnce({ status: 0, stdout: "" }) | ||
| .mockReturnValueOnce({ status: 0, stdout: "examples/a.py\nexamples/data.json\nexamples/b.py\n" }); | ||
|
|
||
| expect(getChangedExamples()).toEqual(["examples/a.py", "examples/b.py"]); | ||
| }); | ||
|
|
||
| test("excludes __init__.py from changed examples", () => { | ||
| process.env.GITHUB_BASE_REF = "main"; | ||
| spawnSync | ||
| .mockReturnValueOnce({ status: 0, stdout: "" }) | ||
| .mockReturnValueOnce({ status: 0, stdout: "" }) | ||
| .mockReturnValueOnce({ status: 0, stdout: "examples/a.py\nexamples/__init__.py\nexamples/b.py\n" }); | ||
|
|
||
| expect(getChangedExamples()).toEqual(["examples/a.py", "examples/b.py"]); | ||
| }); | ||
|
|
||
| test("returns empty array when diff is empty", () => { | ||
| process.env.GITHUB_BASE_REF = "main"; | ||
| spawnSync | ||
| .mockReturnValueOnce({ status: 0, stdout: "" }) | ||
| .mockReturnValueOnce({ status: 0, stdout: "" }) | ||
| .mockReturnValueOnce({ status: 0, stdout: "" }); | ||
|
|
||
| expect(getChangedExamples()).toEqual([]); | ||
| }); | ||
|
|
||
| test("returns empty array when fetch fails", () => { | ||
| process.env.GITHUB_BASE_REF = "main"; | ||
| spawnSync.mockReturnValueOnce({ status: 1, stdout: "" }); | ||
|
|
||
| expect(getChangedExamples()).toEqual([]); | ||
| }); | ||
|
|
||
| test("returns empty array when spawnSync throws", () => { | ||
| process.env.GITHUB_BASE_REF = "main"; | ||
| spawnSync.mockImplementationOnce(() => { throw new Error("git not found"); }); | ||
|
|
||
| expect(getChangedExamples()).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // computeExecutionPlan | ||
| // --------------------------------------------------------------------------- | ||
| describe("computeExecutionPlan", () => { | ||
| test("removes changed files from remaining", () => { | ||
| const all = ["examples/a.py", "examples/b.py"]; | ||
| const changed = ["examples/b.py"]; | ||
|
|
||
| const { remaining } = computeExecutionPlan(all, changed); | ||
|
|
||
| expect(remaining).toEqual(["examples/a.py"]); | ||
| }); | ||
|
|
||
| test("remaining is all files when nothing changed", () => { | ||
| const all = ["examples/a.py", "examples/b.py"]; | ||
|
|
||
| const { remaining } = computeExecutionPlan(all, []); | ||
|
|
||
| expect(remaining).toEqual(all); | ||
| }); | ||
|
|
||
| test("remaining is empty when all files changed", () => { | ||
| const all = ["examples/a.py", "examples/b.py"]; | ||
|
|
||
| const { remaining } = computeExecutionPlan(all, all); | ||
|
|
||
| expect(remaining).toEqual([]); | ||
| }); | ||
|
|
||
| test("excludes deleted/renamed files from phase-1 when not present in all", () => { | ||
| const all = ["examples/a.py", "examples/b.py"]; | ||
| const changed = ["examples/b.py", "examples/deleted.py"]; | ||
|
|
||
| const { changed: validChanged, remaining } = computeExecutionPlan(all, changed); | ||
|
|
||
| expect(validChanged).toEqual(["examples/b.py"]); | ||
| expect(remaining).toEqual(["examples/a.py"]); | ||
| }); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // runExample | ||
| // --------------------------------------------------------------------------- | ||
| describe("runExample", () => { | ||
| beforeEach(() => jest.clearAllMocks()); | ||
|
|
||
| test("runs successfully", () => { | ||
| spawnSync.mockReturnValue({ status: 0 }); | ||
|
|
||
| expect(() => runExample("examples/a.py")).not.toThrow(); | ||
|
|
||
| expect(spawnSync).toHaveBeenCalledWith( | ||
| "uv", | ||
| ["run", "-m", "examples.a"], | ||
| expect.objectContaining({ stdio: "inherit" }) | ||
| ); | ||
| }); | ||
|
|
||
| test("fails and exits with code 1", () => { | ||
| spawnSync.mockReturnValue({ status: 1 }); | ||
|
|
||
| const exitSpy = jest | ||
| .spyOn(process, "exit") | ||
| .mockImplementation(() => { throw new Error("exit"); }); | ||
|
|
||
| expect(() => runExample("examples/a.py")).toThrow("exit"); | ||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
|
|
||
| exitSpy.mockRestore(); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // runAll | ||
| // --------------------------------------------------------------------------- | ||
| describe("runAll", () => { | ||
| beforeEach(() => jest.clearAllMocks()); | ||
|
|
||
| test("runs all files in order", () => { | ||
| spawnSync.mockReturnValue({ status: 0 }); | ||
|
|
||
| runAll(["examples/a.py", "examples/b.py"]); | ||
|
|
||
| expect(spawnSync).toHaveBeenCalledTimes(2); | ||
| expect(spawnSync).toHaveBeenNthCalledWith(1, "uv", ["run", "-m", "examples.a"], expect.anything()); | ||
| expect(spawnSync).toHaveBeenNthCalledWith(2, "uv", ["run", "-m", "examples.b"], expect.anything()); | ||
| }); | ||
|
|
||
| test("stops on first failure and does not run subsequent files", () => { | ||
| spawnSync | ||
| .mockReturnValueOnce({ status: 0 }) | ||
| .mockReturnValueOnce({ status: 1 }); | ||
|
|
||
| const exitSpy = jest | ||
| .spyOn(process, "exit") | ||
| .mockImplementation(() => { throw new Error("exit"); }); | ||
|
|
||
| expect(() => | ||
| runAll(["examples/a.py", "examples/b.py", "examples/c.py"]) | ||
| ).toThrow("exit"); | ||
|
|
||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
| expect(spawnSync).toHaveBeenCalledTimes(2); | ||
|
|
||
| exitSpy.mockRestore(); | ||
| }); | ||
|
|
||
| test("does nothing for empty list", () => { | ||
| runAll([]); | ||
|
|
||
| expect(spawnSync).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,4 @@ | ||
| /** @type {import('jest').Config} */ | ||
| module.exports = { | ||
| testMatch: ["**/__tests__/**/*.test.js"], | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.