From d32e62fd0497cadcdf8522133c04f8d30807bf4e Mon Sep 17 00:00:00 2001 From: 3v0k4 Date: Wed, 5 Jun 2024 21:40:23 +0200 Subject: [PATCH] -P not available in old versions of env --- main.go | 6 +++--- main_test.go | 27 +++------------------------ 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index d68cfcd..e6ab118 100644 --- a/main.go +++ b/main.go @@ -123,12 +123,12 @@ func (p *program) unpathEntry(dir string, entries []fs.DirEntry, unpathN int) (s } func (p *program) run(cmd []string, path string) int { - arg := append([]string{"-P", path}, cmd...) - subcmd := exec.Command("env", arg...) + subcmd := exec.Command(cmd[0], cmd[1:]...) subcmd.Env = append(subcmd.Environ(), fmt.Sprintf("PATH=%s", path)) subcmd.Stdout = p.stdout subcmd.Stderr = p.stderr - if subcmd.Run() == nil { + err := subcmd.Run() + if err == nil { return 0 } else { return 1 diff --git a/main_test.go b/main_test.go index a761be0..4cc3261 100644 --- a/main_test.go +++ b/main_test.go @@ -10,26 +10,6 @@ import ( "testing" ) -func TestUnpathsNonExistingCommand(t *testing.T) { - var stdout, stderr bytes.Buffer - status := newProgram([]string{"unpath", "not-cat", "cat", "main_test.go"}, &stdout, &stderr).main() - if status != 0 { - t.Fatal(stderr) - } -} - -func TestUnpathsCommand(t *testing.T) { - var stdout, stderr bytes.Buffer - status := newProgram([]string{"unpath", "cat", "cat", "main_test.go"}, &stdout, &stderr).main() - if status == 0 { - t.Errorf("got: %d; want: %d", status, 0) - } - message := "env: cat: No such file or directory" - if !strings.Contains(stderr.String(), message) { - t.Errorf("got: %s; want: %s", stderr.String(), message) - } -} - func TestUnpathsNonExistingCommandThroughScript(t *testing.T) { script := createScript("#!/usr/bin/env bash\ncat $1", t.Fatal) var stdout, stderr bytes.Buffer @@ -65,8 +45,7 @@ func Test_e2e_UnpathsSiblingCommand(t *testing.T) { path = fmt.Sprintf("%s:%s", dir, path) arg := []string{"go", "run", "main.go", sibling_, script_, "main_test.go"} - arg = append([]string{"-P", path}, arg...) - cmd := exec.Command("env", arg...) + cmd := exec.Command(arg[0], arg[1:]...) cmd.Env = append(cmd.Environ(), fmt.Sprintf("PATH=%s", path)) err := cmd.Run() if err != nil { @@ -85,8 +64,7 @@ func Test_e2e_UnpathsCommand(t *testing.T) { path = fmt.Sprintf("%s:%s", dir, path) arg := []string{"go", "run", "main.go", command_, script_, "main_test.go"} - arg = append([]string{"-P", path}, arg...) - cmd := exec.Command("env", arg...) + cmd := exec.Command(arg[0], arg[1:]...) cmd.Env = append(cmd.Environ(), fmt.Sprintf("PATH=%s", path)) var stderr bytes.Buffer cmd.Stderr = &stderr @@ -133,6 +111,7 @@ func createScriptIn(dir, content string, fatal func(args ...any)) *os.File { if err != nil { fatal(err) } + defer file.Close() err = os.Chmod(file.Name(), 0777) if err != nil { fatal(err)