Skip to content

Commit f8e94d9

Browse files
authored
Merge pull request #275 from thaJeztah/subtests
rewrite tests to use sub-tests, improve error-handling in tests, and use t.Cleanup()
2 parents 6f4e3ab + 14d46ff commit f8e94d9

File tree

5 files changed

+344
-220
lines changed

5 files changed

+344
-220
lines changed

client/client_test.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func TestStore(t *testing.T) {
113113

114114
for _, v := range valid {
115115
if err := Store(mockProgramFn, &v); err != nil {
116-
t.Fatal(err)
116+
t.Error(err)
117117
}
118118
}
119119

@@ -123,7 +123,7 @@ func TestStore(t *testing.T) {
123123

124124
for _, v := range invalid {
125125
if err := Store(mockProgramFn, &v); err == nil {
126-
t.Fatalf("Expected error for server %s, got nil", v.ServerURL)
126+
t.Errorf("Expected error for server %s, got nil", v.ServerURL)
127127
}
128128
}
129129
}
@@ -152,10 +152,10 @@ func TestGet(t *testing.T) {
152152
}
153153

154154
if c.Username != v.Username {
155-
t.Fatalf("expected username `%s`, got %s", v.Username, c.Username)
155+
t.Errorf("expected username `%s`, got %s", v.Username, c.Username)
156156
}
157157
if c.Secret != v.Secret {
158-
t.Fatalf("expected secret `%s`, got %s", v.Secret, c.Secret)
158+
t.Errorf("expected secret `%s`, got %s", v.Secret, c.Secret)
159159
}
160160
}
161161

@@ -165,10 +165,17 @@ func TestGet(t *testing.T) {
165165
serverURL string
166166
err string
167167
}{
168-
{missingCredsAddress, credentials.NewErrCredentialsNotFound().Error()},
169-
{invalidServerAddress, "error getting credentials - err: exited 1, out: `program failed`"},
170-
{"", fmt.Sprintf("error getting credentials - err: %s, out: `%s`",
171-
missingServerURLErr.Error(), missingServerURLErr.Error())},
168+
{
169+
serverURL: missingCredsAddress,
170+
err: credentials.NewErrCredentialsNotFound().Error(),
171+
},
172+
{
173+
serverURL: invalidServerAddress,
174+
err: "error getting credentials - err: exited 1, out: `program failed`",
175+
},
176+
{
177+
err: fmt.Sprintf("error getting credentials - err: %s, out: `%s`", missingServerURLErr.Error(), missingServerURLErr.Error()),
178+
},
172179
}
173180

174181
for _, v := range invalid {
@@ -177,7 +184,7 @@ func TestGet(t *testing.T) {
177184
t.Fatalf("Expected error for server %s, got nil", v.serverURL)
178185
}
179186
if err.Error() != v.err {
180-
t.Fatalf("Expected error `%s`, got `%v`", v.err, err)
187+
t.Errorf("Expected error `%s`, got `%v`", v.err, err)
181188
}
182189
}
183190
}
@@ -192,11 +199,11 @@ func ExampleErase() {
192199

193200
func TestErase(t *testing.T) {
194201
if err := Erase(mockProgramFn, validServerAddress); err != nil {
195-
t.Fatal(err)
202+
t.Error(err)
196203
}
197204

198205
if err := Erase(mockProgramFn, invalidServerAddress); err == nil {
199-
t.Fatalf("Expected error for server %s, got nil", invalidServerAddress)
206+
t.Errorf("Expected error for server %s, got nil", invalidServerAddress)
200207
}
201208
}
202209

@@ -207,6 +214,6 @@ func TestList(t *testing.T) {
207214
}
208215

209216
if username, exists := auths[validServerAddress]; !exists || username != validUsername {
210-
t.Fatalf("auths[%s] returned %s, %t; expected %s, %t", validServerAddress, username, exists, validUsername, true)
217+
t.Errorf("auths[%s] returned %s, %t; expected %s, %t", validServerAddress, username, exists, validUsername, true)
211218
}
212219
}

credentials/credentials_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (m *memoryStore) List() (map[string]string, error) {
4242
}
4343

