1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-16 11:37:01 +02:00
lazygit/pkg/gui/presentation/files_test.go

149 lines
3.6 KiB
Go
Raw Normal View History

2022-01-21 15:13:51 +02:00
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(
`
2023-03-18 02:32:44 +02:00
dir1
dir2
dir2
M file3
M file4
M file5
2022-01-21 15:13:51 +02:00
M file1
`,
),
collapsedPaths: []string{"dir1"},
},
}
for _, s := range scenarios {
s := s
t.Run(s.name, func(t *testing.T) {
viewModel := filetree.NewFileTree(func() []*models.File { return s.files }, utils.NewDummyLog(), true)
2022-02-13 03:47:15 +02:00
viewModel.SetTree()
2022-01-21 15:13:51 +02:00
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(
`
2023-03-18 02:32:44 +02:00
dir1
dir2
dir2
D file3
M file4
M file5
2022-01-21 15:13:51 +02:00
M file1
`,
),
collapsedPaths: []string{"dir1"},
},
}
for _, s := range scenarios {
s := s
t.Run(s.name, func(t *testing.T) {
2022-01-30 07:42:20 +02:00
viewModel := filetree.NewCommitFileTreeViewModel(func() []*models.CommitFile { return s.files }, utils.NewDummyLog(), true)
2022-03-26 15:54:15 +02:00
viewModel.SetRef(&models.Commit{})
2022-02-13 03:47:15 +02:00
viewModel.SetTree()
2022-01-21 15:13:51 +02:00
for _, path := range s.collapsedPaths {
viewModel.ToggleCollapsed(path)
}
2023-03-19 07:09:03 +02:00
patchBuilder := patch.NewPatchBuilder(
2022-01-21 15:13:51 +02:00
utils.NewDummyLog(),
func(from string, to string, reverse bool, filename string, plain bool) (string, error) {
return "", nil
},
)
2023-03-19 07:09:03 +02:00
patchBuilder.Start("from", "to", false, false)
result := RenderCommitFileTree(viewModel, "", patchBuilder)
2022-01-21 15:13:51 +02:00
assert.EqualValues(t, s.expected, result)
})
}
}