-
Notifications
You must be signed in to change notification settings - Fork 14
/
parser_test.go
154 lines (131 loc) · 3.7 KB
/
parser_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
package nginx_test
import (
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/nginx"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testParser(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
workingDir string
cnbPath string
parser nginx.Parser
)
it.Before(func() {
workingDir = t.TempDir()
cnbPath = t.TempDir()
Expect(os.WriteFile(
filepath.Join(cnbPath, "buildpack.toml"),
[]byte(`
[metadata]
[metadata.default-versions]
nginx = "the-default-version"
[metadata.version-lines]
mainline = "1.17.*"
stable = "1.16.*"
`), 0644)).To(Succeed())
parser = nginx.NewParser()
})
context("Calling ParseYml", func() {
context("when buildpack.yml is valid and specifies an nginx version", func() {
it.Before(func() {
Expect(os.WriteFile(
filepath.Join(workingDir, "buildpack.yml"),
[]byte(`---
nginx:
version: 1.2.3`),
os.ModePerm,
))
})
it("parses out a version from buildpack.yml and detects version source", func() {
version, ok, err := parser.ParseYml(workingDir)
Expect(err).ToNot(HaveOccurred())
Expect(ok).To(Equal(true))
Expect(version).To(Equal("1.2.3"))
})
})
context("buildpack.yml cannot be opened", func() {
it.Before(func() {
Expect(os.WriteFile(
filepath.Join(workingDir, "buildpack.yml"),
[]byte(`some-content`),
0000,
))
})
it("returns an error", func() {
_, _, err := parser.ParseYml(workingDir)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("buildpack.yml cannot be parsed", func() {
it.Before(func() {
Expect(os.WriteFile(
filepath.Join(workingDir, "buildpack.yml"),
[]byte(`%%%`),
0644,
))
})
it("returns an error", func() {
_, _, err := parser.ParseYml(workingDir)
Expect(err).To(MatchError(ContainSubstring("could not find expected directive name")))
})
})
})
context("Calling ResolveVersion", func() {
context("with mainline version", func() {
it("return the buildpack.toml mainline version", func() {
version, err := parser.ResolveVersion(cnbPath, "mainline")
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("1.17.*"))
})
})
context("with stable version", func() {
it("return the buildpack.toml stable version", func() {
version, err := parser.ResolveVersion(cnbPath, "stable")
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("1.16.*"))
})
})
context("with empty version", func() {
it("return the buildpack.toml default version", func() {
version, err := parser.ResolveVersion(cnbPath, "")
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("the-default-version"))
})
})
context("with semver version", func() {
it("return the same semver version", func() {
version, err := parser.ResolveVersion(cnbPath, "1.1.1")
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("1.1.1"))
})
})
})
context("failure cases", func() {
context("buildpack.toml cannot be opened", func() {
it.Before(func() {
Expect(os.Chmod(filepath.Join(cnbPath, "buildpack.toml"), 0000)).To(Succeed())
})
it("returns an error", func() {
_, err := parser.ResolveVersion(cnbPath, "")
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("buildpack.toml cannot be parsed", func() {
it.Before(func() {
Expect(os.WriteFile(
filepath.Join(cnbPath, "buildpack.toml"),
[]byte(`%%%`),
0644,
)).To(Succeed())
})
it("returns an error", func() {
_, err := parser.ResolveVersion(cnbPath, "")
Expect(err).To(MatchError(ContainSubstring("got '%' instead")))
})
})
})
}