Move get file extension logic to util (#1670)

* Move get file extension logic to util

* Update TestGetFileExtension to use fake file

* Add more test cases to TestGetFileExtension
This commit is contained in:
David Pedersen authored and GitHub committed 2024-04-07 15:38:32 +02:00
1 parent 647fa469fb
commit 63815aaa75
5 files changed
+71 -23

No files matched your search

+3 -3
View File
@@ -37,7 +37,7 @@ func copySize(srcs []string) (int64, error) {
} }
func copyFile(src, dst string, preserve []string, info os.FileInfo, nums chan int64) error { func copyFile(src, dst string, preserve []string, info os.FileInfo, nums chan int64) error {
var dst_mode os.FileMode = 0666 var dst_mode os.FileMode = 0o666
preserve_timestamps := false preserve_timestamps := false
for _, s := range preserve { for _, s := range preserve {
switch s { switch s {
@@ -107,9 +107,9 @@ func copyAll(srcs []string, dstDir string, preserve []string) (nums chan int64,
file := filepath.Base(src) file := filepath.Base(src)
dst := filepath.Join(dstDir, file) dst := filepath.Join(dstDir, file)
_, err := os.Lstat(dst) lstat, err := os.Lstat(dst)
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
ext := filepath.Ext(file) ext := getFileExtension(lstat)
basename := file[:len(file)-len(ext)] basename := file[:len(file)-len(ext)]
var newPath string var newPath string
for i := 1; !os.IsNotExist(err); i++ { for i := 1; !os.IsNotExist(err); i++ {
+2 -2
View File
@@ -1622,8 +1622,8 @@ func (e *callExpr) eval(app *app, args []string) {
} }
normal(app) normal(app)
app.ui.cmdPrefix = "rename: " app.ui.cmdPrefix = "rename: "
extension := filepath.Ext(curr.Name()) extension := getFileExtension(curr)
if len(extension) == 0 || extension == curr.Name() || curr.IsDir() { if len(extension) == 0 {
// no extension or .hidden or is directory // no extension or .hidden or is directory
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(curr.Name())...) app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(curr.Name())...)
} else { } else {
+24 -5
View File
@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"io/fs"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv" "strconv"
@@ -291,12 +292,30 @@ func naturalLess(s1, s2 string) bool {
} }
} }
var reModKey = regexp.MustCompile(`<(c|s|a)-(.+)>`) // This function returns the extension of a file with a leading dot
var reRulerSub = regexp.MustCompile(`%[apmcsfithd]|%\{[^}]+\}`) // it returns an empty string if extension could not be determined
// i.e. directories, filenames without extensions
func getFileExtension(file fs.FileInfo) string {
if file.IsDir() {
return ""
}
if strings.Count(file.Name(), ".") == 1 && file.Name()[0] == '.' {
// hidden file without extension
return ""
}
return filepath.Ext(file.Name())
}
var reWord = regexp.MustCompile(`(\pL|\pN)+`) var (
var reWordBeg = regexp.MustCompile(`([^\pL\pN]|^)(\pL|\pN)`) reModKey = regexp.MustCompile(`<(c|s|a)-(.+)>`)
var reWordEnd = regexp.MustCompile(`(\pL|\pN)([^\pL\pN]|$)`) reRulerSub = regexp.MustCompile(`%[apmcsfithd]|%\{[^}]+\}`)
)
var (
reWord = regexp.MustCompile(`(\pL|\pN)+`)
reWordBeg = regexp.MustCompile(`([^\pL\pN]|^)(\pL|\pN)`)
reWordEnd = regexp.MustCompile(`(\pL|\pN)([^\pL\pN]|$)`)
)
func min(a, b int) int { func min(a, b int) int {
if a < b { if a < b {
+38
View File
@@ -5,6 +5,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
"time"
) )
func TestIsRoot(t *testing.T) { func TestIsRoot(t *testing.T) {
@@ -293,3 +294,40 @@ func TestNaturalLess(t *testing.T) {
} }
} }
} }
type fakeFileInfo struct {
name string
isDir bool
}
func (fileinfo fakeFileInfo) Name() string { return fileinfo.name }
func (fileinfo fakeFileInfo) Size() int64 { return 0 }
func (fileinfo fakeFileInfo) Mode() os.FileMode { return os.FileMode(0o000) }
func (fileinfo fakeFileInfo) ModTime() time.Time { return time.Unix(0, 0) }
func (fileinfo fakeFileInfo) IsDir() bool { return fileinfo.isDir }
func (fileinfo fakeFileInfo) Sys() any { return nil }
func TestGetFileExtension(t *testing.T) {
tests := []struct {
name string
fileName string
isDir bool
expectedExtension string
}{
{"normal file", "file.txt", false, ".txt"},
{"file without extension", "file", false, ""},
{"hidden file", ".gitignore", false, ""},
{"hidden file with extension", ".file.txt", false, ".txt"},
{"directory", "dir", true, ""},
{"hidden directory", ".git", true, ""},
{"directory with dot", "profile.d", true, ""},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := getFileExtension(fakeFileInfo{test.fileName, test.isDir}); got != test.expectedExtension {
t.Errorf("at input %q expected %q but got %q", test.fileName, test.expectedExtension, got)
}
})
}
}
+4 -13
View File
@@ -55,7 +55,7 @@ type fakeStat struct {
func (fs *fakeStat) Name() string { return fs.name } func (fs *fakeStat) Name() string { return fs.name }
func (fs *fakeStat) Size() int64 { return 0 } func (fs *fakeStat) Size() int64 { return 0 }
func (fs *fakeStat) Mode() os.FileMode { return os.FileMode(0000) } func (fs *fakeStat) Mode() os.FileMode { return os.FileMode(0o000) }
func (fs *fakeStat) ModTime() time.Time { return time.Unix(0, 0) } func (fs *fakeStat) ModTime() time.Time { return time.Unix(0, 0) }
func (fs *fakeStat) IsDir() bool { return false } func (fs *fakeStat) IsDir() bool { return false }
func (fs *fakeStat) Sys() any { return nil } func (fs *fakeStat) Sys() any { return nil }
@@ -88,7 +88,7 @@ func readdir(path string) ([]*file, error) {
dirSize: -1, dirSize: -1,
accessTime: time.Unix(0, 0), accessTime: time.Unix(0, 0),
changeTime: time.Unix(0, 0), changeTime: time.Unix(0, 0),
ext: filepath.Ext(fpath), ext: getFileExtension(lstat),
err: err, err: err,
}) })
continue continue
@@ -122,10 +122,6 @@ func readdir(path string) ([]*file, error) {
ct = lstat.ModTime() ct = lstat.ModTime()
} }
// returns an empty string if extension could not be determined
// i.e. directories, filenames without extensions
ext := filepath.Ext(fpath)
dirCount := -1 dirCount := -1
if lstat.IsDir() && gOpts.dircounts { if lstat.IsDir() && gOpts.dircounts {
d, err := os.Open(fpath) d, err := os.Open(fpath)
@@ -152,7 +148,7 @@ func readdir(path string) ([]*file, error) {
dirSize: -1, dirSize: -1,
accessTime: at, accessTime: at,
changeTime: ct, changeTime: ct,
ext: ext, ext: getFileExtension(lstat),
err: nil, err: nil,
}) })
} }
@@ -492,7 +488,6 @@ func (nav *nav) loadDirInternal(path string) *dir {
nav.dirPreviewChan <- d nav.dirPreviewChan <- d
} }
nav.dirChan <- d nav.dirChan <- d
}() }()
return d return d
} }
@@ -773,7 +768,6 @@ func matchPattern(pattern, name, path string) bool {
} }
func (nav *nav) previewDir(dir *dir, win *win) { func (nav *nav) previewDir(dir *dir, win *win) {
defer func() { defer func() {
dir.loading = false dir.loading = false
nav.dirChan <- dir nav.dirChan <- dir
@@ -829,11 +823,9 @@ func (nav *nav) previewDir(dir *dir, win *win) {
log.Printf("loading dir: %s", buf.Err()) log.Printf("loading dir: %s", buf.Err())
} }
} }
} }
func (nav *nav) preview(path string, win *win) { func (nav *nav) preview(path string, win *win) {
reg := &reg{loadTime: time.Now(), path: path} reg := &reg{loadTime: time.Now(), path: path}
defer func() { nav.regChan <- reg }() defer func() { nav.regChan <- reg }()
@@ -1408,7 +1400,7 @@ func (nav *nav) moveAsync(app *app, srcs []string, dstDir string) {
app.ui.exprChan <- echo app.ui.exprChan <- echo
continue continue
} else if !os.IsNotExist(err) { } else if !os.IsNotExist(err) {
ext := filepath.Ext(file) ext := getFileExtension(dstStat)
basename := file[:len(file)-len(ext)] basename := file[:len(file)-len(ext)]
var newPath string var newPath string
for i := 1; !os.IsNotExist(err); i++ { for i := 1; !os.IsNotExist(err); i++ {
@@ -1991,7 +1983,6 @@ func (m indexedSelections) Swap(i, j int) {
func (m indexedSelections) Less(i, j int) bool { return m.indices[i] < m.indices[j] } func (m indexedSelections) Less(i, j int) bool { return m.indices[i] < m.indices[j] }
func (nav *nav) currSelections() []string { func (nav *nav) currSelections() []string {
currDirOnly := gOpts.selmode == "dir" currDirOnly := gOpts.selmode == "dir"
currDirPath := "" currDirPath := ""
if currDirOnly { if currDirOnly {