43 lines
819 B
Go
43 lines
819 B
Go
package util
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func CreateWorkDir(t *testing.T) (string, func()) {
|
|
t.Helper()
|
|
dir, err := os.MkdirTemp("", "fs-test-workdir")
|
|
if err != nil {
|
|
t.Fatalf("create temp dir: %s", err)
|
|
}
|
|
return dir, func() {
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
t.Fatalf("remove temp dir: %s", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func DirExists(path string) (bool, error) {
|
|
return PathExists(path, true)
|
|
}
|
|
|
|
func FileExists(path string) (bool, error) {
|
|
return PathExists(path, false)
|
|
}
|
|
|
|
func PathExists(path string, isDir bool) (bool, error) {
|
|
if fi, err := os.Stat(path); err != nil {
|
|
if os.IsNotExist(err) {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
} else {
|
|
if isDir != fi.IsDir() {
|
|
return false, fmt.Errorf("wrong type: "+path+" mode=%s isDir=%t", fi.Mode(), isDir)
|
|
}
|
|
return true, nil
|
|
}
|
|
}
|