Skip to content

Commit f2679b8

Browse files
authored
Merge pull request #4 from GustavoCaso/use-t-fatal-instead-of-t-error
use t.Fatal or t.Fatalf instead of t.Error or t.Errorf
2 parents 20203f4 + 28b7261 commit f2679b8

9 files changed

Lines changed: 179 additions & 189 deletions

File tree

.golangci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ formatters:
3636
# with the given prefixes are grouped after 3rd-party packages.
3737
# Default: []
3838
local-prefixes:
39-
- github.com/my/project
39+
- github.com/GustavoCaso/meeseeks
4040

4141
golines:
4242
# Target maximum line length.
@@ -106,7 +106,7 @@ linters:
106106
- sqlclosecheck # checks that sql.Rows and sql.Stmt are closed
107107
- staticcheck # is a go vet on steroids, applying a ton of static analysis checks
108108
- testableexamples # checks if examples are testable (have an expected output)
109-
- testifylint # checks usage of github.com/stretchr/testify
109+
# - testifylint # checks usage of github.com/stretchr/testify
110110
# - testpackage # makes you use a separate _test package
111111
- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
112112
- unconvert # removes unnecessary type conversions

cmd/meeseeks/main_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestMainCommands(t *testing.T) {
2020
output := stdoutBuf.String() + stderrBuf.String()
2121

2222
if exitCode != 1 {
23-
t.Errorf("Expected exit code 1, got %d", exitCode)
23+
t.Fatalf("Expected exit code 1, got %d", exitCode)
2424
}
2525

2626
expectedMessages := []string{
@@ -34,7 +34,7 @@ func TestMainCommands(t *testing.T) {
3434

3535
for _, msg := range expectedMessages {
3636
if !strings.Contains(output, msg) {
37-
t.Errorf("Expected output to contain %q, got %q", msg, output)
37+
t.Fatalf("Expected output to contain %q, got %q", msg, output)
3838
}
3939
}
4040
},
@@ -47,11 +47,11 @@ func TestMainCommands(t *testing.T) {
4747
output := stdoutBuf.String() + stderrBuf.String()
4848

4949
if exitCode != 1 {
50-
t.Errorf("Expected exit code 1, got %d", exitCode)
50+
t.Fatalf("Expected exit code 1, got %d", exitCode)
5151
}
5252

5353
if !strings.Contains(output, "Unknown command: unknown") {
54-
t.Errorf("Expected unknown command error, got %q", output)
54+
t.Fatalf("Expected unknown command error, got %q", output)
5555
}
5656
},
5757
},
@@ -63,11 +63,11 @@ func TestMainCommands(t *testing.T) {
6363
output := stdoutBuf.String() + stderrBuf.String()
6464

6565
if exitCode != 0 {
66-
t.Errorf("Expected exit code %d, got %d", 0, exitCode)
66+
t.Fatalf("Expected exit code %d, got %d", 0, exitCode)
6767
}
6868

6969
if !strings.Contains(output, "meeseeks version 1.0.0") {
70-
t.Errorf("Expected output to contain %q, got %q", "meeseeks version 1.0.0", output)
70+
t.Fatalf("Expected output to contain %q, got %q", "meeseeks version 1.0.0", output)
7171
}
7272
},
7373
},

