2016-05-13 16:38:06 +00:00
|
|
|
package config
|
2014-02-01 21:09:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2016-08-08 22:48:21 +00:00
|
|
|
"time"
|
2015-05-13 19:43:41 +00:00
|
|
|
|
2016-05-25 16:43:10 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2014-02-01 21:09:52 +00:00
|
|
|
)
|
|
|
|
|
2016-06-09 10:45:24 +00:00
|
|
|
func TestBasicTransfersOnlySetValue(t *testing.T) {
|
2016-08-05 21:13:31 +00:00
|
|
|
cfg := NewFrom(Values{
|
2017-04-12 21:29:11 +00:00
|
|
|
Git: map[string][]string{
|
|
|
|
"lfs.basictransfersonly": []string{"true"},
|
2016-06-09 10:45:24 +00:00
|
|
|
},
|
2016-08-05 21:13:31 +00:00
|
|
|
})
|
2016-06-09 10:45:24 +00:00
|
|
|
|
2016-08-05 21:13:31 +00:00
|
|
|
b := cfg.BasicTransfersOnly()
|
2016-06-09 10:45:24 +00:00
|
|
|
assert.Equal(t, true, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicTransfersOnlyDefault(t *testing.T) {
|
2016-08-05 21:13:31 +00:00
|
|
|
cfg := NewFrom(Values{})
|
2016-06-09 10:45:24 +00:00
|
|
|
|
2016-08-05 21:13:31 +00:00
|
|
|
b := cfg.BasicTransfersOnly()
|
2016-06-09 10:45:24 +00:00
|
|
|
assert.Equal(t, false, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicTransfersOnlyInvalidValue(t *testing.T) {
|
2016-08-05 21:13:31 +00:00
|
|
|
cfg := NewFrom(Values{
|
2017-04-12 21:29:11 +00:00
|
|
|
Git: map[string][]string{
|
|
|
|
"lfs.basictransfersonly": []string{"wat"},
|
2016-06-09 10:45:24 +00:00
|
|
|
},
|
2016-08-05 21:13:31 +00:00
|
|
|
})
|
2016-06-09 10:45:24 +00:00
|
|
|
|
2016-08-05 21:13:31 +00:00
|
|
|
b := cfg.BasicTransfersOnly()
|
2016-06-09 10:45:24 +00:00
|
|
|
assert.Equal(t, false, b)
|
|
|
|
}
|
|
|
|
|
2016-07-25 18:50:58 +00:00
|
|
|
func TestTusTransfersAllowedSetValue(t *testing.T) {
|
2016-08-05 21:13:31 +00:00
|
|
|
cfg := NewFrom(Values{
|
2017-04-12 21:29:11 +00:00
|
|
|
Git: map[string][]string{
|
|
|
|
"lfs.tustransfers": []string{"true"},
|
2016-07-25 18:50:58 +00:00
|
|
|
},
|
2016-08-05 21:13:31 +00:00
|
|
|
})
|
2016-07-25 18:50:58 +00:00
|
|
|
|
2016-08-05 21:13:31 +00:00
|
|
|
b := cfg.TusTransfersAllowed()
|
2016-07-25 18:50:58 +00:00
|
|
|
assert.Equal(t, true, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTusTransfersAllowedDefault(t *testing.T) {
|
2016-08-05 21:13:31 +00:00
|
|
|
cfg := NewFrom(Values{})
|
2016-07-25 18:50:58 +00:00
|
|
|
|
2016-08-05 21:13:31 +00:00
|
|
|
b := cfg.TusTransfersAllowed()
|
2016-07-25 18:50:58 +00:00
|
|
|
assert.Equal(t, false, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTusTransfersAllowedInvalidValue(t *testing.T) {
|
2016-08-05 21:13:31 +00:00
|
|
|
cfg := NewFrom(Values{
|
2017-04-12 21:29:11 +00:00
|
|
|
Git: map[string][]string{
|
|
|
|
"lfs.tustransfers": []string{"wat"},
|
2016-07-25 18:50:58 +00:00
|
|
|
},
|
2016-08-05 21:13:31 +00:00
|
|
|
})
|
2016-07-25 18:50:58 +00:00
|
|
|
|
2016-08-05 21:13:31 +00:00
|
|
|
b := cfg.TusTransfersAllowed()
|
2016-07-25 18:50:58 +00:00
|
|
|
assert.Equal(t, false, b)
|
|
|
|
}
|
|
|
|
|
2015-07-10 20:54:06 +00:00
|
|
|
func TestLoadValidExtension(t *testing.T) {
|
2016-08-05 21:13:31 +00:00
|
|
|
cfg := NewFrom(Values{
|
2017-04-12 21:29:11 +00:00
|
|
|
Git: map[string][]string{},
|
2016-08-05 21:13:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
cfg.extensions = map[string]Extension{
|
|
|
|
"foo": Extension{
|
|
|
|
"foo",
|
|
|
|
"foo-clean %f",
|
|
|
|
"foo-smudge %f",
|
|
|
|
2,
|
2015-07-10 20:54:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-08-05 21:13:31 +00:00
|
|
|
ext := cfg.Extensions()["foo"]
|
2015-07-10 20:54:06 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "foo", ext.Name)
|
|
|
|
assert.Equal(t, "foo-clean %f", ext.Clean)
|
|
|
|
assert.Equal(t, "foo-smudge %f", ext.Smudge)
|
|
|
|
assert.Equal(t, 2, ext.Priority)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadInvalidExtension(t *testing.T) {
|
2016-08-05 21:13:31 +00:00
|
|
|
cfg := NewFrom(Values{})
|
|
|
|
ext := cfg.Extensions()["foo"]
|
2015-07-10 20:54:06 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "", ext.Name)
|
|
|
|
assert.Equal(t, "", ext.Clean)
|
|
|
|
assert.Equal(t, "", ext.Smudge)
|
|
|
|
assert.Equal(t, 0, ext.Priority)
|
|
|
|
}
|
2015-08-28 21:19:52 +00:00
|
|
|
|
2015-08-04 16:46:51 +00:00
|
|
|
func TestFetchPruneConfigDefault(t *testing.T) {
|
2016-08-05 21:13:31 +00:00
|
|
|
cfg := NewFrom(Values{})
|
|
|
|
fp := cfg.FetchPruneConfig()
|
2015-08-04 16:46:51 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 7, fp.FetchRecentRefsDays)
|
2015-08-17 11:04:43 +00:00
|
|
|
assert.Equal(t, 0, fp.FetchRecentCommitsDays)
|
2015-08-04 16:46:51 +00:00
|
|
|
assert.Equal(t, 3, fp.PruneOffsetDays)
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.True(t, fp.FetchRecentRefsIncludeRemotes)
|
2015-09-08 14:00:28 +00:00
|
|
|
assert.Equal(t, 3, fp.PruneOffsetDays)
|
|
|
|
assert.Equal(t, "origin", fp.PruneRemoteName)
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.False(t, fp.PruneVerifyRemoteAlways)
|
2015-08-04 16:46:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
func TestFetchPruneConfigCustom(t *testing.T) {
|
2016-08-05 21:13:31 +00:00
|
|
|
cfg := NewFrom(Values{
|
2017-04-12 21:29:11 +00:00
|
|
|
Git: map[string][]string{
|
|
|
|
"lfs.fetchrecentrefsdays": []string{"12"},
|
|
|
|
"lfs.fetchrecentremoterefs": []string{"false"},
|
|
|
|
"lfs.fetchrecentcommitsdays": []string{"9"},
|
|
|
|
"lfs.pruneoffsetdays": []string{"30"},
|
|
|
|
"lfs.pruneverifyremotealways": []string{"true"},
|
|
|
|
"lfs.pruneremotetocheck": []string{"upstream"},
|
2015-08-04 16:46:51 +00:00
|
|
|
},
|
2016-08-05 21:13:31 +00:00
|
|
|
})
|
|
|
|
fp := cfg.FetchPruneConfig()
|
2015-08-04 16:46:51 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 12, fp.FetchRecentRefsDays)
|
|
|
|
assert.Equal(t, 9, fp.FetchRecentCommitsDays)
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.False(t, fp.FetchRecentRefsIncludeRemotes)
|
2015-09-08 14:00:28 +00:00
|
|
|
assert.Equal(t, 30, fp.PruneOffsetDays)
|
|
|
|
assert.Equal(t, "upstream", fp.PruneRemoteName)
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.True(t, fp.PruneVerifyRemoteAlways)
|
2015-09-01 18:32:55 +00:00
|
|
|
}
|
2016-05-31 16:38:18 +00:00
|
|
|
|
|
|
|
func TestFetchIncludeExcludesAreCleaned(t *testing.T) {
|
2016-08-05 21:13:31 +00:00
|
|
|
cfg := NewFrom(Values{
|
2017-04-12 21:29:11 +00:00
|
|
|
Git: map[string][]string{
|
|
|
|
"lfs.fetchinclude": []string{"/path/to/clean/"},
|
|
|
|
"lfs.fetchexclude": []string{"/other/path/to/clean/"},
|
2016-08-03 23:42:21 +00:00
|
|
|
},
|
2016-05-31 16:38:18 +00:00
|
|
|
})
|
|
|
|
|
2016-08-05 21:13:31 +00:00
|
|
|
assert.Equal(t, []string{"/path/to/clean"}, cfg.FetchIncludePaths())
|
|
|
|
assert.Equal(t, []string{"/other/path/to/clean"}, cfg.FetchExcludePaths())
|
2016-05-31 16:38:18 +00:00
|
|
|
}
|
2016-08-08 22:48:21 +00:00
|
|
|
|
|
|
|
func TestUnmarshalMultipleTypes(t *testing.T) {
|
|
|
|
cfg := NewFrom(Values{
|
2017-04-12 21:29:11 +00:00
|
|
|
Git: map[string][]string{
|
|
|
|
"string": []string{"string"},
|
|
|
|
"int": []string{"1"},
|
|
|
|
"bool": []string{"true"},
|
2016-08-08 22:48:21 +00:00
|
|
|
},
|
2017-04-12 21:29:11 +00:00
|
|
|
Os: map[string][]string{
|
|
|
|
"string": []string{"string"},
|
|
|
|
"int": []string{"1"},
|
|
|
|
"bool": []string{"true"},
|
2016-08-08 22:48:21 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
v := &struct {
|
|
|
|
GitString string `git:"string"`
|
|
|
|
GitInt int `git:"int"`
|
|
|
|
GitBool bool `git:"bool"`
|
|
|
|
OsString string `os:"string"`
|
|
|
|
OsInt int `os:"int"`
|
|
|
|
OsBool bool `os:"bool"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
assert.Nil(t, cfg.Unmarshal(v))
|
|
|
|
|
|
|
|
assert.Equal(t, "string", v.GitString)
|
|
|
|
assert.Equal(t, 1, v.GitInt)
|
|
|
|
assert.Equal(t, true, v.GitBool)
|
|
|
|
assert.Equal(t, "string", v.OsString)
|
|
|
|
assert.Equal(t, 1, v.OsInt)
|
|
|
|
assert.Equal(t, true, v.OsBool)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalErrsOnNonPointerType(t *testing.T) {
|
|
|
|
type T struct {
|
|
|
|
Foo string `git:"foo"`
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := NewFrom(Values{})
|
|
|
|
|
|
|
|
err := cfg.Unmarshal(T{})
|
|
|
|
|
|
|
|
assert.Equal(t, "lfs/config: unable to parse non-pointer type of config.T", err.Error())
|
|
|
|
}
|
|
|
|
|
2016-08-09 16:08:56 +00:00
|
|
|
func TestUnmarshalLeavesNonZeroValuesWhenKeysEmpty(t *testing.T) {
|
|
|
|
v := &struct {
|
|
|
|
String string `git:"string"`
|
|
|
|
Int int `git:"int"`
|
|
|
|
Bool bool `git:"bool"`
|
|
|
|
}{"foo", 1, true}
|
|
|
|
|
|
|
|
cfg := NewFrom(Values{})
|
|
|
|
|
|
|
|
err := cfg.Unmarshal(v)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "foo", v.String)
|
|
|
|
assert.Equal(t, 1, v.Int)
|
|
|
|
assert.Equal(t, true, v.Bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalOverridesNonZeroValuesWhenValuesPresent(t *testing.T) {
|
2016-08-08 22:48:21 +00:00
|
|
|
v := &struct {
|
|
|
|
String string `git:"string"`
|
|
|
|
Int int `git:"int"`
|
|
|
|
Bool bool `git:"bool"`
|
|
|
|
}{"foo", 1, true}
|
|
|
|
|
|
|
|
cfg := NewFrom(Values{
|
2017-04-12 21:29:11 +00:00
|
|
|
Git: map[string][]string{
|
|
|
|
"string": []string{"bar"},
|
|
|
|
"int": []string{"2"},
|
|
|
|
"bool": []string{"false"},
|
2016-08-08 22:48:21 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
err := cfg.Unmarshal(v)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
2016-08-09 16:08:56 +00:00
|
|
|
assert.Equal(t, "bar", v.String)
|
|
|
|
assert.Equal(t, 2, v.Int)
|
|
|
|
assert.Equal(t, false, v.Bool)
|
2016-08-08 22:48:21 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 17:01:23 +00:00
|
|
|
func TestUnmarshalAllowsBothOsAndGitTags(t *testing.T) {
|
2016-08-08 22:48:21 +00:00
|
|
|
v := &struct {
|
|
|
|
String string `git:"string" os:"STRING"`
|
|
|
|
}{}
|
|
|
|
|
2017-05-12 17:01:23 +00:00
|
|
|
cfg := NewFrom(Values{
|
|
|
|
Git: map[string][]string{"string": []string{"foo"}},
|
|
|
|
Os: map[string][]string{"STRING": []string{"bar"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
err := cfg.Unmarshal(v)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "foo", v.String)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalYieldsToDefaultIfBothEnvsMissing(t *testing.T) {
|
|
|
|
v := &struct {
|
|
|
|
String string `git:"string" os:"STRING"`
|
|
|
|
}{"foo"}
|
|
|
|
|
2016-08-08 22:48:21 +00:00
|
|
|
cfg := NewFrom(Values{})
|
|
|
|
|
|
|
|
err := cfg.Unmarshal(v)
|
|
|
|
|
2017-05-12 17:01:23 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "foo", v.String)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalOverridesDefaultIfAnyEnvPresent(t *testing.T) {
|
|
|
|
v := &struct {
|
|
|
|
String string `git:"string" os:"STRING"`
|
|
|
|
}{"foo"}
|
|
|
|
|
|
|
|
cfg := NewFrom(Values{
|
|
|
|
Git: map[string][]string{"string": []string{"bar"}},
|
|
|
|
Os: map[string][]string{"STRING": []string{"baz"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
err := cfg.Unmarshal(v)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "bar", v.String)
|
2016-08-08 22:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalIgnoresUnknownEnvironments(t *testing.T) {
|
|
|
|
v := &struct {
|
|
|
|
String string `unknown:"string"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
cfg := NewFrom(Values{})
|
|
|
|
|
|
|
|
assert.Nil(t, cfg.Unmarshal(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalErrsOnUnsupportedTypes(t *testing.T) {
|
|
|
|
v := &struct {
|
|
|
|
Unsupported time.Duration `git:"duration"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
cfg := NewFrom(Values{
|
2017-04-12 21:29:11 +00:00
|
|
|
Git: map[string][]string{"duration": []string{"foo"}},
|
2016-08-08 22:48:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err := cfg.Unmarshal(v)
|
|
|
|
|
|
|
|
assert.Equal(t, "lfs/config: unsupported target type for field \"Unsupported\": time.Duration", err.Error())
|
|
|
|
}
|