-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild_test.go
285 lines (251 loc) · 6.98 KB
/
build_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package poetryrun_test
import (
"bytes"
"errors"
"fmt"
"os"
"testing"
"github.com/paketo-buildpacks/libreload-packit"
"github.com/paketo-buildpacks/libreload-packit/watchexec"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/scribe"
poetryrun "github.com/paketo-buildpacks/poetry-run"
"github.com/paketo-buildpacks/poetry-run/fakes"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
. "github.com/paketo-buildpacks/occam/matchers"
)
func testBuild(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
layersDir string
workingDir string
cnbDir string
buffer *bytes.Buffer
pyProjectParser *fakes.PyProjectParser
reloader *fakes.Reloader
build packit.BuildFunc
buildContext packit.BuildContext
)
it.Before(func() {
var err error
layersDir, err = os.MkdirTemp("", "layers")
Expect(err).NotTo(HaveOccurred())
cnbDir, err = os.MkdirTemp("", "cnb")
Expect(err).NotTo(HaveOccurred())
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
buffer = bytes.NewBuffer(nil)
logger := scribe.NewEmitter(buffer).WithLevel("DEBUG")
pyProjectParser = &fakes.PyProjectParser{}
pyProjectParser.ParseCall.Returns.String = "some-script"
reloader = &fakes.Reloader{}
reloader.TransformReloadableProcessesCall.Stub = func(process packit.Process, spec libreload.ReloadableProcessSpec) (packit.Process, packit.Process) {
return watchexec.NewWatchexecReloader().TransformReloadableProcesses(process, spec)
}
build = poetryrun.Build(pyProjectParser, logger, reloader)
buildContext = packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{},
},
Layers: packit.Layers{Path: layersDir},
}
})
it.After(func() {
Expect(os.RemoveAll(layersDir)).To(Succeed())
Expect(os.RemoveAll(cnbDir)).To(Succeed())
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
context("with BP_POETRY_RUN_TARGET not set", func() {
it("returns a result that sets the 'poetry run' launch command", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(packit.BuildResult{
Plan: packit.BuildpackPlan{
Entries: nil,
},
Layers: nil,
Launch: packit.LaunchMetadata{
Processes: []packit.Process{
{
Type: "web",
Command: "poetry",
Args: []string{
"run",
"some-script",
},
Default: true,
Direct: true,
},
},
},
}))
Expect(buffer.String()).To(ContainLines(
ContainSubstring("Finding the poetry run target"),
ContainSubstring("Found pyproject.toml script=some-script"),
ContainSubstring("Assigning launch processes:"),
ContainSubstring("web (default): poetry run some-script"),
))
})
context("when live reload is true", func() {
it.Before(func() {
reloader.ShouldEnableLiveReloadCall.Returns.Bool = true
})
it("adds a reloadable start command and makes it the default", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(packit.BuildResult{
Plan: packit.BuildpackPlan{
Entries: nil,
},
Layers: nil,
Launch: packit.LaunchMetadata{
Processes: []packit.Process{
{
Type: "reload-web",
Command: "watchexec",
Args: []string{
"--restart",
"--watch", workingDir,
"--shell", "none",
"--",
"poetry",
"run",
"some-script",
},
Default: true,
Direct: true,
},
{
Type: "web",
Command: "poetry",
Args: []string{"run", "some-script"},
Direct: true,
},
},
},
}))
})
})
})
context("with BP_POETRY_RUN_TARGET set", func() {
it.Before(func() {
Expect(os.Setenv("BP_POETRY_RUN_TARGET", "a custom command")).To(Succeed())
})
it.After(func() {
Expect(os.Unsetenv("BP_POETRY_RUN_TARGET")).To(Succeed())
})
it("will use the value of BP_POETRY_RUN_TARGET and not use the pyproject.toml parser", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(packit.BuildResult{
Plan: packit.BuildpackPlan{
Entries: nil,
},
Layers: nil,
Launch: packit.LaunchMetadata{
Processes: []packit.Process{
{
Type: "web",
Command: "poetry",
Args: []string{
"run",
"a",
"custom",
"command",
},
Default: true,
Direct: true,
},
},
},
}))
Expect(buffer.String()).To(ContainLines(
ContainSubstring("Finding the poetry run target"),
ContainSubstring("Found BP_POETRY_RUN_TARGET=a custom command"),
ContainSubstring("Assigning launch processes:"),
ContainSubstring("web (default): poetry run a custom command"),
))
Expect(pyProjectParser.ParseCall.CallCount).To(Equal(0))
})
context("when live reload is enabled", func() {
it.Before(func() {
reloader.ShouldEnableLiveReloadCall.Returns.Bool = true
})
it("adds a reloadable start command and makes it the default", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(packit.BuildResult{
Plan: packit.BuildpackPlan{
Entries: nil,
},
Layers: nil,
Launch: packit.LaunchMetadata{
Processes: []packit.Process{
{
Type: "reload-web",
Command: "watchexec",
Args: []string{
"--restart",
"--watch", workingDir,
"--shell", "none",
"--",
"poetry",
"run",
"a",
"custom",
"command",
},
Default: true,
Direct: true,
},
{
Type: "web",
Command: "poetry",
Args: []string{
"run",
"a",
"custom",
"command",
},
Direct: true,
},
},
},
}))
})
})
})
context("failure cases", func() {
context("when BP_POETRY_RUN_TARGET is not set", func() {
it.Before(func() {
Expect(os.Unsetenv("BP_POETRY_RUN_TARGET")).To(Succeed())
})
context(" and the pyproject.toml parser returns an error", func() {
it.Before(func() {
pyProjectParser.ParseCall.Returns.Error = fmt.Errorf("some error")
})
it("returns the error", func() {
_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("some error")))
})
})
})
context("when reloader returns an error", func() {
it.Before(func() {
reloader.ShouldEnableLiveReloadCall.Returns.Error = errors.New("failed to parse")
})
it("returns an error", func() {
_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("failed to parse")))
})
})
})
}