Skip to content

Commit

Permalink
-P not available in old versions of env
Browse files Browse the repository at this point in the history
  • Loading branch information
3v0k4 committed Jun 6, 2024
1 parent 9fd1e28 commit d7d89ce
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ func (p *program) unpathEntry(dir string, entries []fs.DirEntry, entriesIndex in
}

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
Expand Down
29 changes: 4 additions & 25 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,6 @@ import (
)

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
status := newProgram([]string{"unpath", "not-cat", script.Name(), "main_test.go"}, &stdout, &stderr).main()
Expand All @@ -39,7 +19,7 @@ func TestUnpathsNonExistingCommandThroughScript(t *testing.T) {
}
}

func TestUnpathsCommandThroughScript(t *testing.T) {
func TestUnpathsCommand(t *testing.T) {
script := createScript("#!/usr/bin/env bash\ncat $1", t.Fatal)
var stdout, stderr bytes.Buffer
status := newProgram([]string{"unpath", "cat", script.Name(), "main_test.go"}, &stdout, &stderr).main()
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d7d89ce

Please sign in to comment.