mirror of
https://github.com/rclone/rclone.git
synced 2025-08-16 16:41:34 +02:00
configstruct: fix parsing of invalid booleans in the config
Apparently fmt.Sscanln doesn't parse bool's properly and this isn't likely to be fixed by the Go team who regard sscanf as a mistake. This only uses sscan for integers and uses the correct routine for everything else. This also implements parsing time.Duration See: https://github.com/golang/go/issues/43306
This commit is contained in:
@ -1,11 +1,9 @@
|
||||
package configstruct
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCamelToSnake(t *testing.T) {
|
||||
@ -22,39 +20,3 @@ func TestCamelToSnake(t *testing.T) {
|
||||
assert.Equal(t, test.want, got, test.in)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringToInterface(t *testing.T) {
|
||||
item := struct{ A int }{2}
|
||||
for _, test := range []struct {
|
||||
in string
|
||||
def interface{}
|
||||
want interface{}
|
||||
err string
|
||||
}{
|
||||
{"", string(""), "", ""},
|
||||
{" string ", string(""), " string ", ""},
|
||||
{"123", int(0), int(123), ""},
|
||||
{"0x123", int(0), int(0x123), ""},
|
||||
{" 0x123 ", int(0), int(0x123), ""},
|
||||
{"-123", int(0), int(-123), ""},
|
||||
{"0", false, false, ""},
|
||||
{"1", false, true, ""},
|
||||
{"FALSE", false, false, ""},
|
||||
{"true", false, true, ""},
|
||||
{"123", uint(0), uint(123), ""},
|
||||
{"123", int64(0), int64(123), ""},
|
||||
{"123x", int64(0), nil, "parsing \"123x\" as int64 failed: expected newline"},
|
||||
{"truth", false, nil, "parsing \"truth\" as bool failed: syntax error scanning boolean"},
|
||||
{"struct", item, nil, "parsing \"struct\" as struct { A int } failed: can't scan type: *struct { A int }"},
|
||||
} {
|
||||
what := fmt.Sprintf("parse %q as %T", test.in, test.def)
|
||||
got, err := StringToInterface(test.def, test.in)
|
||||
if test.err == "" {
|
||||
require.NoError(t, err, what)
|
||||
assert.Equal(t, test.want, got, what)
|
||||
} else {
|
||||
assert.Nil(t, got)
|
||||
assert.EqualError(t, err, test.err, what)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user