mirror of
https://github.com/rclone/rclone.git
synced 2026-01-03 17:13:18 +00: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,8 @@
|
||||
package vfscommon
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
@@ -10,6 +12,9 @@ import (
|
||||
// Check CacheMode it satisfies the pflag interface
|
||||
var _ pflag.Value = (*CacheMode)(nil)
|
||||
|
||||
// Check CacheMode it satisfies the json.Unmarshaller interface
|
||||
var _ json.Unmarshaler = (*CacheMode)(nil)
|
||||
|
||||
func TestCacheModeString(t *testing.T) {
|
||||
assert.Equal(t, "off", CacheModeOff.String())
|
||||
assert.Equal(t, "full", CacheModeFull.String())
|
||||
@@ -34,3 +39,27 @@ func TestCacheModeType(t *testing.T) {
|
||||
var m CacheMode
|
||||
assert.Equal(t, "CacheMode", m.Type())
|
||||
}
|
||||
|
||||
func TestCacheModeUnmarshalJSON(t *testing.T) {
|
||||
var m CacheMode
|
||||
|
||||
err := json.Unmarshal([]byte(`"full"`), &m)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, CacheModeFull, m)
|
||||
|
||||
err = json.Unmarshal([]byte(`"potato"`), &m)
|
||||
assert.Error(t, err, "Unknown cache mode level")
|
||||
|
||||
err = json.Unmarshal([]byte(`""`), &m)
|
||||
assert.Error(t, err, "Unknown cache mode level")
|
||||
|
||||
err = json.Unmarshal([]byte(strconv.Itoa(int(CacheModeFull))), &m)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, CacheModeFull, m)
|
||||
|
||||
err = json.Unmarshal([]byte("-1"), &m)
|
||||
assert.Error(t, err, "Unknown cache mode level")
|
||||
|
||||
err = json.Unmarshal([]byte("99"), &m)
|
||||
assert.Error(t, err, "Unknown cache mode level")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user