forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test.go
158 lines (122 loc) · 3.62 KB
/
run_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
package packit_test
import (
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/fakes"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testRun(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
workingDir string
tmpDir string
cnbDir string
exitHandler *fakes.ExitHandler
buildCalled bool
detectCalled bool
)
build := func(packit.BuildContext) (packit.BuildResult, error) {
buildCalled = true
return packit.BuildResult{}, nil
}
detect := func(packit.DetectContext) (packit.DetectResult, error) {
detectCalled = true
return packit.DetectResult{}, nil
}
it.Before(func() {
var err error
workingDir, err = os.Getwd()
Expect(err).NotTo(HaveOccurred())
tmpDir, err = os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())
tmpDir, err = filepath.EvalSymlinks(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(os.Chdir(tmpDir)).To(Succeed())
cnbDir, err = os.MkdirTemp("", "cnb")
Expect(err).NotTo(HaveOccurred())
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), []byte(`
api = "0.5"
[buildpack]
id = "some-id"
name = "some-name"
version = "some-version"
clear-env = false
`), 0644)).To(Succeed())
exitHandler = &fakes.ExitHandler{}
buildCalled = false
detectCalled = false
})
it.After(func() {
Expect(os.Chdir(workingDir)).To(Succeed())
Expect(os.RemoveAll(tmpDir)).To(Succeed())
Expect(os.RemoveAll(cnbDir)).To(Succeed())
})
context("when running the detect executable", func() {
var (
args []string
buildPlanPath string
)
it.Before(func() {
buildPlanPath = filepath.Join(tmpDir, "buildplan.toml")
args = []string{filepath.Join(cnbDir, "bin", "detect"), "", buildPlanPath}
})
it.After(func() {
Expect(os.Remove(buildPlanPath)).To(Succeed())
})
it("calls the DetectFunc", func() {
packit.Run(detect, build, packit.WithArgs(args), packit.WithExitHandler(exitHandler))
Expect(detectCalled).To(BeTrue())
Expect(buildCalled).To(BeFalse())
Expect(exitHandler.ErrorCall.CallCount).To(Equal(0))
})
})
context("when running the build executable", func() {
var (
args []string
layersDir string
planPath string
)
it.Before(func() {
file, err := os.CreateTemp("", "plan.toml")
Expect(err).NotTo(HaveOccurred())
defer file.Close()
_, err = file.WriteString(`
[[entries]]
name = "some-entry"
version = "some-version"
[entries.metadata]
some-key = "some-value"
`)
Expect(err).NotTo(HaveOccurred())
planPath = file.Name()
layersDir, err = os.MkdirTemp("", "layers")
Expect(err).NotTo(HaveOccurred())
args = []string{filepath.Join(cnbDir, "bin", "build"), layersDir, "", planPath}
})
it.After(func() {
Expect(os.RemoveAll(layersDir)).To(Succeed())
Expect(os.Remove(planPath)).To(Succeed())
})
it("calls the BuildFunc", func() {
packit.Run(detect, build, packit.WithArgs(args), packit.WithExitHandler(exitHandler))
Expect(buildCalled).To(BeTrue())
Expect(detectCalled).To(BeFalse())
Expect(exitHandler.ErrorCall.CallCount).To(Equal(0))
})
})
context("when running any other executable", func() {
var args []string
it.Before(func() {
args = []string{filepath.Join(cnbDir, "bin", "something-else"), "some", "args"}
})
it("returns an error", func() {
packit.Run(nil, nil, packit.WithArgs(args), packit.WithExitHandler(exitHandler))
Expect(buildCalled).To(BeFalse())
Expect(detectCalled).To(BeFalse())
Expect(exitHandler.ErrorCall.Receives.Error).To(MatchError("failed to run buildpack: unknown lifecycle phase \"something-else\""))
})
})
}