Skip to content

Commit

Permalink
Refactor/cleanup, tests and new inputs: omitUntestedPackages and `o…
Browse files Browse the repository at this point in the history
…mitPie` (#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
  • Loading branch information
robherley committed Jul 24, 2022
1 parent 2950056 commit d2be031
Show file tree
Hide file tree
Showing 17 changed files with 1,130 additions and 400 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ GitHub Action for running `go test ...` and getting rich summary and annotations

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.

## Inputs

- `moduleDirectory` (optional): relative path to the directory containing the `go.mod` of the module you wish to test
- Default: `.`
- `testArguments` (optional): arguments to pass to `go test`, `-json` will be prepended automatically
- Default: `./...`
- `omitUntestedPackages` (optional): omit any go packages that don't have any tests from the summary output
- Default: `false`
- `omitPie` (optional): omit the pie chart from the summary output
- Default: `false`

## Demo

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

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

## Inputs

- `moduleDirectory` (optional): relative path to the directory containing the `go.mod` of the module you wish to test
- Default: `.`
- `testArguments` (optional): arguments to pass to `go test`, `-json` will be prepended automatically
- Default: `./...`

## Example workflow

```yaml
Expand Down
92 changes: 92 additions & 0 deletions __tests__/events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { getTestStdout, mockActionsCoreLogging } from './helpers'
import { parseTestEvents } from '../src/events'

describe('events', () => {
beforeEach(() => {
mockActionsCoreLogging()
})

it('correctly parses test2json output', async () => {
const stdout = await getTestStdout()

const testsEvents = parseTestEvents(stdout)

expect(testsEvents).toHaveLength(59)
expect(testsEvents[58]).toEqual({
time: new Date('2022-07-11T02:42:12.111Z'),
action: 'fail',
package: 'github.com/robherley/go-test-example/boom',
test: undefined,
elapsed: 0.103,
output: undefined,
isCached: false,
isSubtest: false,
isPackageLevel: true,
isConclusive: true,
})
})

it('correctly indicates a package level test', () => {
const packageLevelStdout =
'{"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"}'

const packageLevelTestEvents = parseTestEvents(packageLevelStdout)
expect(packageLevelTestEvents[0]).toHaveProperty('isPackageLevel', true)

const otherStdout =
'{"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"}'

const otherTestEvents = parseTestEvents(otherStdout)
expect(otherTestEvents[0]).toHaveProperty('isPackageLevel', false)
})

it('correctly indicates a subtest', () => {
const subTestStdout =
'{"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"}'

const subTestEvents = parseTestEvents(subTestStdout)
expect(subTestEvents[0]).toHaveProperty('isSubtest', true)

const topLevelTestStdout =
'{"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"}'

const topLevelTestEvents = parseTestEvents(topLevelTestStdout)
expect(topLevelTestEvents[0]).toHaveProperty('isSubtest', false)
})

it('correctly indicates conclusive tests', () => {
const getStdout = (action: string) =>
`{"Time":"2022-07-10T22:42:12.108414-04:00","Action":"${action}","Package":"github.com/robherley/go-test-example/boom","Test":"TestFatal","Elapsed":0}`

const testCases: [string, boolean][] = [
['run', false],
['pause', false],
['cont', false],
['bench', false],
['output', false],
['pass', true],
['fail', true],
['skip', true],
]

for (let [action, isConclusive] of testCases) {
const stdout = getStdout(action)
const testEvents = parseTestEvents(stdout)
expect(testEvents[0]).toHaveProperty('isConclusive', isConclusive)
}
})

it('correctly indicates a cached test', () => {
const cachedStdout =
'{"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"}'

const cachedTestEvents = parseTestEvents(cachedStdout)
expect(cachedTestEvents[0]).toHaveProperty('isCached', true)

const otherStdout =
'{"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"}'

const otherTestEvents = parseTestEvents(otherStdout)
expect(otherTestEvents[0]).toHaveProperty('isCached', false)
})
})
17 changes: 16 additions & 1 deletion __tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,23 @@ module github.com/robherley/go-test-example
go 1.18
`

export const createSummaryFile = async () => {
process.env['GITHUB_STEP_SUMMARY'] = testSummaryFilePath
await fs.writeFile(testSummaryFilePath, '', { encoding: 'utf8' })
core.summary.emptyBuffer()
}

export const removeSummaryFile = async () => {
delete process.env['GITHUB_STEP_SUMMARY']
await fs.unlink(testSummaryFilePath)
core.summary.emptyBuffer()
}

export const setupActionsInputs = () => {
process.env['INPUT_MODULEDIRECTORY'] = testModuleDirectory
process.env['INPUT_TESTARGUMENTS'] = testArguments
process.env['INPUT_OMITUNTESTEDPACKAGES'] = 'false'
process.env['INPUT_OMITPIE'] = 'false'
}

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

export const mockActionsCoreLogging = () => {
export const mockActionsCoreLogging = (silent = true) => {
type LogFuncs = 'debug' | 'error' | 'warning' | 'notice' | 'info'
const logMethods: LogFuncs[] = ['debug', 'error', 'warning', 'notice', 'info']
logMethods.forEach(method => {
jest
.spyOn(core, method)
.mockImplementation(
(msg: string | Error, props?: core.AnnotationProperties) => {
if (silent) return
console.log(
`[mock: core.${method}(${props ? JSON.stringify(props) : ''})]:`,
msg
Expand Down
Loading

0 comments on commit d2be031

Please sign in to comment.