4444
func TestStore(t *testing.T) {
45-
serverURL := "https://index.docker.io/v1/"
45+
const serverURL = "https://index.docker.io/v1/"
4646
creds := &Credentials{
4747
ServerURL: serverURL,
4848
Username: "foo",
@@ -65,11 +65,11 @@ func TestStore(t *testing.T) {
6565
}
6666

6767
if c.Username != "foo" {
68-
t.Fatalf("expected username foo, got %s\n", c.Username)
68+
t.Errorf("expected username foo, got %s\n", c.Username)
6969
}
7070

7171
if c.Secret != "bar" {
72-
t.Fatalf("expected username bar, got %s\n", c.Secret)
72+
t.Errorf("expected username bar, got %s\n", c.Secret)
7373
}
7474
}
7575

@@ -89,7 +89,7 @@ func TestStoreMissingServerURL(t *testing.T) {
8989
h := newMemoryStore()
9090

9191
if err := Store(h, in); IsCredentialsMissingServerURL(err) == false {
92-
t.Fatal(err)
92+
t.Error(err)
9393
}
9494
}
9595

@@ -109,12 +109,12 @@ func TestStoreMissingUsername(t *testing.T) {
109109
h := newMemoryStore()
110110

111111
if err := Store(h, in); IsCredentialsMissingUsername(err) == false {
112-
t.Fatal(err)
112+
t.Error(err)
113113
}
114114
}
115115

116116
func TestGet(t *testing.T) {
117-
serverURL := "https://index.docker.io/v1/"
117+
const serverURL = "https://index.docker.io/v1/"
118118
creds := &Credentials{
119119
ServerURL: serverURL,
120120
Username: "foo",
@@ -147,16 +147,16 @@ func TestGet(t *testing.T) {
147147
}
148148

149149
if c.Username != "foo" {
150-
t.Fatalf("expected username foo, got %s\n", c.Username)
150+
t.Errorf("expected username foo, got %s\n", c.Username)
151151
}
152152

153153
if c.Secret != "bar" {
154-
t.Fatalf("expected username bar, got %s\n", c.Secret)
154+
t.Errorf("expected username bar, got %s\n", c.Secret)
155155
}
156156
}
157157

158158
func TestGetMissingServerURL(t *testing.T) {
159-
serverURL := "https://index.docker.io/v1/"
159+
const serverURL = "https://index.docker.io/v1/"
160160
creds := &Credentials{
161161
ServerURL: serverURL,
162162
Username: "foo",
@@ -177,12 +177,12 @@ func TestGetMissingServerURL(t *testing.T) {
177177
w := new(bytes.Buffer)
178178

179179
if err := Get(h, buf, w); IsCredentialsMissingServerURL(err) == false {
180-
t.Fatal(err)
180+
t.Error(err)
181181
}
182182
}
183183

184184
func TestErase(t *testing.T) {
185-
serverURL := "https://index.docker.io/v1/"
185+
const serverURL = "https://index.docker.io/v1/"
186186
creds := &Credentials{
187187
ServerURL: serverURL,
188188
Username: "foo",
@@ -206,12 +206,12 @@ func TestErase(t *testing.T) {
206206

207207
w := new(bytes.Buffer)
208208
if err := Get(h, buf, w); err == nil {
209-
t.Fatal("expected error getting missing creds, got empty")
209+
t.Error("expected error getting missing creds, got empty")
210210
}
211211
}
212212

213213
func TestEraseMissingServerURL(t *testing.T) {
214-
serverURL := "https://index.docker.io/v1/"
214+
const serverURL = "https://index.docker.io/v1/"
215215
creds := &Credentials{
216216
ServerURL: serverURL,
217217
Username: "foo",
@@ -244,6 +244,6 @@ func TestList(t *testing.T) {
244244
}
245245
// testing that there is an output
246246
if out.Len() == 0 {
247-
t.Fatalf("expected output in the writer, got %d", 0)
247+
t.Error("expected output in the writer, got 0")
248248
}
249249
}

0 commit comments

Comments
 (0)