-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpip_cleanup_test.go
134 lines (111 loc) · 3.92 KB
/
pip_cleanup_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
package cpython_test
import (
"bytes"
"errors"
"testing"
"github.com/paketo-buildpacks/cpython"
"github.com/paketo-buildpacks/cpython/fakes"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testPipCleanup(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
layerPath string
pythonProcess *fakes.Executable
pipCleanup cpython.PythonPipCleanup
)
it.Before(func() {
pythonProcess = &fakes.Executable{}
pipCleanup = cpython.NewPipCleanup(pythonProcess, scribe.NewEmitter(bytes.NewBuffer(nil)))
})
context("Execute", func() {
var (
pythonInvocationArgs [][]string
packages []string
)
it.Before(func() {
pythonInvocationArgs = [][]string{}
packages = []string{}
pythonProcess.ExecuteCall.Stub = func(e pexec.Execution) error {
pythonInvocationArgs = append(pythonInvocationArgs, e.Args)
return nil
}
})
context("when packages are installed", func() {
it.Before(func() {
packages = []string{"somepkg"}
})
it("uninstalls packages", func() {
err := pipCleanup.Cleanup(packages, layerPath)
Expect(err).NotTo(HaveOccurred())
Expect(pythonProcess.ExecuteCall.CallCount).To(Equal(3))
Expect(pythonInvocationArgs[0]).To(Equal([]string{"-m", "pip", "--version"}))
Expect(pythonInvocationArgs[1]).To(Equal([]string{"-m", "pip", "show", "-q", packages[0]}))
Expect(pythonInvocationArgs[2]).To(Equal([]string{"-m", "pip", "uninstall", "-y", packages[0]}))
})
})
context("when packages are not installed", func() {
it.Before(func() {
packages = []string{"somepkg-that-does-not-exist"}
pythonProcess.ExecuteCall.Stub = func(e pexec.Execution) error {
pythonInvocationArgs = append(pythonInvocationArgs, e.Args)
if e.Args[2] == "show" {
return errors.New("pip package not found")
}
return nil
}
})
it("does not uninstall packages", func() {
err := pipCleanup.Cleanup(packages, layerPath)
Expect(err).NotTo(HaveOccurred())
Expect(pythonProcess.ExecuteCall.CallCount).To(Equal(2))
Expect(pythonInvocationArgs[0]).To(Equal([]string{"-m", "pip", "--version"}))
Expect(pythonInvocationArgs[1]).To(Equal([]string{"-m", "pip", "show", "-q", packages[0]}))
})
})
context("failure cases", func() {
context("when pip version returns an error", func() {
it.Before(func() {
packages = []string{"somepkg"}
pythonProcess.ExecuteCall.Stub = func(e pexec.Execution) error {
pythonInvocationArgs = append(pythonInvocationArgs, e.Args)
return errors.New("pip is broken")
}
})
it("fails with error", func() {
err := pipCleanup.Cleanup(packages, layerPath)
Expect(err).Should(MatchError(And(
ContainSubstring("pip is broken"),
)))
Expect(pythonProcess.ExecuteCall.CallCount).To(Equal(1))
Expect(pythonInvocationArgs[0]).To(Equal([]string{"-m", "pip", "--version"}))
})
})
context("when pip uninstall returns an error", func() {
it.Before(func() {
packages = []string{"somepkg"}
pythonProcess.ExecuteCall.Stub = func(e pexec.Execution) error {
pythonInvocationArgs = append(pythonInvocationArgs, e.Args)
if e.Args[2] == "uninstall" {
return errors.New("failed to uninstall pip package")
}
return nil
}
})
it("fails with error", func() {
err := pipCleanup.Cleanup(packages, layerPath)
Expect(err).Should(MatchError(And(
ContainSubstring("failed to uninstall pip package"),
)))
Expect(pythonProcess.ExecuteCall.CallCount).To(Equal(3))
Expect(pythonInvocationArgs[0]).To(Equal([]string{"-m", "pip", "--version"}))
Expect(pythonInvocationArgs[1]).To(Equal([]string{"-m", "pip", "show", "-q", packages[0]}))
Expect(pythonInvocationArgs[2]).To(Equal([]string{"-m", "pip", "uninstall", "-y", packages[0]}))
})
})
})
})
}