-
Notifications
You must be signed in to change notification settings - Fork 5
/
php_version_resolver_test.go
252 lines (218 loc) · 7.95 KB
/
php_version_resolver_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
package composer_test
import (
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/composer"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testPhpVersionResolver(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
workingDir string
phpVersionResolver composer.PhpVersionResolver
)
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
phpVersionResolver = composer.NewPhpVersionResolver()
})
it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
context("when both composer.lock and composer.json contain php versions", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "composer.json"), []byte(`{
"require": {
"php-64bit": "php-64bit.version.from-composer-json",
"php": "php-32bit.version.from-composer-json"
}
}`), os.ModePerm)).To(Succeed())
})
context("when composer.lock contains the 64bit php version", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "composer.lock"), []byte(`{
"platform": {
"php-64bit": "php-64bit.version.from-composer-lock"
}
}`), os.ModePerm)).To(Succeed())
})
it(`requires "php" with version metadata from composer.lock 64bit`, func() {
version, versionSource, err := phpVersionResolver.Resolve(
filepath.Join(workingDir, "composer.json"),
filepath.Join(workingDir, "composer.lock"))
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("php-64bit.version.from-composer-lock"))
Expect(versionSource).To(Equal("composer.lock"))
})
})
context("when composer.lock contains the 32bit php version", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "composer.lock"), []byte(`{
"platform": {
"php": "php.version.from-composer-lock"
}
}`), os.ModePerm)).To(Succeed())
})
it(`requires "php" with version metadata from composer.lock 32bit`, func() {
version, versionSource, err := phpVersionResolver.Resolve(
filepath.Join(workingDir, "composer.json"),
filepath.Join(workingDir, "composer.lock"))
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("php.version.from-composer-lock"))
Expect(versionSource).To(Equal("composer.lock"))
})
})
context("when composer.lock contains both the 32bit and 64bit php version", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "composer.lock"), []byte(`{
"platform": {
"php-64bit": "php-64bit.version.from-composer-lock",
"php": "php.version.from-composer-lock"
}
}`), os.ModePerm)).To(Succeed())
})
it(`requires "php" with 64bit version metadata from composer.lock`, func() {
version, versionSource, err := phpVersionResolver.Resolve(
filepath.Join(workingDir, "composer.json"),
filepath.Join(workingDir, "composer.lock"))
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("php-64bit.version.from-composer-lock"))
Expect(versionSource).To(Equal("composer.lock"))
})
})
})
context("when composer.lock does not have any Platform dependencies", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "composer.lock"), []byte(`{
"platform": []
}`), os.ModePerm)).To(Succeed())
})
it(`returns empty versions`, func() {
version, versionSource, err := phpVersionResolver.Resolve(
filepath.Join(workingDir, "composer.json"),
filepath.Join(workingDir, "composer.lock"))
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal(""))
Expect(versionSource).To(Equal(""))
})
})
context("when composer.lock is not present", func() {
context("when composer.json contains the 64bit PHP version", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "composer.json"), []byte(`{
"require": {
"php-64bit": "php-64bit.version.from-composer-json"
}
}`), os.ModePerm)).To(Succeed())
})
it(`requires "php" with version metadata from composer.json 64bit`, func() {
version, versionSource, err := phpVersionResolver.Resolve(
filepath.Join(workingDir, "composer.json"),
filepath.Join(workingDir, "composer.lock"))
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("php-64bit.version.from-composer-json"))
Expect(versionSource).To(Equal("composer.json"))
})
})
context("when composer.json contains the 32bit PHP version", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "composer.json"), []byte(`{
"require": {
"php": "php-32bit.version.from-composer-json"
}
}`), os.ModePerm)).To(Succeed())
})
it(`requires "php" with version metadata from composer.json 32 bit`, func() {
version, versionSource, err := phpVersionResolver.Resolve(
filepath.Join(workingDir, "composer.json"),
filepath.Join(workingDir, "composer.lock"))
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("php-32bit.version.from-composer-json"))
Expect(versionSource).To(Equal("composer.json"))
})
})
context("when composer.json contains both the 64bit and 32bit PHP version", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "composer.json"), []byte(`{
"require": {
"php-64bit": "php-64bit.version.from-composer-json",
"php": "php-32bit.version.from-composer-json"
}
}`), os.ModePerm)).To(Succeed())
})
it(`requires "php" with version metadata from composer.json 64bit`, func() {
version, versionSource, err := phpVersionResolver.Resolve(
filepath.Join(workingDir, "composer.json"),
filepath.Join(workingDir, "composer.lock"))
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("php-64bit.version.from-composer-json"))
Expect(versionSource).To(Equal("composer.json"))
})
})
})
context("composer.lock is not present and composer.json does not have any platform dependencies", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "composer.json"), []byte(`{}`), os.ModePerm)).To(Succeed())
})
it("returns empty result", func() {
version, versionSource, err := phpVersionResolver.Resolve(
filepath.Join(workingDir, "composer.json"),
filepath.Join(workingDir, "composer.lock"))
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal(""))
Expect(versionSource).To(Equal(""))
})
})
context("failing handling", func() {
context("composer.lock cannot be STAT'ed", func() {
var (
workingDir string
err error
)
it.Before(func() {
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
Expect(os.Chmod(workingDir, 0000)).To(Succeed())
})
it("will return the error", func() {
_, _, err := phpVersionResolver.Resolve(
filepath.Join(workingDir, "composer.json"),
filepath.Join(workingDir, "composer.lock"))
Expect(err).To(HaveOccurred())
})
})
context("composer.lock cannot be deserialized", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "composer.lock"), []byte(`hi`), os.ModePerm)).To(Succeed())
})
it("returns error", func() {
_, _, err := phpVersionResolver.Resolve(
filepath.Join(workingDir, "composer.json"),
filepath.Join(workingDir, "composer.lock"))
Expect(err).To(HaveOccurred())
})
})
context("composer.json cannot be deserialized", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "composer.json"), []byte(`hi`), os.ModePerm)).To(Succeed())
})
it("returns error", func() {
_, _, err := phpVersionResolver.Resolve(
filepath.Join(workingDir, "composer.json"),
filepath.Join(workingDir, "composer.lock"))
Expect(err).To(HaveOccurred())
})
})
context("composer.json is not present", func() {
it("returns error", func() {
_, _, err := phpVersionResolver.Resolve(
filepath.Join(workingDir, "composer.json"),
filepath.Join(workingDir, "composer.lock"))
Expect(err).To(HaveOccurred())
})
})
})
}