-
Notifications
You must be signed in to change notification settings - Fork 368
[jsweep] Clean messages_staged.cjs #27487
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
+149
β16
Merged
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // @ts-check | ||
| import { describe, it, expect, beforeEach, vi } from "vitest"; | ||
|
|
||
| // messages_core.cjs calls core.warning on parse failures - provide a stub | ||
| const mockCore = { | ||
| info: vi.fn(), | ||
| warning: vi.fn(), | ||
| error: vi.fn(), | ||
| setFailed: vi.fn(), | ||
| setOutput: vi.fn(), | ||
| }; | ||
| global.core = mockCore; | ||
|
|
||
| const { getStagedTitle, getStagedDescription } = require("./messages_staged.cjs"); | ||
|
|
||
| const OPERATION = "Create Issues"; | ||
|
|
||
| describe("messages_staged", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| delete process.env.GH_AW_SAFE_OUTPUT_MESSAGES; | ||
| }); | ||
|
|
||
| describe("getStagedTitle", () => { | ||
| it("returns the default title with operation substituted", () => { | ||
| const title = getStagedTitle({ operation: OPERATION }); | ||
| expect(title).toBe(`## π Preview: ${OPERATION}`); | ||
| }); | ||
|
|
||
| it("substitutes a different operation value", () => { | ||
| const title = getStagedTitle({ operation: "Add Comments" }); | ||
| expect(title).toBe("## π Preview: Add Comments"); | ||
| }); | ||
|
|
||
| it("uses custom template from config", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ stagedTitle: "Staging: {operation}" }); | ||
| const title = getStagedTitle({ operation: OPERATION }); | ||
| expect(title).toBe(`Staging: ${OPERATION}`); | ||
| }); | ||
|
|
||
| it("falls back to default when stagedTitle is absent from config", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ runSuccess: "other" }); | ||
| const title = getStagedTitle({ operation: OPERATION }); | ||
| expect(title).toBe(`## π Preview: ${OPERATION}`); | ||
| }); | ||
|
|
||
| it("falls back to default when stagedTitle is an empty string", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ stagedTitle: "" }); | ||
| const title = getStagedTitle({ operation: OPERATION }); | ||
| expect(title).toBe(`## π Preview: ${OPERATION}`); | ||
| }); | ||
|
|
||
| it("falls back to default when stagedTitle is a falsy non-string value", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ stagedTitle: 0 }); | ||
| const title = getStagedTitle({ operation: OPERATION }); | ||
| expect(title).toBe(`## π Preview: ${OPERATION}`); | ||
| }); | ||
|
|
||
| it("falls back to default when config is invalid JSON", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = "not-json"; | ||
| const title = getStagedTitle({ operation: OPERATION }); | ||
| expect(title).toBe(`## π Preview: ${OPERATION}`); | ||
| }); | ||
|
|
||
| it("supports snake_case placeholders from camelCase context keys", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ stagedTitle: "{operation_name}: {operation}" }); | ||
| const title = getStagedTitle({ operation: OPERATION, operationName: "Create Comment" }); | ||
| expect(title).toBe(`Create Comment: ${OPERATION}`); | ||
| }); | ||
|
|
||
| it("returns an empty-operation title when operation is an empty string", () => { | ||
| const title = getStagedTitle({ operation: "" }); | ||
| expect(title).toBe("## π Preview: "); | ||
| }); | ||
|
|
||
| it("leaves unrecognised placeholders unchanged in custom template", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ stagedTitle: "{operation} | {unknown}" }); | ||
| const title = getStagedTitle({ operation: OPERATION }); | ||
| expect(title).toBe(`${OPERATION} | {unknown}`); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getStagedDescription", () => { | ||
| it("returns the default description", () => { | ||
| const desc = getStagedDescription({ operation: OPERATION }); | ||
| expect(desc).toBe("π The following operations would be performed if staged mode was disabled:"); | ||
| }); | ||
|
|
||
| it("uses custom description template from config", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ stagedDescription: "Preview of: {operation}" }); | ||
| const desc = getStagedDescription({ operation: OPERATION }); | ||
| expect(desc).toBe(`Preview of: ${OPERATION}`); | ||
| }); | ||
|
|
||
| it("falls back to default when stagedDescription is absent from config", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ stagedTitle: "title only" }); | ||
| const desc = getStagedDescription({ operation: OPERATION }); | ||
| expect(desc).toBe("π The following operations would be performed if staged mode was disabled:"); | ||
| }); | ||
|
|
||
| it("falls back to default when stagedDescription is an empty string", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ stagedDescription: "" }); | ||
| const desc = getStagedDescription({ operation: OPERATION }); | ||
| expect(desc).toBe("π The following operations would be performed if staged mode was disabled:"); | ||
| }); | ||
|
|
||
| it("falls back to default when stagedDescription is a falsy non-string value", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ stagedDescription: false }); | ||
| const desc = getStagedDescription({ operation: OPERATION }); | ||
| expect(desc).toBe("π The following operations would be performed if staged mode was disabled:"); | ||
| }); | ||
|
|
||
| it("falls back to default when config is invalid JSON", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = "{bad json"; | ||
| const desc = getStagedDescription({ operation: OPERATION }); | ||
| expect(desc).toBe("π The following operations would be performed if staged mode was disabled:"); | ||
| }); | ||
|
|
||
| it("supports custom description with operation placeholder", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ stagedDescription: "Would run: {operation}" }); | ||
| const desc = getStagedDescription({ operation: "Close PRs" }); | ||
| expect(desc).toBe("Would run: Close PRs"); | ||
| }); | ||
|
|
||
| it("default description does not contain unfilled placeholders", () => { | ||
| const desc = getStagedDescription({ operation: OPERATION }); | ||
| expect(desc).not.toMatch(/\{[^}]+\}/); | ||
| }); | ||
| }); | ||
|
|
||
| describe("independent config keys", () => { | ||
| it("getStagedTitle and getStagedDescription use their own config keys independently", () => { | ||
| process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ | ||
| stagedTitle: "Custom title: {operation}", | ||
| stagedDescription: "Custom desc: {operation}", | ||
| }); | ||
|
|
||
| const title = getStagedTitle({ operation: OPERATION }); | ||
| const desc = getStagedDescription({ operation: OPERATION }); | ||
|
|
||
| expect(title).toBe(`Custom title: ${OPERATION}`); | ||
| expect(desc).toBe(`Custom desc: ${OPERATION}`); | ||
| }); | ||
| }); | ||
| }); | ||
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.
The updated implementation in
messages_staged.cjsnow relies on??for template fallback, which behaves differently for empty-string (and other falsy, non-nullish) configured values. There isnβt currently a test that locks in the intended behavior whenstagedTitle/stagedDescriptionis set to""(or another falsy value) in GH_AW_SAFE_OUTPUT_MESSAGES. Adding an explicit test case for this would prevent accidental behavior changes and clarify whether empty strings should fall back or be treated as valid templates.