From 4d7bd341135e3cea601d56560512a6d9e4edd057 Mon Sep 17 00:00:00 2001 From: bang <3656828039@qq.com> Date: Sun, 14 Jun 2026 22:35:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(config):=20TestDefaultConfigValues=20?= =?UTF-8?q?=E6=94=B9=E6=B5=8B=E7=BA=AF=E9=BB=98=E8=AE=A4=E5=80=BC=EF=BC=8C?= =?UTF-8?q?=E6=A0=B9=E6=B2=BB=E9=9D=9E=20hermetic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit NewGlobalConfig 会先填默认再被 config.json 覆盖,旧测试却把它当纯默认值断言, 导致默认与 json 不一致的字段(MaxMemTableSize 1024 vs 10000)恒红,且依赖文件存在。 抽出 defaultGlobalConfig()(纯默认,不读文件/不解析命令行),NewGlobalConfig 在其上 叠加 Init+ParseFlags;测试改测 defaultGlobalConfig(),名副其实且不再依赖外部文件。 Co-Authored-By: Claude Opus 4.8 (1M context) --- config/global.go | 10 ++++++++-- config/global_test.go | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/config/global.go b/config/global.go index 06d7293..3ff092a 100644 --- a/config/global.go +++ b/config/global.go @@ -91,9 +91,11 @@ func (g *GlobalConfig) Init() { slog.Info("config initialized") } -func NewGlobalConfig() *GlobalConfig { +// defaultGlobalConfig 返回纯代码默认值,不读取配置文件、不解析命令行。 +// NewGlobalConfig 在其上叠加 Init()(文件覆盖) 与 ParseFlags()(命令行覆盖)。 +func defaultGlobalConfig() *GlobalConfig { logDir := defaultLogDir() - global := &GlobalConfig{ + return &GlobalConfig{ Name: "Raft", Port: 8080, @@ -116,6 +118,10 @@ func NewGlobalConfig() *GlobalConfig { RaftSnapshotThreshold: 1000, // 默认快照阈值 RaftSnapshotKeepEntries: 100, // 默认保留条目数 } +} + +func NewGlobalConfig() *GlobalConfig { + global := defaultGlobalConfig() global.Init() global.ParseFlags() return global diff --git a/config/global_test.go b/config/global_test.go index 6a9434f..2a5c854 100644 --- a/config/global_test.go +++ b/config/global_test.go @@ -104,7 +104,9 @@ func TestParseFlagsPriority(t *testing.T) { } func TestDefaultConfigValues(t *testing.T) { - g := NewGlobalConfig() + // 测纯代码默认值:用 defaultGlobalConfig() 而非 NewGlobalConfig(), + // 后者会被 config.json 覆盖,使本测试依赖文件是否存在/内容(非 hermetic)。 + g := defaultGlobalConfig() expectedDefaults := []struct { name string