Fix tests

This commit is contained in:
Ingo Oppermann
2023-02-21 12:57:33 +01:00
parent d77e4d7160
commit 1c04961fc1
13 changed files with 275 additions and 19 deletions

View File

@@ -553,6 +553,51 @@ func (fs *diskFilesystem) List(path, pattern string) []FileInfo {
return files
}
func (fs *diskFilesystem) LookPath(file string) (string, error) {
if strings.Contains(file, "/") {
file = fs.cleanPath(file)
err := fs.findExecutable(file)
if err == nil {
return file, nil
}
return "", os.ErrNotExist
}
path := os.Getenv("PATH")
for _, dir := range filepath.SplitList(path) {
if dir == "" {
// Unix shell semantics: path element "" means "."
dir = "."
}
path := filepath.Join(dir, file)
path = fs.cleanPath(path)
if err := fs.findExecutable(path); err == nil {
if !filepath.IsAbs(path) {
return path, os.ErrNotExist
}
return path, nil
}
}
return "", os.ErrNotExist
}
func (fs *diskFilesystem) findExecutable(file string) error {
d, err := fs.Stat(file)
if err != nil {
return err
}
m := d.Mode()
if m.IsDir() {
return fmt.Errorf("is a directory")
}
if m&0111 != 0 {
return nil
}
return os.ErrPermission
}
func (fs *diskFilesystem) walk(path string, walkfn func(path string, info os.FileInfo)) {
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {

View File

@@ -66,6 +66,12 @@ type ReadFilesystem interface {
// List lists all files that are currently on the filesystem.
List(path, pattern string) []FileInfo
// LookPath searches for an executable named file in the directories named by the PATH environment
// variable. If file contains a slash, it is tried directly and the PATH is not consulted. Otherwise,
// on success, the result is an absolute path. On non-disk filesystems. Only the mere existence
// of that file is verfied.
LookPath(file string) (string, error)
}
type WriteFilesystem interface {

View File

@@ -697,6 +697,39 @@ func (fs *memFilesystem) List(path, pattern string) []FileInfo {
return files
}
func (fs *memFilesystem) LookPath(file string) (string, error) {
if strings.Contains(file, "/") {
file = fs.cleanPath(file)
info, err := fs.Stat(file)
if err == nil {
if !info.Mode().IsRegular() {
return file, os.ErrNotExist
}
return file, nil
}
return "", os.ErrNotExist
}
path := os.Getenv("PATH")
for _, dir := range filepath.SplitList(path) {
if dir == "" {
// Unix shell semantics: path element "" means "."
dir = "."
}
path := filepath.Join(dir, file)
path = fs.cleanPath(path)
if info, err := fs.Stat(path); err == nil {
if !filepath.IsAbs(path) {
return path, os.ErrNotExist
}
if !info.Mode().IsRegular() {
return path, os.ErrNotExist
}
return path, nil
}
}
return "", os.ErrNotExist
}
func (fs *memFilesystem) cleanPath(path string) string {
if !filepath.IsAbs(path) {
path = filepath.Join("/", path)

View File

@@ -511,6 +511,39 @@ func (fs *s3Filesystem) List(path, pattern string) []FileInfo {
return files
}
func (fs *s3Filesystem) LookPath(file string) (string, error) {
if strings.Contains(file, "/") {
file = fs.cleanPath(file)
info, err := fs.Stat(file)
if err == nil {
if !info.Mode().IsRegular() {
return file, os.ErrNotExist
}
return file, nil
}
return "", os.ErrNotExist
}
path := os.Getenv("PATH")
for _, dir := range filepath.SplitList(path) {
if dir == "" {
// Unix shell semantics: path element "" means "."
dir = "."
}
path := filepath.Join(dir, file)
path = fs.cleanPath(path)
if info, err := fs.Stat(path); err == nil {
if !filepath.IsAbs(path) {
return path, os.ErrNotExist
}
if !info.Mode().IsRegular() {
return path, os.ErrNotExist
}
return path, nil
}
}
return "", os.ErrNotExist
}
func (fs *s3Filesystem) isDir(path string) bool {
if !strings.HasSuffix(path, "/") {
path = path + "/"