forked from paketo-buildpacks/httpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_parser_test.go
102 lines (82 loc) · 2.77 KB
/
version_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
package httpd_test
import (
"os"
"testing"
"github.com/initializ-buildpacks/httpd"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testVersionParser(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
versionParser httpd.VersionParser
)
it.Before(func() {
versionParser = httpd.NewVersionParser()
})
context("ParseVersion", func() {
context("when there is no buildpack.yml", func() {
it("returns a * for the version and empty version source", func() {
version, versionSource, err := versionParser.ParseVersion("some-path")
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("*"))
Expect(versionSource).To(Equal(""))
})
})
context("when there is a buildpack.yml", func() {
var path string
it.Before(func() {
file, err := os.CreateTemp("", "buildpack.yml")
Expect(err).NotTo(HaveOccurred())
_, err = file.WriteString(`{"httpd": {"version": "some-version"}}`)
Expect(err).NotTo(HaveOccurred())
path = file.Name()
Expect(file.Close()).To(Succeed())
})
it.After(func() {
Expect(os.Remove(path)).To(Succeed())
})
it("parses the version", func() {
version, versionSource, err := versionParser.ParseVersion(path)
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("some-version"))
Expect(versionSource).To(Equal("buildpack.yml"))
})
context("when there is not httpd version in the buildpack.yml", func() {
it.Before(func() {
err := os.WriteFile(path, []byte(`{"some-thing": {"version": "some-version"}}`), 0644)
Expect(err).NotTo(HaveOccurred())
})
it("returns a * for the version and empty version source", func() {
version, versionSource, err := versionParser.ParseVersion(path)
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("*"))
Expect(versionSource).To(Equal(""))
})
})
context("failure cases", func() {
context("when the file cannot be opened", func() {
it.Before(func() {
Expect(os.Chmod(path, 0000)).To(Succeed())
})
it("returns an error", func() {
_, _, err := versionParser.ParseVersion(path)
Expect(err).To(MatchError(ContainSubstring("failed to parse buildpack.yml")))
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("when the file contains malformed yaml", func() {
it.Before(func() {
err := os.WriteFile(path, []byte("%%%"), 0644)
Expect(err).NotTo(HaveOccurred())
})
it("returns an error", func() {
_, _, err := versionParser.ParseVersion(path)
Expect(err).To(MatchError(ContainSubstring("failed to parse buildpack.yml")))
Expect(err).To(MatchError(ContainSubstring("could not find expected directive name")))
})
})
})
})
})
}