-
Notifications
You must be signed in to change notification settings - Fork 0
150 lines (135 loc) · 5.12 KB
/
run-tests.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
name: Run Tests
on:
push:
branches:
- 'main'
pull_request:
branches: ["main"]
jobs:
test:
name: Run Tests
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_PORT: 5432
POSTGRES_DB: postgres
options: >-
--health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
ports:
- 5432:5432
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'
cache: true
- name: Install dependencies
run: go mod download
- name: Install Goose
run: go install github.com/pressly/goose/v3/cmd/[email protected]
- name: Run migrations
run: goose -dir .sqlc/migrations postgres "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" up
- name: Run tests
id: tests
continue-on-error: true
run: |
# run tests and capture output
go test -v -coverprofile=coverage.out ./... 2>&1 | tee test_output.txt
# store the exit code explicitly
echo "::set-output name=exit_code::${PIPESTATUS[0]}"
- name: Generate coverage report
run: go tool cover -html=coverage.out -o coverage.html
- name: Upload coverage report
if: ${{ !env.ACT && github.event_name == 'pull_request' }}
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage.html
- name: Comment PR
if: ${{ !env.ACT && github.event_name == 'pull_request' && always() }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
// Read test output
const testOutput = fs.readFileSync('test_output.txt', 'utf8');
// Get coverage - look for the last coverage number in the output
let coverage = 'N/A';
const coverageMatches = testOutput.match(/coverage: (\d+\.\d+)% of statements/g) || [];
if (coverageMatches.length > 0) {
const lastMatch = coverageMatches[coverageMatches.length - 1];
coverage = lastMatch.match(/(\d+\.\d+)%/)[1] + '%';
}
// Check if any tests failed
const hasFailed = testOutput.includes('FAIL') && !testOutput.includes('FAIL\t[build failed]');
const testStatus = hasFailed ? 'failure' : 'success';
const color = testStatus === 'success' ? '✅' : '❌';
// Parse test failures
let failureDetails = '';
if (hasFailed) {
const errorTraces = testOutput.match(/\s+.*?_test\.go:\d+:[\s\S]*?Test:\s+.*$/gm) || [];
const failures = testOutput.match(/--- FAIL: .*?(?=(?:---|\z))/gs) || [];
failureDetails = `
<details>
<summary>❌ Test Failures</summary>
\`\`\`
${failures.join('\n')}
Error Details:
${errorTraces.map(trace => trace.trim()).join('\n')}
\`\`\`
</details>
`;
}
const output = `### Test Results ${color}
**Status**: ${testStatus}
**Coverage**: ${coverage}
**OS**: \`${{ runner.os }}\`
${failureDetails}
<details>
<summary>Test Details</summary>
* Triggered by: @${{ github.actor }}
* Commit: ${{ github.sha }}
* Branch: ${{ github.ref }}
* Workflow: ${{ github.workflow }}
</details>`;
// Find existing comment
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('### Test Results')
);
if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output
});
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: output
});
}
- uses: actions/cache@v4
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-