-
Notifications
You must be signed in to change notification settings - Fork 8
/
scripts_test.go
108 lines (90 loc) · 3.18 KB
/
scripts_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
103
104
105
106
107
108
package noderunscript_test
import (
"os"
"path/filepath"
"testing"
noderunscript "github.com/paketo-buildpacks/node-run-script"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testScripts(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
workingDir string
)
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
Expect(os.WriteFile(filepath.Join(workingDir, "package.json"), []byte(`{
"name": "mypackage",
"scripts": {
"build": "mybuildcommand --args",
"some-script": "somecommand --args",
"other-script": "othercommand --args"
}
}`), 0600)).To(Succeed())
})
it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
it("returns a list of scripts to run and the package manager used", func() {
scripts, manager, err := noderunscript.ScriptsToRun(workingDir, "build")
Expect(err).NotTo(HaveOccurred())
Expect(scripts).To(Equal([]string{"build"}))
Expect(manager).To(Equal("npm"))
})
context("when a yarn.lock file is present", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "yarn.lock"), nil, 0600)).To(Succeed())
})
it("identifies yarn as the package manager", func() {
scripts, manager, err := noderunscript.ScriptsToRun(workingDir, "build")
Expect(err).NotTo(HaveOccurred())
Expect(scripts).To(Equal([]string{"build"}))
Expect(manager).To(Equal("yarn"))
})
})
context("when specific scripts are requested to run", func() {
it("returns those scripts", func() {
scripts, manager, err := noderunscript.ScriptsToRun(workingDir, "some-script")
Expect(err).NotTo(HaveOccurred())
Expect(scripts).To(Equal([]string{"some-script"}))
Expect(manager).To(Equal("npm"))
})
})
context("when a list of scripts is requested to run", func() {
it("returns those scripts", func() {
scripts, manager, err := noderunscript.ScriptsToRun(workingDir, "some-script, other-script")
Expect(err).NotTo(HaveOccurred())
Expect(scripts).To(Equal([]string{"some-script", "other-script"}))
Expect(manager).To(Equal("npm"))
})
})
context("when a requested script is missing from the package.json", func() {
it("returns an error", func() {
_, _, err := noderunscript.ScriptsToRun(workingDir, "missing-script")
Expect(err).To(MatchError(`could not find script(s) [missing-script] in package.json`))
})
})
context("failure cases", func() {
context("when the package.json file does not exist", func() {
it.Before(func() {
Expect(os.Remove(filepath.Join(workingDir, "package.json"))).To(Succeed())
})
it("returns an error", func() {
_, _, err := noderunscript.ScriptsToRun(workingDir, "some-script")
Expect(err).To(MatchError(ContainSubstring("no such file or directory")))
})
})
context("when the package.json file is malformed", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "package.json"), []byte("%%%"), 0600)).To(Succeed())
})
it("returns an error", func() {
_, _, err := noderunscript.ScriptsToRun(workingDir, "some-script")
Expect(err).To(MatchError(ContainSubstring("invalid character '%'")))
})
})
})
}