Skip to content

Commit

Permalink
Fix bug changing run pwd when using --dir (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnorth authored Feb 18, 2024
1 parent 877a894 commit 01abe48
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
19 changes: 14 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/cmd-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 12 additions & 23 deletions test/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"testing"
Expand All @@ -28,7 +27,7 @@ type TestCase struct {

type TestCaseDetails struct {
Cmd string
Dir string
Pwd string
Files map[string]string
Out string
Err string
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -120,18 +109,18 @@ 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("")
errbuf := bytes.NewBufferString("")
cmd.Stdout = outbuf
cmd.Stderr = errbuf

err := cmd.Run()
_ = cmd.Run()

outbyte, err := io.ReadAll(outbuf)
if err != nil {
Expand All @@ -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)
}
}

0 comments on commit 01abe48

Please sign in to comment.