-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
122 lines (108 loc) · 3.42 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestUnpathsNonExistingCommand(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()
if status != 0 {
t.Fatal(stderr.String())
}
}
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()
if status == 0 {
t.Fatal(stderr)
}
message := "cat: command not found"
if !strings.Contains(stderr.String(), message) {
t.Errorf("got: %s; want: %s", stderr.String(), message)
}
}
func Test_e2e_UnpathsSiblingCommand(t *testing.T) {
dir := createDir(t.Fatal)
command := createScriptIn(dir, "#!/usr/bin/env bash\ncat $1", t.Fatal)
command_ := filepath.Base(command.Name())
sibling := createScriptIn(dir, "#!/usr/bin/env bash\ncat $1", t.Fatal)
sibling_ := filepath.Base(sibling.Name())
script := createScriptIn(dir, fmt.Sprintf("#!/usr/bin/env bash\n%s $1", command_), t.Fatal)
script_ := filepath.Base(script.Name())
path, _ := os.LookupEnv("PATH")
path = fmt.Sprintf("%s:%s", dir, path)
arg := []string{"go", "run", "main.go", sibling_, script_, "main_test.go"}
cmd := exec.Command(arg[0], arg[1:]...)
cmd.Env = append(cmd.Environ(), fmt.Sprintf("PATH=%s", path))
err := cmd.Run()
if err != nil {
t.Fatal(err)
}
}
func Test_e2e_UnpathsCommand(t *testing.T) {
dir := createDir(t.Fatal)
command := createScriptIn(dir, "#!/usr/bin/env bash\ncat $1", t.Fatal)
command_ := filepath.Base(command.Name())
script := createScriptIn(dir, fmt.Sprintf("#!/usr/bin/env bash\n%s $1", command_), t.Fatal)
script_ := filepath.Base(script.Name())
path, _ := os.LookupEnv("PATH")
path = fmt.Sprintf("%s:%s", dir, path)
arg := []string{"go", "run", "main.go", command_, script_, "main_test.go"}
cmd := exec.Command(arg[0], arg[1:]...)
cmd.Env = append(cmd.Environ(), fmt.Sprintf("PATH=%s", path))
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err == nil {
t.Fatal(err)
}
message := fmt.Sprintf("%s: command not found", command_)
if !strings.Contains(stderr.String(), message) {
t.Errorf("got: %s; want: %s", stderr.String(), message)
}
}
func Test_e2e_UnpathsCommandsRecursively(t *testing.T) {
script := createScript("#!/usr/bin/env bash\ncat $1", t.Fatal)
cmd := exec.Command("go", "run", "main.go", "non-cat", "go", "run", "main.go", "cat", script.Name(), "main_test.go")
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err == nil {
t.Fatal(err)
}
message := fmt.Sprintf("cat: command not found")
if !strings.Contains(stderr.String(), message) {
t.Errorf("got: %s; want: %s", stderr.String(), message)
}
}
func createScript(content string, fatal func(args ...any)) *os.File {
dir := createDir(fatal)
return createScriptIn(dir, content, fatal)
}
func createDir(fatal func(args ...any)) string {
dir, err := os.MkdirTemp("", "bin")
if err != nil {
fatal(err)
}
return dir
}
func createScriptIn(dir, content string, fatal func(args ...any)) *os.File {
file, err := os.CreateTemp(dir, "script")
if err != nil {
fatal(err)
}
defer file.Close()
err = os.Chmod(file.Name(), 0777)
if err != nil {
fatal(err)
}
fmt.Fprintf(file, content)
return file
}