forked from paketo-buildpacks/node-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_version_parser_test.go
102 lines (82 loc) · 2.56 KB
/
node_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 nodeengine_test
import (
"fmt"
"os"
"testing"
nodeengine "github.com/paketo-buildpacks/node-engine"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testNodeVersionParser(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
path string
parser nodeengine.NodeVersionParser
)
it.Before(func() {
file, err := os.CreateTemp("", ".node-version")
Expect(err).NotTo(HaveOccurred())
defer file.Close()
path = file.Name()
parser = nodeengine.NewNodeVersionParser()
})
it.After(func() {
Expect(os.RemoveAll(path)).To(Succeed())
})
it("returns a version constraint", func() {
testCases := map[string]string{
"10.2": "10.2",
"10.2.3": "10.2.3",
"v10.2.3": "10.2.3",
}
for input, output := range testCases {
err := os.WriteFile(path, []byte(input), 0644)
Expect(err).NotTo(HaveOccurred())
version, err := parser.ParseVersion(path)
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal(output), fmt.Sprintf("input of %q failed to produce output of %q", input, output))
}
})
context("when the .node-version file does not exist", func() {
it.Before(func() {
Expect(os.RemoveAll(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 .node-version contains a malformed semver number", func() {
it.Before(func() {
err := os.WriteFile(path, []byte("1.2.this is not a number"), 0644)
Expect(err).NotTo(HaveOccurred())
})
it("returns an error", func() {
_, err := parser.ParseVersion(path)
Expect(err).To(MatchError("invalid version constraint specified in .node-version: \"1.2.this is not a number\""))
})
})
context("when the .node-version contains a value prefixed with `lts/`", func() {
it.Before(func() {
err := os.WriteFile(path, []byte("lts/*"), 0644)
Expect(err).NotTo(HaveOccurred())
})
it("returns an error", func() {
_, err := parser.ParseVersion(path)
Expect(err).To(MatchError("invalid version constraint specified in .node-version: \"lts/*\""))
})
})
context("when the .node-version contains the value `node`", func() {
it.Before(func() {
err := os.WriteFile(path, []byte("node"), 0644)
Expect(err).NotTo(HaveOccurred())
})
it("returns an error", func() {
_, err := parser.ParseVersion(path)
Expect(err).To(MatchError("invalid version constraint specified in .node-version: \"node\""))
})
})
})
}