Compare commits

...

2 commits

Author SHA1 Message Date
Lea Anthony
f7db15e583
Find go.mod in path 2022-10-19 22:19:11 +11:00
Lea Anthony
955c7db5fb
Add FileFileInParents 2022-10-19 21:09:34 +11:00
3 changed files with 95 additions and 9 deletions

View file

@ -1,13 +1,13 @@
package build
import (
"os"
"path/filepath"
"fmt"
"github.com/wailsapp/wails/v2/cmd/wails/internal"
"github.com/wailsapp/wails/v2/internal/fs"
"github.com/wailsapp/wails/v2/internal/gomod"
"github.com/wailsapp/wails/v2/internal/goversion"
"github.com/wailsapp/wails/v2/pkg/clilogger"
"os"
)
func SyncGoMod(logger *clilogger.CLILogger, updateWailsVersion bool) error {
@ -15,7 +15,10 @@ func SyncGoMod(logger *clilogger.CLILogger, updateWailsVersion bool) error {
if err != nil {
return err
}
gomodFilename := filepath.Join(cwd, "go.mod")
gomodFilename := fs.FindFileInParents(cwd, "go.mod")
if gomodFilename == "" {
return fmt.Errorf("no go.mod file found")
}
gomodData, err := os.ReadFile(gomodFilename)
if err != nil {
return err

View file

@ -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
}

View file

@ -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)
}
})
}
}