summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_commands/tag_loader_test.go
blob: 59c4d0337ab1e4ac8644e3e1692b8aa505e1964a (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
package git_commands

import (
	"testing"

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

const tagsOutput = `tag1 this is my message
tag2
tag3 this is my other message
`

func TestGetTags(t *testing.T) {
	type scenario struct {
		testName      string
		runner        *oscommands.FakeCmdObjRunner
		expectedTags  []*models.Tag
		expectedError error
	}

	scenarios := []scenario{
		{
			testName: "should return no tags if there are none",
			runner: oscommands.NewFakeRunner(t).
				ExpectGitArgs([]string{"tag", "--list", "-n", "--sort=-creatordate"}, "", nil),
			expectedTags:  []*models.Tag{},
			expectedError: nil,
		},
		{
			testName: "should return tags if present",
			runner: oscommands.NewFakeRunner(t).
				ExpectGitArgs([]string{"tag", "--list", "-n", "--sort=-creatordate"}, tagsOutput, nil),
			expectedTags: []*models.Tag{
				{Name: "tag1", Message: "this is my message"},
				{Name: "tag2", Message: ""},
				{Name: "tag3", Message: "this is my other message"},
			},
			expectedError: nil,
		},
	}

	for _, scenario := range scenarios {
		scenario := scenario
		t.Run(scenario.testName, func(t *testing.T) {
			loader := &TagLoader{
				Common: utils.NewDummyCommon(),
				cmd:    oscommands.NewDummyCmdObjBuilder(scenario.runner),
			}

			tags, err := loader.GetTags()

			assert.Equal(t, scenario.expectedTags, tags)
			assert.Equal(t, scenario.expectedError, err)

			scenario.runner.CheckForMissingCalls()
		})
	}
}