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