-
Notifications
You must be signed in to change notification settings - Fork 5
/
buildpack_yml_parser_test.go
89 lines (71 loc) · 1.99 KB
/
buildpack_yml_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
package bundler_test
import (
"os"
"testing"
"github.com/paketo-buildpacks/bundler"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testBuildpackYMLParser(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
path string
parser bundler.BuildpackYMLParser
)
it.Before(func() {
file, err := os.CreateTemp("", "buildpack.yml")
Expect(err).NotTo(HaveOccurred())
defer file.Close()
_, err = file.WriteString(`---
bundler:
version: 1.2.3
`)
Expect(err).NotTo(HaveOccurred())
path = file.Name()
parser = bundler.NewBuildpackYMLParser()
})
it.After(func() {
Expect(os.RemoveAll(path)).To(Succeed())
})
context("ParseVersion", func() {
it("parses the bundler version from a buildpack.yml file", func() {
version, err := parser.ParseVersion(path)
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("1.2.3"))
})
context("when the buildpack.yml file does not exist", func() {
it.Before(func() {
Expect(os.Remove(path)).To(Succeed())
})
it("returns an empty version", func() {
version, err := parser.ParseVersion(path)
Expect(err).NotTo(HaveOccurred())
Expect(version).To(BeEmpty())
})
})
context("failure cases", func() {
context("when the buildpack.yml file cannot be read", func() {
it.Before(func() {
Expect(os.Chmod(path, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(path, 0644)).To(Succeed())
})
it("returns an error", func() {
_, err := parser.ParseVersion(path)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("when the contents of the buildpack.yml file are malformed", func() {
it.Before(func() {
err := os.WriteFile(path, []byte("%%%"), 0644)
Expect(err).NotTo(HaveOccurred())
})
it("returns an error", func() {
_, err := parser.ParseVersion(path)
Expect(err).To(MatchError(ContainSubstring("could not find expected directive name")))
})
})
})
})
}