summaryrefslogtreecommitdiffstats
path: root/pkg/gui/presentation/files_test.go
blob: c40f6247ed61022bddc0b706c0c10ac9b8d394ae (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package presentation

import (
	"strings"
	"testing"

	"github.com/gookit/color"
	"github.com/jesseduffield/lazygit/pkg/commands/models"
	"github.com/jesseduffield/lazygit/pkg/commands/patch"
	"github.com/jesseduffield/lazygit/pkg/gui/filetree"
	"github.com/jesseduffield/lazygit/pkg/utils"
	"github.com/stretchr/testify/assert"
	"github.com/xo/terminfo"
)

func init() {
	color.ForceSetColorLevel(terminfo.ColorLevelNone)
}

func toStringSlice(str string) []string {
	return strings.Split(strings.TrimSpace(str), "\n")
}

func TestRenderFileTree(t *testing.T) {
	scenarios := []struct {
		name           string
		root           *filetree.FileNode
		files          []*models.File
		collapsedPaths []string
		expected       []string
	}{
		{
			name:     "nil node",
			files:    nil,
			expected: []string{},
		},
		{
			name: "leaf node",
			files: []*models.File{
				{Name: "test", ShortStatus: " M", HasStagedChanges: true},
			},
			expected: []string{" M test"},
		},
		{
			name: "big example",
			files: []*models.File{
				{Name: "dir1/file2", ShortStatus: "M ", HasUnstagedChanges: true},
				{Name: "dir1/file3", ShortStatus: "M ", HasUnstagedChanges: true},
				{Name: "dir2/dir2/file3", ShortStatus: " M", HasStagedChanges: true},
				{Name: "dir2/dir2/file4", ShortStatus: "M ", HasUnstagedChanges: true},
				{Name: "dir2/file5", ShortStatus: "M ", HasUnstagedChanges: true},
				{Name: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
			},
			expected: toStringSlice(
				`
dir1 ►
dir2 ▼
├─ dir2 ▼
│  ├─  M file3
│  └─ M  file4
└─ M  file5
M  file1
`,
			),
			collapsedPaths: []string{"dir1"},
		},
	}

	for _, s := range scenarios {
		s := s
		t.Run(s.name, func(t *testing.T) {
			viewModel := filetree.NewFileTreeViewModel(s.files, utils.NewDummyLog(), true)
			for _, path := range s.collapsedPaths {
				viewModel.ToggleCollapsed(path)
			}
			result := RenderFileTree(viewModel, "", nil)
			assert.EqualValues(t, s.expected, result)
		})
	}
}

func TestRenderCommitFileTree(t *testing.T) {
	scenarios := []struct {
		name           string
		root           *filetree.FileNode
		files          []*models.CommitFile
		collapsedPaths []string
		expected       []string
	}{
		{
			name:     "nil node",
			files:    nil,
			expected: []string{},
		},
		{
			name: "leaf node",
			files: []*models.CommitFile{
				{Name: "test", ChangeStatus: "A"},
			},
			expected: []string{"A test"},
		},
		{
			name: "big example",
			files: []*models.CommitFile{
				{Name: "dir1/file2", ChangeStatus: "M"},
				{Name: "dir1/file3", ChangeStatus: "A"},
				{Name: "dir2/dir2/file3", ChangeStatus: "D"},
				{Name: "dir2/dir2/file4", ChangeStatus: "M"},
				{Name: "dir2/file5", ChangeStatus: "M"},
				{Name: "file1", ChangeStatus: "M"},
			},
			expected: toStringSlice(
				`
dir1 ►
dir2 ▼
├─ dir2 ▼
│  ├─ D file3
│  └─ M file4
└─ M file5
M file1
`,
			),
			collapsedPaths: []string{"dir1"},
		},
	}

	for _, s := range scenarios {
		s := s
		t.Run(s.name, func(t *testing.T) {
			viewModel := filetree.NewCommitFileTreeViewModel(s.files, utils.NewDummyLog(), true)
			for _, path := range s.collapsedPaths {
				viewModel.ToggleCollapsed(path)
			}
			patchManager := patch.NewPatchManager(
				utils.NewDummyLog(),
				func(patch string, flags ...string) error { return nil },
				func(from string, to string, reverse bool, filename string, plain bool) (string, error) {
					return "", nil
				},
			)
			patchManager.Start("from", "to", false, false)
			result := RenderCommitFileTree(viewModel, "", patchManager)
			assert.EqualValues(t, s.expected, result)
		})
	}
}