Add Go formatting check to CI and fix code style #6
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: Test Suite | |
| on: | |
| push: | |
| pull_request: | |
| permissions: | |
| contents: read | |
| checks: write | |
| pull-requests: write | |
| jobs: | |
| format: | |
| name: Check Formatting | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: '1.21' | |
| cache: true | |
| - name: Check Go formatting | |
| run: | | |
| unformatted=$(gofmt -l .) | |
| if [ -n "$unformatted" ]; then | |
| echo "The following files are not formatted correctly:" | |
| echo "$unformatted" | |
| echo "" | |
| echo "Please run 'gofmt -w .' to format your code" | |
| exit 1 | |
| fi | |
| echo "All Go files are properly formatted" | |
| test: | |
| name: Run Tests | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: '1.21' | |
| cache: true | |
| - name: Download dependencies | |
| run: go mod download | |
| - name: Install go-junit-report | |
| run: go install github.com/jstemmer/go-junit-report/v2@latest | |
| - name: Run tests with coverage | |
| run: | | |
| go test ./... -v -race -coverprofile=coverage.out -covermode=atomic 2>&1 | tee test-output.txt || true | |
| cat test-output.txt | go-junit-report -set-exit-code > test-results.xml | |
| - name: Generate coverage report | |
| run: | | |
| echo "## Test Coverage Report" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| go tool cover -func=coverage.out | tail -n 1 | awk '{print "**Total Coverage: " $3 "**"}' >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "### Coverage by Package" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "| Package | Coverage |" >> $GITHUB_STEP_SUMMARY | |
| echo "|---------|----------|" >> $GITHUB_STEP_SUMMARY | |
| go tool cover -func=coverage.out | grep -v "total:" | awk '{print "| " $1 " | " $3 " |"}' | sort -u >> $GITHUB_STEP_SUMMARY | |
| - name: Test Report | |
| uses: dorny/test-reporter@v1 | |
| if: always() | |
| with: | |
| name: Go Test Results | |
| path: test-results.xml | |
| reporter: 'java-junit' | |
| fail-on-error: true | |
| fail-on-empty: true | |
| - name: Upload coverage to GitHub | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: coverage-report | |
| path: coverage.out | |
| - name: Check test results | |
| run: | | |
| if grep -q 'failures="[1-9]' test-results.xml || grep -q 'errors="[1-9]' test-results.xml; then | |
| echo "Some tests failed" | |
| exit 1 | |
| else | |
| echo "All tests passed" | |
| fi |