Skip to content

Commit cc61e28

Browse files
committed
refactor(tests): use t.Setenv in place of os
This ensures proper cleanup after use. It also requires sequential (vs parallel) test execution. This is good since there might be race conditions and the tests will sometime fail because the variable was unset before the test executes.
1 parent 998df21 commit cc61e28

File tree

4 files changed

+7
-23
lines changed

4 files changed

+7
-23
lines changed

pkg/installers/pip/detect_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package pip_test
66

77
import (
8-
"os"
98
"testing"
109

1110
"github.com/paketo-buildpacks/packit/v2"
@@ -50,11 +49,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
5049

5150
context("when BP_PIP_VERSION is set", func() {
5251
it.Before(func() {
53-
Expect(os.Setenv("BP_PIP_VERSION", "some-version")).To(Succeed())
54-
})
55-
56-
it.After(func() {
57-
Expect(os.Unsetenv("BP_PIP_VERSION")).To(Succeed())
52+
t.Setenv("BP_PIP_VERSION", "some-version")
5853
})
5954

6055
it("returns a build plan that provides the version of pip from BP_PIP_VERSION", func() {
@@ -85,7 +80,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
8580

8681
context("when the provided version is of the form X.Y", func() {
8782
it.Before(func() {
88-
Expect(os.Setenv("BP_PIP_VERSION", "2.11")).To(Succeed())
83+
t.Setenv("BP_PIP_VERSION", "2.11")
8984
})
9085

9186
it("selects the version X.Y.0", func() {
@@ -118,7 +113,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
118113

119114
context("when the provided version is of the form X.Y.Z", func() {
120115
it.Before(func() {
121-
Expect(os.Setenv("BP_PIP_VERSION", "22.1.3")).To(Succeed())
116+
t.Setenv("BP_PIP_VERSION", "22.1.3")
122117
})
123118

124119
it("selects the exact provided version X.Y.Z", func() {
@@ -151,7 +146,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
151146

152147
context("when the provided version is of some other form", func() {
153148
it.Before(func() {
154-
Expect(os.Setenv("BP_PIP_VERSION", "some.other")).To(Succeed())
149+
t.Setenv("BP_PIP_VERSION", "some.other")
155150
})
156151

157152
it("selects the exact provided version", func() {

pkg/installers/poetry/detect_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
7878

7979
context("when the BP_POETRY_VERSION is set", func() {
8080
it.Before(func() {
81-
Expect(os.Setenv("BP_POETRY_VERSION", "some-version")).To(Succeed())
82-
})
83-
84-
it.After(func() {
85-
Expect(os.Unsetenv("BP_POETRY_VERSION")).To(Succeed())
81+
t.Setenv("BP_POETRY_VERSION", "some-version")
8682
})
8783

8884
it("returns a plan that requires that version of poetry", func() {

pkg/installers/uv/detect_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
4141
})
4242

4343
context("when the BP_UV_VERSION is NOT set", func() {
44-
it.Before(func() {
45-
Expect(os.Unsetenv("BP_UV_VERSION")).To(Succeed())
46-
})
4744
it("returns a plan that provides uv", func() {
4845
result, err := detect(packit.DetectContext{
4946
WorkingDir: workingDir,
@@ -61,11 +58,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
6158

6259
context("when the BP_UV_VERSION is set", func() {
6360
it.Before(func() {
64-
Expect(os.Setenv("BP_UV_VERSION", "some-version")).To(Succeed())
65-
})
66-
67-
it.After(func() {
68-
Expect(os.Unsetenv("BP_UV_VERSION")).To(Succeed())
61+
t.Setenv("BP_UV_VERSION", "some-version")
6962
})
7063

7164
it("returns a plan that requires that version of poetry", func() {

pkg/installers/uv/init_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func TestUnit(t *testing.T) {
1616
suite := spec.New("uv", spec.Report(report.Terminal{}), spec.Parallel())
1717
suite("Build", testBuild)
18-
suite("Detect", testDetect)
18+
suite("Detect", testDetect, spec.Sequential())
1919
suite("InstallProcess", testUvInstallProcess)
2020
suite("Parser", testUvLockParser)
2121
suite.Run(t)

0 commit comments

Comments
 (0)