1
0

feat: 配置 golangci-lint 静态分析并修复存量违规

- 新增 backend/.golangci.yml 配置 12 个 linter(forbidigo、errorlint、errcheck、staticcheck、revive、gocritic、gosec、bodyclose、noctx、nilerr、goimports、gocyclo)
- 新增 lefthook.yml 配置 pre-commit hook 自动运行 lint
- 修复存量代码违规:errors.Is/As 替换、zap.Error 替换、import 排序、errcheck 修复
- 更新 README 补充编码规范说明
- 归档 backend-code-lint 变更
This commit is contained in:
2026-04-24 13:01:48 +08:00
parent 4c78ab6cc8
commit 4c6b49099d
96 changed files with 1290 additions and 1348 deletions

View File

@@ -7,16 +7,17 @@ import (
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"gorm.io/gorm"
"nex/backend/internal/domain"
"nex/backend/internal/handler"
"nex/backend/internal/handler/middleware"
"nex/backend/internal/repository"
"nex/backend/internal/service"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"gorm.io/gorm"
)
func init() {
@@ -97,7 +98,7 @@ func TestOpenAI_CompleteFlow(t *testing.T) {
r.ServeHTTP(w, req)
assert.Equal(t, 201, w.Code)
var createdModel domain.Model
json.Unmarshal(w.Body.Bytes(), &createdModel)
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &createdModel))
assert.NotEmpty(t, createdModel.ID)
// 3. 列出 Provider
@@ -106,7 +107,7 @@ func TestOpenAI_CompleteFlow(t *testing.T) {
r.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
var providers []domain.Provider
json.Unmarshal(w.Body.Bytes(), &providers)
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &providers))
assert.Len(t, providers, 1)
assert.Equal(t, "sk-test-key", providers[0].APIKey)
@@ -116,7 +117,7 @@ func TestOpenAI_CompleteFlow(t *testing.T) {
r.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
var models []domain.Model
json.Unmarshal(w.Body.Bytes(), &models)
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &models))
assert.Len(t, models, 1)
assert.Equal(t, "gpt-4", models[0].ModelName)
@@ -163,7 +164,7 @@ func TestAnthropic_ModelCreation(t *testing.T) {
r.ServeHTTP(w, req)
assert.Equal(t, 201, w.Code)
var createdModel domain.Model
json.Unmarshal(w.Body.Bytes(), &createdModel)
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &createdModel))
// 验证创建成功
w = httptest.NewRecorder()
@@ -194,9 +195,9 @@ func TestStats_RecordingAndQuery(t *testing.T) {
// 直接通过 repository 记录统计(模拟代理请求后的统计记录)
statsRepo := repository.NewStatsRepository(db)
statsRepo.Record("p1", "gpt-4")
statsRepo.Record("p1", "gpt-4")
statsRepo.Record("p1", "gpt-4")
require.NoError(t, statsRepo.Record("p1", "gpt-4"))
require.NoError(t, statsRepo.Record("p1", "gpt-4"))
require.NoError(t, statsRepo.Record("p1", "gpt-4"))
// 查询统计
w = httptest.NewRecorder()
@@ -205,7 +206,7 @@ func TestStats_RecordingAndQuery(t *testing.T) {
assert.Equal(t, 200, w.Code)
var stats []domain.UsageStats
json.Unmarshal(w.Body.Bytes(), &stats)
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &stats))
assert.Len(t, stats, 1)
assert.Equal(t, 3, stats[0].RequestCount)