-
Notifications
You must be signed in to change notification settings - Fork 17
/
install_build_process_test.go
167 lines (140 loc) · 5.55 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
157
158
159
160
161
162
163
164
165
166
167
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"
. "github.com/paketo-buildpacks/occam/matchers"
)
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
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{}
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
fmt.Fprintln(execution.Stdout, "stdout output")
fmt.Fprintln(execution.Stderr, "stderr output")
return nil
}
environment = &fakes.EnvironmentConfig{}
environment.LookupCall.Returns.Value = "some-val"
environment.LookupCall.Returns.Found = true
buffer = 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.Args).To(Equal([]string{"install", "--unsafe-perm", "--cache", cacheDir}))
Expect(executable.ExecuteCall.Receives.Execution.Dir).To(Equal(workingDir))
Expect(executable.ExecuteCall.Receives.Execution.Env).To(Equal(append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path", "NODE_ENV=development")))
Expect(buffer.String()).To(ContainLines(
fmt.Sprintf(" Running 'npm install --unsafe-perm --cache %s'", cacheDir),
" stdout output",
" stderr output",
))
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.Args).To(Equal([]string{"install", "--unsafe-perm", "--cache", cacheDir}))
Expect(executable.ExecuteCall.Receives.Execution.Dir).To(Equal(workingDir))
Expect(executable.ExecuteCall.Receives.Execution.Env).To(Equal(append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path")))
Expect(buffer.String()).To(ContainLines(
fmt.Sprintf(" Running 'npm install --unsafe-perm --cache %s'", cacheDir),
" stdout output",
" stderr output",
))
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(ContainLines(
fmt.Sprintf(" Running 'npm install --unsafe-perm --cache %s'", cacheDir),
" install error on stdout",
" install error on stderr",
))
Expect(err).To(MatchError("npm install failed: failed to execute"))
})
})
})
})
}