2024-09-16 21:01:16 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-12-02 17:14:50 +00:00
|
|
|
"reflect"
|
2024-09-16 21:01:16 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2025-01-05 19:05:33 +00:00
|
|
|
|
|
|
|
"github.com/gtsteffaniak/filebrowser/backend/settings"
|
2024-09-16 21:01:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_GetRealPath(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2025-01-05 19:05:33 +00:00
|
|
|
trimPrefix := filepath.Dir(filepath.Dir(cwd))
|
2024-09-16 21:01:16 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
paths []string
|
|
|
|
want struct {
|
|
|
|
path string
|
|
|
|
isDir bool
|
|
|
|
}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "current directory",
|
|
|
|
paths: []string{
|
|
|
|
"./",
|
|
|
|
},
|
|
|
|
want: struct {
|
|
|
|
path string
|
|
|
|
isDir bool
|
|
|
|
}{
|
2025-01-05 19:05:33 +00:00
|
|
|
path: "",
|
2024-09-16 21:01:16 +00:00
|
|
|
isDir: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "current directory",
|
|
|
|
paths: []string{
|
2025-01-05 19:05:33 +00:00
|
|
|
"./files/file.go",
|
2024-09-16 21:01:16 +00:00
|
|
|
},
|
|
|
|
want: struct {
|
|
|
|
path string
|
|
|
|
isDir bool
|
|
|
|
}{
|
2025-01-05 19:05:33 +00:00
|
|
|
path: "/files/file.go",
|
2024-09-16 21:01:16 +00:00
|
|
|
isDir: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "other test case",
|
|
|
|
paths: []string{
|
|
|
|
"/mnt/doesnt/exist",
|
|
|
|
},
|
|
|
|
want: struct {
|
|
|
|
path string
|
|
|
|
isDir bool
|
|
|
|
}{
|
|
|
|
path: "/mnt/doesnt/exist",
|
|
|
|
isDir: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2025-01-05 19:05:33 +00:00
|
|
|
idx := Index{
|
|
|
|
Source: settings.Source{
|
|
|
|
Path: trimPrefix,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-09-16 21:01:16 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2025-01-05 19:05:33 +00:00
|
|
|
realPath, isDir, _ := idx.GetRealPath(tt.paths...)
|
2024-09-16 21:01:16 +00:00
|
|
|
adjustedRealPath := strings.TrimPrefix(realPath, trimPrefix)
|
|
|
|
if tt.want.path != adjustedRealPath || tt.want.isDir != isDir {
|
|
|
|
t.Errorf("expected %v:%v but got: %v:%v", tt.want.path, tt.want.isDir, adjustedRealPath, isDir)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-12-02 17:14:50 +00:00
|
|
|
|
|
|
|
func TestSortItems(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
input FileInfo
|
|
|
|
expected FileInfo
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Numeric and Lexicographical Sorting",
|
|
|
|
input: FileInfo{
|
|
|
|
Folders: []ItemInfo{
|
2025-01-05 19:05:33 +00:00
|
|
|
{Name: "10.txt"},
|
|
|
|
{Name: "2.txt"},
|
2024-12-02 17:14:50 +00:00
|
|
|
{Name: "apple"},
|
|
|
|
{Name: "Banana"},
|
|
|
|
},
|
|
|
|
Files: []ItemInfo{
|
2025-01-05 19:05:33 +00:00
|
|
|
{Name: "File2.txt"},
|
|
|
|
{Name: "File10.txt"},
|
2024-12-02 17:14:50 +00:00
|
|
|
{Name: "File1"},
|
|
|
|
{Name: "banana"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: FileInfo{
|
|
|
|
Folders: []ItemInfo{
|
2025-01-05 19:05:33 +00:00
|
|
|
{Name: "2.txt"},
|
|
|
|
{Name: "10.txt"},
|
2024-12-02 17:14:50 +00:00
|
|
|
{Name: "apple"},
|
|
|
|
{Name: "Banana"},
|
|
|
|
},
|
|
|
|
Files: []ItemInfo{
|
|
|
|
{Name: "banana"},
|
|
|
|
{Name: "File1"},
|
2025-01-05 19:05:33 +00:00
|
|
|
{Name: "File10.txt"},
|
|
|
|
{Name: "File2.txt"},
|
2024-12-02 17:14:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Only Lexicographical Sorting",
|
|
|
|
input: FileInfo{
|
|
|
|
Folders: []ItemInfo{
|
2025-01-05 19:05:33 +00:00
|
|
|
{Name: "dog.txt"},
|
|
|
|
{Name: "Cat.txt"},
|
2024-12-02 17:14:50 +00:00
|
|
|
{Name: "apple"},
|
|
|
|
},
|
|
|
|
Files: []ItemInfo{
|
|
|
|
{Name: "Zebra"},
|
|
|
|
{Name: "apple"},
|
|
|
|
{Name: "cat"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: FileInfo{
|
|
|
|
Folders: []ItemInfo{
|
|
|
|
{Name: "apple"},
|
2025-01-05 19:05:33 +00:00
|
|
|
{Name: "Cat.txt"},
|
|
|
|
{Name: "dog.txt"},
|
2024-12-02 17:14:50 +00:00
|
|
|
},
|
|
|
|
Files: []ItemInfo{
|
|
|
|
{Name: "apple"},
|
|
|
|
{Name: "cat"},
|
|
|
|
{Name: "Zebra"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
test.input.SortItems()
|
|
|
|
|
|
|
|
getNames := func(items []ItemInfo) []string {
|
|
|
|
names := []string{}
|
|
|
|
for _, folder := range items {
|
|
|
|
names = append(names, folder.Name)
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
actualFolderNames := getNames(test.input.Folders)
|
|
|
|
expectedFolderNames := getNames(test.expected.Folders)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actualFolderNames, expectedFolderNames) {
|
|
|
|
t.Errorf("Folders not sorted correctly.\nGot: %v\nExpected: %v", actualFolderNames, expectedFolderNames)
|
|
|
|
}
|
|
|
|
|
|
|
|
actualFileNames := getNames(test.input.Files)
|
|
|
|
expectedFileNames := getNames(test.expected.Files)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actualFileNames, expectedFileNames) {
|
|
|
|
t.Errorf("Files not sorted correctly.\nGot: %v\nExpected: %v", actualFileNames, expectedFileNames)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|