forked from paketo-buildpacks/npm-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_build_process_test.go
156 lines (129 loc) · 4.84 KB
/
install_build_process_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package npminstall_test
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
npminstall "github.com/paketo-buildpacks/npm-install"
"github.com/paketo-buildpacks/npm-install/fakes"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testInstallBuildProcess(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
modulesDir string
cacheDir string
workingDir string
executable *fakes.Executable
environment *fakes.EnvironmentConfig
buffer *bytes.Buffer
commandOutput *bytes.Buffer
process npminstall.InstallBuildProcess
)
it.Before(func() {
var err error
modulesDir, err = os.MkdirTemp("", "modules")
Expect(err).NotTo(HaveOccurred())
cacheDir, err = os.MkdirTemp("", "cache")
Expect(err).NotTo(HaveOccurred())
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
executable = &fakes.Executable{}
environment = &fakes.EnvironmentConfig{}
environment.GetValueCall.Returns.String = "some-val"
buffer = bytes.NewBuffer(nil)
commandOutput = bytes.NewBuffer(nil)
process = npminstall.NewInstallBuildProcess(executable, environment, scribe.NewLogger(buffer))
})
it.After(func() {
Expect(os.RemoveAll(modulesDir)).To(Succeed())
Expect(os.RemoveAll(cacheDir)).To(Succeed())
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
context("ShouldRun", func() {
it("returns true", func() {
run, sha, err := process.ShouldRun(workingDir, nil, "some-npmrc-path")
Expect(err).NotTo(HaveOccurred())
Expect(run).To(BeTrue())
Expect(sha).To(BeEmpty())
})
})
context("Run", func() {
it.Before(func() {
Expect(os.MkdirAll(filepath.Join(workingDir, "node_modules"), os.ModePerm)).To(Succeed())
})
context("launch is false", func() {
it("succeeds", func() {
Expect(process.Run(modulesDir, cacheDir, workingDir, "some-npmrc-path", false)).To(Succeed())
Expect(executable.ExecuteCall.Receives.Execution).To(Equal(pexec.Execution{
Args: []string{"install", "--unsafe-perm", "--cache", cacheDir},
Dir: workingDir,
Stdout: commandOutput,
Stderr: commandOutput,
Env: append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path", "NODE_ENV=development"),
}))
path, err := os.Readlink(filepath.Join(workingDir, "node_modules"))
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join(modulesDir, "node_modules")))
})
})
context("launch is true", func() {
it("succeeds", func() {
Expect(process.Run(modulesDir, cacheDir, workingDir, "some-npmrc-path", true)).To(Succeed())
Expect(executable.ExecuteCall.Receives.Execution).To(Equal(pexec.Execution{
Args: []string{"install", "--unsafe-perm", "--cache", cacheDir},
Dir: workingDir,
Stdout: commandOutput,
Stderr: commandOutput,
Env: append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"),
}))
path, err := os.Readlink(filepath.Join(workingDir, "node_modules"))
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join(modulesDir, "node_modules")))
})
})
context("failure cases", func() {
context("when unable to write node_modules directory in layer", func() {
it.Before(func() {
Expect(os.Chmod(modulesDir, 0000)).To(Succeed())
})
it("fails", func() {
err := process.Run(modulesDir, cacheDir, workingDir, "", true)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("when the node_modules directory cannot be symlinked into the working directory", func() {
it.Before(func() {
Expect(os.MkdirAll(filepath.Join(workingDir, "node_modules"), os.ModePerm)).To(Succeed())
Expect(os.Chmod(filepath.Join(workingDir, "node_modules"), 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(filepath.Join(workingDir, "node_modules"), os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
err := process.Run(modulesDir, cacheDir, workingDir, "", true)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("when the executable fails", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
fmt.Fprintln(execution.Stdout, "install error on stdout")
fmt.Fprintln(execution.Stderr, "install error on stderr")
return errors.New("failed to execute")
}
})
it("returns an error", func() {
err := process.Run(modulesDir, cacheDir, workingDir, "", true)
Expect(buffer.String()).To(ContainSubstring(" install error on stdout\n install error on stderr\n"))
Expect(err).To(MatchError("npm install failed: failed to execute"))
})
})
})
})
}