From 01abe4890dc80a4b67d4a61678329b0495648825 Mon Sep 17 00:00:00 2001 From: Max North Date: Sun, 18 Feb 2024 15:25:55 -0500 Subject: [PATCH] Fix bug changing run pwd when using --dir (#21) --- cmd/root.go | 19 ++++++++++++++----- cmd/run.go | 1 + test/e2e/cmd-run.yml | 6 ++++++ test/e2e/e2e.go | 35 ++++++++++++----------------------- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index e95185b..4392895 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,22 +4,31 @@ import ( "errors" "fmt" "os" - "path" + "path/filepath" + "strings" "github.com/spf13/cobra" ) +var initWorkDir string + func NewRootCmd() *cobra.Command { rootCmd := &cobra.Command{ Use: "nv", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - dir := cmd.Flag("dir").Value.String() workDir, _ := os.Getwd() - dirPath := path.Join(workDir, dir) - if err := os.Chdir(dirPath); err != nil { + initWorkDir = workDir + + dir := cmd.Flag("dir").Value.String() + if !strings.HasPrefix(dir, "/") { + dir = filepath.Join(workDir, dir) + } + + if err := os.Chdir(dir); err != nil { // TODO: other kinds of errors? permission issues? - return fmt.Errorf("target directory not found: %s", dirPath) + return fmt.Errorf("target directory not found: %s", dir) } + return nil }, RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/run.go b/cmd/run.go index 7030d65..9b1f0a1 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -55,6 +55,7 @@ func execCommand(cobraCmd *cobra.Command, commandArgs []string) error { args = commandArgs[1:] } cmd := exec.Command(command, args...) + cmd.Dir = initWorkDir cmd.Stdin = cobraCmd.InOrStdin() cmd.Stdout = cobraCmd.OutOrStdout() cmd.Stderr = cobraCmd.ErrOrStderr() diff --git a/test/e2e/cmd-run.yml b/test/e2e/cmd-run.yml index 554ad20..9190fbd 100644 --- a/test/e2e/cmd-run.yml +++ b/test/e2e/cmd-run.yml @@ -34,3 +34,9 @@ test: files: *defaultFiles out: | something + - it: runs command in current dir even when --dir arg provided + with: + cmd: nv --dir /bin -- pwd + pwd: /var + out: | + /var diff --git a/test/e2e/e2e.go b/test/e2e/e2e.go index c0a275b..0219539 100644 --- a/test/e2e/e2e.go +++ b/test/e2e/e2e.go @@ -6,7 +6,6 @@ import ( "os" "os/exec" "path" - "path/filepath" "regexp" "strings" "testing" @@ -28,7 +27,7 @@ type TestCase struct { type TestCaseDetails struct { Cmd string - Dir string + Pwd string Files map[string]string Out string Err string @@ -45,16 +44,6 @@ func RunTestFile(t *testing.T, fileName string) { } } -func getTestFiles(t *testing.T) []string { - globTarget := path.Join(test.RootDir(), "test/e2e") + "/*.yml" - yamlPaths, err := filepath.Glob(globTarget) - if err != nil { - t.Fatalf("failed to glob test yaml files at path: %s", globTarget) - } - - return yamlPaths -} - func loadTestDef(t *testing.T, filePath string) *TestSubject { yamlData, err := os.ReadFile(filePath) if err != nil { @@ -73,14 +62,14 @@ func loadTestDef(t *testing.T, filePath string) *TestSubject { } func runTestCase(t *testing.T, testSubject *TestSubject, testCase *TestCase) { - if testCase.With.Dir != "" && testCase.With.Files != nil { - t.Fatal("invalid test config, only one of with.dir or with.files can be set, not both") - } else if testCase.With.Dir == "" { + if testCase.With.Pwd != "" && testCase.With.Files != nil { + t.Fatal("invalid test config, only one of with.pwd or with.files can be set, not both") + } else if testCase.With.Pwd == "" { tmpDir := createTmpDir(t, testCase) - testCase.With.Dir = tmpDir + testCase.With.Pwd = tmpDir } - exitCode, outstr, errstr := runCommand(t, testCase.With.Cmd, testCase.With.Dir) + exitCode, outstr, errstr := runCommand(t, testCase.With.Cmd, testCase.With.Pwd) if exitCode != testCase.With.Exit { t.Fatalf("error: actual exit code '%d' does not match expected '%d'", exitCode, testCase.With.Exit) @@ -111,7 +100,7 @@ func createTmpDir(t *testing.T, testCase *TestCase) string { return tmpDir } -func runCommand(t *testing.T, cmdStr, dir string) (exitCode int, outstr string, errstr string) { +func runCommand(t *testing.T, cmdStr, pwd string) (exitCode int, outstr string, errstr string) { cmdFields := strings.Fields(cmdStr) cmdName, args := cmdFields[0], []string{} if len(cmdFields) > 1 { @@ -120,10 +109,10 @@ func runCommand(t *testing.T, cmdStr, dir string) (exitCode int, outstr string, cmd := exec.Command(cmdName, args...) - if strings.HasPrefix(dir, "/") { - cmd.Dir = dir + if strings.HasPrefix(pwd, "/") { + cmd.Dir = pwd } else { - cmd.Dir = path.Join(test.RootDir(), dir) + cmd.Dir = path.Join(test.RootDir(), pwd) } outbuf := bytes.NewBufferString("") @@ -131,7 +120,7 @@ func runCommand(t *testing.T, cmdStr, dir string) (exitCode int, outstr string, cmd.Stdout = outbuf cmd.Stderr = errbuf - err := cmd.Run() + _ = cmd.Run() outbyte, err := io.ReadAll(outbuf) if err != nil { @@ -152,7 +141,7 @@ func runCommand(t *testing.T, cmdStr, dir string) (exitCode int, outstr string, func setPath(nvPath string) { path := os.Getenv("PATH") - if strings.Index(path, nvPath) == -1 { + if !strings.Contains(path, nvPath) { os.Setenv("PATH", path+":"+nvPath) } }