From 932863bef508868168954ef69572750cf87f463f Mon Sep 17 00:00:00 2001 From: ChristopherHX Date: Wed, 25 Jan 2023 09:14:51 +0100 Subject: [PATCH] feat: step summary of test results (#1580) * feat: step summary of test results * fix: indent style * fix: handle failed tests * fix upload / create a logs artifact * Update checks.yml * fix: always upload logs * fix: run success * Move steps into a composite action * use args and not the hardcoded ones * format composite action * format --- .github/actions/run-tests/action.yml | 77 ++++++++++++++++++++++++++++ .github/workflows/checks.yml | 12 +++-- 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 .github/actions/run-tests/action.yml diff --git a/.github/actions/run-tests/action.yml b/.github/actions/run-tests/action.yml new file mode 100644 index 0000000..09cb3da --- /dev/null +++ b/.github/actions/run-tests/action.yml @@ -0,0 +1,77 @@ +name: 'run-tests' +description: 'Runs go test and upload a step summary' +inputs: + filter: + description: 'The go test pattern for the tests to run' + required: false + default: '' + upload-logs-name: + description: 'Choose the name of the log artifact' + required: false + default: logs-${{ github.job }}-${{ strategy.job-index }} + upload-logs: + description: 'If true uploads logs of each tests as an artifact' + required: false + default: 'true' +runs: + using: composite + steps: + - uses: actions/github-script@v6 + with: + github-token: none # No reason to grant access to the GITHUB_TOKEN + script: | + let myOutput = ''; + var fs = require('fs'); + var uploadLogs = process.env.UPLOAD_LOGS === 'true'; + if(uploadLogs) { + await io.mkdirP('logs'); + } + var filename = null; + const options = {}; + options.ignoreReturnCode = true; + options.env = Object.assign({}, process.env); + delete options.env.ACTIONS_RUNTIME_URL; + delete options.env.ACTIONS_RUNTIME_TOKEN; + delete options.env.ACTIONS_CACHE_URL; + options.listeners = { + stdout: (data) => { + for(line of data.toString().split('\n')) { + if(/^\s*(===\s[^\s]+\s|---\s[^\s]+:\s)/.test(line)) { + if(uploadLogs) { + var runprefix = "=== RUN "; + if(line.startsWith(runprefix)) { + filename = "logs/" + line.substring(runprefix.length).replace(/[^A-Za-z0-9]/g, '-') + ".txt"; + fs.writeFileSync(filename, line + "\n"); + } else if(filename) { + fs.appendFileSync(filename, line + "\n"); + filename = null; + } + } + myOutput += line + "\n"; + } else if(filename) { + fs.appendFileSync(filename, line + "\n"); + } + } + } + }; + var args = ['test', '-v', '-cover', '-coverprofile=coverage.txt', '-covermode=atomic', '-timeout', '15m']; + var filter = process.env.FILTER; + if(filter) { + args.push('-run'); + args.push(filter); + } + args.push('./...'); + var exitcode = await exec.exec('go', args, options); + if(process.env.GITHUB_STEP_SUMMARY) { + core.summary.addCodeBlock(myOutput); + await core.summary.write(); + } + process.exit(exitcode); + env: + FILTER: ${{ inputs.filter }} + UPLOAD_LOGS: ${{ inputs.upload-logs }} + - uses: actions/upload-artifact@v3 + if: always() && inputs.upload-logs == 'true' && !env.ACT + with: + name: ${{ inputs.upload-logs-name }} + path: logs diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 781675f..561d00c 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -50,7 +50,10 @@ jobs: key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- - - run: go test -v -cover -coverprofile=coverage.txt -covermode=atomic -timeout 15m ./... + - name: Run Tests + uses: ./.github/actions/run-tests + with: + upload-logs-name: logs-linux - name: Upload Codecov report uses: codecov/codecov-action@v3.1.1 with: @@ -73,8 +76,11 @@ jobs: with: go-version: ${{ env.GO_VERSION }} check-latest: true - - run: go test -v -run ^TestRunEventHostEnvironment$ ./... - # TODO merge coverage with test-linux + - name: Run Tests + uses: ./.github/actions/run-tests + with: + filter: '^TestRunEventHostEnvironment$' + upload-logs-name: logs-${{ matrix.os }} snapshot: name: snapshot