forked from paketo-buildpacks/npm-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_json_parser_test.go
70 lines (56 loc) · 1.58 KB
/
package_json_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
package npminstall_test
import (
"os"
"testing"
npminstall "github.com/paketo-buildpacks/npm-install"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testPackageJSONParser(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect
context("ParseVersion", func() {
var (
path string
parser npminstall.PackageJSONParser
)
it.Before(func() {
file, err := os.CreateTemp("", "package.json")
Expect(err).NotTo(HaveOccurred())
defer file.Close()
_, err = file.WriteString(`{
"engines": {
"node": "1.2.3"
}
}`)
Expect(err).NotTo(HaveOccurred())
path = file.Name()
parser = npminstall.NewPackageJSONParser()
})
it.After(func() {
Expect(os.RemoveAll(path)).To(Succeed())
})
it("parses the node engine version from a package.json file", func() {
version, err := parser.ParseVersion(path)
Expect(err).NotTo(HaveOccurred())
Expect(version).To(Equal("1.2.3"))
})
context("failure cases", func() {
context("when the package.json file does not exist", func() {
it("returns an error", func() {
_, err := parser.ParseVersion("/missing/file")
Expect(err).To(MatchError(ContainSubstring("no such file or directory")))
})
})
context("when the package.json contents 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("invalid character")))
})
})
})
})
}