mirror of
https://github.com/ollama/ollama.git
synced 2026-05-13 14:27:00 +00:00
Merge 2d5da7d014 into 3af1a008e2
This commit is contained in:
commit
3695722160
2 changed files with 45 additions and 4 deletions
|
|
@ -179,7 +179,8 @@ func BoolWithDefault(k string) func(defaultValue bool) bool {
|
|||
if s := Var(k); s != "" {
|
||||
b, err := strconv.ParseBool(s)
|
||||
if err != nil {
|
||||
return true
|
||||
slog.Warn("invalid boolean value, using default", "key", k, "value", s, "default", defaultValue)
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return b
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package envconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"math"
|
||||
"os"
|
||||
|
|
@ -188,9 +189,12 @@ func TestBool(t *testing.T) {
|
|||
"false": false,
|
||||
"1": true,
|
||||
"0": false,
|
||||
// invalid values
|
||||
"random": true,
|
||||
"something": true,
|
||||
// invalid values fall back to default (false for Bool)
|
||||
"random": false,
|
||||
"something": false,
|
||||
"yes": false,
|
||||
"on": false,
|
||||
"enabled": false,
|
||||
}
|
||||
|
||||
for k, v := range cases {
|
||||
|
|
@ -203,6 +207,42 @@ func TestBool(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestBoolWithDefault(t *testing.T) {
|
||||
cases := []struct {
|
||||
value string
|
||||
defaultValue bool
|
||||
expect bool
|
||||
}{
|
||||
// valid values override the default
|
||||
{"true", false, true},
|
||||
{"false", true, false},
|
||||
{"1", false, true},
|
||||
{"0", true, false},
|
||||
// empty value returns the default unchanged
|
||||
{"", true, true},
|
||||
{"", false, false},
|
||||
// invalid values fall back to the default, not hardcoded true
|
||||
{"yes", false, false},
|
||||
{"yes", true, true},
|
||||
{"on", false, false},
|
||||
{"on", true, true},
|
||||
{"enabled", false, false},
|
||||
{"enabled", true, true},
|
||||
{"random", false, false},
|
||||
{"random", true, true},
|
||||
}
|
||||
|
||||
for _, tt := range cases {
|
||||
name := fmt.Sprintf("value=%q default=%v", tt.value, tt.defaultValue)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Setenv("OLLAMA_BOOL_WITH_DEFAULT", tt.value)
|
||||
if b := BoolWithDefault("OLLAMA_BOOL_WITH_DEFAULT")(tt.defaultValue); b != tt.expect {
|
||||
t.Errorf("%s: expected %v, got %v", name, tt.expect, b)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint(t *testing.T) {
|
||||
cases := map[string]uint{
|
||||
"0": 0,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue