-
Notifications
You must be signed in to change notification settings - Fork 0
146 lines (121 loc) · 3.85 KB
/
test.yml
File metadata and controls
146 lines (121 loc) · 3.85 KB
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
name: Test and Coverage
on:
push:
branches: [ main, develop, '**-nl-to-cmd' ]
pull_request:
branches: [ main, develop ]
jobs:
test:
name: Test with Coverage
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['1.24', '1.25']
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Cache Go modules
uses: actions/cache@v4
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-${{ matrix.go-version }}-
- name: Download dependencies
run: go mod download
- name: Run tests
run: |
# 运行所有测试
go test -v ./...
# 只计算核心库代码的覆盖率(排除cmd目录)
go test -coverprofile=coverage.out ./internal/... ./pkg/...
- name: Run race detector
run: go test -v -race ./...
- name: Calculate coverage
id: coverage
run: |
# 提取总覆盖率,处理可能的格式变化
COVERAGE=$(go tool cover -func=coverage.out | grep total | grep -oP '\d+\.\d+(?=%)')
if [ -z "$COVERAGE" ]; then
# 如果上面的方法失败,尝试另一种方法
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $NF}' | sed 's/%//')
fi
echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT
echo "Test coverage: $COVERAGE%"
- name: Check coverage threshold
run: |
COVERAGE=${{ steps.coverage.outputs.coverage }}
THRESHOLD=60
if [ -z "$COVERAGE" ]; then
echo "❌ Failed to calculate coverage"
exit 1
fi
# 使用 awk 进行浮点数比较(更可靠)
if awk "BEGIN {exit !($COVERAGE < $THRESHOLD)}"; then
echo "❌ Coverage $COVERAGE% is below threshold $THRESHOLD%"
exit 1
else
echo "✅ Coverage $COVERAGE% meets threshold $THRESHOLD%"
fi
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
file: ./coverage.out
flags: unittests
name: codecov-umbrella
fail_ci_if_error: false
- name: Generate coverage report
if: matrix.go-version == '1.25'
run: go tool cover -html=coverage.out -o coverage.html
- name: Upload coverage report
if: matrix.go-version == '1.25'
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage.html
lint:
name: Lint
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.25'
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v9
with:
version: latest
args: --timeout=5m
skip-config-verify: true
build:
name: Build
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
go-version: ['1.25']
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Build
run: go build -v ./cmd/aicli
- name: Verify binary
if: runner.os != 'Windows'
run: |
./aicli --version || echo "Binary built successfully (version command not yet implemented)"
- name: Verify binary (Windows)
if: runner.os == 'Windows'
run: |
.\aicli.exe --version || echo "Binary built successfully (version command not yet implemented)"