cmd/meeseeks/run_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ func TestRunCommand_ConfigValidation(t *testing.T) {
3535
output := stdoutBuf.String() + stderrBuf.String()
3636

3737
if exitCode != tt.expectedExit {
38-
t.Errorf("Expected exit code %d, got %d", tt.expectedExit, exitCode)
38+
t.Fatalf("Expected exit code %d, got %d", tt.expectedExit, exitCode)
3939
}
4040

4141
if !strings.Contains(output, tt.errorMessage) {
42-
t.Errorf("Expected error message %q, got %q", tt.errorMessage, output)
42+
t.Fatalf("Expected error message %q, got %q", tt.errorMessage, output)
4343
}
4444
})
4545
}
@@ -97,7 +97,7 @@ func TestRunCommand_Foreground(t *testing.T) {
9797

9898
err = cmd.Start()
9999
if err != nil {
100-
t.Errorf("error starting in foreground: %s", err.Error())
100+
t.Fatalf("error starting in foreground: %s", err.Error())
101101
}
102102

103103
// Start goroutines to read from pipes and populate buffers
@@ -129,13 +129,13 @@ func TestRunCommand_Foreground(t *testing.T) {
129129
if err != nil && !strings.Contains(err.Error(), "signal") &&
130130
!strings.Contains(err.Error(), "interrupt") &&
131131
!strings.Contains(err.Error(), "context deadline") {
132-
t.Errorf("Unexpected error (ignoring interrupt/timeout): %v", err)
132+
t.Fatalf("Unexpected error (ignoring interrupt/timeout): %v", err)
133133
}
134134

135135
output := stdout.String() + stderr.String()
136136
expected := "Started meeseeks program_count=1"
137137
if !strings.Contains(output, expected) {
138-
t.Errorf("Expected output to contain %q, got %q", expected, output)
138+
t.Fatalf("Expected output to contain %q, got %q", expected, output)
139139
}
140140
}
141141

@@ -179,17 +179,17 @@ func TestRunCommand_Detached(t *testing.T) {
179179

180180
output := stdout.String() + stderr.String()
181181
if !strings.Contains(output, "Started meeseeks (detached)") {
182-
t.Errorf("Expected `Started meeseeks (detached)`, got: %q", output)
182+
t.Fatalf("Expected `Started meeseeks (detached)`, got: %q", output)
183183
}
184184

185185
if _, err := os.Stat(expectedPidFile); os.IsNotExist(err) {
186-
t.Errorf("PID file was not created at %s", expectedPidFile)
186+
t.Fatalf("PID file was not created at %s", expectedPidFile)
187187
}
188188

189189
time.Sleep(500 * time.Millisecond)
190190

191191
if _, err := os.Stat(expectedSocketPath); os.IsNotExist(err) {
192-
t.Errorf("Socket file was not created at %s", expectedSocketPath)
192+
t.Fatalf("Socket file was not created at %s", expectedSocketPath)
193193
}
194194

195195
var stdoutBuf, stderrBuf bytes.Buffer
@@ -201,25 +201,25 @@ func TestRunCommand_Detached(t *testing.T) {
201201
5*time.Second,
202202
)
203203
if exitCode != 0 {
204-
t.Errorf("Expected exit code %d, got %d", 0, exitCode)
204+
t.Fatalf("Expected exit code %d, got %d", 0, exitCode)
205205
}
206206
statusOutput := stdoutBuf.String() + stderrBuf.String()
207207

208208
if strings.Contains(statusOutput, "meeseeks server not running") {
209-
t.Errorf("Status command could not connect to daemon: %q", statusOutput)
209+
t.Fatalf("Status command could not connect to daemon: %q", statusOutput)
210210
}
211211

212212
exitCode = runCLICommand(t, []string{"exit"}, nil, nil, 5*time.Second)
213213
if exitCode != 0 {
214-
t.Errorf("Expected exit code %d, got %d", 0, exitCode)
214+
t.Fatalf("Expected exit code %d, got %d", 0, exitCode)
215215
}
216216

217217
if _, err := os.Stat(expectedPidFile); !os.IsNotExist(err) {
218-
t.Error("PID file still exists. Stoping meeseeks should remove the PID file")
218+
t.Fatal("PID file still exists. Stoping meeseeks should remove the PID file")
219219
}
220220

221221
if _, err := os.Stat(expectedSocketPath); !os.IsNotExist(err) {
222-
t.Error("Socket file still exists. Stoping meeseeks should remove the Socket file")
222+
t.Fatal("Socket file still exists. Stoping meeseeks should remove the Socket file")
223223
}
224224

225225
var stdoutBuf2, stderrBuf2 bytes.Buffer
@@ -231,11 +231,11 @@ func TestRunCommand_Detached(t *testing.T) {
231231
5*time.Second,
232232
)
233233
if exitCode != 1 {
234-
t.Errorf("Expected exit code %d, got %d", 1, exitCode)
234+
t.Fatalf("Expected exit code %d, got %d", 1, exitCode)
235235
}
236236
statusOutput = stdoutBuf2.String() + stderrBuf2.String()
237237

238238
if !strings.Contains(statusOutput, "meeseeks server not running") {
239-
t.Error("Status command should not work after exiting meeseeks")
239+
t.Fatal("Status command should not work after exiting meeseeks")
240240
}
241241
}

cmd/meeseeks/test_helpers.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ func ensureNoDaemonRunning(t *testing.T) {
141141
expectedSocketPath := getSocketPath()
142142

143143
if _, err := os.Stat(expectedPidFile); !os.IsNotExist(err) {
144-
t.Error("PID file still exists. Stoping meeseeks should remove the PID file")
144+
t.Fatal("PID file still exists. Stoping meeseeks should remove the PID file")
145145
}
146146

147147
if _, err := os.Stat(expectedSocketPath); !os.IsNotExist(err) {
148-
t.Error("Socket file still exists. Stoping meeseeks should remove the Socket file")
148+
t.Fatal("Socket file still exists. Stoping meeseeks should remove the Socket file")
149149
}
150150
}
151151

@@ -166,11 +166,11 @@ func runCommandTests(t *testing.T, tests []commandTestCase) {
166166
output := stdoutBuf.String() + stderrBuf.String()
167167

168168
if exitCode != tt.expectedExit {
169-
t.Errorf("Expected exit code %d, got %d", tt.expectedExit, exitCode)
169+
t.Fatalf("Expected exit code %d, got %d", tt.expectedExit, exitCode)
170170
}
171171

172172
if !strings.Contains(output, tt.shouldContain) {
173-
t.Errorf("Expected output to contain %q, got %q", tt.shouldContain, output)
173+
t.Fatalf("Expected output to contain %q, got %q", tt.shouldContain, output)
174174
}
175175
})
176176
}
@@ -183,12 +183,12 @@ func testCommandHelp(t *testing.T, command string, expectedMessages []string) {
183183
output := stdoutBuf.String() + stderrBuf.String()
184184

185185
if exitCode != 0 {
186-
t.Errorf("Expected exit code 0 for help, got %d", exitCode)
186+
t.Fatalf("Expected exit code 0 for help, got %d", exitCode)
187187
}
188188

189189
for _, msg := range expectedMessages {
190190
if !strings.Contains(output, msg) {
191-
t.Errorf("Expected help output to contain %q, got %q", msg, output)
191+
t.Fatalf("Expected help output to contain %q, got %q", msg, output)
192192
}
193193
}
194194
}

