endpoint unit test #16
Workflow file for this run
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
| name: Api Tests | |
| on: | |
| pull_request: | |
| branches: | |
| - main | |
| types: | |
| - opened | |
| - synchronize | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| api_test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v4 | |
| with: | |
| go-version: '1.24.3' | |
| cache-dependency-path: MyMusicBoxApi/go.sum | |
| - name: Tidy modules (sync go.mod & go.sum) | |
| working-directory: MyMusicBoxApi | |
| run: go mod tidy | |
| - name: Run tests with coverage | |
| working-directory: MyMusicBoxApi | |
| run: | | |
| go test -v -coverprofile=coverage.out ./... | |
| go tool cover -func=coverage.out > coverage.txt | |
| - name: Post test coverage to PR | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const coverage = fs.readFileSync('MyMusicBoxApi/coverage.txt', 'utf8'); | |
| const lines = coverage.trim().split('\n'); | |
| const totalLine = lines[lines.length - 1]; | |
| const percentage = totalLine.split('\t').pop(); | |
| const body = `### 🧪 Go Test Coverage\n\n\`\`\`\n${totalLine}\n\`\`\``; | |
| github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: body | |
| }); | |
| - name: Generate coverage badge | |
| uses: ncruces/go-coverage-report@v0.4.1 | |
| with: | |
| coverage-file: MyMusicBoxApi/coverage.out | |
| output: .github/badges/coverage.svg | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| # Optional: Add a GIST_ID secret if you want to publish the badge externally | |
| # gist: ${{ secrets.GIST_ID }} | |
| - name: Upload coverage badge as artifact (optional) | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: coverage-badge | |
| path: .github/badges/coverage.svg |