Skip to content

Commit 60f4290

Browse files
committed
refactor: convert table tests to test case structs
1 parent 409dc41 commit 60f4290

1 file changed

Lines changed: 49 additions & 68 deletions

File tree

internal/runtime/python/python_test.go

Lines changed: 49 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -64,78 +64,70 @@ func Test_Python_IgnoreDirectories(t *testing.T) {
6464
}
6565

6666
func Test_getVenvPath(t *testing.T) {
67-
tests := []struct {
68-
name string
67+
tests := map[string]struct {
6968
projectDirPath string
7069
expectedPath string
7170
}{
72-
{
73-
name: "Get venv path",
71+
"Get venv path": {
7472
projectDirPath: "/path/to/project",
7573
expectedPath: "/path/to/project/.venv",
7674
},
7775
}
78-
for _, tt := range tests {
79-
t.Run(tt.name, func(t *testing.T) {
80-
result := getVenvPath(tt.projectDirPath)
81-
require.Equal(t, tt.expectedPath, result)
76+
for name, tc := range tests {
77+
t.Run(name, func(t *testing.T) {
78+
result := getVenvPath(tc.projectDirPath)
79+
require.Equal(t, tc.expectedPath, result)
8280
})
8381
}
8482
}
8583

8684
func Test_getPythonExecutable(t *testing.T) {
87-
tests := []struct {
88-
name string
85+
tests := map[string]struct {
8986
expectedExecutable string
9087
skipOnOS string
9188
}{
92-
{
93-
name: "Get python executable on Unix",
89+
"Get python executable on Unix": {
9490
expectedExecutable: "python3",
9591
skipOnOS: "windows",
9692
},
97-
{
98-
name: "Get python executable on Windows",
93+
"Get python executable on Windows": {
9994
expectedExecutable: "python",
10095
skipOnOS: "linux",
10196
},
10297
}
103-
for _, tt := range tests {
104-
t.Run(tt.name, func(t *testing.T) {
105-
if tt.skipOnOS != "" {
98+
for name, tc := range tests {
99+
t.Run(name, func(t *testing.T) {
100+
if tc.skipOnOS != "" {
106101
return
107102
}
108103
result := getPythonExecutable()
109-
require.Equal(t, tt.expectedExecutable, result)
104+
require.Equal(t, tc.expectedExecutable, result)
110105
})
111106
}
112107
}
113108

114109
func Test_getPipExecutable(t *testing.T) {
115-
tests := []struct {
116-
name string
110+
tests := map[string]struct {
117111
venvPath string
118112
expectedPath string
119113
skipOnOS string
120114
}{
121-
{
122-
name: "Get pip path on Unix",
115+
"Get pip path on Unix": {
123116
venvPath: "/path/to/.venv",
124117
expectedPath: "/path/to/.venv/bin/pip",
125118
skipOnOS: "windows",
126119
},
127-
{
128-
name: "Get pip path on Windows",
120+
"Get pip path on Windows": {
129121
venvPath: "C:\\path\\to\\.venv",
130122
expectedPath: "C:\\path\\to\\.venv\\Scripts\\pip.exe",
131123
skipOnOS: "linux",
132124
},
133125
}
134-
for _, tt := range tests {
135-
t.Run(tt.name, func(t *testing.T) {
136-
result := getPipExecutable(tt.venvPath)
126+
for name, tc := range tests {
127+
t.Run(name, func(t *testing.T) {
128+
result := getPipExecutable(tc.venvPath)
137129
// Only assert on the appropriate OS
138-
if tt.skipOnOS != "" {
130+
if tc.skipOnOS != "" {
139131
// Skip OS-specific test
140132
return
141133
}
@@ -145,95 +137,87 @@ func Test_getPipExecutable(t *testing.T) {
145137
}
146138

147139
func Test_installRequirementsTxt(t *testing.T) {
148-
tests := []struct {
149-
name string
140+
tests := map[string]struct {
150141
existingContent string
151142
expectedContent string
152143
expectedOutput string
153144
expectedError bool
154145
}{
155-
{
156-
name: "Skip when slack-cli-hooks already exists",
146+
"Skip when slack-cli-hooks already exists": {
157147
existingContent: "slack-cli-hooks\npytest==8.3.2\nruff==0.7.2",
158148
expectedContent: "slack-cli-hooks\npytest==8.3.2\nruff==0.7.2",
159149
expectedOutput: "Found requirements.txt with",
160150
expectedError: false,
161151
},
162-
{
163-
name: "Add after slack-bolt when it exists",
152+
"Add after slack-bolt when it exists": {
164153
existingContent: "slack-bolt==2.31.2\npytest==8.3.2\nruff==0.7.2",
165154
expectedContent: "slack-bolt==2.31.2\nslack-cli-hooks<1.0.0\npytest==8.3.2\nruff==0.7.2",
166155
expectedOutput: "Updated requirements.txt with",
167156
expectedError: false,
168157
},
169-
{
170-
name: "Add at end when slack-bolt doesn't exist",
158+
"Add at end when slack-bolt doesn't exist": {
171159
existingContent: "pytest==8.3.2\nruff==0.7.2",
172160
expectedContent: "pytest==8.3.2\nruff==0.7.2\nslack-cli-hooks<1.0.0",
173161
expectedOutput: "Updated requirements.txt with",
174162
expectedError: false,
175163
},
176164
}
177-
for _, tt := range tests {
178-
t.Run(tt.name, func(t *testing.T) {
165+
for name, tc := range tests {
166+
t.Run(name, func(t *testing.T) {
179167
fs := slackdeps.NewFsMock()
180168
projectDirPath := "/path/to/project"
181169

182170
// Create requirements.txt
183171
requirementsPath := filepath.Join(projectDirPath, "requirements.txt")
184172
err := fs.MkdirAll(projectDirPath, 0755)
185173
require.NoError(t, err)
186-
err = afero.WriteFile(fs, requirementsPath, []byte(tt.existingContent), 0644)
174+
err = afero.WriteFile(fs, requirementsPath, []byte(tc.existingContent), 0644)
187175
require.NoError(t, err)
188176

189177
// Test
190178
output, err := installRequirementsTxt(fs, projectDirPath)
191179

192180
// Assertions
193-
if tt.expectedError {
181+
if tc.expectedError {
194182
require.Error(t, err)
195183
} else {
196184
require.NoError(t, err)
197185
}
198186

199-
require.Contains(t, output, tt.expectedOutput)
187+
require.Contains(t, output, tc.expectedOutput)
200188

201189
// Verify file content
202190
content, err := afero.ReadFile(fs, requirementsPath)
203191
require.NoError(t, err)
204-
require.Equal(t, tt.expectedContent, string(content))
192+
require.Equal(t, tc.expectedContent, string(content))
205193
})
206194
}
207195
}
208196

209197
func Test_installPyProjectToml(t *testing.T) {
210-
tests := []struct {
211-
name string
198+
tests := map[string]struct {
212199
existingContent string
213200
shouldContain []string
214201
expectedOutput string
215202
expectedError bool
216203
}{
217-
{
218-
name: "Skip when slack-cli-hooks already exists",
204+
"Skip when slack-cli-hooks already exists": {
219205
existingContent: `[project]
220206
name = "my-app"
221207
dependencies = ["slack-cli-hooks<1.0.0", "pytest==8.3.2"]`,
222208
shouldContain: []string{"slack-cli-hooks"},
223209
expectedOutput: "Found pyproject.toml with",
224210
expectedError: false,
225211
},
226-
{
227-
name: "Add after slack-bolt when it exists",
212+
"Add after slack-bolt when it exists": {
228213
existingContent: `[project]
229214
name = "my-app"
230215
dependencies = ["slack-bolt>=1.0.0", "pytest==8.3.2"]`,
231216
shouldContain: []string{"slack-bolt", "slack-cli-hooks", "pytest"},
232217
expectedOutput: "Updated pyproject.toml with",
233218
expectedError: false,
234219
},
235-
{
236-
name: "Add at end when slack-bolt doesn't exist",
220+
"Add at end when slack-bolt doesn't exist": {
237221
existingContent: `[project]
238222
name = "my-app"
239223
dependencies = ["pytest==8.3.2"]`,
@@ -242,74 +226,71 @@ dependencies = ["pytest==8.3.2"]`,
242226
expectedError: false,
243227
},
244228
}
245-
for _, tt := range tests {
246-
t.Run(tt.name, func(t *testing.T) {
229+
for name, tc := range tests {
230+
t.Run(name, func(t *testing.T) {
247231
fs := slackdeps.NewFsMock()
248232
projectDirPath := "/path/to/project"
249233

250234
// Create pyproject.toml
251235
pyprojectPath := filepath.Join(projectDirPath, "pyproject.toml")
252236
err := fs.MkdirAll(projectDirPath, 0755)
253237
require.NoError(t, err)
254-
err = afero.WriteFile(fs, pyprojectPath, []byte(tt.existingContent), 0644)
238+
err = afero.WriteFile(fs, pyprojectPath, []byte(tc.existingContent), 0644)
255239
require.NoError(t, err)
256240

257241
// Test
258242
output, err := installPyProjectToml(fs, projectDirPath)
259243

260244
// Assertions
261-
if tt.expectedError {
245+
if tc.expectedError {
262246
require.Error(t, err)
263247
} else {
264248
require.NoError(t, err)
265249
}
266250

267-
require.Contains(t, output, tt.expectedOutput)
251+
require.Contains(t, output, tc.expectedOutput)
268252

269253
// Verify file content contains expected strings
270254
content, err := afero.ReadFile(fs, pyprojectPath)
271255
require.NoError(t, err)
272-
for _, expected := range tt.shouldContain {
256+
for _, expected := range tc.shouldContain {
273257
require.Contains(t, string(content), expected)
274258
}
275259
})
276260
}
277261
}
278262

279263
func Test_venvExists(t *testing.T) {
280-
tests := []struct {
281-
name string
264+
tests := map[string]struct {
282265
venvPath string
283266
createPipFile bool
284267
expectedResult bool
285268
}{
286-
{
287-
name: "Venv exists when pip file present",
269+
"Venv exists when pip file present": {
288270
venvPath: "/path/to/.venv",
289271
createPipFile: true,
290272
expectedResult: true,
291273
},
292-
{
293-
name: "Venv does not exist when pip file absent",
274+
"Venv does not exist when pip file absent": {
294275
venvPath: "/path/to/.venv",
295276
createPipFile: false,
296277
expectedResult: false,
297278
},
298279
}
299-
for _, tt := range tests {
300-
t.Run(tt.name, func(t *testing.T) {
280+
for name, tc := range tests {
281+
t.Run(name, func(t *testing.T) {
301282
fs := slackdeps.NewFsMock()
302283

303-
if tt.createPipFile {
304-
pipPath := getPipExecutable(tt.venvPath)
284+
if tc.createPipFile {
285+
pipPath := getPipExecutable(tc.venvPath)
305286
err := fs.MkdirAll(filepath.Dir(pipPath), 0755)
306287
require.NoError(t, err)
307288
err = afero.WriteFile(fs, pipPath, []byte(""), 0755)
308289
require.NoError(t, err)
309290
}
310291

311-
result := venvExists(fs, tt.venvPath)
312-
require.Equal(t, tt.expectedResult, result)
292+
result := venvExists(fs, tc.venvPath)
293+
require.Equal(t, tc.expectedResult, result)
313294
})
314295
}
315296
}

0 commit comments

Comments
 (0)