cmd/meeseeks/utils_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,20 @@ func TestCreateProgramFromConfig(t *testing.T) {
6363

6464
if tt.expectError {
6565
if err == nil {
66-
t.Errorf("Expected error but got none")
67-
return
66+
t.Fatalf("Expected error but got none")
6867
}
6968
if !strings.Contains(err.Error(), tt.errorMessage) {
70-
t.Errorf("Expected error containing %q, got %q", tt.errorMessage, err.Error())
69+
t.Fatalf("Expected error containing %q, got %q", tt.errorMessage, err.Error())
7170
}
7271
return
7372
}
7473

7574
if err != nil {
76-
t.Errorf("Unexpected error: %v", err)
77-
return
75+
t.Fatalf("Unexpected error: %v", err)
7876
}
7977

8078
if prog.Name() != tt.expectedName {
81-
t.Errorf("Expected name %q, got %q", tt.expectedName, prog.Name())
79+
t.Fatalf("Expected name %q, got %q", tt.expectedName, prog.Name())
8280
}
8381
})
8482
}
@@ -91,7 +89,7 @@ func TestGetMeeseeksDir(t *testing.T) {
9189

9290
result := getMeeseeksDir()
9391
if result != customDir {
94-
t.Errorf("Expected %q, got %q", customDir, result)
92+
t.Fatalf("Expected %q, got %q", customDir, result)
9593
}
9694
})
9795

