github.com/psexton/git-lfs@v2.1.1-0.20170517224304-289a18b2bc53+incompatible/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestBasicTransfersOnlySetValue(t *testing.T) {
    11  	cfg := NewFrom(Values{
    12  		Git: map[string][]string{
    13  			"lfs.basictransfersonly": []string{"true"},
    14  		},
    15  	})
    16  
    17  	b := cfg.BasicTransfersOnly()
    18  	assert.Equal(t, true, b)
    19  }
    20  
    21  func TestBasicTransfersOnlyDefault(t *testing.T) {
    22  	cfg := NewFrom(Values{})
    23  
    24  	b := cfg.BasicTransfersOnly()
    25  	assert.Equal(t, false, b)
    26  }
    27  
    28  func TestBasicTransfersOnlyInvalidValue(t *testing.T) {
    29  	cfg := NewFrom(Values{
    30  		Git: map[string][]string{
    31  			"lfs.basictransfersonly": []string{"wat"},
    32  		},
    33  	})
    34  
    35  	b := cfg.BasicTransfersOnly()
    36  	assert.Equal(t, false, b)
    37  }
    38  
    39  func TestTusTransfersAllowedSetValue(t *testing.T) {
    40  	cfg := NewFrom(Values{
    41  		Git: map[string][]string{
    42  			"lfs.tustransfers": []string{"true"},
    43  		},
    44  	})
    45  
    46  	b := cfg.TusTransfersAllowed()
    47  	assert.Equal(t, true, b)
    48  }
    49  
    50  func TestTusTransfersAllowedDefault(t *testing.T) {
    51  	cfg := NewFrom(Values{})
    52  
    53  	b := cfg.TusTransfersAllowed()
    54  	assert.Equal(t, false, b)
    55  }
    56  
    57  func TestTusTransfersAllowedInvalidValue(t *testing.T) {
    58  	cfg := NewFrom(Values{
    59  		Git: map[string][]string{
    60  			"lfs.tustransfers": []string{"wat"},
    61  		},
    62  	})
    63  
    64  	b := cfg.TusTransfersAllowed()
    65  	assert.Equal(t, false, b)
    66  }
    67  
    68  func TestLoadValidExtension(t *testing.T) {
    69  	cfg := NewFrom(Values{
    70  		Git: map[string][]string{},
    71  	})
    72  
    73  	cfg.extensions = map[string]Extension{
    74  		"foo": Extension{
    75  			"foo",
    76  			"foo-clean %f",
    77  			"foo-smudge %f",
    78  			2,
    79  		},
    80  	}
    81  
    82  	ext := cfg.Extensions()["foo"]
    83  
    84  	assert.Equal(t, "foo", ext.Name)
    85  	assert.Equal(t, "foo-clean %f", ext.Clean)
    86  	assert.Equal(t, "foo-smudge %f", ext.Smudge)
    87  	assert.Equal(t, 2, ext.Priority)
    88  }
    89  
    90  func TestLoadInvalidExtension(t *testing.T) {
    91  	cfg := NewFrom(Values{})
    92  	ext := cfg.Extensions()["foo"]
    93  
    94  	assert.Equal(t, "", ext.Name)
    95  	assert.Equal(t, "", ext.Clean)
    96  	assert.Equal(t, "", ext.Smudge)
    97  	assert.Equal(t, 0, ext.Priority)
    98  }
    99  
   100  func TestFetchPruneConfigDefault(t *testing.T) {
   101  	cfg := NewFrom(Values{})
   102  	fp := cfg.FetchPruneConfig()
   103  
   104  	assert.Equal(t, 7, fp.FetchRecentRefsDays)
   105  	assert.Equal(t, 0, fp.FetchRecentCommitsDays)
   106  	assert.Equal(t, 3, fp.PruneOffsetDays)
   107  	assert.True(t, fp.FetchRecentRefsIncludeRemotes)
   108  	assert.Equal(t, 3, fp.PruneOffsetDays)
   109  	assert.Equal(t, "origin", fp.PruneRemoteName)
   110  	assert.False(t, fp.PruneVerifyRemoteAlways)
   111  
   112  }
   113  func TestFetchPruneConfigCustom(t *testing.T) {
   114  	cfg := NewFrom(Values{
   115  		Git: map[string][]string{
   116  			"lfs.fetchrecentrefsdays":     []string{"12"},
   117  			"lfs.fetchrecentremoterefs":   []string{"false"},
   118  			"lfs.fetchrecentcommitsdays":  []string{"9"},
   119  			"lfs.pruneoffsetdays":         []string{"30"},
   120  			"lfs.pruneverifyremotealways": []string{"true"},
   121  			"lfs.pruneremotetocheck":      []string{"upstream"},
   122  		},
   123  	})
   124  	fp := cfg.FetchPruneConfig()
   125  
   126  	assert.Equal(t, 12, fp.FetchRecentRefsDays)
   127  	assert.Equal(t, 9, fp.FetchRecentCommitsDays)
   128  	assert.False(t, fp.FetchRecentRefsIncludeRemotes)
   129  	assert.Equal(t, 30, fp.PruneOffsetDays)
   130  	assert.Equal(t, "upstream", fp.PruneRemoteName)
   131  	assert.True(t, fp.PruneVerifyRemoteAlways)
   132  }
   133  
   134  func TestFetchIncludeExcludesAreCleaned(t *testing.T) {
   135  	cfg := NewFrom(Values{
   136  		Git: map[string][]string{
   137  			"lfs.fetchinclude": []string{"/path/to/clean/"},
   138  			"lfs.fetchexclude": []string{"/other/path/to/clean/"},
   139  		},
   140  	})
   141  
   142  	assert.Equal(t, []string{"/path/to/clean"}, cfg.FetchIncludePaths())
   143  	assert.Equal(t, []string{"/other/path/to/clean"}, cfg.FetchExcludePaths())
   144  }
   145  
   146  func TestUnmarshalMultipleTypes(t *testing.T) {
   147  	cfg := NewFrom(Values{
   148  		Git: map[string][]string{
   149  			"string": []string{"string"},
   150  			"int":    []string{"1"},
   151  			"bool":   []string{"true"},
   152  		},
   153  		Os: map[string][]string{
   154  			"string": []string{"string"},
   155  			"int":    []string{"1"},
   156  			"bool":   []string{"true"},
   157  		},
   158  	})
   159  
   160  	v := &struct {
   161  		GitString string `git:"string"`
   162  		GitInt    int    `git:"int"`
   163  		GitBool   bool   `git:"bool"`
   164  		OsString  string `os:"string"`
   165  		OsInt     int    `os:"int"`
   166  		OsBool    bool   `os:"bool"`
   167  	}{}
   168  
   169  	assert.Nil(t, cfg.Unmarshal(v))
   170  
   171  	assert.Equal(t, "string", v.GitString)
   172  	assert.Equal(t, 1, v.GitInt)
   173  	assert.Equal(t, true, v.GitBool)
   174  	assert.Equal(t, "string", v.OsString)
   175  	assert.Equal(t, 1, v.OsInt)
   176  	assert.Equal(t, true, v.OsBool)
   177  }
   178  
   179  func TestUnmarshalErrsOnNonPointerType(t *testing.T) {
   180  	type T struct {
   181  		Foo string `git:"foo"`
   182  	}
   183  
   184  	cfg := NewFrom(Values{})
   185  
   186  	err := cfg.Unmarshal(T{})
   187  
   188  	assert.Equal(t, "lfs/config: unable to parse non-pointer type of config.T", err.Error())
   189  }
   190  
   191  func TestUnmarshalLeavesNonZeroValuesWhenKeysEmpty(t *testing.T) {
   192  	v := &struct {
   193  		String string `git:"string"`
   194  		Int    int    `git:"int"`
   195  		Bool   bool   `git:"bool"`
   196  	}{"foo", 1, true}
   197  
   198  	cfg := NewFrom(Values{})
   199  
   200  	err := cfg.Unmarshal(v)
   201  
   202  	assert.Nil(t, err)
   203  	assert.Equal(t, "foo", v.String)
   204  	assert.Equal(t, 1, v.Int)
   205  	assert.Equal(t, true, v.Bool)
   206  }
   207  
   208  func TestUnmarshalOverridesNonZeroValuesWhenValuesPresent(t *testing.T) {
   209  	v := &struct {
   210  		String string `git:"string"`
   211  		Int    int    `git:"int"`
   212  		Bool   bool   `git:"bool"`
   213  	}{"foo", 1, true}
   214  
   215  	cfg := NewFrom(Values{
   216  		Git: map[string][]string{
   217  			"string": []string{"bar"},
   218  			"int":    []string{"2"},
   219  			"bool":   []string{"false"},
   220  		},
   221  	})
   222  
   223  	err := cfg.Unmarshal(v)
   224  
   225  	assert.Nil(t, err)
   226  	assert.Equal(t, "bar", v.String)
   227  	assert.Equal(t, 2, v.Int)
   228  	assert.Equal(t, false, v.Bool)
   229  }
   230  
   231  func TestUnmarshalAllowsBothOsAndGitTags(t *testing.T) {
   232  	v := &struct {
   233  		String string `git:"string" os:"STRING"`
   234  	}{}
   235  
   236  	cfg := NewFrom(Values{
   237  		Git: map[string][]string{"string": []string{"foo"}},
   238  		Os:  map[string][]string{"STRING": []string{"bar"}},
   239  	})
   240  
   241  	err := cfg.Unmarshal(v)
   242  
   243  	assert.Nil(t, err)
   244  	assert.Equal(t, "foo", v.String)
   245  }
   246  
   247  func TestUnmarshalYieldsToDefaultIfBothEnvsMissing(t *testing.T) {
   248  	v := &struct {
   249  		String string `git:"string" os:"STRING"`
   250  	}{"foo"}
   251  
   252  	cfg := NewFrom(Values{})
   253  
   254  	err := cfg.Unmarshal(v)
   255  
   256  	assert.Nil(t, err)
   257  	assert.Equal(t, "foo", v.String)
   258  }
   259  
   260  func TestUnmarshalOverridesDefaultIfAnyEnvPresent(t *testing.T) {
   261  	v := &struct {
   262  		String string `git:"string" os:"STRING"`
   263  	}{"foo"}
   264  
   265  	cfg := NewFrom(Values{
   266  		Git: map[string][]string{"string": []string{"bar"}},
   267  		Os:  map[string][]string{"STRING": []string{"baz"}},
   268  	})
   269  
   270  	err := cfg.Unmarshal(v)
   271  
   272  	assert.Nil(t, err)
   273  	assert.Equal(t, "bar", v.String)
   274  }
   275  
   276  func TestUnmarshalIgnoresUnknownEnvironments(t *testing.T) {
   277  	v := &struct {
   278  		String string `unknown:"string"`
   279  	}{}
   280  
   281  	cfg := NewFrom(Values{})
   282  
   283  	assert.Nil(t, cfg.Unmarshal(v))
   284  }
   285  
   286  func TestUnmarshalErrsOnUnsupportedTypes(t *testing.T) {
   287  	v := &struct {
   288  		Unsupported time.Duration `git:"duration"`
   289  	}{}
   290  
   291  	cfg := NewFrom(Values{
   292  		Git: map[string][]string{"duration": []string{"foo"}},
   293  	})
   294  
   295  	err := cfg.Unmarshal(v)
   296  
   297  	assert.Equal(t, "lfs/config: unsupported target type for field \"Unsupported\": time.Duration", err.Error())
   298  }