-
Notifications
You must be signed in to change notification settings - Fork 26
/
executable_test.go
172 lines (143 loc) · 4.56 KB
/
executable_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package pexec_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/onsi/gomega/gexec"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testPexec(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
tmpDir string
stdout, stderr *bytes.Buffer
executable pexec.Executable
)
it.Before(func() {
var err error
tmpDir, err = os.MkdirTemp("", "executable")
Expect(err).NotTo(HaveOccurred())
tmpDir, err = filepath.EvalSymlinks(tmpDir)
Expect(err).NotTo(HaveOccurred())
stdout = bytes.NewBuffer(nil)
stderr = bytes.NewBuffer(nil)
executable = pexec.NewExecutable(filepath.Base(fakeCLI))
})
it.After(func() {
Expect(os.RemoveAll(tmpDir)).To(Succeed())
})
context("Execute", func() {
it("executes the given arguments against the executable", func() {
err := executable.Execute(pexec.Execution{
Args: []string{"something"},
Stdout: stdout,
})
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring(fmt.Sprintf("Arguments: [%s something]", fakeCLI)))
})
context("when given a execution directory", func() {
it("executes within that directory", func() {
err := executable.Execute(pexec.Execution{
Dir: tmpDir,
Stdout: stdout,
})
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring(fmt.Sprintf("PWD=%s", tmpDir)))
})
})
context("when given an execution environment", func() {
it("executes with that environment", func() {
err := executable.Execute(pexec.Execution{
Env: []string{"SOME_KEY=some-value"},
Stdout: stdout,
})
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("SOME_KEY=some-value"))
})
})
context("when given a writer for stdout and stderr", func() {
it("pipes stdout to that writer", func() {
err := executable.Execute(pexec.Execution{
Stdout: stdout,
Stderr: stderr,
})
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("Output on stdout"))
Expect(stderr.String()).To(ContainSubstring("Output on stderr"))
})
})
context("when the executable is on the PATH given as an argument", func() {
var path string
it.Before(func() {
path = os.Getenv("PATH")
os.Setenv("PATH", "some-path")
})
it.After(func() {
os.Setenv("PATH", path)
})
it("executes the given arguments against the executable", func() {
err := executable.Execute(pexec.Execution{
Args: []string{"something"},
Env: []string{fmt.Sprintf("PATH=%s", filepath.Dir(fakeCLI))},
Stdout: stdout,
})
Expect(err).NotTo(HaveOccurred())
Expect(stdout).To(ContainSubstring(fmt.Sprintf("Arguments: [%s something]", fakeCLI)))
Expect(os.Getenv("PATH")).To(Equal("some-path"))
})
})
context("when given a reader for stdin", func() {
it("pipes that reader to stdout", func() {
err := executable.Execute(pexec.Execution{
Stdin: strings.NewReader("something on stdin"),
Stdout: stdout,
})
Expect(err).NotTo(HaveOccurred())
Expect(stdout.String()).To(ContainSubstring("Input on stdin\nsomething on stdin\n"))
})
})
context("failure cases", func() {
context("when the executable cannot be found on the path", func() {
it.Before(func() {
executable = pexec.NewExecutable("unknown-executable")
})
it("returns an error", func() {
err := executable.Execute(pexec.Execution{})
Expect(err).To(MatchError("exec: \"unknown-executable\": executable file not found in $PATH"))
})
})
context("when the executable errors", func() {
var (
errorCLI string
path string
)
it.Before(func() {
Expect(os.Setenv("PATH", existingPath)).To(Succeed())
var err error
errorCLI, err = gexec.Build("github.com/paketo-buildpacks/packit/v2/fakes/some-executable", "-ldflags", "-X main.fail=true")
Expect(err).NotTo(HaveOccurred())
path = os.Getenv("PATH")
Expect(os.Setenv("PATH", filepath.Dir(errorCLI))).To(Succeed())
})
it.After(func() {
Expect(os.Setenv("PATH", path)).To(Succeed())
})
it("executes the given arguments against the executable", func() {
err := executable.Execute(pexec.Execution{
Args: []string{"something"},
Stdout: stdout,
Stderr: stderr,
})
Expect(err).To(MatchError("exit status 1"))
Expect(stdout).To(ContainSubstring("Error on stdout"))
Expect(stderr).To(ContainSubstring("Error on stderr"))
})
})
})
})
}