forked from paketo-buildpacks/npm-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prune_build_process_test.go
101 lines (83 loc) · 2.78 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
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"
)
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
commandOutput *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{}
environment = &fakes.EnvironmentConfig{}
environment.GetValueCall.Returns.String = "some-val"
buffer = bytes.NewBuffer(nil)
commandOutput = 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).To(Equal(pexec.Execution{
Args: []string{"prune"},
Dir: workingDir,
Stdout: commandOutput,
Stderr: commandOutput,
Env: append(os.Environ(), "NPM_CONFIG_LOGLEVEL=some-val", "NPM_CONFIG_GLOBALCONFIG=some-npmrc-path"),
}))
})
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(ContainSubstring(" install error on stdout\n install error on stderr\n"))
Expect(err).To(MatchError("npm install failed: failed to execute"))
})
})
})
})
}