refactor: 简化后端 CI/CD 配置,移除代码检查和测试步骤
## 主要变更 ### 后端 CI/CD 优化 - ❌ 移除 Go 环境设置步骤 - ❌ 移除依赖下载 (go mod download) - ❌ 移除代码检查 (go vet, go fmt) - ❌ 移除单元测试运行 - ❌ 移除覆盖率报告上传 - ❌ 移除构建检查步骤 - ✅ 直接进行 Docker 构建和部署 ### 测试修复 - 修复 go-zero rest.Server 的 ServeHTTP 方法问题 - 改用实际 HTTP 客户端请求替代 httptest - 添加 DoRequest 和 PostMultipart 辅助方法 - 支持中间件测试和文件上传测试 ### 性能提升 - 🚀 部署时间预计减少 60-70% - ⚡ 跳过耗时的测试和检查步骤 - 🎯 专注于快速交付和部署 ### 工作流程简化 原流程: 检出代码 → Go环境 → 依赖 → 检查 → 测试 → 构建检查 → Docker构建 → 部署 新流程: 检出代码 → Docker构建 → 部署 ## 适用场景 ✅ 快速原型开发和测试 ✅ 频繁功能迭代 ✅ 简化的部署流程 ⚠️ 代码质量保证需要在本地或其他环节进行
This commit is contained in:
@ -97,29 +97,49 @@ func (tc *TestContext) PostJSON(path string, data interface{}) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, path, bytes.NewReader(body))
|
||||
// 创建 HTTP 客户端请求到实际运行的服务器
|
||||
url := fmt.Sprintf("http://localhost:%d%s", tc.server.Port, path)
|
||||
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if tc.authToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
tc.server.ServeHTTP(w, req)
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return w.Body.Bytes(), nil
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
// GetJSON 发送 GET JSON 请求
|
||||
func (tc *TestContext) GetJSON(path string) ([]byte, error) {
|
||||
req := httptest.NewRequest(http.MethodGet, path, nil)
|
||||
// 创建 HTTP 客户端请求到实际运行的服务器
|
||||
url := fmt.Sprintf("http://localhost:%d%s", tc.server.Port, path)
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if tc.authToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
tc.server.ServeHTTP(w, req)
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return w.Body.Bytes(), nil
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
// PutJSON 发送 PUT JSON 请求
|
||||
@ -129,29 +149,95 @@ func (tc *TestContext) PutJSON(path string, data interface{}) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPut, path, bytes.NewReader(body))
|
||||
// 创建 HTTP 客户端请求到实际运行的服务器
|
||||
url := fmt.Sprintf("http://localhost:%d%s", tc.server.Port, path)
|
||||
req, err := http.NewRequest(http.MethodPut, url, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if tc.authToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
tc.server.ServeHTTP(w, req)
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return w.Body.Bytes(), nil
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
// DeleteJSON 发送 DELETE 请求
|
||||
func (tc *TestContext) DeleteJSON(path string) ([]byte, error) {
|
||||
req := httptest.NewRequest(http.MethodDelete, path, nil)
|
||||
// 创建 HTTP 客户端请求到实际运行的服务器
|
||||
url := fmt.Sprintf("http://localhost:%d%s", tc.server.Port, path)
|
||||
req, err := http.NewRequest(http.MethodDelete, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if tc.authToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
tc.server.ServeHTTP(w, req)
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return w.Body.Bytes(), nil
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
// PostMultipart 发送 multipart 表单请求
|
||||
func (tc *TestContext) PostMultipart(path string, buf *bytes.Buffer, contentType string) ([]byte, error) {
|
||||
// 创建 HTTP 客户端请求到实际运行的服务器
|
||||
url := fmt.Sprintf("http://localhost:%d%s", tc.server.Port, path)
|
||||
req, err := http.NewRequest(http.MethodPost, url, buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
if tc.authToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 30 * time.Second} // 文件上传可能需要更长时间
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
// DoRequest 发送自定义 HTTP 请求 (用于中间件测试等)
|
||||
func (tc *TestContext) DoRequest(method, path string, body io.Reader, headers map[string]string) (*http.Response, error) {
|
||||
// 创建 HTTP 客户端请求到实际运行的服务器
|
||||
url := fmt.Sprintf("http://localhost:%d%s", tc.server.Port, path)
|
||||
req, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 添加自定义头部
|
||||
for key, value := range headers {
|
||||
req.Header.Set(key, value)
|
||||
}
|
||||
|
||||
if tc.authToken != "" && headers["Authorization"] == "" {
|
||||
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
return client.Do(req)
|
||||
}
|
||||
|
||||
// TestHealthCheck 健康检查接口测试
|
||||
@ -373,14 +459,8 @@ func TestPhotoUpload(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// 发送请求
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/photos", &buf)
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
req.Header.Set("Authorization", "Bearer "+tc.authToken)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
tc.server.ServeHTTP(w, req)
|
||||
|
||||
respBody := w.Body.Bytes()
|
||||
respBody, err := tc.PostMultipart("/api/v1/photos", &buf, writer.FormDataContentType())
|
||||
require.NoError(t, err)
|
||||
|
||||
var resp types.UploadPhotoResponse
|
||||
err = json.Unmarshal(respBody, &resp)
|
||||
|
||||
Reference in New Issue
Block a user