Fixed fs package tests on Windows

This commit is contained in:
Eduard Urbach 2025-02-14 20:35:44 +01:00
parent 88b3f468d1
commit 4df847bf60
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
2 changed files with 62 additions and 13 deletions

View File

@ -2,23 +2,27 @@
package fs
import (
"io/fs"
"path/filepath"
"strings"
)
import "os"
// Walk calls your callback function for every file name inside the directory.
// It doesn't distinguish between files and directories.
func Walk(directory string, callBack func(string)) error {
return filepath.WalkDir(directory, func(path string, d fs.DirEntry, err error) error {
fileName := d.Name()
f, err := os.Open(directory)
if strings.HasPrefix(fileName, ".") {
return filepath.SkipDir
}
if err != nil {
return err
}
callBack(fileName)
return nil
})
files, err := f.Readdirnames(0)
f.Close()
if err != nil {
return err
}
for _, file := range files {
callBack(file)
}
return nil
}

45
src/fs/bench_test.go Normal file
View File

@ -0,0 +1,45 @@
package fs_test
import (
"os"
"testing"
"git.akyoto.dev/cli/q/src/fs"
"git.akyoto.dev/go/assert"
)
func BenchmarkReadDir(b *testing.B) {
for b.Loop() {
files, err := os.ReadDir(".")
assert.Nil(b, err)
for _, file := range files {
func(string) {}(file.Name())
}
}
}
func BenchmarkReaddirnames(b *testing.B) {
for b.Loop() {
f, err := os.Open(".")
assert.Nil(b, err)
files, err := f.Readdirnames(0)
assert.Nil(b, err)
for _, file := range files {
func(string) {}(file)
}
f.Close()
}
}
func BenchmarkWalk(b *testing.B) {
for b.Loop() {
err := fs.Walk(".", func(file string) {
func(string) {}(file)
})
assert.Nil(b, err)
}
}