summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_config/cached_git_config_test.go
blob: 6ba2c5b91b62f0a493c48a5edfef7c8c88e0b9b3 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package git_config

import (
	"os/exec"
	"strings"
	"testing"

	"github.com/jesseduffield/lazygit/pkg/utils"
	"github.com/stretchr/testify/assert"
)

func TestGetBool(t *testing.T) {
	type scenario struct {
		testName      string
		mockResponses map[string]string
		expected      bool
	}

	scenarios := []scenario{
		{
			"Option global and local config commit.gpgsign is not set",
			map[string]string{},
			false,
		},
		{
			"Some other random key is set",
			map[string]string{"blah": "blah"},
			false,
		},
		{
			"Option commit.gpgsign is true",
			map[string]string{"commit.gpgsign": "True"},
			true,
		},
		{
			"Option commit.gpgsign is on",
			map[string]string{"commit.gpgsign": "ON"},
			true,
		},
		{
			"Option commit.gpgsign is yes",
			map[string]string{"commit.gpgsign": "YeS"},
			true,
		},
		{
			"Option commit.gpgsign is 1",
			map[string]string{"commit.gpgsign": "1"},
			true,
		},
	}

	for _, s := range scenarios {
		s := s
		t.Run(s.testName, func(t *testing.T) {
			fake := NewFakeGitConfig(s.mockResponses)
			real := NewCachedGitConfig(
				func(cmd *exec.Cmd) (string, error) {
					assert.Equal(t, "config --get --null commit.gpgsign", strings.Join(cmd.Args[1:], " "))
					return fake.Get("commit.gpgsign"), nil
				},
				utils.NewDummyLog(),
			)
			result := real.GetBool("commit.gpgsign")
			assert.Equal(t, s.expected, result)
		})
	}
}

func TestGet(t *testing.T) {
	type scenario struct {
		testName      string
		mockResponses map[string]string
		expected      string
	}

	scenarios := []scenario{
		{
			"not set",
			map[string]string{},
			"",
		},
		{
			"is set",
			map[string]string{"commit.gpgsign": "blah"},
			"blah",
		},
	}

	for _, s := range scenarios {
		s := s
		t.Run(s.testName, func(t *testing.T) {
			fake := NewFakeGitConfig(s.mockResponses)
			real := NewCachedGitConfig(
				func(cmd *exec.Cmd) (string, error) {
					assert.Equal(t, "config --get --null commit.gpgsign", strings.Join(cmd.Args[1:], " "))
					return fake.Get("commit.gpgsign"), nil
				},
				utils.NewDummyLog(),
			)
			result := real.Get("commit.gpgsign")
			assert.Equal(t, s.expected, result)
		})
	}

	// verifying that the cache is used
	count := 0
	real := NewCachedGitConfig(
		func(cmd *exec.Cmd) (string, error) {
			count++
			assert.Equal(t, "config --get --null commit.gpgsign", strings.Join(cmd.Args[1:], " "))
			return "blah", nil
		},
		utils.NewDummyLog(),
	)
	result := real.Get("commit.gpgsign")
	assert.Equal(t, "blah", result)
	result = real.Get("commit.gpgsign")
	assert.Equal(t, "blah", result)
	assert.Equal(t, 1, count)
}