| 1 | package config_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | . "github.com/git-lfs/git-lfs/v3/config" |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | ) |
| 9 | |
| 10 | func TestEnvironmentGetDelegatesToFetcher(t *testing.T) { |
| 11 | fetcher := MapFetcher(map[string][]string{ |
| 12 | "foo": []string{"bar", "baz"}, |
| 13 | }) |
| 14 | |
| 15 | env := EnvironmentOf(fetcher) |
| 16 | val, ok := env.Get("foo") |
| 17 | |
| 18 | assert.True(t, ok) |
| 19 | assert.Equal(t, "baz", val) |
| 20 | } |
| 21 | |
| 22 | func TestEnvironmentGetAllDelegatesToFetcher(t *testing.T) { |
| 23 | fetcher := MapFetcher(map[string][]string{ |
| 24 | "foo": []string{"bar", "baz"}, |
| 25 | }) |
| 26 | |
| 27 | env := EnvironmentOf(fetcher) |
| 28 | vals := env.GetAll("foo") |
| 29 | |
| 30 | assert.Equal(t, []string{"bar", "baz"}, vals) |
| 31 | } |
| 32 | |
| 33 | func TestEnvironmentUnsetBoolDefault(t *testing.T) { |
| 34 | env := EnvironmentOf(MapFetcher(nil)) |
| 35 | assert.True(t, env.Bool("unset", true)) |
| 36 | } |
| 37 | |
| 38 | func TestEnvironmentBoolTruthyConversion(t *testing.T) { |
| 39 | for _, c := range []EnvironmentConversionTestCase{ |
| 40 | {"", false, GetBoolDefault(false)}, |
| 41 | |
| 42 | {"true", true, GetBoolDefault(false)}, |
| 43 | {"1", true, GetBoolDefault(false)}, |
| 44 | {"on", true, GetBoolDefault(false)}, |
| 45 | {"yes", true, GetBoolDefault(false)}, |
| 46 | {"t", true, GetBoolDefault(false)}, |
| 47 | |
| 48 | {"false", false, GetBoolDefault(true)}, |
| 49 | {"0", false, GetBoolDefault(true)}, |
| 50 | {"off", false, GetBoolDefault(true)}, |
| 51 | {"no", false, GetBoolDefault(true)}, |
| 52 | {"f", false, GetBoolDefault(true)}, |
| 53 | } { |
| 54 | c.Assert(t) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestEnvironmentIntTestCases(t *testing.T) { |
| 59 | for _, c := range []EnvironmentConversionTestCase{ |
| 60 | {"", 1, GetIntDefault(1)}, |
| 61 | |
| 62 | {"1", 1, GetIntDefault(0)}, |
| 63 | {"3", 3, GetIntDefault(0)}, |
| 64 | |
| 65 | {"malformed", 7, GetIntDefault(7)}, |
| 66 | } { |
| 67 | c.Assert(t) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | type EnvironmentConversionTestCase struct { |
| 72 | Val string |
| 73 | Expected interface{} |
| 74 | |
| 75 | GotFn func(env Environment, key string) interface{} |
| 76 | } |
| 77 | |
| 78 | var ( |
| 79 | GetBoolDefault = func(def bool) func(e Environment, key string) interface{} { |
| 80 | return func(e Environment, key string) interface{} { |
| 81 | return e.Bool(key, def) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | GetIntDefault = func(def int) func(e Environment, key string) interface{} { |
| 86 | return func(e Environment, key string) interface{} { |
| 87 | return e.Int(key, def) |
| 88 | } |
| 89 | } |
| 90 | ) |
| 91 | |
| 92 | func (c *EnvironmentConversionTestCase) Assert(t *testing.T) { |
| 93 | fetcher := MapFetcher(map[string][]string{ |
| 94 | c.Val: []string{c.Val}, |
| 95 | }) |
| 96 | |
| 97 | env := EnvironmentOf(fetcher) |
| 98 | got := c.GotFn(env, c.Val) |
| 99 | |
| 100 | if c.Expected != got { |
| 101 | t.Errorf("lfs/config: expected val=%q to be %q (got: %q)", c.Val, c.Expected, got) |
| 102 | } |
| 103 | } |