File: environment_test.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (103 lines) | stat: -rw-r--r-- 2,381 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package config_test

import (
	"testing"

	. "github.com/git-lfs/git-lfs/v3/config"
	"github.com/stretchr/testify/assert"
)

func TestEnvironmentGetDelegatesToFetcher(t *testing.T) {
	fetcher := MapFetcher(map[string][]string{
		"foo": []string{"bar", "baz"},
	})

	env := EnvironmentOf(fetcher)
	val, ok := env.Get("foo")

	assert.True(t, ok)
	assert.Equal(t, "baz", val)
}

func TestEnvironmentGetAllDelegatesToFetcher(t *testing.T) {
	fetcher := MapFetcher(map[string][]string{
		"foo": []string{"bar", "baz"},
	})

	env := EnvironmentOf(fetcher)
	vals := env.GetAll("foo")

	assert.Equal(t, []string{"bar", "baz"}, vals)
}

func TestEnvironmentUnsetBoolDefault(t *testing.T) {
	env := EnvironmentOf(MapFetcher(nil))
	assert.True(t, env.Bool("unset", true))
}

func TestEnvironmentBoolTruthyConversion(t *testing.T) {
	for _, c := range []EnvironmentConversionTestCase{
		{"", false, GetBoolDefault(false)},

		{"true", true, GetBoolDefault(false)},
		{"1", true, GetBoolDefault(false)},
		{"on", true, GetBoolDefault(false)},
		{"yes", true, GetBoolDefault(false)},
		{"t", true, GetBoolDefault(false)},

		{"false", false, GetBoolDefault(true)},
		{"0", false, GetBoolDefault(true)},
		{"off", false, GetBoolDefault(true)},
		{"no", false, GetBoolDefault(true)},
		{"f", false, GetBoolDefault(true)},
	} {
		c.Assert(t)
	}
}

func TestEnvironmentIntTestCases(t *testing.T) {
	for _, c := range []EnvironmentConversionTestCase{
		{"", 1, GetIntDefault(1)},

		{"1", 1, GetIntDefault(0)},
		{"3", 3, GetIntDefault(0)},

		{"malformed", 7, GetIntDefault(7)},
	} {
		c.Assert(t)
	}
}

type EnvironmentConversionTestCase struct {
	Val      string
	Expected interface{}

	GotFn func(env Environment, key string) interface{}
}

var (
	GetBoolDefault = func(def bool) func(e Environment, key string) interface{} {
		return func(e Environment, key string) interface{} {
			return e.Bool(key, def)
		}
	}

	GetIntDefault = func(def int) func(e Environment, key string) interface{} {
		return func(e Environment, key string) interface{} {
			return e.Int(key, def)
		}
	}
)

func (c *EnvironmentConversionTestCase) Assert(t *testing.T) {
	fetcher := MapFetcher(map[string][]string{
		c.Val: []string{c.Val},
	})

	env := EnvironmentOf(fetcher)
	got := c.GotFn(env, c.Val)

	if c.Expected != got {
		t.Errorf("lfs/config: expected val=%q to be %q (got: %q)", c.Val, c.Expected, got)
	}
}