mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-14 14:45:49 +01:00
Add FileFileInParents
This commit is contained in:
parent
39addf2011
commit
955c7db5fb
2 changed files with 88 additions and 5 deletions
|
|
@ -378,3 +378,27 @@ func FindPathToFile(fsys fs.FS, file string) (string, error) {
|
|||
}
|
||||
return "", fmt.Errorf("no index.html found")
|
||||
}
|
||||
|
||||
// FindFileInParents searches for a file in the current directory and all parent directories.
|
||||
// Returns the absolute path to the file if found, otherwise an empty string
|
||||
func FindFileInParents(path string, filename string) string {
|
||||
|
||||
// Check for bad paths
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
var pathToFile string
|
||||
for {
|
||||
pathToFile = filepath.Join(path, filename)
|
||||
if _, err := os.Stat(pathToFile); err == nil {
|
||||
break
|
||||
}
|
||||
parent := filepath.Dir(path)
|
||||
if parent == path {
|
||||
return ""
|
||||
}
|
||||
path = parent
|
||||
}
|
||||
return pathToFile
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"github.com/samber/lo"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
|
@ -10,22 +11,80 @@ import (
|
|||
|
||||
func TestRelativePath(t *testing.T) {
|
||||
|
||||
is := is.New(t)
|
||||
i := is.New(t)
|
||||
|
||||
cwd, err := os.Getwd()
|
||||
is.Equal(err, nil)
|
||||
i.Equal(err, nil)
|
||||
|
||||
// Check current directory
|
||||
actual := RelativePath(".")
|
||||
is.Equal(actual, cwd)
|
||||
i.Equal(actual, cwd)
|
||||
|
||||
// Check 2 parameters
|
||||
actual = RelativePath("..", "fs")
|
||||
is.Equal(actual, cwd)
|
||||
i.Equal(actual, cwd)
|
||||
|
||||
// Check 3 parameters including filename
|
||||
actual = RelativePath("..", "fs", "fs.go")
|
||||
expected := filepath.Join(cwd, "fs.go")
|
||||
is.Equal(actual, expected)
|
||||
i.Equal(actual, expected)
|
||||
|
||||
}
|
||||
|
||||
func Test_FindFileInParents(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func() (startDir string, configDir string)
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "should error when no wails.json file is found in local or parent dirs",
|
||||
setup: func() (string, string) {
|
||||
tempDir := os.TempDir()
|
||||
testDir := lo.Must(os.MkdirTemp(tempDir, "projectPath"))
|
||||
_ = os.MkdirAll(testDir, 0755)
|
||||
return testDir, ""
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "should find wails.json in local path",
|
||||
setup: func() (string, string) {
|
||||
tempDir := os.TempDir()
|
||||
testDir := lo.Must(os.MkdirTemp(tempDir, "projectPath"))
|
||||
_ = os.MkdirAll(testDir, 0755)
|
||||
configFile := filepath.Join(testDir, "wails.json")
|
||||
_ = os.WriteFile(configFile, []byte("{}"), 0755)
|
||||
return testDir, configFile
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "should find wails.json in parent path",
|
||||
setup: func() (string, string) {
|
||||
tempDir := os.TempDir()
|
||||
testDir := lo.Must(os.MkdirTemp(tempDir, "projectPath"))
|
||||
_ = os.MkdirAll(testDir, 0755)
|
||||
parentDir := filepath.Dir(testDir)
|
||||
configFile := filepath.Join(parentDir, "wails.json")
|
||||
_ = os.WriteFile(configFile, []byte("{}"), 0755)
|
||||
return testDir, configFile
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
path, expectedPath := tt.setup()
|
||||
defer func() {
|
||||
if expectedPath != "" {
|
||||
_ = os.Remove(expectedPath)
|
||||
}
|
||||
}()
|
||||
got := FindFileInParents(path, "wails.json")
|
||||
if got != expectedPath {
|
||||
t.Errorf("FindFileInParents() got = %v, want %v", got, expectedPath)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue