Skip to content

Commit 1c20ebc

Browse files
committed
添加GitHub Actions工作流配置和示例测试文件
1 parent d281542 commit 1c20ebc

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-0
lines changed

.github/act-readme.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 如何使用act在本地测试GitHub Actions
2+
3+
[act](https://github.com/nektos/act) 是一个工具,可以在本地运行GitHub Actions,无需将代码推送到GitHub即可测试工作流。
4+
5+
## 安装act
6+
7+
### macOS
8+
```bash
9+
brew install act
10+
```
11+
12+
### 其他操作系统
13+
请参考[官方安装指南](https://github.com/nektos/act#installation)
14+
15+
## 使用方法
16+
17+
在项目根目录下运行:
18+
19+
```bash
20+
# 列出所有可用的actions
21+
act -l
22+
23+
# 运行所有actions
24+
act
25+
26+
# 只运行测试作业
27+
act -j test
28+
29+
# 使用指定的事件触发工作流
30+
act push
31+
32+
# 在运行act时指定要使用的Docker镜像(必要时)
33+
act -P ubuntu-latest=node:16-buster-slim
34+
```
35+
36+
## 针对本项目的示例命令
37+
38+
在推送代码前,您可以通过以下命令来测试我们的测试工作流:
39+
40+
```bash
41+
# 运行测试工作流
42+
act -j test
43+
44+
# 或者指定使用与Go 1.20兼容的Docker镜像
45+
act -j test -P ubuntu-latest=golang:1.20
46+
```
47+
48+
## 注意事项
49+
50+
1. act使用Docker来模拟GitHub Actions的环境,因此您需要安装Docker。
51+
2. 某些GitHub Actions可能在本地运行时与在GitHub上运行略有不同,但对于基本的测试目的来说应该足够了。
52+
3. act默认使用最小的Docker镜像,如果需要更完整的环境,可以使用 `-P ubuntu-latest=catthehacker/ubuntu:act-latest` 参数。

.github/workflows/test.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Go Tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
name: Run Tests
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
17+
- name: Set up Go
18+
uses: actions/setup-go@v4
19+
with:
20+
go-version: '1.20'
21+
cache: true
22+
23+
- name: Install dependencies
24+
run: go mod download
25+
26+
- name: Verify dependencies
27+
run: go mod verify
28+
29+
- name: Run tests
30+
run: go test -v ./...
31+
32+
- name: Run tests with race detection
33+
run: go test -race -v ./...
34+
35+
- name: Run go vet
36+
run: go vet ./...
37+
38+
- name: Install golangci-lint
39+
if: success() || failure()
40+
uses: golangci/golangci-lint-action@v3
41+
with:
42+
version: latest
43+
args: --timeout=5m
44+
45+
- name: Build
46+
run: go build -v ./...

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,56 @@ Go Composer SDK 是一个全面的 Go 语言库,提供对 PHP Composer 包管
99
- **全面的功能集**:包含依赖管理、仓库配置、认证、安全审计等功能
1010
- **多平台支持**:支持 Windows、macOS 和 Linux
1111
- **模块化设计**:按功能分组的代码结构,易于使用和维护
12+
- **自动化测试**:使用GitHub Actions自动化测试确保代码质量
1213

1314
## 安装
1415

1516
```bash
1617
go get github.com/scagogogo/go-composer-sdk
1718
```
1819

20+
## 开发与测试
21+
22+
### 运行测试
23+
24+
```bash
25+
# 运行所有测试
26+
go test -v ./...
27+
28+
# 运行带有race检测的测试
29+
go test -race -v ./...
30+
```
31+
32+
### GitHub Actions
33+
34+
本项目使用GitHub Actions进行持续集成,每次提交代码或创建Pull Request时会自动运行测试。GitHub Actions工作流配置位于 `.github/workflows/test.yml`
35+
36+
工作流程包括:
37+
- 安装Go依赖
38+
- 验证依赖关系
39+
- 运行单元测试
40+
- 运行带有race检测的测试
41+
- 运行go vet静态分析
42+
- 使用golangci-lint进行代码质量检查
43+
- 构建项目
44+
45+
### 在本地测试GitHub Actions
46+
47+
您可以使用[act](https://github.com/nektos/act)工具在本地测试GitHub Actions工作流:
48+
49+
```bash
50+
# 安装act (macOS)
51+
brew install act
52+
53+
# 列出所有可用的actions
54+
act -l
55+
56+
# 运行测试作业
57+
act -j test -P ubuntu-latest=golang:1.20 --container-architecture linux/amd64
58+
```
59+
60+
详细的说明请参考 `.github/act-readme.md` 文件。
61+
1962
## 快速开始
2063

2164
### 基本用法

pkg/examples/example.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package examples
2+
3+
// Sum 计算两个整数的和
4+
func Sum(a, b int) int {
5+
return a + b
6+
}

pkg/examples/example_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package examples
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// 测试示例
10+
func TestSum(t *testing.T) {
11+
// 使用testify/assert简化测试断言
12+
assert.Equal(t, 5, Sum(2, 3), "Sum(2, 3) should equal 5")
13+
assert.NotEqual(t, 10, Sum(2, 3), "Sum(2, 3) should not equal 10")
14+
}
15+
16+
// 表格驱动测试示例
17+
func TestSumTableDriven(t *testing.T) {
18+
tests := []struct {
19+
name string
20+
a, b int
21+
expected int
22+
}{
23+
{"positive numbers", 2, 3, 5},
24+
{"negative numbers", -2, -3, -5},
25+
{"mixed numbers", -2, 3, 1},
26+
}
27+
28+
for _, tt := range tests {
29+
t.Run(tt.name, func(t *testing.T) {
30+
result := Sum(tt.a, tt.b)
31+
assert.Equal(t, tt.expected, result, "Sum(%d, %d) should equal %d", tt.a, tt.b, tt.expected)
32+
})
33+
}
34+
}

scripts/test-action.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
# 测试GitHub Actions脚本
4+
# 在提交代码前运行此脚本,以确保GitHub Actions工作流可以正常运行
5+
6+
set -e
7+
8+
# 检查act是否已安装
9+
if ! command -v act &> /dev/null; then
10+
echo "错误: act工具未安装。请通过以下命令安装:"
11+
echo " macOS: brew install act"
12+
echo " 其他系统: 请访问 https://github.com/nektos/act#installation"
13+
exit 1
14+
fi
15+
16+
# 检查docker是否运行
17+
if ! docker info &> /dev/null; then
18+
echo "错误: Docker服务未运行,请启动Docker服务后再试。"
19+
exit 1
20+
fi
21+
22+
echo "运行单元测试..."
23+
go test -v ./...
24+
25+
echo "运行GitHub Actions工作流..."
26+
act -j test -P ubuntu-latest=golang:1.20 --container-architecture linux/amd64
27+
28+
if [ $? -eq 0 ]; then
29+
echo "✅ 测试通过! 现在可以安全地提交代码了。"
30+
else
31+
echo "❌ 测试失败! 请修复问题后再提交代码。"
32+
exit 1
33+
fi

0 commit comments

Comments
 (0)