-
Notifications
You must be signed in to change notification settings - Fork 17
/
prune_build_process_test.go
111 lines (93 loc) · 3.18 KB
/
prune_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
package npminstall_test
import (
"bytes"
"errors"
"fmt"
"os"
"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 testPruneBuildProcess(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.PruneBuildProcess
)
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.NewPruneBuildProcess(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("succeeds", func() {
Expect(process.Run(modulesDir, cacheDir, workingDir, "some-npmrc-path", true)).To(Succeed())
Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{"prune"}))
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(
" Running 'npm prune'",
" stdout output",
" stderr output",
))
})
context("failure cases", func() {
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(
" Running 'npm prune'",
" install error on stdout",
" install error on stderr",
))
Expect(err).To(MatchError("npm install failed: failed to execute"))
})
})
})
})
}