-
Notifications
You must be signed in to change notification settings - Fork 477
Guard setup JS sync fs/exec calls with contextual error handling #47425
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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
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
86 changes: 86 additions & 0 deletions
86
actions/setup/js/check_workflow_timestamp_error_handling.test.cjs
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,86 @@ | ||
| import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; | ||
| import fs from "fs"; | ||
| import os from "os"; | ||
| import path from "path"; | ||
|
|
||
| const mockCore = { | ||
| debug: vi.fn(), | ||
| info: vi.fn(), | ||
| notice: vi.fn(), | ||
| warning: vi.fn(), | ||
| error: vi.fn(), | ||
| setFailed: vi.fn(), | ||
| setOutput: vi.fn(), | ||
| exportVariable: vi.fn(), | ||
| summary: { | ||
| addRaw: vi.fn().mockReturnThis(), | ||
| write: vi.fn().mockResolvedValue(), | ||
| }, | ||
| }; | ||
|
|
||
| global.core = mockCore; | ||
|
|
||
| const { main } = await import("./check_workflow_timestamp.cjs"); | ||
|
|
||
| describe("check_workflow_timestamp error handling", () => { | ||
| let tmpDir; | ||
| let workflowsDir; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "workflow-stat-test-")); | ||
| workflowsDir = path.join(tmpDir, ".github", "workflows"); | ||
| fs.mkdirSync(workflowsDir, { recursive: true }); | ||
| process.env.GITHUB_WORKSPACE = tmpDir; | ||
| process.env.GH_AW_WORKFLOW_FILE = "test.lock.yml"; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| fs.rmSync(tmpDir, { recursive: true, force: true }); | ||
| delete process.env.GITHUB_WORKSPACE; | ||
| delete process.env.GH_AW_WORKFLOW_FILE; | ||
| }); | ||
|
|
||
| it("reports the workflow source path when source stat inspection fails", async () => { | ||
| const workflowFile = path.join(workflowsDir, "test.md"); | ||
| const lockFile = path.join(workflowsDir, "test.lock.yml"); | ||
| fs.writeFileSync(workflowFile, "# Workflow content"); | ||
| fs.writeFileSync(lockFile, "# Lock content"); | ||
|
|
||
| vi.spyOn(fs, "statSync").mockImplementation(targetPath => { | ||
| if (targetPath === workflowFile) { | ||
| throw new Error("source unreadable"); | ||
| } | ||
| return { | ||
| mtime: new Date(), | ||
| isFile: () => true, | ||
| }; | ||
| }); | ||
|
|
||
| await main(); | ||
|
|
||
| expect(mockCore.setFailed).toHaveBeenCalledWith(`Failed to inspect workflow source ${workflowFile}: source unreadable`); | ||
| }); | ||
|
|
||
| it("reports the lock file path when lock stat inspection fails", async () => { | ||
| const workflowFile = path.join(workflowsDir, "test.md"); | ||
| const lockFile = path.join(workflowsDir, "test.lock.yml"); | ||
| fs.writeFileSync(workflowFile, "# Workflow content"); | ||
| fs.writeFileSync(lockFile, "# Lock content"); | ||
|
|
||
| vi.spyOn(fs, "statSync").mockImplementation(targetPath => { | ||
| if (targetPath === lockFile) { | ||
| throw new Error("lock unreadable"); | ||
| } | ||
| return { | ||
| mtime: new Date(), | ||
| isFile: () => true, | ||
| }; | ||
| }); | ||
|
|
||
| await main(); | ||
|
|
||
| expect(mockCore.setFailed).toHaveBeenCalledWith(`Failed to inspect lock file ${lockFile}: lock unreadable`); | ||
| }); | ||
| }); |
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
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
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
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/diagnosing-bugs] Both
statSynccalls share a singletry/catch, so the error message can't tell the operator whether the workflow.mdor the.lock.ymlfile was unreadable.💡 Suggested fix: separate the guards
The two files have different diagnostic meanings — one missing is a config problem, the other is a build artifact gap. Separate errors make triage unambiguous.
@copilot please address this.