Skip to content

Commit a780f45

Browse files
authored
organized test cases for clear reporting. (#2)
* organized test cases for clear reporting. * corrected final-result to test for skipped steps * added missing checkout step * corrected final result logic * reverted to testing * added outcome step * switched to test outcomes * Debugging test case logic * adjusted test outcome logic * corrected invalid Bash script * removed debugging; Adjust output to be more readable * change test summary icon; write negative test outcome to GITHUB_STEP_SUMMARY * debugging failure of positive test * Added more grainular results * corrected ouputs assignments * corrected ouputs assignments use * added exit fail * added body type validation check * fixed linting errors * adjust exception handling for new exception; added clear messages * removed redundant message; incremented version * reverted debugging; added dependabot --------- Co-authored-by: Daniel I. Kelley <[email protected]>
1 parent 593ca9a commit a780f45

File tree

3 files changed

+114
-21
lines changed

3 files changed

+114
-21
lines changed

.github/workflows/ci.yml

Lines changed: 107 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ permissions:
1212

1313
jobs:
1414
lint-and-test:
15+
name: Linting and Positive Tests
1516
runs-on: ubuntu-latest
17+
outputs:
18+
test_result: ${{ steps.positive-test-outcome.outcome }}
19+
lint_result: ${{ steps.lint-outcome.outcome }}
1620
steps:
17-
- uses: actions/checkout@v4
21+
- name: Checkout Code
22+
uses: actions/checkout@v4
1823

1924
- name: Validate action.yml Metadata
25+
id: validate-action-yml
2026
run: |
2127
yq eval '.' action.yml > /dev/null
2228
@@ -27,7 +33,7 @@ jobs:
2733
label: "test-success-path-defaults"
2834
script: |
2935
console.log("✅ github-script-post-comment action executed in CI.");
30-
return "Self-test successful.";
36+
return "✅ Test Case 1 - Composite Action - Success Path with Defaults.";
3137
3238
- name: Upload Embedded JavaScript
3339
uses: actions/upload-artifact@v4
@@ -37,47 +43,129 @@ jobs:
3743
path: 'final_script.js'
3844

3945
- name: Lint Embedded JavaScript and project files
46+
id: lint-check
4047
env:
4148
NODE_ENV: development
4249
run: |
4350
npm ci
4451
npx eslint .
4552
46-
- name: Test Case 2 - Composite Action - No Return
53+
- name: Test Case 2 - Composite Action - Success Path with github-step-summary
54+
uses: ./
55+
id: test-success-path-github-step-summary
56+
with:
57+
label: "test-success-path-github-step-summary"
58+
post-target: github-step-summary
59+
script: |
60+
console.log("✅ github-script-post-comment action executed in CI.");
61+
return "✅ Test Case 2 - Composite Action - Success Path with github-step-summary.";
62+
63+
- name: Test Case 3 - Composite Action - Success Path with pull-request
64+
uses: ./
65+
id: test-success-path-pull-request
66+
with:
67+
label: "test-success-path-pull-request"
68+
post-target: pull-request
69+
script: |
70+
console.log("✅ github-script-post-comment action executed in CI.");
71+
return "✅ Test Case 3 - Composite Action - Success Path with pull-request.";
72+
- name: Lint Outcome
73+
id: lint-outcome
74+
if: always()
75+
run: |
76+
echo "steps.validate-action-yml.outcome = ${{ steps.validate-action-yml.outcome }}"
77+
echo "steps.lint-check.outcome = ${{ steps.lint-check.outcome }}"
78+
79+
if [[ "${{ steps.validate-action-yml.outcome }}" == "success" && "${{ steps.lint-check.outcome }}" == "success" ]]; then
80+
echo "✅ Linting passed."
81+
echo "✅ Linting passed." >> $GITHUB_STEP_SUMMARY
82+
else
83+
echo "❌ One or more lint rules failed."
84+
echo "❌ One or more lint rules failed." >> $GITHUB_STEP_SUMMARY
85+
exit 1
86+
fi
87+
- name: Job Outcome
88+
id: positive-test-outcome
89+
if: always()
90+
run: |
91+
echo "steps.test-success-path-defaults.outcome = ${{ steps.test-success-path-defaults.outcome }}"
92+
echo "steps.test-success-path-github-step-summary.outcome = ${{ steps.test-success-path-github-step-summary.outcome }}"
93+
echo "steps.test-success-path-pull-request.outcome = ${{ steps.test-success-path-pull-request.outcome }}"
94+
95+
if [[ "${{ steps.test-success-path-defaults.outcome }}" == "success" && "${{ steps.test-success-path-github-step-summary.outcome }}" == "success" && "${{ steps.test-success-path-pull-request.outcome }}" == "success" ]]; then
96+
echo "✅ All positive tests passed."
97+
echo "✅ All positive tests passed." >> $GITHUB_STEP_SUMMARY
98+
else
99+
echo "❌ One or more positive tests failed or did not run."
100+
echo "❌ One or more positive tests failed or did not run." >> $GITHUB_STEP_SUMMARY
101+
exit 1
102+
fi
103+
104+
negative-tests:
105+
name: Negative Tests - Expected to Fail
106+
runs-on: ubuntu-latest
107+
outputs:
108+
test_result: ${{ steps.negative-tests-outcome.outcome }}
109+
steps:
110+
- name: Checkout Code
111+
uses: actions/checkout@v4
112+
113+
- name: Test Case 4 - Composite Action - No Return
47114
uses: ./
48115
id: test-no-return
49116
continue-on-error: true
50117
with:
51118
label: "test-no-return"
52119
script: |
120+
console.log("Test Case 2 - Composite Action - No Return");
53121
console.log("Expect error: script does not return string.");
54122
55-
- name: Test Case 3 - Composite Action - Return Non-String
123+
- name: Test Case 5 - Composite Action - Return Non-String
56124
uses: ./
57125
id: test-return-non-string
58126
continue-on-error: true
59127
with:
60128
label: "test-return-non-string"
61129
script: |
130+
console.log("Test Case 3 - Composite Action - Return Non-String");
62131
console.log("Expect error: script returns non-string.");
63132
return {title: "Test Case 3", message: "test case return non-string", score: 50};
64133
65-
- name: Test Case 4 - Composite Action - Success Path with github-step-summary
66-
uses: ./
67-
id: test-success-path-github-step-summary
68-
with:
69-
label: "test-success-path-github-step-summary"
70-
post-target: github-step-summary
71-
script: |
72-
console.log("✅ github-script-post-comment action executed in CI.");
73-
return "Self-test successful.";
134+
- name: Job Outcome
135+
id: negative-tests-outcome
136+
if: always()
137+
run: |
138+
echo "steps.test-no-return.outcome = ${{ steps.test-no-return.outcome }}"
139+
echo "steps.test-return-non-string.outcome = ${{ steps.test-return-non-string.outcome }}"
140+
141+
if [[ "${{ steps.test-no-return.outcome }}" == "failure" && "${{ steps.test-return-non-string.outcome }}" == "failure" ]]; then
142+
echo "✅ All negative tests failed as expected."
143+
echo "✅ All negative tests failed as expected." >> $GITHUB_STEP_SUMMARY
144+
else
145+
echo "❌ One or more negative tests did not fail as expected."
146+
echo "❌ One or more negative tests did not fail as expected." >> $GITHUB_STEP_SUMMARY
147+
exit 1
148+
fi
74149
75-
- name: Test Case 5 - Composite Action - Success Path with pull-request
150+
test-summary:
151+
name: Test Summary
152+
runs-on: ubuntu-latest
153+
needs: [lint-and-test, negative-tests]
154+
if: always()
155+
156+
steps:
157+
- name: Checkout Code
158+
uses: actions/checkout@v5
159+
- name: Add Test Summary
76160
uses: ./
77-
id: test-success-path-pull-request
78161
with:
79-
label: "test-success-path-pull-request"
80-
post-target: pull-request
162+
label: "Test Summary"
81163
script: |
82-
console.log("✅ github-script-post-comment action executed in CI.");
83-
return "Self-test successful.";
164+
let body = "## 🧪 Test Summary\n\n"
165+
+ "| Check | Status |\n"
166+
+ "|-------|--------|\n"
167+
+ "| Lint Rules | ${{ needs.lint-and-test.outputs.lint_result == 'success' && '✅ Passed' || '❌ Failed' }} |\n"
168+
+ "| Positive Tests | ${{ needs.lint-and-test.outputs.test_result == 'success' && '✅ Passed' || '❌ Failed' }} |\n"
169+
+ "| Negative Tests | ${{ needs.negative-tests.outputs.test_result == 'success' && '✅ Passed' || '❌ Failed' }} |\n\n";
170+
171+
return body;

action.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ runs:
113113
// Store the result as an output for downstream steps
114114
core.setOutput('result', JSON.stringify(body));
115115
116+
// Validate that the returned body is a string
117+
if (typeof body !== 'string') {
118+
throw new TypeError(`Returned value from user script must be a string. Received type '${typeof body}'. `);
119+
}
120+
116121
// Output to GITHUB_STEP_SUMMARY
117122
if (postTarget === 'both' || postTarget === 'github-step-summary') {
118123
const summaryPath = process.env.GITHUB_STEP_SUMMARY;
@@ -143,7 +148,7 @@ runs:
143148
console.error(`❌ Error processing ${{ inputs.label }} results:`, error);
144149
145150
// Detect when user forgot to return a value (causing body = undefined)
146-
if (error.status === 'ERR_INVALID_ARG_TYPE' && /The "data" argument must be of type string/i.test(error.message)) {
151+
if (error instanceof TypeError && /Returned value from user script must be a string/i.test(error.message)) {
147152
console.error(`💡 Hint: Your script returned a non-string value (e.g., object, array, or undefined) — or nothing at all.\n`
148153
+ `Please return a string, or serialize your result using JSON.stringify.\n\n`
149154
+ `For example:\n\n`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aguilita1/github-script-post-comment",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"description": "GitHub Action to run custom JavaScript and post results to GitHub summary and Pull Request",
55
"author": "Daniel I. Kelley",
66
"license": "MIT",

0 commit comments

Comments
 (0)