forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_test.go
234 lines (199 loc) · 5.92 KB
/
project_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
package project
import (
"io/ioutil"
"log"
"math/rand"
"os"
"testing"
"time"
"github.com/heroku/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
h "github.com/buildpacks/pack/testhelpers"
)
func TestProject(t *testing.T) {
h.RequireDocker(t)
color.Disable(true)
defer color.Disable(false)
rand.Seed(time.Now().UTC().UnixNano())
spec.Run(t, "Provider", testProject, spec.Parallel(), spec.Report(report.Terminal{}))
}
func testProject(t *testing.T, when spec.G, it spec.S) {
when("#ReadProjectDescriptor", func() {
it("should parse a valid project.toml file", func() {
projectToml := `
[project]
name = "gallant"
[[project.licenses]]
type = "MIT"
[build]
exclude = [ "*.jar" ]
[[build.buildpacks]]
id = "example/lua"
version = "1.0"
[[build.buildpacks]]
uri = "https://example.com/buildpack"
[[build.env]]
name = "JAVA_OPTS"
value = "-Xmx300m"
[metadata]
pipeline = "Lucerne"
`
tmpProjectToml, err := createTmpProjectTomlFile(projectToml)
if err != nil {
t.Fatal(err)
}
projectDescriptor, err := ReadProjectDescriptor(tmpProjectToml.Name())
if err != nil {
t.Fatal(err)
}
var expected string
expected = "gallant"
if projectDescriptor.Project.Name != expected {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
expected, projectDescriptor.Project.Name)
}
expected = "example/lua"
if projectDescriptor.Build.Buildpacks[0].ID != expected {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
expected, projectDescriptor.Build.Buildpacks[0].ID)
}
expected = "1.0"
if projectDescriptor.Build.Buildpacks[0].Version != expected {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
expected, projectDescriptor.Build.Buildpacks[0].Version)
}
expected = "https://example.com/buildpack"
if projectDescriptor.Build.Buildpacks[1].URI != expected {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
expected, projectDescriptor.Build.Buildpacks[1].URI)
}
expected = "JAVA_OPTS"
if projectDescriptor.Build.Env[0].Name != expected {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
expected, projectDescriptor.Build.Env[0].Name)
}
expected = "-Xmx300m"
if projectDescriptor.Build.Env[0].Value != expected {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
expected, projectDescriptor.Build.Env[0].Value)
}
expected = "MIT"
if projectDescriptor.Project.Licenses[0].Type != expected {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
expected, projectDescriptor.Project.Licenses[0].Type)
}
expected = "Lucerne"
if projectDescriptor.Metadata["pipeline"] != expected {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
expected, projectDescriptor.Metadata["pipeline"])
}
})
it("should create empty build ENV", func() {
projectToml := `
[project]
name = "gallant"
`
tmpProjectToml, err := createTmpProjectTomlFile(projectToml)
if err != nil {
t.Fatal(err)
}
projectDescriptor, err := ReadProjectDescriptor(tmpProjectToml.Name())
if err != nil {
t.Fatal(err)
}
expected := 0
if len(projectDescriptor.Build.Env) != 0 {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
expected, string(len(projectDescriptor.Build.Env)))
}
for _, envVar := range projectDescriptor.Build.Env {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
"[]", envVar)
}
})
it("should fail for an invalid project.toml path", func() {
_, err := ReadProjectDescriptor("/path/that/does/not/exist/project.toml")
if !os.IsNotExist(err) {
t.Fatalf("Expected\n-----\n%#v\n-----\nbut got\n-----\n%#v\n",
"project.toml does not exist error", "no error")
}
})
})
it("should enforce mutual exclusivity between exclude and include", func() {
projectToml := `
[project]
name = "bad excludes and includes"
[build]
exclude = [ "*.jar" ]
include = [ "*.jpg" ]
`
tmpProjectToml, err := createTmpProjectTomlFile(projectToml)
if err != nil {
t.Fatal(err)
}
_, err = ReadProjectDescriptor(tmpProjectToml.Name())
if err == nil {
t.Fatalf(
"Expected error for having both exclude and include defined")
}
})
it("should have an id or uri defined for buildpacks", func() {
projectToml := `
[project]
name = "missing buildpacks id and uri"
[[build.buildpacks]]
version = "1.2.3"
`
tmpProjectToml, err := createTmpProjectTomlFile(projectToml)
if err != nil {
t.Fatal(err)
}
_, err = ReadProjectDescriptor(tmpProjectToml.Name())
if err == nil {
t.Fatalf("Expected error for NOT having id or uri defined for buildpacks")
}
})
it("should not allow both uri and version", func() {
projectToml := `
[project]
name = "cannot have both uri and version defined"
[[build.buildpacks]]
uri = "https://example.com/buildpack"
version = "1.2.3"
`
tmpProjectToml, err := createTmpProjectTomlFile(projectToml)
if err != nil {
t.Fatal(err)
}
_, err = ReadProjectDescriptor(tmpProjectToml.Name())
if err == nil {
t.Fatal("Expected error for having both uri and version defined for a buildpack(s)")
}
})
it("should require either a type or uri for licenses", func() {
projectToml := `
[project]
name = "licenses should have either a type or uri defined"
[[project.licenses]]
`
tmpProjectToml, err := createTmpProjectTomlFile(projectToml)
if err != nil {
t.Fatal(err)
}
_, err = ReadProjectDescriptor(tmpProjectToml.Name())
if err == nil {
t.Fatal("Expected error for having neither type or uri defined for licenses")
}
})
}
func createTmpProjectTomlFile(projectToml string) (*os.File, error) {
tmpProjectToml, err := ioutil.TempFile(os.TempDir(), "project-")
if err != nil {
log.Fatal("Failed to create temporary project toml file", err)
}
if _, err := tmpProjectToml.Write([]byte(projectToml)); err != nil {
log.Fatal("Failed to write to temporary file", err)
}
return tmpProjectToml, err
}