Skip to content

Commit d2be031

Browse files
authored
Refactor/cleanup, tests and new inputs: omitUntestedPackages and omitPie (#4)
* mild refactor * update tests w/ cheerio * add tests for results * double break for hidden pie * update inputs * renderer: test for no test events * compile dist
1 parent 2950056 commit d2be031

17 files changed

+1130
-400
lines changed

README.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ GitHub Action for running `go test ...` and getting rich summary and annotations
44

55
Powered by [Job Summaries](https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/), this Action will generate a convenient interactive viewer for tests based on Go's [test2json](https://pkg.go.dev/cmd/test2json) output. If there are any errors during `go test`, the Action will report back the same exit code, which will fail the job.
66

7+
## Inputs
8+
9+
- `moduleDirectory` (optional): relative path to the directory containing the `go.mod` of the module you wish to test
10+
- Default: `.`
11+
- `testArguments` (optional): arguments to pass to `go test`, `-json` will be prepended automatically
12+
- Default: `./...`
13+
- `omitUntestedPackages` (optional): omit any go packages that don't have any tests from the summary output
14+
- Default: `false`
15+
- `omitPie` (optional): omit the pie chart from the summary output
16+
- Default: `false`
17+
718
## Demo
819

920
To interact with an example, [check it out here](https://github.com/robherley/go-test-example/actions/runs/2647255176/attempts/1).
@@ -16,13 +27,6 @@ Expand for per-test (with subtest) results and to view raw test output:
1627

1728
![summary expanded](docs/img/expanded.png)
1829

19-
## Inputs
20-
21-
- `moduleDirectory` (optional): relative path to the directory containing the `go.mod` of the module you wish to test
22-
- Default: `.`
23-
- `testArguments` (optional): arguments to pass to `go test`, `-json` will be prepended automatically
24-
- Default: `./...`
25-
2630
## Example workflow
2731

2832
```yaml

__tests__/events.test.ts

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { getTestStdout, mockActionsCoreLogging } from './helpers'
2+
import { parseTestEvents } from '../src/events'
3+
4+
describe('events', () => {
5+
beforeEach(() => {
6+
mockActionsCoreLogging()
7+
})
8+
9+
it('correctly parses test2json output', async () => {
10+
const stdout = await getTestStdout()
11+
12+
const testsEvents = parseTestEvents(stdout)
13+
14+
expect(testsEvents).toHaveLength(59)
15+
expect(testsEvents[58]).toEqual({
16+
time: new Date('2022-07-11T02:42:12.111Z'),
17+
action: 'fail',
18+
package: 'github.com/robherley/go-test-example/boom',
19+
test: undefined,
20+
elapsed: 0.103,
21+
output: undefined,
22+
isCached: false,
23+
isSubtest: false,
24+
isPackageLevel: true,
25+
isConclusive: true,
26+
})
27+
})
28+
29+
it('correctly indicates a package level test', () => {
30+
const packageLevelStdout =
31+
'{"Time":"2022-07-10T22:42:11.92576-04:00","Action":"output","Package":"github.com/robherley/go-test-example","Output":"? \\tgithub.com/robherley/go-test-example\\t[no test files]\\n"}'
32+
33+
const packageLevelTestEvents = parseTestEvents(packageLevelStdout)
34+
expect(packageLevelTestEvents[0]).toHaveProperty('isPackageLevel', true)
35+
36+
const otherStdout =
37+
'{"Time":"2022-07-10T22:42:12.108346-04:00","Action":"output","Package":"github.com/robherley/go-test-example/boom","Test":"TestFatal","Output":"=== RUN TestFatal\\n"}'
38+
39+
const otherTestEvents = parseTestEvents(otherStdout)
40+
expect(otherTestEvents[0]).toHaveProperty('isPackageLevel', false)
41+
})
42+
43+
it('correctly indicates a subtest', () => {
44+
const subTestStdout =
45+
'{"Time":"2022-07-10T22:42:11.9313-04:00","Action":"output","Package":"github.com/robherley/go-test-example/success","Test":"TestSuccess/Subtest(2)","Output":" success_test.go:19: hello from subtest #2\\n"}'
46+
47+
const subTestEvents = parseTestEvents(subTestStdout)
48+
expect(subTestEvents[0]).toHaveProperty('isSubtest', true)
49+
50+
const topLevelTestStdout =
51+
'{"Time":"2022-07-10T22:42:11.931141-04:00","Action":"output","Package":"github.com/robherley/go-test-example/success","Test":"TestSuccess","Output":"=== RUN TestSuccess\\n"}'
52+
53+
const topLevelTestEvents = parseTestEvents(topLevelTestStdout)
54+
expect(topLevelTestEvents[0]).toHaveProperty('isSubtest', false)
55+
})
56+
57+
it('correctly indicates conclusive tests', () => {
58+
const getStdout = (action: string) =>
59+
`{"Time":"2022-07-10T22:42:12.108414-04:00","Action":"${action}","Package":"github.com/robherley/go-test-example/boom","Test":"TestFatal","Elapsed":0}`
60+
61+
const testCases: [string, boolean][] = [
62+
['run', false],
63+
['pause', false],
64+
['cont', false],
65+
['bench', false],
66+
['output', false],
67+
['pass', true],
68+
['fail', true],
69+
['skip', true],
70+
]
71+
72+
for (let [action, isConclusive] of testCases) {
73+
const stdout = getStdout(action)
74+
const testEvents = parseTestEvents(stdout)
75+
expect(testEvents[0]).toHaveProperty('isConclusive', isConclusive)
76+
}
77+
})
78+
79+
it('correctly indicates a cached test', () => {
80+
const cachedStdout =
81+
'{"Time":"2022-07-10T22:42:11.931552-04:00","Action":"output","Package":"github.com/robherley/go-test-example/success","Output":"ok \\tgithub.com/robherley/go-test-example/success\\t(cached)\\n"}'
82+
83+
const cachedTestEvents = parseTestEvents(cachedStdout)
84+
expect(cachedTestEvents[0]).toHaveProperty('isCached', true)
85+
86+
const otherStdout =
87+
'{"Time":"2022-07-10T22:42:11.931552-04:00","Action":"output","Package":"github.com/robherley/go-test-example/success","Output":"ok \\tgithub.com/robherley/go-test-example/success"}'
88+
89+
const otherTestEvents = parseTestEvents(otherStdout)
90+
expect(otherTestEvents[0]).toHaveProperty('isCached', false)
91+
})
92+
})

__tests__/helpers.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,23 @@ module github.com/robherley/go-test-example
2020
go 1.18
2121
`
2222

23+
export const createSummaryFile = async () => {
24+
process.env['GITHUB_STEP_SUMMARY'] = testSummaryFilePath
25+
await fs.writeFile(testSummaryFilePath, '', { encoding: 'utf8' })
26+
core.summary.emptyBuffer()
27+
}
28+
29+
export const removeSummaryFile = async () => {
30+
delete process.env['GITHUB_STEP_SUMMARY']
31+
await fs.unlink(testSummaryFilePath)
32+
core.summary.emptyBuffer()
33+
}
34+
2335
export const setupActionsInputs = () => {
2436
process.env['INPUT_MODULEDIRECTORY'] = testModuleDirectory
2537
process.env['INPUT_TESTARGUMENTS'] = testArguments
38+
process.env['INPUT_OMITUNTESTEDPACKAGES'] = 'false'
39+
process.env['INPUT_OMITPIE'] = 'false'
2640
}
2741

2842
export const createFakeGoModule = async () => {
@@ -48,14 +62,15 @@ export const getTestStdout = async (): Promise<string> => {
4862
return buf.toString()
4963
}
5064

51-
export const mockActionsCoreLogging = () => {
65+
export const mockActionsCoreLogging = (silent = true) => {
5266
type LogFuncs = 'debug' | 'error' | 'warning' | 'notice' | 'info'
5367
const logMethods: LogFuncs[] = ['debug', 'error', 'warning', 'notice', 'info']
5468
logMethods.forEach(method => {
5569
jest
5670
.spyOn(core, method)
5771
.mockImplementation(
5872
(msg: string | Error, props?: core.AnnotationProperties) => {
73+
if (silent) return
5974
console.log(
6075
`[mock: core.${method}(${props ? JSON.stringify(props) : ''})]:`,
6176
msg

0 commit comments

Comments
 (0)