-
Notifications
You must be signed in to change notification settings - Fork 9
/
build_test.go
216 lines (183 loc) · 5.65 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
package nodestart_test
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/libreload-packit"
nodestart "github.com/paketo-buildpacks/node-start/v2"
"github.com/paketo-buildpacks/node-start/v2/fakes"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/scribe"
"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
reloader *fakes.Reloader
buildContext packit.BuildContext
build packit.BuildFunc
)
it.Before(func() {
layersDir = t.TempDir()
cnbDir = t.TempDir()
workingDir = t.TempDir()
Expect(os.WriteFile(filepath.Join(workingDir, "server.js"), nil, 0600)).To(Succeed())
reloader = &fakes.Reloader{}
buffer = bytes.NewBuffer(nil)
logger := scribe.NewEmitter(buffer)
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},
}
build = nodestart.Build(logger, reloader)
})
it("returns a result that provides a node start 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: "node",
Args: []string{"server.js"},
Default: true,
Direct: true,
},
},
},
}))
Expect(buffer.String()).To(ContainSubstring("Some Buildpack some-version"))
Expect(buffer.String()).To(ContainSubstring("Assigning launch processes"))
Expect(buffer.String()).To(ContainSubstring("node server.js"))
})
context("sniff test that files that end something other than js work", func() {
it.Before(func() {
layersDir = t.TempDir()
cnbDir = t.TempDir()
workingDir = t.TempDir()
Expect(os.WriteFile(filepath.Join(workingDir, "app.mjs"), nil, 0600)).To(Succeed())
reloader = &fakes.Reloader{}
buffer = bytes.NewBuffer(nil)
logger := scribe.NewEmitter(buffer)
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},
}
build = nodestart.Build(logger, reloader)
})
it("returns a result that provides a node start 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: "node",
Args: []string{"app.mjs"},
Default: true,
Direct: true,
},
},
},
}))
Expect(buffer.String()).To(ContainSubstring("Some Buildpack some-version"))
Expect(buffer.String()).To(ContainSubstring("Assigning launch processes"))
Expect(buffer.String()).To(ContainSubstring("node app.mjs"))
})
})
context("when live reload is enabled", func() {
it.Before(func() {
reloader.ShouldEnableLiveReloadCall.Returns.Bool = true
reloader.TransformReloadableProcessesCall.Returns.Reloadable = packit.Process{
Type: "Reloadable",
Command: "Reloadable-Command",
}
reloader.TransformReloadableProcessesCall.Returns.NonReloadable = packit.Process{
Type: "Nonreloadable",
Command: "Nonreloadable-Command",
}
})
it("adds a reloadable start command and makes it the default", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())
Expect(result.Launch.Processes).To(Equal([]packit.Process{
{
Type: "web",
Command: "Reloadable-Command",
},
{
Type: "no-reload",
Command: "Nonreloadable-Command",
},
}))
Expect(reloader.TransformReloadableProcessesCall.Receives.OriginalProcess).To(Equal(packit.Process{
Type: "web",
Command: "node",
Args: []string{"server.js"},
Default: true,
Direct: true,
}))
Expect(reloader.TransformReloadableProcessesCall.Receives.Spec).To(Equal(libreload.ReloadableProcessSpec{
WatchPaths: []string{workingDir},
}))
Expect(buffer.String()).To(ContainSubstring("Some Buildpack some-version"))
Expect(buffer.String()).To(ContainSubstring("Assigning launch processes"))
})
})
context("failure cases", func() {
context("when the application finding fails", func() {
it.Before(func() {
Expect(os.Remove(filepath.Join(workingDir, "server.js"))).To(Succeed())
})
it("returns an error", func() {
_, err := build(buildContext)
Expect(err).To(MatchError(fmt.Errorf("could not find app in %s: expected one of server.js | server.cjs | server.mjs | app.js | app.cjs | app.mjs | main.js | main.cjs | main.mjs | index.js | index.cjs | index.mjs", workingDir)))
})
})
context("when the reloader returns an error", func() {
it.Before(func() {
reloader.ShouldEnableLiveReloadCall.Returns.Error = errors.New("reloader error")
})
it("returns an error", func() {
_, err := build(buildContext)
Expect(err).To(MatchError("reloader error"))
})
})
})
}