@@ -101,7 +99,7 @@ func TestGetMeeseeksDir(t *testing.T) {
10199
expected := filepath.Join(homeDir, ".meeseeks")
102100

103101
if result != expected {
104-
t.Errorf("Expected %q, got %q", expected, result)
102+
t.Fatalf("Expected %q, got %q", expected, result)
105103
}
106104
})
107105
}
@@ -126,7 +124,7 @@ func TestPathFunctions(t *testing.T) {
126124
t.Run(tt.name, func(t *testing.T) {
127125
result := tt.function()
128126
if result != tt.expected {
129-
t.Errorf("Expected %q, got %q", tt.expected, result)
127+
t.Fatalf("Expected %q, got %q", tt.expected, result)
130128
}
131129
})
132130
}
@@ -151,7 +149,7 @@ func TestPathFunctions(t *testing.T) {
151149
t.Run(tt.name, func(t *testing.T) {
152150
result := tt.function()
153151
if result != tt.expected {
154-
t.Errorf("Expected %q, got %q", tt.expected, result)
152+
t.Fatalf("Expected %q, got %q", tt.expected, result)
155153
}
156154
})
157155
}

internal/config/config_test.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,22 @@ func TestLoadConfig(t *testing.T) {
186186

187187
if tt.wantErr {
188188
if err == nil {
189-
t.Errorf("LoadConfig() expected error but got none")
189+
t.Fatalf("LoadConfig() expected error but got none")
190190
return
191191
}
192192
if tt.errContains != "" && !containsString(err.Error(), tt.errContains) {
193-
t.Errorf("LoadConfig() error = %q, want error containing %q", err.Error(), tt.errContains)
193+
t.Fatalf("LoadConfig() error = %q, want error containing %q", err.Error(), tt.errContains)
194194
}
195195
return
196196
}
197197

198198
if err != nil {
199-
t.Errorf("LoadConfig() unexpected error = %v", err)
199+
t.Fatalf("LoadConfig() unexpected error = %v", err)
200200
return
201201
}
202202

203203
if !configsEqual(config, tt.expected) {
204-
t.Errorf("LoadConfig() = %+v, want %+v", config, tt.expected)
204+
t.Fatalf("LoadConfig() = %+v, want %+v", config, tt.expected)
205205
}
206206
})
207207
}
@@ -261,22 +261,20 @@ func TestProgramConfig_GetInterval(t *testing.T) {
261261

262262
if tt.wantErr {
263263
if err == nil {
264-
t.Errorf("GetInterval() expected error but got none")
265-
return
264+
t.Fatalf("GetInterval() expected error but got none")
266265
}
267266
if tt.errContains != "" && !containsString(err.Error(), tt.errContains) {
268-
t.Errorf("GetInterval() error = %q, want error containing %q", err.Error(), tt.errContains)
267+
t.Fatalf("GetInterval() error = %q, want error containing %q", err.Error(), tt.errContains)
269268
}
270269
return
271270
}
272271

273272
if err != nil {
274-
t.Errorf("GetInterval() unexpected error = %v", err)
275-
return
273+
t.Fatalf("GetInterval() unexpected error = %v", err)
276274
}
277275

278276
if duration != tt.expected {
279-
t.Errorf("GetInterval() = %v, want %v", duration, tt.expected)
277+
t.Fatalf("GetInterval() = %v, want %v", duration, tt.expected)
280278
}
281279
})
282280
}
@@ -361,17 +359,16 @@ func TestConfig_Validate(t *testing.T) {
361359

362360
if tt.wantErr {
363361
if err == nil {
364-
t.Errorf("Validate() expected error but got none")
365-
return
362+
t.Fatalf("Validate() expected error but got none")
366363
}
367364
if tt.errContains != "" && !containsString(err.Error(), tt.errContains) {
368-
t.Errorf("Validate() error = %q, want error containing %q", err.Error(), tt.errContains)
365+
t.Fatalf("Validate() error = %q, want error containing %q", err.Error(), tt.errContains)
369366
}
370367
return
371368
}
372369

373370
if err != nil {
374-
t.Errorf("Validate() unexpected error = %v", err)
371+
t.Fatalf("Validate() unexpected error = %v", err)
375372
}
376373
})
377374
}

0 commit comments

Comments
 (0)