mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
Six env flags, two conventions, and both failed silently against the other. Five read `== "true"`, so BOOTNODE_ENABLED=1 left bootnode off and CALENDAR_ENABLED=1 left the calendar off. One read `!= "1"`, so BASE_ENABLE_ADMIN_UI=true served no admin. And ZAP_DISABLED read `!= "true"`, so an operator who wrote ZAP_DISABLED=1 to stop the listener watched it come up anyway. In every case the flag was accepted, ignored, and never mentioned again. This tree already answers "read a boolean from a string" four times, with strconv.ParseBool (tools/filesystem, tools/search, tools/router, core/settings_query). The hand-rolled comparisons were the deviation, so osutils.Bool is that answer given a name and a default, and all six sites read it. Strictly more permissive: every spelling that worked before still works, and the ones that silently did nothing now do what they say. An unparseable value yields the default rather than its opposite — someone who typed something meaningless has not asked for the other thing. Coverage: plugins/zap 5.9% -> 23.5%, and DefaultConfig is pinned including the case that started this (ZAP_DISABLED=1 must disable) and a garbage ZAP_PORT, which must keep 9999 rather than quietly binding somewhere else. Co-authored-by: Hanzo Dev <[email protected]>
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package osutils_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hanzoai/base/tools/osutils"
|
|
)
|
|
|
|
func TestBool(t *testing.T) {
|
|
const name = "BASE_TEST_FLAG"
|
|
for _, tc := range []struct {
|
|
raw string
|
|
set bool
|
|
def bool
|
|
want bool
|
|
}{
|
|
// The spellings an operator actually writes, all honoured.
|
|
{"1", true, false, true},
|
|
{"true", true, false, true},
|
|
{"TRUE", true, false, true},
|
|
{"True", true, false, true},
|
|
{"t", true, false, true},
|
|
{"0", true, true, false},
|
|
{"false", true, true, false},
|
|
{"FALSE", true, true, false},
|
|
{"f", true, true, false},
|
|
// Surrounding space is a copy-paste artefact, not an opinion.
|
|
{" true ", true, false, true},
|
|
{" 0 ", true, true, false},
|
|
// Nothing said: the default stands.
|
|
{"", true, true, true},
|
|
{"", true, false, false},
|
|
{"", false, true, true},
|
|
{"", false, false, false},
|
|
// Nothing meaningful said: still the default, never its opposite.
|
|
{"yes", true, false, false},
|
|
{"on", true, false, false},
|
|
{"maybe", true, true, true},
|
|
{"2", true, false, false},
|
|
} {
|
|
label := tc.raw
|
|
if !tc.set {
|
|
label = "<unset>"
|
|
}
|
|
t.Run(label, func(t *testing.T) {
|
|
if tc.set {
|
|
t.Setenv(name, tc.raw)
|
|
} else {
|
|
t.Setenv(name, "")
|
|
}
|
|
if got := osutils.Bool(name, tc.def); got != tc.want {
|
|
t.Fatalf("Bool(%s=%q, def=%v) = %v, want %v", name, tc.raw, tc.def, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|