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

+4 -13
View File
@@ -55,7 +55,7 @@ type fakeStat struct {
func (fs *fakeStat) Name() string { return fs.name }
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) IsDir() bool { return false }
func (fs *fakeStat) Sys() any { return nil }
@@ -88,7 +88,7 @@ func readdir(path string) ([]*file, error) {
dirSize: -1,
accessTime: time.Unix(0, 0),
changeTime: time.Unix(0, 0),
ext: filepath.Ext(fpath),
ext: getFileExtension(lstat),
err: err,
})
continue
@@ -122,10 +122,6 @@ func readdir(path string) ([]*file, error) {
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
if lstat.IsDir() && gOpts.dircounts {
d, err := os.Open(fpath)
@@ -152,7 +148,7 @@ func readdir(path string) ([]*file, error) {
dirSize: -1,
accessTime: at,
changeTime: ct,
ext: ext,
ext: getFileExtension(lstat),
err: nil,
})
}
@@ -492,7 +488,6 @@ func (nav *nav) loadDirInternal(path string) *dir {
nav.dirPreviewChan <- d
}
nav.dirChan <- d
}()
return d
}
@@ -773,7 +768,6 @@ func matchPattern(pattern, name, path string) bool {
}
func (nav *nav) previewDir(dir *dir, win *win) {
defer func() {
dir.loading = false
nav.dirChan <- dir
@@ -829,11 +823,9 @@ func (nav *nav) previewDir(dir *dir, win *win) {
log.Printf("loading dir: %s", buf.Err())
}
}
}
func (nav *nav) preview(path string, win *win) {
reg := &reg{loadTime: time.Now(), path: path}
defer func() { nav.regChan <- reg }()
@@ -1408,7 +1400,7 @@ func (nav *nav) moveAsync(app *app, srcs []string, dstDir string) {
app.ui.exprChan <- echo
continue
} else if !os.IsNotExist(err) {
ext := filepath.Ext(file)
ext := getFileExtension(dstStat)
basename := file[:len(file)-len(ext)]
var newPath string
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 (nav *nav) currSelections() []string {
currDirOnly := gOpts.selmode == "dir"
currDirPath := ""
if currDirOnly {