Skip to content

Commit eb097f6

Browse files
jdsutherlandnywilken
authored andcommitted
Fix test failure panics when err is nil (#776)
Currently several tests use `assert.Regexp(.. err.Error())` where err is unchecked. If there is no error (it's nil) then the tests blow up and we don't get a useful failure.
1 parent 70022ab commit eb097f6

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

cmd/configure_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ func TestBareConfigure(t *testing.T) {
3232
}
3333

3434
err = runConfigure(cfg, flags)
35-
assert.Regexp(t, "no token configured", err.Error())
35+
if assert.Error(t, err) {
36+
assert.Regexp(t, "no token configured", err.Error())
37+
}
3638
}
3739

3840
func TestConfigureShow(t *testing.T) {
@@ -379,8 +381,9 @@ func TestConfigureDefaultWorkspaceWithoutClobbering(t *testing.T) {
379381
assert.NoError(t, err)
380382

381383
err = runConfigure(cfg, flags)
382-
assert.Error(t, err)
383-
assert.Regexp(t, "already something", err.Error())
384+
if assert.Error(t, err) {
385+
assert.Regexp(t, "already something", err.Error())
386+
}
384387
}
385388

386389
func TestConfigureExplicitWorkspaceWithoutClobberingNonDirectory(t *testing.T) {

cmd/submit_test.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ func TestSubmitWithoutToken(t *testing.T) {
2424
}
2525

2626
err := runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{})
27-
assert.Regexp(t, "Welcome to Exercism", err.Error())
28-
assert.Regexp(t, "exercism.io/my/settings", err.Error())
27+
if assert.Error(t, err) {
28+
assert.Regexp(t, "Welcome to Exercism", err.Error())
29+
assert.Regexp(t, "exercism.io/my/settings", err.Error())
30+
}
2931
}
3032

3133
func TestSubmitWithoutWorkspace(t *testing.T) {
@@ -39,7 +41,9 @@ func TestSubmitWithoutWorkspace(t *testing.T) {
3941
}
4042

4143
err := runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{})
42-
assert.Regexp(t, "re-run the configure", err.Error())
44+
if assert.Error(t, err) {
45+
assert.Regexp(t, "re-run the configure", err.Error())
46+
}
4347
}
4448

4549
func TestSubmitNonExistentFile(t *testing.T) {
@@ -68,7 +72,9 @@ func TestSubmitNonExistentFile(t *testing.T) {
6872
filepath.Join(tmpDir, "file-2.txt"),
6973
}
7074
err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), files)
71-
assert.Regexp(t, "cannot be found", err.Error())
75+
if assert.Error(t, err) {
76+
assert.Regexp(t, "cannot be found", err.Error())
77+
}
7278
}
7379

7480
func TestSubmitExerciseWithoutMetadataFile(t *testing.T) {
@@ -94,8 +100,9 @@ func TestSubmitExerciseWithoutMetadataFile(t *testing.T) {
94100
}
95101

96102
err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{file})
97-
assert.Error(t, err)
98-
assert.Regexp(t, "doesn't have the necessary metadata", err.Error())
103+
if assert.Error(t, err) {
104+
assert.Regexp(t, "doesn't have the necessary metadata", err.Error())
105+
}
99106
}
100107

101108
func TestSubmitFilesAndDir(t *testing.T) {
@@ -124,8 +131,10 @@ func TestSubmitFilesAndDir(t *testing.T) {
124131
filepath.Join(tmpDir, "file-2.txt"),
125132
}
126133
err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), files)
127-
assert.Regexp(t, "submitting a directory", err.Error())
128-
assert.Regexp(t, "Please change into the directory and provide the path to the file\\(s\\) you wish to submit", err.Error())
134+
if assert.Error(t, err) {
135+
assert.Regexp(t, "submitting a directory", err.Error())
136+
assert.Regexp(t, "Please change into the directory and provide the path to the file\\(s\\) you wish to submit", err.Error())
137+
}
129138
}
130139

131140
func TestSubmitFiles(t *testing.T) {
@@ -339,8 +348,9 @@ func TestSubmitWithEnormousFile(t *testing.T) {
339348

340349
err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{file})
341350

342-
assert.Error(t, err)
343-
assert.Regexp(t, "Please reduce the size of the file and try again.", err.Error())
351+
if assert.Error(t, err) {
352+
assert.Regexp(t, "Please reduce the size of the file and try again.", err.Error())
353+
}
344354
}
345355

346356
func TestSubmitFilesForTeamExercise(t *testing.T) {
@@ -426,8 +436,9 @@ func TestSubmitOnlyEmptyFile(t *testing.T) {
426436
err = ioutil.WriteFile(file, []byte(""), os.FileMode(0755))
427437

428438
err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{file})
429-
assert.Error(t, err)
430-
assert.Regexp(t, "No files found", err.Error())
439+
if assert.Error(t, err) {
440+
assert.Regexp(t, "No files found", err.Error())
441+
}
431442
}
432443

433444
func TestSubmitFilesFromDifferentSolutions(t *testing.T) {
@@ -462,8 +473,9 @@ func TestSubmitFilesFromDifferentSolutions(t *testing.T) {
462473
}
463474

464475
err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{file1, file2})
465-
assert.Error(t, err)
466-
assert.Regexp(t, "different solutions", err.Error())
476+
if assert.Error(t, err) {
477+
assert.Regexp(t, "different solutions", err.Error())
478+
}
467479
}
468480

469481
func fakeSubmitServer(t *testing.T, submittedFiles map[string]string) *httptest.Server {

0 commit comments

Comments
 (0)