-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpythonpath_process_test.go
143 lines (112 loc) · 4.86 KB
/
pythonpath_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
package poetryinstall_test
import (
"fmt"
"os"
"path/filepath"
"testing"
poetryinstall "github.com/paketo-buildpacks/poetry-install"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testPythonPathProcess(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
venvDir string
pythonPathProcess poetryinstall.PythonPathProcess
)
it.Before(func() {
var err error
venvDir, err = os.MkdirTemp("", "poetry-venv")
Expect(err).NotTo(HaveOccurred())
Expect(os.MkdirAll(filepath.Join(venvDir, "lib", "python3.8", "site-packages"), os.ModePerm)).To(Succeed())
pythonPathProcess = poetryinstall.NewPythonPathProcess()
})
it.After(func() {
Expect(os.RemoveAll(venvDir)).To(Succeed())
})
context("Execute", func() {
it("runs installation", func() {
pythonPath, err := pythonPathProcess.Execute(venvDir)
Expect(err).NotTo(HaveOccurred())
Expect(pythonPath).To(Equal(filepath.Join(venvDir, "lib", "python3.8", "site-packages")))
})
context("failure cases", func() {
context("when the venvDir/lib directory cannot be read", func() {
it.Before(func() {
Expect(os.Chmod(filepath.Join(venvDir, "lib"), 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(filepath.Join(venvDir, "lib"), os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := pythonPathProcess.Execute(venvDir)
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("failed to read directory: '%s'", filepath.Join(venvDir, "lib")))))
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("when there are too many entries in the venv/lib directory", func() {
it.Before(func() {
Expect(os.MkdirAll(filepath.Join(venvDir, "lib", "additional"), os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := pythonPathProcess.Execute(venvDir)
Expect(err).To(MatchError(fmt.Sprintf("expected one directory and zero files in directory: '%s' - found multiple", filepath.Join(venvDir, "lib"))))
})
})
context("If the entry at venv/lib/python* is not a directory", func() {
it.Before(func() {
Expect(os.RemoveAll(filepath.Join(venvDir, "lib", "python3.8"))).To(Succeed())
_, err := os.Create(filepath.Join(venvDir, "lib", "python3.8"))
Expect(err).NotTo(HaveOccurred())
})
it("returns an error", func() {
_, err := pythonPathProcess.Execute(venvDir)
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("expected a directory at: '%s'", filepath.Join(venvDir, "lib", "python3.8")))))
})
})
context("when the venvDir/lib/python* directory cannot be read", func() {
it.Before(func() {
Expect(os.Chmod(filepath.Join(venvDir, "lib", "python3.8"), 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(filepath.Join(venvDir, "lib", "python3.8"), os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := pythonPathProcess.Execute(venvDir)
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("failed to read directory: '%s'", filepath.Join(venvDir, "lib", "python3.8")))))
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("when there are too many entries in the venv/lib/python* directory", func() {
it.Before(func() {
Expect(os.MkdirAll(filepath.Join(venvDir, "lib", "python3.8", "additional"), os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := pythonPathProcess.Execute(venvDir)
Expect(err).To(MatchError(fmt.Sprintf("expected one directory and zero files in directory: '%s' - found multiple", filepath.Join(venvDir, "lib", "python3.8"))))
})
})
context("If the only entry under venv/lib/python*/ is not called site-packages", func() {
it.Before(func() {
Expect(os.RemoveAll(filepath.Join(venvDir, "lib", "python3.8", "site-packages"))).To(Succeed())
Expect(os.MkdirAll(filepath.Join(venvDir, "lib", "python3.8", "other-directory"), os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := pythonPathProcess.Execute(venvDir)
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf(`expected "site-packages" directory at: '%s'`, filepath.Join(venvDir, "lib", "python3.8", "site-packages")))))
})
})
context("If the entry at venv/lib/python*/site-packages is not a directory", func() {
it.Before(func() {
Expect(os.RemoveAll(filepath.Join(venvDir, "lib", "python3.8", "site-packages"))).To(Succeed())
_, err := os.Create(filepath.Join(venvDir, "lib", "python3.8", "site-packages"))
Expect(err).NotTo(HaveOccurred())
})
it("returns an error", func() {
_, err := pythonPathProcess.Execute(venvDir)
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("expected a directory at: '%s'", filepath.Join(venvDir, "lib", "python3.8", "site-packages")))))
})
})
})
})
}