Skip to content

Commit

Permalink
Resolved result ref order can affect unit test outcome
Browse files Browse the repository at this point in the history
Test TestTaskParamResolver_ResolveResultRefs performs a diff on a slice
of resolved result references. Unfortunately the ordering of this slice
is not guaranteed so sometimes the test fails.

This PR updates the unit test to check each entry appears as expected in
the tt.want slice (confirming also that the lengths of the slices
match). Also adds a small helper func to format the resolved result refs
for printing in the event that they don't match.
  • Loading branch information
Scott authored and tekton-robot committed Mar 4, 2020
1 parent 5bbf652 commit 60d76bd
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions pkg/reconciler/pipelinerun/resources/resultrefresolution_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package resources

import (
"fmt"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -256,16 +258,34 @@ func TestTaskParamResolver_ResolveResultRefs(t *testing.T) {
t.Logf("test name: %s\n", tt.name)
got, err := extractResultRefsFromParam(tt.fields.pipelineRunState, tt.args.param)
if (err != nil) != tt.wantErr {
t.Errorf("ResolveResultRef() error = %v, wantErr %v", err, tt.wantErr)
return
t.Fatalf("ResolveResultRef() error = %v, wantErr %v", err, tt.wantErr)
}
if d := cmp.Diff(tt.want, got); d != "" {
t.Fatalf("ResolveResultRef -want, +got: %v", d)
if len(tt.want) != len(got) {
t.Fatalf("incorrect number of refs, want %d, got %d", len(tt.want), len(got))
}
for _, rGot := range got {
foundMatch := false
for _, rWant := range tt.want {
if d := cmp.Diff(rGot, rWant); d == "" {
foundMatch = true
}
}
if !foundMatch {
t.Fatalf("Expected resolved refs:\n%s\n\nbut received:\n%s\n", resolvedSliceAsString(tt.want), resolvedSliceAsString(got))
}
}
})
}
}

func resolvedSliceAsString(rs []*ResolvedResultRef) string {
var s []string
for _, r := range rs {
s = append(s, fmt.Sprintf("%#v", *r))
}
return fmt.Sprintf("[\n%s\n]", strings.Join(s, ",\n"))
}

func TestResolveResultRefs(t *testing.T) {
type args struct {
pipelineRunState PipelineRunState
Expand Down

0 comments on commit 60d76bd

Please sign in to comment.