From 955c7db5fbd49640d7a2bfb7a458ed012a3c0098 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Wed, 19 Oct 2022 21:09:34 +1100 Subject: [PATCH 1/2] Add `FileFileInParents` --- v2/internal/fs/fs.go | 24 ++++++++++++++ v2/internal/fs/fs_test.go | 69 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/v2/internal/fs/fs.go b/v2/internal/fs/fs.go index 84e42a392..7deae9810 100644 --- a/v2/internal/fs/fs.go +++ b/v2/internal/fs/fs.go @@ -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 +} diff --git a/v2/internal/fs/fs_test.go b/v2/internal/fs/fs_test.go index 2cc524123..efc4929e6 100644 --- a/v2/internal/fs/fs_test.go +++ b/v2/internal/fs/fs_test.go @@ -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) + } + }) + } +} From f7db15e583908810090b8276c5cbf2fb8b8186b7 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Wed, 19 Oct 2022 22:19:11 +1100 Subject: [PATCH 2/2] Find go.mod in path --- v2/cmd/wails/internal/commands/build/gomod.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/v2/cmd/wails/internal/commands/build/gomod.go b/v2/cmd/wails/internal/commands/build/gomod.go index 19eefd7c6..29bc9ffb6 100644 --- a/v2/cmd/wails/internal/commands/build/gomod.go +++ b/v2/cmd/wails/internal/commands/build/gomod.go @@ -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