chore: stabilize lint and verify builds

This commit is contained in:
2026-02-06 11:51:32 +08:00
parent edede17880
commit 1782f64417
114 changed files with 3032 additions and 1345 deletions

View File

@@ -2,6 +2,7 @@ package testx
import (
"context"
"fmt"
"os"
"testing"
@@ -21,7 +22,7 @@ import (
"go.uber.org/dig"
"github.com/rogeecn/fabfile"
. "github.com/smartystreets/goconvey/convey"
convey "github.com/smartystreets/goconvey/convey"
)
func Default(providers ...container.ProviderContainer) container.Providers {
@@ -39,7 +40,7 @@ type orderRefundTestWorker struct {
river.WorkerDefaults[jobs_args.OrderRefundJob]
}
func (w *orderRefundTestWorker) Work(ctx context.Context, job *river.Job[jobs_args.OrderRefundJob]) error {
func (w *orderRefundTestWorker) Work(_ context.Context, _ *river.Job[jobs_args.OrderRefundJob]) error {
return nil
}
@@ -47,7 +48,7 @@ type mediaAssetProcessTestWorker struct {
river.WorkerDefaults[jobs_args.MediaAssetProcessJob]
}
func (w *mediaAssetProcessTestWorker) Work(ctx context.Context, job *river.Job[jobs_args.MediaAssetProcessJob]) error {
func (w *mediaAssetProcessTestWorker) Work(_ context.Context, _ *river.Job[jobs_args.MediaAssetProcessJob]) error {
return nil
}
@@ -65,26 +66,29 @@ func (w *notificationTestWorker) Work(ctx context.Context, job *river.Job[jobs_a
Content: arg.Content,
IsRead: false,
}
return models.NotificationQuery.WithContext(ctx).Create(n)
}
func testJobWorkersProvider() container.ProviderContainer {
return container.ProviderContainer{
Provider: func(opts ...opt.Option) error {
Provider: func(_ ...opt.Option) error {
return container.Container.Provide(func(__job *job.Job) (contracts.Initial, error) {
obj := &orderRefundTestWorker{}
if err := river.AddWorkerSafely(__job.Workers, obj); err != nil {
return nil, err
return nil, fmt.Errorf("register order refund test worker: %w", err)
}
obj2 := &mediaAssetProcessTestWorker{}
if err := river.AddWorkerSafely(__job.Workers, obj2); err != nil {
return nil, err
return nil, fmt.Errorf("register media process test worker: %w", err)
}
obj3 := &notificationTestWorker{}
if err := river.AddWorkerSafely(__job.Workers, obj3); err != nil {
return nil, err
return nil, fmt.Errorf("register notification test worker: %w", err)
}
return obj, nil
}, atom.GroupInitial)
},
@@ -92,40 +96,38 @@ func testJobWorkersProvider() container.ProviderContainer {
}
func Serve(providers container.Providers, t *testing.T, invoke any) {
Convey("tests boot up", t, func() {
// 关键语义:测试用例可能会在同一进程内多次调用 Serve。
// atom/config.Load 会向全局 dig 容器重复 Provide *viper.Viper若不重置会导致 “already provided”。
// 因此每次测试启动前都重置容器,保证各测试套件相互独立。
convey.Convey("tests boot up", t, func() {
baseCtx := context.Background()
container.Close()
container.Container = dig.New()
So(container.Container.Provide(func() context.Context { return context.Background() }), ShouldBeNil)
convey.So(container.Container.Provide(func() context.Context { return baseCtx }), convey.ShouldBeNil)
file := fabfile.MustFind("config.toml")
// 支持通过 ENV_LOCAL 指定测试环境配置config.<env>.toml
localEnv := os.Getenv("ENV_LOCAL")
if localEnv != "" {
file = fabfile.MustFind("config." + localEnv + ".toml")
}
So(atom.LoadProviders(file, providers), ShouldBeNil)
So(os.Setenv("JOB_INLINE", "1"), ShouldBeNil)
convey.So(atom.LoadProviders(file, providers), convey.ShouldBeNil)
convey.So(os.Setenv("JOB_INLINE", "1"), convey.ShouldBeNil)
t.Cleanup(func() {
_ = os.Unsetenv("JOB_INLINE")
})
So(container.Container.Invoke(func(p struct {
convey.So(container.Container.Invoke(func(params struct {
dig.In
Initials []contracts.Initial `group:"initials"`
Job *job.Job
},
) error {
_ = p.Initials
ctx, cancel := context.WithCancel(context.Background())
_ = params.Initials
jobCtx, cancel := context.WithCancel(baseCtx)
t.Cleanup(cancel)
go func() {
_ = p.Job.Start(ctx)
_ = params.Job.Start(jobCtx)
}()
return nil
}), ShouldBeNil)
So(container.Container.Invoke(invoke), ShouldBeNil)
}), convey.ShouldBeNil)
convey.So(container.Container.Invoke(invoke), convey.ShouldBeNil)
})
}