| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/assert" |
| 7 |
) |
| 8 |
|
| 9 |
func TestParseConfigBool(t *testing.T) { |
| 10 |
t.Parallel() |
| 11 |
|
| 12 |
tests := []struct { |
| 13 |
in string |
| 14 |
want OptBool |
| 15 |
}{ |
| 16 |
// Truthy values, case-insensitive. |
| 17 |
{"true", OptBoolTrue}, |
| 18 |
{"True", OptBoolTrue}, |
| 19 |
{"TRUE", OptBoolTrue}, |
| 20 |
{"yes", OptBoolTrue}, |
| 21 |
{"Yes", OptBoolTrue}, |
| 22 |
{"YES", OptBoolTrue}, |
| 23 |
{"on", OptBoolTrue}, |
| 24 |
{"On", OptBoolTrue}, |
| 25 |
{"ON", OptBoolTrue}, |
| 26 |
{"1", OptBoolTrue}, |
| 27 |
|
| 28 |
// Falsy values, case-insensitive. |
| 29 |
{"false", OptBoolFalse}, |
| 30 |
{"False", OptBoolFalse}, |
| 31 |
{"FALSE", OptBoolFalse}, |
| 32 |
{"no", OptBoolFalse}, |
| 33 |
{"No", OptBoolFalse}, |
| 34 |
{"NO", OptBoolFalse}, |
| 35 |
{"off", OptBoolFalse}, |
| 36 |
{"Off", OptBoolFalse}, |
| 37 |
{"OFF", OptBoolFalse}, |
| 38 |
{"0", OptBoolFalse}, |
| 39 |
|
| 40 |
// Arbitrary integers, mirroring upstream's !!v on git_parse_int. |
| 41 |
{"2", OptBoolTrue}, |
| 42 |
{"-1", OptBoolTrue}, |
| 43 |
{"010", OptBoolTrue}, // strconv.Atoi parses decimal, not octal. |
| 44 |
{"99999", OptBoolTrue}, |
| 45 |
{"-0", OptBoolFalse}, |
| 46 |
{"0000", OptBoolFalse}, |
| 47 |
{"+1", OptBoolTrue}, |
| 48 |
|
| 49 |
// Anything else stays unset, including empty / unrecognised. |
| 50 |
{"", OptBoolUnset}, |
| 51 |
{"maybe", OptBoolUnset}, |
| 52 |
{"t", OptBoolUnset}, // strconv.ParseBool would accept this; Git does not. |
| 53 |
{"T", OptBoolUnset}, |
| 54 |
{"f", OptBoolUnset}, |
| 55 |
{"F", OptBoolUnset}, |
| 56 |
{"0x1", OptBoolUnset}, // not decimal |
| 57 |
{"1.5", OptBoolUnset}, // not an integer |
| 58 |
{" true ", OptBoolUnset}, // trimming is the caller's job. |
| 59 |
} |
| 60 |
|
| 61 |
for _, tc := range tests { |
| 62 |
t.Run(tc.in, func(t *testing.T) { |
| 63 |
t.Parallel() |
| 64 |
got := parseConfigBool(tc.in) |
| 65 |
assert.Equal(t, tc.want, got, "parseConfigBool(%q)", tc.in) |
| 66 |
}) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func TestUnmarshalProtectNTFSAcceptsGitStyleBooleans(t *testing.T) { |
| 71 |
t.Parallel() |
| 72 |
|
| 73 |
tests := []struct { |
| 74 |
value string |
| 75 |
want OptBool |
| 76 |
}{ |
| 77 |
{"true", OptBoolTrue}, |
| 78 |
{"yes", OptBoolTrue}, |
| 79 |
{"on", OptBoolTrue}, |
| 80 |
{"1", OptBoolTrue}, |
| 81 |
{"false", OptBoolFalse}, |
| 82 |
{"no", OptBoolFalse}, |
| 83 |
{"off", OptBoolFalse}, |
| 84 |
{"0", OptBoolFalse}, |
| 85 |
} |
| 86 |
|
| 87 |
for _, tc := range tests { |
| 88 |
t.Run(tc.value, func(t *testing.T) { |
| 89 |
t.Parallel() |
| 90 |
|
| 91 |
body := []byte("[core]\n\tprotectNTFS = " + tc.value + "\n\tprotectHFS = " + tc.value + "\n") |
| 92 |
cfg := NewConfig() |
| 93 |
err := cfg.Unmarshal(body) |
| 94 |
assert.NoError(t, err) |
| 95 |
assert.Equal(t, tc.want, cfg.Core.ProtectNTFS, "protectNTFS") |
| 96 |
assert.Equal(t, tc.want, cfg.Core.ProtectHFS, "protectHFS") |
| 97 |
}) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestUnmarshalProtectNTFSUnsetForUnrecognised(t *testing.T) { |
| 102 |
t.Parallel() |
| 103 |
|
| 104 |
body := []byte("[core]\n\tprotectNTFS = maybe\n\tprotectHFS = maybe\n") |
| 105 |
cfg := NewConfig() |
| 106 |
err := cfg.Unmarshal(body) |
| 107 |
assert.NoError(t, err) |
| 108 |
assert.Equal(t, OptBoolUnset, cfg.Core.ProtectNTFS, |
| 109 |
"unrecognised value should leave protectNTFS unset") |
| 110 |
assert.Equal(t, OptBoolUnset, cfg.Core.ProtectHFS, |
| 111 |
"unrecognised value should leave protectHFS unset") |
| 112 |
} |