mirror of
https://github.com/rclone/rclone.git
synced 2025-06-06 04:44:35 +08:00
fs: Add string alternatives for setting options over the rc
Before this change options were read and set in native format. This means for example nanoseconds for durations or an integer for enumerated types, which isn't very convenient for humans. This change enables these types to be set with a string with the syntax as used in the command line instead, so `"10s"` rather than `10000000000` or `"DEBUG"` rather than `8` for log level.
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
@ -9,8 +10,15 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// Interface which flags must satisfy - only defined for _test.go
|
||||
// since we don't want to pull in pflag here
|
||||
type flagger interface {
|
||||
pflag.Value
|
||||
json.Unmarshaler
|
||||
}
|
||||
|
||||
// Check it satisfies the interface
|
||||
var _ pflag.Value = (*SizeSuffix)(nil)
|
||||
var _ flagger = (*SizeSuffix)(nil)
|
||||
|
||||
func TestSizeSuffixString(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
@ -102,3 +110,37 @@ func TestSizeSuffixScan(t *testing.T) {
|
||||
assert.Equal(t, 1, n)
|
||||
assert.Equal(t, SizeSuffix(17<<20), v)
|
||||
}
|
||||
|
||||
func TestSizeSuffixUnmarshalJSON(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
in string
|
||||
want int64
|
||||
err bool
|
||||
}{
|
||||
{`"0"`, 0, false},
|
||||
{`"102B"`, 102, false},
|
||||
{`"1K"`, 1024, false},
|
||||
{`"2.5"`, 1024 * 2.5, false},
|
||||
{`"1M"`, 1024 * 1024, false},
|
||||
{`"1.g"`, 1024 * 1024 * 1024, false},
|
||||
{`"10G"`, 10 * 1024 * 1024 * 1024, false},
|
||||
{`"off"`, -1, false},
|
||||
{`""`, 0, true},
|
||||
{`"1q"`, 0, true},
|
||||
{`"-1K"`, 0, true},
|
||||
{`0`, 0, false},
|
||||
{`102`, 102, false},
|
||||
{`1024`, 1024, false},
|
||||
{`1000000000`, 1000000000, false},
|
||||
{`1.1.1`, 0, true},
|
||||
} {
|
||||
var ss SizeSuffix
|
||||
err := json.Unmarshal([]byte(test.in), &ss)
|
||||
if test.err {
|
||||
require.Error(t, err, test.in)
|
||||
} else {
|
||||
require.NoError(t, err, test.in)
|
||||
}
|
||||
assert.Equal(t, test.want, int64(ss))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user