Skip to content

Commit d2c174c

Browse files
committed
refactor: migrate Ginkgo v1 AfterEach cleanup to v2 DeferCleanup
Replace all AfterEach cleanup blocks with DeferCleanup calls co-located in BeforeEach/JustBeforeEach, following Ginkgo v2 idioms. Bare AfterEach at Describe-level are replaced with direct DeferCleanup calls in the Describe body.
1 parent 7f4e705 commit d2c174c

8 files changed

Lines changed: 44 additions & 130 deletions

File tree

src/python/conda/conda_test.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ var _ = Describe("Conda", func() {
3737
BeforeEach(func() {
3838
buildDir, err = os.MkdirTemp("", "python-buildpack.build.")
3939
Expect(err).To(BeNil())
40+
DeferCleanup(os.RemoveAll, buildDir)
4041
cacheDir, err = os.MkdirTemp("", "python-buildpack.cache.")
4142
Expect(err).To(BeNil())
43+
DeferCleanup(os.RemoveAll, cacheDir)
4244
depsDir, err = os.MkdirTemp("", "python-buildpack.deps.")
4345
Expect(err).To(BeNil())
46+
DeferCleanup(os.RemoveAll, depsDir)
4447
depsIdx = "13"
4548
depDir = filepath.Join(depsDir, depsIdx)
4649

@@ -59,13 +62,6 @@ var _ = Describe("Conda", func() {
5962
subject = conda.New(mockInstaller, mockStager, mockCommand, logger)
6063
})
6164

62-
AfterEach(func() {
63-
mockCtrl.Finish()
64-
Expect(os.RemoveAll(buildDir)).To(Succeed())
65-
Expect(os.RemoveAll(cacheDir)).To(Succeed())
66-
Expect(os.RemoveAll(depsDir)).To(Succeed())
67-
})
68-
6965
Describe("Version", func() {
7066
Context("runtime.txt specifies python 3", func() {
7167
BeforeEach(func() {
@@ -129,13 +125,12 @@ var _ = Describe("Conda", func() {
129125

130126
BeforeEach(func() {
131127
condaPkgs = os.Getenv("CONDA_PKGS_DIRS")
132-
})
133-
134-
AfterEach(func() {
135-
if condaPkgs != "" {
136-
os.Setenv("CONDA_PKGS_DIRS", condaPkgs)
137-
}
138-
os.Unsetenv("BP_DEBUG")
128+
DeferCleanup(func() {
129+
if condaPkgs != "" {
130+
os.Setenv("CONDA_PKGS_DIRS", condaPkgs)
131+
}
132+
os.Unsetenv("BP_DEBUG")
133+
})
139134
})
140135

141136
It("uses staging cache for conda cache", func() {

src/python/finalize/finalize_test.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ var _ = Describe("Finalize", func() {
3838
BeforeEach(func() {
3939
buildDir, err = os.MkdirTemp("", "python-buildpack.build.")
4040
Expect(err).To(BeNil())
41+
DeferCleanup(os.RemoveAll, buildDir)
4142

4243
depsDir, err = os.MkdirTemp("", "python-buildpack.deps.")
4344
Expect(err).To(BeNil())
45+
DeferCleanup(os.RemoveAll, depsDir)
46+
47+
DeferCleanup(os.Setenv, "DISABLE_COLLECTSTATIC", "")
4448

4549
depsIdx = "9"
4650
Expect(os.MkdirAll(filepath.Join(depsDir, depsIdx), 0755)).To(Succeed())
@@ -68,18 +72,6 @@ var _ = Describe("Finalize", func() {
6872
}
6973
})
7074

71-
AfterEach(func() {
72-
mockCtrl.Finish()
73-
74-
err = os.RemoveAll(buildDir)
75-
Expect(err).To(BeNil())
76-
77-
err = os.RemoveAll(depsDir)
78-
Expect(err).To(BeNil())
79-
80-
os.Setenv("DISABLE_COLLECTSTATIC", "")
81-
})
82-
8375
Describe("HandleCollectStatic", func() {
8476
Context("When DISABLE_COLLECTSTATIC is set", func() {
8577
BeforeEach(func() {

src/python/hooks/appdynamics_test.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ var _ = Describe("Appdynamics", func() {
3131
BeforeEach(func() {
3232
buildDir, err = os.MkdirTemp("", "python-buildpack.build.")
3333
Expect(err).NotTo(HaveOccurred())
34+
DeferCleanup(os.RemoveAll, buildDir)
3435

3536
depsDir, err = os.MkdirTemp("", "python-buildpack.deps.")
3637
Expect(err).NotTo(HaveOccurred())
38+
DeferCleanup(os.RemoveAll, depsDir)
3739

3840
buffer = new(bytes.Buffer)
3941
logger := libbuildpack.NewLogger(ansicleaner.New(buffer))
@@ -49,11 +51,6 @@ var _ = Describe("Appdynamics", func() {
4951
}
5052
})
5153

52-
AfterEach(func() {
53-
Expect(os.RemoveAll(buildDir)).To(Succeed())
54-
Expect(os.RemoveAll(depsDir)).To(Succeed())
55-
})
56-
5754
Context("GenerateStartUpCommand", func() {
5855
It("Returns the command when it is provided in the correct format", func() {
5956
startCommand := "web: python flask.py"
@@ -77,10 +74,7 @@ var _ = Describe("Appdynamics", func() {
7774
)
7875
BeforeEach(func() {
7976
tempProcDir, err = os.MkdirTemp("", "Procfiles")
80-
})
81-
82-
AfterEach(func() {
83-
Expect(os.RemoveAll(tempProcDir)).To(Succeed())
77+
DeferCleanup(os.RemoveAll, tempProcDir)
8478
})
8579

8680
It("rewrites the procfile with pyagent", func() {
@@ -129,9 +123,7 @@ var _ = Describe("Appdynamics", func() {
129123
defer f.Close()
130124
_, err = f.WriteString("Flask")
131125
Expect(err).NotTo(HaveOccurred())
132-
})
133-
AfterEach(func() {
134-
Expect(os.Remove(filepath.Join(buildDir, "requirements.txt"))).To(Succeed())
126+
DeferCleanup(os.Remove, filepath.Join(buildDir, "requirements.txt"))
135127
})
136128

137129
It("rewrites requirements.txt", func() {
@@ -182,10 +174,7 @@ export APPD_KEY_2=APPD_VAL_2`
182174
Expect(os.Getenv("VCAP_SERVICES")).To(Equal(""))
183175
Expect(os.WriteFile(filepath.Join(buildDir, "Procfile"), []byte("web: python app.py"), 0644)).To(Succeed())
184176
Expect(err).NotTo(HaveOccurred())
185-
})
186-
187-
AfterEach(func() {
188-
Expect(os.Remove(filepath.Join(buildDir, "Procfile"))).To(Succeed())
177+
DeferCleanup(os.Remove, filepath.Join(buildDir, "Procfile"))
189178
})
190179

191180
It("VCAP_SERVICES is not present", func() {
@@ -205,11 +194,8 @@ export APPD_KEY_2=APPD_VAL_2`
205194
os.Setenv("VCAP_SERVICES", `{"service": [{"credentials": {"login": "name"}, "name": "443"}]}`)
206195
Expect(os.WriteFile(filepath.Join(buildDir, "Procfile"), []byte("web: python app.py"), 0644)).To(Succeed())
207196
Expect(err).NotTo(HaveOccurred())
208-
})
209-
210-
AfterEach(func() {
211-
Expect(os.Remove(filepath.Join(buildDir, "Procfile"))).To(Succeed())
212-
os.Unsetenv("VCAP_SERVICES")
197+
DeferCleanup(os.Remove, filepath.Join(buildDir, "Procfile"))
198+
DeferCleanup(os.Unsetenv, "VCAP_SERVICES")
213199
})
214200

215201
It("VCAP_SERVICES has no appdynamics", func() {
@@ -235,10 +221,7 @@ export APPD_KEY_2=APPD_VAL_2`
235221

236222
Expect(os.WriteFile(filepath.Join(buildDir, "Procfile"), []byte("web: python app.py"), 0644)).To(Succeed())
237223
Expect(err).NotTo(HaveOccurred())
238-
})
239-
240-
AfterEach(func() {
241-
os.Unsetenv("VCAP_SERVICES")
224+
DeferCleanup(os.Unsetenv, "VCAP_SERVICES")
242225
})
243226

244227
It(fmt.Sprintf("VCAP_SERVICES has %s", serviceName), func() {

src/python/hooks/hooks_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var _ = Describe("Hooks", func() {
2525
BeforeEach(func() {
2626
buildDir, err = os.MkdirTemp("", "python-buildpack.build.")
2727
Expect(err).To(BeNil())
28+
DeferCleanup(os.RemoveAll, buildDir)
2829

2930
buffer = new(bytes.Buffer)
3031
logger := libbuildpack.NewLogger(ansicleaner.New(buffer))
@@ -35,10 +36,6 @@ var _ = Describe("Hooks", func() {
3536
hook = &hooks.AppHook{}
3637
})
3738

38-
AfterEach(func() {
39-
Expect(os.RemoveAll(buildDir)).To(Succeed())
40-
})
41-
4239
Describe("BeforeCompile", func() {
4340
Context("bin/pre_compile exists", func() {
4441
BeforeEach(func() {

src/python/hooks/sealights_test.go

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ var _ = Describe("Sealights", func() {
2323
BeforeEach(func() {
2424
buildDir, err = os.MkdirTemp("", "python-buildpack.build.")
2525
Expect(err).NotTo(HaveOccurred())
26+
DeferCleanup(os.RemoveAll, buildDir)
2627

2728
depsDir, err = os.MkdirTemp("", "python-buildpack.deps.")
2829
Expect(err).NotTo(HaveOccurred())
30+
DeferCleanup(os.RemoveAll, depsDir)
2931

3032
buffer = new(bytes.Buffer)
3133
logger := libbuildpack.NewLogger(ansicleaner.New(buffer))
@@ -37,11 +39,6 @@ var _ = Describe("Sealights", func() {
3739
}
3840
})
3941

40-
AfterEach(func() {
41-
Expect(os.RemoveAll(buildDir)).To(Succeed())
42-
Expect(os.RemoveAll(depsDir)).To(Succeed())
43-
})
44-
4542
Context("GenerateStartUpCommand", func() {
4643
It("Returns the command when it is provided in the correct format and empty config", func() {
4744
slConfig := hooks.NewSealightsConfig()
@@ -97,13 +94,10 @@ var _ = Describe("Sealights", func() {
9794
BeforeEach(func() {
9895
tempProcDir, err = os.MkdirTemp("", "Procfiles")
9996
Expect(err).NotTo(HaveOccurred())
97+
DeferCleanup(os.RemoveAll, tempProcDir)
10098
slConfig = hooks.NewSealightsConfig()
10199
})
102100

103-
AfterEach(func() {
104-
Expect(os.RemoveAll(tempProcDir)).To(Succeed())
105-
})
106-
107101
It("rewrites the procfile with sl-python", func() {
108102
Expect(os.WriteFile(filepath.Join(tempProcDir, "Procfile"), []byte("web: python app.py"), 0666)).To(Succeed())
109103
Expect(err).NotTo(HaveOccurred())
@@ -144,9 +138,7 @@ var _ = Describe("Sealights", func() {
144138
requirementsFilepath := filepath.Join(buildDir, "requirements.txt")
145139
Expect(libbuildpack.FileExists(requirementsFilepath)).ToNot(BeTrue())
146140
Expect(os.WriteFile(requirementsFilepath, []byte("Flask"), 0644)).To(Succeed())
147-
})
148-
AfterEach(func() {
149-
Expect(os.Remove(filepath.Join(buildDir, "requirements.txt"))).To(Succeed())
141+
DeferCleanup(os.Remove, filepath.Join(buildDir, "requirements.txt"))
150142
})
151143

152144
It("Rewrites requirements.txt", func() {
@@ -169,10 +161,7 @@ var _ = Describe("Sealights", func() {
169161
Expect(os.Getenv("VCAP_SERVICES")).To(Equal(""))
170162
Expect(os.WriteFile(filepath.Join(buildDir, "Procfile"), []byte("web: python app.py"), 0644)).To(Succeed())
171163
Expect(err).NotTo(HaveOccurred())
172-
})
173-
174-
AfterEach(func() {
175-
Expect(os.Remove(filepath.Join(buildDir, "Procfile"))).To(Succeed())
164+
DeferCleanup(os.Remove, filepath.Join(buildDir, "Procfile"))
176165
})
177166

178167
It("BeforeCompile does not modify the existing Procfile", func() {
@@ -190,11 +179,8 @@ var _ = Describe("Sealights", func() {
190179
os.Setenv("VCAP_SERVICES", `{"service": [{"credentials": {"login": "name"}, "name": "443"}]}`)
191180
Expect(os.WriteFile(filepath.Join(buildDir, "Procfile"), []byte("web: python app.py"), 0644)).To(Succeed())
192181
Expect(err).NotTo(HaveOccurred())
193-
})
194-
195-
AfterEach(func() {
196-
Expect(os.Remove(filepath.Join(buildDir, "Procfile"))).To(Succeed())
197-
os.Unsetenv("VCAP_SERVICES")
182+
DeferCleanup(os.Remove, filepath.Join(buildDir, "Procfile"))
183+
DeferCleanup(os.Unsetenv, "VCAP_SERVICES")
198184
})
199185

200186
It("BeforeCompile does not modify the existing Procfile", func() {
@@ -212,11 +198,8 @@ var _ = Describe("Sealights", func() {
212198
os.Setenv("VCAP_SERVICES", `{"sealights":[{"credentials":{"token":"","tokenFile":"token.txt"}}]}`)
213199
Expect(os.WriteFile(filepath.Join(buildDir, "Procfile"), []byte("web: python app.py"), 0644)).To(Succeed())
214200
Expect(err).NotTo(HaveOccurred())
215-
})
216-
217-
AfterEach(func() {
218-
os.Unsetenv("VCAP_SERVICES")
219-
os.Unsetenv("SL_TOKEN")
201+
DeferCleanup(os.Unsetenv, "VCAP_SERVICES")
202+
DeferCleanup(os.Unsetenv, "SL_TOKEN")
220203
})
221204
It("BeforeCompile modify the existing Procfile", func() {
222205
err := sealights.BeforeCompile(stager)
@@ -252,10 +235,7 @@ var _ = Describe("Sealights", func() {
252235
os.Setenv("VCAP_SERVICES", `{"some-sealights":[{"credentials":{"token":"","tokenFile":"token.txt"}}]}`)
253236
Expect(os.WriteFile(filepath.Join(buildDir, "Procfile"), []byte("web: python app.py"), 0644)).To(Succeed())
254237
Expect(err).NotTo(HaveOccurred())
255-
})
256-
257-
AfterEach(func() {
258-
os.Unsetenv("VCAP_SERVICES")
238+
DeferCleanup(os.Unsetenv, "VCAP_SERVICES")
259239
})
260240
It("BeforeCompile modify the existing Procfile", func() {
261241
err := sealights.BeforeCompile(stager)
@@ -276,10 +256,7 @@ var _ = Describe("Sealights", func() {
276256
os.Setenv("VCAP_SERVICES", `{"user-provided":[{"binding_guid":"af3fea1b-8beb-4397-968e-6440d2906551","binding_name":null,"credentials":{"token":"","tokenFile":"sltoken.txt"},"instance_guid":"d5c36671-dc09-4cd9-8697-70adc0c0f6e9","instance_name":"sealights","label":"user-provided","name":"sealights","syslog_drain_url":null,"tags":[],"volume_mounts":[]}]}`)
277257
Expect(os.WriteFile(filepath.Join(buildDir, "Procfile"), []byte("web: python app.py"), 0644)).To(Succeed())
278258
Expect(err).NotTo(HaveOccurred())
279-
})
280-
281-
AfterEach(func() {
282-
os.Unsetenv("VCAP_SERVICES")
259+
DeferCleanup(os.Unsetenv, "VCAP_SERVICES")
283260
})
284261
It("BeforeCompile modify the existing Procfile", func() {
285262
err := sealights.BeforeCompile(stager)
@@ -300,10 +277,7 @@ var _ = Describe("Sealights", func() {
300277
os.Setenv("VCAP_SERVICES", `{"sealights":[{"credentials":{"token":"","tokenFile":"token.txt"}}]}`)
301278
Expect(os.WriteFile(filepath.Join(buildDir, "Procfile"), []byte("python app.py"), 0644)).To(Succeed())
302279
Expect(err).NotTo(HaveOccurred())
303-
})
304-
305-
AfterEach(func() {
306-
os.Unsetenv("VCAP_SERVICES")
280+
DeferCleanup(os.Unsetenv, "VCAP_SERVICES")
307281
})
308282
It("BeforeCompile is not modify the existing Procfile - bad format", func() {
309283
err := sealights.BeforeCompile(stager)

src/python/pyfinder/manage_py_finder_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ var _ = Describe("ManagePyFinder", func() {
2020
BeforeEach(func() {
2121
tempDir, err = os.MkdirTemp("", "pyfinder")
2222
Expect(err).NotTo(HaveOccurred())
23+
DeferCleanup(os.RemoveAll, tempDir)
2324
finder = ManagePyFinder{}
2425
})
2526

26-
AfterEach(func() {
27-
Expect(os.RemoveAll(tempDir)).To(Succeed())
28-
})
29-
3027
Describe("FindManagePy", func() {
3128
BeforeEach(func() {
3229
Expect(os.MkdirAll(filepath.Join(tempDir, "a", "b"), 0755)).To(Succeed())

src/python/requirements/requirements_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ var _ = Describe("Reqs", func() {
1818
BeforeEach(func() {
1919
tempDir, err = os.MkdirTemp("", "requirements")
2020
Expect(err).NotTo(HaveOccurred())
21+
DeferCleanup(os.RemoveAll, tempDir)
2122
req = Reqs{}
2223
})
2324

24-
AfterEach(func() {
25-
Expect(os.RemoveAll(tempDir)).To(Succeed())
26-
})
27-
2825
Describe("FindAnyPackage", func() {
2926
Context("succeed", func() {
3027

0 commit comments

Comments
 (0)