Skip to content

Commit

Permalink
dockerfile: extend test harness
Browse files Browse the repository at this point in the history
This adds a way to specify expected errors.

Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Dec 15, 2021
1 parent d390f79 commit 467ad76
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions rhel/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dockerfile

import (
"bytes"
"context"
"encoding/json"
"io/fs"
Expand All @@ -20,7 +21,9 @@ func TestGetLabels(t *testing.T) {
}
for _, de := range de {
n := de.Name()
if !strings.HasPrefix(n, "Dockerfile") || strings.HasSuffix(n, ".want") {
if !strings.HasPrefix(n, "Dockerfile") ||
strings.HasSuffix(n, ".want") ||
strings.HasSuffix(n, ".want.err") {
continue
}
t.Run(n, func(t *testing.T) {
Expand All @@ -34,14 +37,25 @@ func TestGetLabels(t *testing.T) {
t.Fatal(err)
}
defer w.Close()
wantErr, _ := fs.ReadFile(td, n+".want.err")

want := make(map[string]string)
if err := json.NewDecoder(w).Decode(&want); err != nil {
t.Error(err)
}
got, err := GetLabels(ctx, f)
if err != nil {
t.Error(err)
if len(wantErr) == 0 {
if err != nil {
t.Error(err)
}
} else {
if err == nil {
t.Error("got nil, wanted error")
} else {
if got, want := err.Error(), string(bytes.TrimSpace(wantErr)); got != want {
t.Errorf("got: %+#q, want: %+#q", got, want)
}
}
}

if !cmp.Equal(got, want) {
Expand Down

0 comments on commit 467ad76

Please sign in to comment.