forked from paketo-buildpacks/yarn-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_json_parser_test.go
81 lines (65 loc) · 2.08 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
71
72
73
74
75
76
77
78
79
80
81
package yarnstart_test
import (
"os"
"path/filepath"
"testing"
. "github.com/onsi/gomega"
yarnstart "github.com/paketo-buildpacks/yarn-start"
"github.com/sclevine/spec"
)
func testPackageJsonParser(t *testing.T, context spec.G, it spec.S) {
Expect := NewWithT(t).Expect
context("when parsing a valid package.json with start scripts", func() {
var packageLocation string
var workingDir string
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
content := `{
"scripts": {
"poststart": "echo \"poststart\"",
"prestart": "echo \"prestart\"",
"start": "echo \"start\" && node server.js"
}
},
`
packageLocation = filepath.Join(workingDir, "package.json")
Expect(os.WriteFile(packageLocation, []byte(content), 0600)).To(Succeed())
})
it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
it("successfully extracts the scripts information", func() {
pkg, err := yarnstart.NewPackageJsonFromPath(packageLocation)
Expect(err).ToNot(HaveOccurred())
Expect(pkg.Scripts.Start).To(ContainSubstring(`echo "start" && node server.js`))
Expect(pkg.Scripts.PreStart).To(Equal(`echo "prestart"`))
Expect(pkg.Scripts.PostStart).To(Equal(`echo "poststart"`))
})
})
context("when the package.json is not a valid json file", func() {
var packageLocation string
var workingDir string
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
packageLocation = filepath.Join(workingDir, "package.json")
Expect(os.WriteFile(packageLocation, nil, 0600)).To(Succeed())
})
it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
it("fails parsing", func() {
_, err := yarnstart.NewPackageJsonFromPath(packageLocation)
Expect(err).To(HaveOccurred())
})
})
context("when the path to package.json is invalid", func() {
it("fails parsing", func() {
_, err := yarnstart.NewPackageJsonFromPath("/tmp/non-existent")
Expect(err).To(HaveOccurred())
})
})
}