1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-12 04:23:03 +02:00
lazygit/pkg/gui/filetree/file_manager_test.go

157 lines
4.2 KiB
Go
Raw Normal View History

2021-03-21 06:25:29 +02:00
package filetree
import (
"testing"
"github.com/gookit/color"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/stretchr/testify/assert"
"github.com/xo/terminfo"
)
2021-11-01 00:16:45 +02:00
func init() {
color.ForceSetColorLevel(terminfo.ColorLevelNone)
2021-11-01 00:16:45 +02:00
}
2021-11-01 00:16:45 +02:00
func TestRender(t *testing.T) {
scenarios := []struct {
2021-03-21 06:01:13 +02:00
name string
root *FileNode
2021-03-21 06:01:13 +02:00
collapsedPaths map[string]bool
expected []string
}{
{
name: "nil node",
root: nil,
expected: []string{},
},
{
name: "leaf node",
root: &FileNode{
2021-03-21 01:03:51 +02:00
Path: "",
Children: []*FileNode{
2021-03-21 01:03:51 +02:00
{File: &models.File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Path: "test"},
},
},
expected: []string{" M test"},
},
{
name: "big example",
root: &FileNode{
2021-03-21 01:03:51 +02:00
Path: "",
Children: []*FileNode{
{
2021-03-21 06:01:13 +02:00
Path: "dir1",
Children: []*FileNode{
{
2021-03-21 02:26:16 +02:00
File: &models.File{Name: "dir1/file2", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir1/file2",
},
2021-03-21 06:01:13 +02:00
{
File: &models.File{Name: "dir1/file3", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir1/file3",
},
},
},
{
2021-03-21 01:03:51 +02:00
Path: "dir2",
Children: []*FileNode{
{
2021-03-21 02:26:16 +02:00
Path: "dir2/dir2",
Children: []*FileNode{
{
2021-03-21 02:26:16 +02:00
File: &models.File{Name: "dir2/dir2/file3", ShortStatus: " M", HasStagedChanges: true},
Path: "dir2/dir2/file3",
},
{
2021-03-21 02:26:16 +02:00
File: &models.File{Name: "dir2/dir2/file4", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir2/dir2/file4",
},
},
},
{
2021-03-21 02:26:16 +02:00
File: &models.File{Name: "dir2/file5", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir2/file5",
},
},
},
{
File: &models.File{Name: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
2021-03-21 01:03:51 +02:00
Path: "file1",
},
},
},
2021-03-21 06:01:13 +02:00
expected: []string{"dir1 ►", "dir2 ▼", "├─ dir2 ▼", "│ ├─ M file3", "│ └─ M file4", "└─ M file5", "M file1"},
collapsedPaths: map[string]bool{"dir1": true},
},
}
for _, s := range scenarios {
s := s
t.Run(s.name, func(t *testing.T) {
mngr := &FileManager{tree: s.root, collapsedPaths: s.collapsedPaths}
result := mngr.Render("", nil)
assert.EqualValues(t, s.expected, result)
})
}
}
2021-08-03 22:44:43 +02:00
func TestFilterAction(t *testing.T) {
scenarios := []struct {
name string
filter FileManagerDisplayFilter
files []*models.File
expected []*models.File
}{
{
name: "filter files with unstaged changes",
filter: DisplayUnstaged,
files: []*models.File{
2021-08-03 22:47:11 +02:00
{Name: "dir2/dir2/file4", ShortStatus: "M ", HasUnstagedChanges: true},
{Name: "dir2/file5", ShortStatus: "M ", HasStagedChanges: true},
{Name: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
2021-08-03 22:44:43 +02:00
},
expected: []*models.File{
2021-08-03 22:47:11 +02:00
{Name: "dir2/dir2/file4", ShortStatus: "M ", HasUnstagedChanges: true},
{Name: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
2021-08-03 22:44:43 +02:00
},
},
{
name: "filter files with staged changes",
filter: DisplayStaged,
files: []*models.File{
2021-08-03 22:47:11 +02:00
{Name: "dir2/dir2/file4", ShortStatus: "M ", HasStagedChanges: true},
{Name: "dir2/file5", ShortStatus: "M ", HasStagedChanges: false},
{Name: "file1", ShortStatus: "M ", HasStagedChanges: true},
2021-08-03 22:44:43 +02:00
},
expected: []*models.File{
2021-08-03 22:47:11 +02:00
{Name: "dir2/dir2/file4", ShortStatus: "M ", HasStagedChanges: true},
{Name: "file1", ShortStatus: "M ", HasStagedChanges: true},
2021-08-03 22:44:43 +02:00
},
},
{
name: "filter all files",
filter: DisplayAll,
files: []*models.File{
2021-08-03 22:47:11 +02:00
{Name: "dir2/dir2/file4", ShortStatus: "M ", HasUnstagedChanges: true},
{Name: "dir2/file5", ShortStatus: "M ", HasUnstagedChanges: true},
{Name: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
2021-08-03 22:44:43 +02:00
},
expected: []*models.File{
2021-08-03 22:47:11 +02:00
{Name: "dir2/dir2/file4", ShortStatus: "M ", HasUnstagedChanges: true},
{Name: "dir2/file5", ShortStatus: "M ", HasUnstagedChanges: true},
{Name: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
2021-08-03 22:44:43 +02:00
},
},
}
for _, s := range scenarios {
s := s
t.Run(s.name, func(t *testing.T) {
mngr := &FileManager{files: s.files, filter: s.filter}
result := mngr.GetFilesForDisplay()
assert.EqualValues(t, s.expected, result)
})
}
}