-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild_test.go
342 lines (288 loc) · 9.96 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package railsassets_test
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/scribe"
railsassets "github.com/paketo-buildpacks/rails-assets"
"github.com/paketo-buildpacks/rails-assets/fakes"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
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
clock chronos.Clock
buildProcess *fakes.BuildProcess
calculator *fakes.Calculator
environmentSetup *fakes.EnvironmentSetup
build packit.BuildFunc
)
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())
err = os.MkdirAll(filepath.Join(workingDir, "app", "assets"), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
buildProcess = &fakes.BuildProcess{}
buffer = bytes.NewBuffer(nil)
logger := scribe.NewEmitter(buffer)
clock = chronos.DefaultClock
calculator = &fakes.Calculator{}
calculator.SumCall.Returns.String = "some-calculator-sha"
environmentSetup = &fakes.EnvironmentSetup{}
build = railsassets.Build(buildProcess, calculator, environmentSetup, logger, clock)
})
it.After(func() {
Expect(os.RemoveAll(layersDir)).To(Succeed())
Expect(os.RemoveAll(cnbDir)).To(Succeed())
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
it("returns a result that precompiles assets", func() {
result, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
Layers: packit.Layers{Path: layersDir},
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(packit.BuildResult{
Layers: []packit.Layer{
{
Path: filepath.Join(layersDir, "assets"),
Name: "assets",
Launch: true,
SharedEnv: packit.Environment{},
BuildEnv: packit.Environment{},
LaunchEnv: packit.Environment{
"RAILS_ENV.default": "production",
"RAILS_SERVE_STATIC_FILES.default": "true",
"RAILS_LOG_TO_STDOUT.default": "true",
},
ProcessLaunchEnv: map[string]packit.Environment{},
Metadata: map[string]interface{}{
"cache_sha": "some-calculator-sha",
},
},
},
}))
Expect(buildProcess.ExecuteCall.CallCount).To(Equal(1))
Expect(buildProcess.ExecuteCall.Receives.WorkingDir).To(Equal(workingDir))
Expect(buffer.String()).To(ContainSubstring("Some Buildpack some-version"))
Expect(buffer.String()).To(ContainSubstring("Executing build process"))
Expect(buffer.String()).To(ContainSubstring("Configuring launch environment"))
Expect(buffer.String()).To(ContainSubstring(`RAILS_ENV -> "production"`))
Expect(buffer.String()).To(ContainSubstring(`RAILS_SERVE_STATIC_FILES -> "true"`))
})
context("when checksum matches", func() {
it.Before(func() {
err := os.WriteFile(filepath.Join(layersDir, fmt.Sprintf("%s.toml", railsassets.LayerNameAssets)), []byte(`
[metadata]
cache_sha = "some-calculator-sha"
`), 0600)
Expect(err).NotTo(HaveOccurred())
Expect(os.MkdirAll(filepath.Join(layersDir, "assets", "env.launch"), os.ModePerm)).To(Succeed())
err = os.WriteFile(filepath.Join(layersDir, "assets", "env.launch", "RAILS_ENV.default"), []byte("production"), 0600)
Expect(err).NotTo(HaveOccurred())
err = os.WriteFile(filepath.Join(layersDir, "assets", "env.launch", "RAILS_SERVE_STATIC_FILES.default"), []byte("true"), 0600)
Expect(err).NotTo(HaveOccurred())
calculator.SumCall.Returns.String = "some-calculator-sha"
})
it("reuses the cached layer", func() {
result, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Layers: packit.Layers{Path: layersDir},
})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(packit.BuildResult{
Layers: []packit.Layer{
{
Path: filepath.Join(layersDir, "assets"),
Name: "assets",
Launch: true,
SharedEnv: packit.Environment{},
BuildEnv: packit.Environment{},
LaunchEnv: packit.Environment{
"RAILS_ENV.default": "production",
"RAILS_SERVE_STATIC_FILES.default": "true",
},
ProcessLaunchEnv: map[string]packit.Environment{},
Metadata: map[string]interface{}{
"cache_sha": "some-calculator-sha",
},
},
},
}))
Expect(calculator.SumCall.Receives.Paths).To(Equal([]string{
filepath.Join(workingDir, "app", "assets"),
}))
Expect(buildProcess.ExecuteCall.CallCount).To(Equal(0))
Expect(buffer.String()).To(ContainSubstring("Some Buildpack some-version"))
Expect(buffer.String()).To(ContainSubstring("Reusing cached layer"))
})
context("when there is a lib/assets directory", func() {
it.Before(func() {
Expect(os.RemoveAll(filepath.Join(workingDir, "app", "assets"))).To(Succeed())
Expect(os.MkdirAll(filepath.Join(workingDir, "lib", "assets"), os.ModePerm)).To(Succeed())
})
it("uses that directory to calculate the checksum", func() {
_, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Layers: packit.Layers{Path: layersDir},
})
Expect(err).NotTo(HaveOccurred())
Expect(calculator.SumCall.Receives.Paths).To(Equal([]string{
filepath.Join(workingDir, "lib", "assets"),
}))
})
})
context("when there is a vendor/assets directory", func() {
it.Before(func() {
Expect(os.RemoveAll(filepath.Join(workingDir, "app", "assets"))).To(Succeed())
Expect(os.MkdirAll(filepath.Join(workingDir, "vendor", "assets"), os.ModePerm)).To(Succeed())
})
it("uses that directory to calculate the checksum", func() {
_, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Layers: packit.Layers{Path: layersDir},
})
Expect(err).NotTo(HaveOccurred())
Expect(calculator.SumCall.Receives.Paths).To(Equal([]string{
filepath.Join(workingDir, "vendor", "assets"),
}))
})
})
context("when there is a app/javascript directory", func() {
it.Before(func() {
Expect(os.RemoveAll(filepath.Join(workingDir, "app", "assets"))).To(Succeed())
Expect(os.MkdirAll(filepath.Join(workingDir, "app", "javascript"), os.ModePerm)).To(Succeed())
})
it("uses that directory to calculate the checksum", func() {
_, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Layers: packit.Layers{Path: layersDir},
})
Expect(err).NotTo(HaveOccurred())
Expect(calculator.SumCall.Receives.Paths).To(Equal([]string{
filepath.Join(workingDir, "app", "javascript"),
}))
})
})
context("when there are extra assets directories", func() {
it.Before(func() {
os.Setenv("BP_RAILS_ASSETS_EXTRA_SOURCE_PATHS", "custom/assets")
Expect(os.RemoveAll(filepath.Join(workingDir, "app", "assets"))).To(Succeed())
Expect(os.MkdirAll(filepath.Join(workingDir, "custom", "assets"), os.ModePerm)).To(Succeed())
})
it.After(func() {
os.Unsetenv("BP_RAILS_ASSETS_EXTRA_SOURCE_PATHS")
})
it("uses that directory to calculate the checksum", func() {
_, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Layers: packit.Layers{Path: layersDir},
})
Expect(err).NotTo(HaveOccurred())
Expect(calculator.SumCall.Receives.Paths).To(Equal([]string{
filepath.Join(workingDir, "custom", "assets"),
}))
})
})
context("failure cases", func() {
context("when environment linking fails", func() {
it.Before(func() {
environmentSetup.LinkCall.Returns.Error = errors.New("some-error")
})
it("returns the error", func() {
_, err := build(packit.BuildContext{})
Expect(err).To(MatchError("some-error"))
})
})
})
})
context("failure cases", func() {
context("when environment setup fails", func() {
it.Before(func() {
environmentSetup.ResetLocalCall.Returns.Error = errors.New("some-error")
})
it("returns the error", func() {
_, err := build(packit.BuildContext{})
Expect(err).To(MatchError("some-error"))
})
})
context("when calculator sum fails", func() {
it.Before(func() {
calculator.SumCall.Returns.Error = errors.New("some-error")
})
it("returns the error", func() {
_, err := build(packit.BuildContext{})
Expect(err).To(MatchError("some-error"))
})
})
context("when reset layer fails", func() {
it.Before(func() {
environmentSetup.ResetLayerCall.Returns.Error = errors.New("some-error")
})
it("returns the error", func() {
_, err := build(packit.BuildContext{})
Expect(err).To(MatchError("some-error"))
})
})
context("when precompile process fails", func() {
it.Before(func() {
buildProcess.ExecuteCall.Returns.Error = errors.New("some-error")
})
it("returns the error", func() {
_, err := build(packit.BuildContext{})
Expect(err).To(MatchError("some-error"))
})
})
})
}