Fixed fs package tests on Windows
This commit is contained in:
parent
88b3f468d1
commit
4df847bf60
@ -2,23 +2,27 @@
|
|||||||
|
|
||||||
package fs
|
package fs
|
||||||
|
|
||||||
import (
|
import "os"
|
||||||
"io/fs"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Walk calls your callback function for every file name inside the directory.
|
// Walk calls your callback function for every file name inside the directory.
|
||||||
// It doesn't distinguish between files and directories.
|
// It doesn't distinguish between files and directories.
|
||||||
func Walk(directory string, callBack func(string)) error {
|
func Walk(directory string, callBack func(string)) error {
|
||||||
return filepath.WalkDir(directory, func(path string, d fs.DirEntry, err error) error {
|
f, err := os.Open(directory)
|
||||||
fileName := d.Name()
|
|
||||||
|
|
||||||
if strings.HasPrefix(fileName, ".") {
|
if err != nil {
|
||||||
return filepath.SkipDir
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
files, err := f.Readdirnames(0)
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
callBack(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
callBack(fileName)
|
|
||||||
return nil
|
return nil
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
45
src/fs/bench_test.go
Normal file
45
src/fs/bench_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user