-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinstall_process_test.go
123 lines (101 loc) · 3.66 KB
/
install_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
package poetryinstall_test
import (
"bytes"
"errors"
"fmt"
"os"
"testing"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
poetryinstall "github.com/paketo-buildpacks/poetry-install"
"github.com/paketo-buildpacks/poetry-install/fakes"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
. "github.com/paketo-buildpacks/occam/matchers"
)
func testInstallProcess(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
packagesLayerPath string
cacheLayerPath string
workingDir string
executable *fakes.Executable
buffer *bytes.Buffer
executableInvocations []pexec.Execution
poetryInstallProcess poetryinstall.PoetryInstallProcess
)
it.Before(func() {
var err error
packagesLayerPath, err = os.MkdirTemp("", "packages")
Expect(err).NotTo(HaveOccurred())
cacheLayerPath, err = os.MkdirTemp("", "cache")
Expect(err).NotTo(HaveOccurred())
workingDir, err = os.MkdirTemp("", "workingdir")
Expect(err).NotTo(HaveOccurred())
executable = &fakes.Executable{}
executableInvocations = []pexec.Execution{}
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
executableInvocations = append(executableInvocations, execution)
// Various path constructs (like .. and // and whitespace) to validate that we are cleaning the absolute filepath
// when required
fmt.Fprintln(execution.Stdout, "//some/path/xyz/../to/some/venv//")
fmt.Fprintln(execution.Stderr, "stderr output")
Expect(err).NotTo(HaveOccurred())
return nil
}
buffer = bytes.NewBuffer(nil)
poetryInstallProcess = poetryinstall.NewPoetryInstallProcess(executable, scribe.NewEmitter(buffer))
})
it.After(func() {
Expect(os.RemoveAll(packagesLayerPath)).To(Succeed())
Expect(os.RemoveAll(cacheLayerPath)).To(Succeed())
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
context("Execute", func() {
it("runs installation", func() {
venvDir, err := poetryInstallProcess.Execute(workingDir, packagesLayerPath, cacheLayerPath)
Expect(err).NotTo(HaveOccurred())
Expect(executable.ExecuteCall.CallCount).To(Equal(2))
Expect(executableInvocations).To(HaveLen(2))
Expect(executableInvocations[0]).To(MatchFields(IgnoreExtras, Fields{
"Args": Equal([]string{
"install",
}),
"Dir": Equal(workingDir),
"Env": ContainElements([]string{
fmt.Sprintf("POETRY_VIRTUALENVS_PATH=%s", packagesLayerPath),
fmt.Sprintf("POETRY_CACHE_DIR=%s", cacheLayerPath),
}),
}))
Expect(executableInvocations[1]).To(MatchFields(IgnoreExtras, Fields{
"Args": Equal([]string{
"env", "info", "--path",
}),
"Dir": Equal(workingDir),
"Env": ContainElements([]string{
fmt.Sprintf("POETRY_VIRTUALENVS_PATH=%s", packagesLayerPath),
fmt.Sprintf("POETRY_CACHE_DIR=%s", cacheLayerPath),
}),
}))
Expect(venvDir).To(Equal("/some/path/to/some/venv"))
Expect(buffer.String()).To(ContainLines(
fmt.Sprintf(" Running 'POETRY_CACHE_DIR=%s POETRY_VIRTUALENVS_PATH=%s poetry install'", cacheLayerPath, packagesLayerPath),
" //some/path/xyz/../to/some/venv//",
" stderr output",
))
})
context("failure cases", func() {
context("when executable returns an error", func() {
it.Before(func() {
executable.ExecuteCall.Stub = nil
executable.ExecuteCall.Returns.Error = errors.New("could not run executable")
})
it("returns an error", func() {
_, err := poetryInstallProcess.Execute(workingDir, packagesLayerPath, cacheLayerPath)
Expect(err).To(MatchError("poetry install failed:\nerror: could not run executable"))
})
})
})
})
}