From 32f5da5c090e2f28eef9fed869cf3ab25937f38f Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 17 Jan 2020 12:16:14 +0100 Subject: [PATCH 01/11] =?UTF-8?q?Use=20golden=20package=20for=20unit=20tes?= =?UTF-8?q?t=20for=20Task=20=F0=9F=90=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allow to remove the potential long expected output from the test code. See gotest.tools/v3/golden for documentation. Signed-off-by: Vincent Demeester --- go.mod | 5 +- go.sum | 12 -- pkg/cmd/task/describe_test.go | 118 +------------- pkg/cmd/task/list_test.go | 17 +- .../testdata/TestTaskDescribe_Full.golden | 35 ++++ .../testdata/TestTaskDescribe_OnlyName.golden | 22 +++ ...tTaskDescribe_OnlyNameDiffNameSpace.golden | 22 +++ .../TestTaskDescribe_OnlyNameParams.golden | 25 +++ .../testdata/TestTaskList_Only_Tasks.golden | 4 + vendor/gotest.tools/v3/golden/golden.go | 153 ++++++++++++++++++ vendor/modules.txt | 1 + 11 files changed, 273 insertions(+), 141 deletions(-) create mode 100644 pkg/cmd/task/testdata/TestTaskDescribe_Full.golden create mode 100644 pkg/cmd/task/testdata/TestTaskDescribe_OnlyName.golden create mode 100644 pkg/cmd/task/testdata/TestTaskDescribe_OnlyNameDiffNameSpace.golden create mode 100644 pkg/cmd/task/testdata/TestTaskDescribe_OnlyNameParams.golden create mode 100644 pkg/cmd/task/testdata/TestTaskList_Only_Tasks.golden create mode 100644 vendor/gotest.tools/v3/golden/golden.go diff --git a/go.mod b/go.mod index 6a8b8aa1f..eceace9b5 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,6 @@ require ( github.com/ghodss/yaml v1.0.0 github.com/google/go-cmp v0.3.1 github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367 // indirect - github.com/google/licenseclassifier v0.0.0-20191208174752-5628aef0e29a // indirect github.com/google/uuid v1.1.1 // indirect github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d // indirect github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect @@ -24,7 +23,6 @@ require ( github.com/imdario/mergo v0.3.8 // indirect github.com/jonboulle/clockwork v0.1.1-0.20190114141812-62fb9bc030d1 github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3 // indirect - github.com/knative/test-infra v0.0.0-20191223203026-935a8f052a48 // indirect github.com/markbates/inflect v1.0.4 // indirect github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a // indirect github.com/mattn/go-isatty v0.0.9 // indirect @@ -34,7 +32,6 @@ require ( github.com/onsi/gomega v1.7.0 // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/pkg/errors v0.8.1 - github.com/sergi/go-diff v1.1.0 // indirect github.com/spf13/cobra v0.0.5 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.4.0 @@ -47,7 +44,9 @@ require ( golang.org/x/net v0.0.0-20190923162816-aa69164e4478 // indirect golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe // indirect + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.2.4 // indirect gotest.tools/v3 v3.0.0 k8s.io/api v0.0.0-20191004102255-dacd7df5a50b k8s.io/apimachinery v0.0.0-20191004074956-01f8b7d1121a diff --git a/go.sum b/go.sum index a99fb0429..b05a8002d 100644 --- a/go.sum +++ b/go.sum @@ -32,8 +32,6 @@ github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnweb github.com/census-instrumentation/opencensus-proto v0.2.1 h1:glEXhBS5PSLLv4IXzLA5yPRVX4bilULVyxxbrfOtDAk= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= -github.com/chmouel/cobra v0.0.0-20191021105835-a78788917390 h1:r2QCtXeuDAgROGLHUyLAnC4ju/o9zGIZ+kZLaNBKgqI= -github.com/chmouel/cobra v0.0.0-20191021105835-a78788917390/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/chmouel/cobra v0.0.0-20200107083527-379e7a80af0c h1:8YsovAhAya2jnvoIn5aCqyu2E/Zl1+3KBP5tpNXL+H8= github.com/chmouel/cobra v0.0.0-20200107083527-379e7a80af0c/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= @@ -98,8 +96,6 @@ github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367 h1:ScAXWS+TR6MZKex+7 github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI= github.com/google/licenseclassifier v0.0.0-20190926221455-842c0d70d702 h1:nVgx26pAe6l/02mYomOuZssv28XkacGw/0WeiTVorqw= github.com/google/licenseclassifier v0.0.0-20190926221455-842c0d70d702/go.mod h1:qsqn2hxC+vURpyBRygGUuinTO42MFRLcsmQ/P8v94+M= -github.com/google/licenseclassifier v0.0.0-20191208174752-5628aef0e29a h1:PCQssw0nLgVvuzH737oT/qi1j09RbvMmAQpjpNEJ0p8= -github.com/google/licenseclassifier v0.0.0-20191208174752-5628aef0e29a/go.mod h1:qsqn2hxC+vURpyBRygGUuinTO42MFRLcsmQ/P8v94+M= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= @@ -151,8 +147,6 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:C github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/knative/test-infra v0.0.0-20191223203026-935a8f052a48 h1:kaGwu3xnMa5jZ17XxyeLTE0g9Yp59ZLy3o1tjyj4u4c= -github.com/knative/test-infra v0.0.0-20191223203026-935a8f052a48/go.mod h1:l77IWBscEV5T4sYb64/9iwRCVY4UXEIqMcAppsblHW4= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -220,8 +214,6 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= -github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= -github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= @@ -412,13 +404,10 @@ k8s.io/api v0.0.0-20191004102255-dacd7df5a50b h1:38Nx0U83WjBqn1hUWxlgKc7mvH7WhyH k8s.io/api v0.0.0-20191004102255-dacd7df5a50b/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA= k8s.io/apimachinery v0.0.0-20191004074956-01f8b7d1121a h1:lDydUqHrbL/1l5ZQrqD1RIlabhmX8aiZEtxVUb+30iU= k8s.io/apimachinery v0.0.0-20191004074956-01f8b7d1121a/go.mod h1:ccL7Eh7zubPUSh9A3USN90/OzHNSVN6zxzde07TDCL0= -k8s.io/apimachinery v0.17.0 h1:xRBnuie9rXcPxUkDizUsGvPf1cnlZCFu210op7J7LJo= k8s.io/cli-runtime v0.0.0-20191004110054-fe9b9282443f h1:vJOrMsZe+RD884n+WQ5So2oOp7SajI0Op3oOBg64ZsY= k8s.io/cli-runtime v0.0.0-20191004110054-fe9b9282443f/go.mod h1:qWnH3/b8sp/l7EvlDh7ulDU3UWA4P4N1NFbEEP791tM= -k8s.io/cli-runtime v0.17.0 h1:XEuStbJBHCQlEKFyTQmceDKEWOSYHZkcYWKp3SsQ9Hk= k8s.io/client-go v0.0.0-20191004102537-eb5b9a8cfde7 h1:WyPHgjjXvF4zVVwKGZKKiJGBUW45AuN44uSOuH8euuE= k8s.io/client-go v0.0.0-20191004102537-eb5b9a8cfde7/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s= -k8s.io/client-go v11.0.0+incompatible h1:LBbX2+lOwY9flffWlJM7f1Ct8V2SRNiMRDFeiwnJo9o= k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8= k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I= k8s.io/kube-openapi v0.0.0-20171101183504-39a7bf85c140 h1:j1Zez+Xb4OWvCdROqeq8sP2ACi/qWV1tj/imP0/8a0k= @@ -427,7 +416,6 @@ knative.dev/caching v0.0.0-20190719140829-2032732871ff h1:PrlDvOGvCASqW5Fs3ZGes0 knative.dev/caching v0.0.0-20190719140829-2032732871ff/go.mod h1:dHXFU6CGlLlbzaWc32g80cR92iuBSpsslDNBWI8C7eg= knative.dev/pkg v0.0.0-20190909195211-528ad1c1dd62 h1:0neKnC9hstF5Lewj6OaAJdXqk22IZ7pnOPQ8Zl8cfq4= knative.dev/pkg v0.0.0-20190909195211-528ad1c1dd62/go.mod h1:pgODObA1dTyhNoFxPZTTjNWfx6F0aKsKzn+vaT9XO/Q= -knative.dev/pkg v0.0.0-20200108203553-8c6241207427 h1:swUPz/GhGE0bdXX8Ys6AXhjshbCcYXgW9weH7LvK28U= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= diff --git a/pkg/cmd/task/describe_test.go b/pkg/cmd/task/describe_test.go index ea39ef34c..859ba1b86 100644 --- a/pkg/cmd/task/describe_test.go +++ b/pkg/cmd/task/describe_test.go @@ -16,9 +16,11 @@ package task import ( "errors" + "fmt" "testing" "time" + "gotest.tools/v3/golden" "k8s.io/apimachinery/pkg/runtime" "github.com/jonboulle/clockwork" @@ -95,30 +97,7 @@ func TestTaskDescribe_OnlyName(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: task-1 -Namespace: ns - -Input Resources - - No input resources - -Output Resources - - No output resources - -Params - - No params - -Steps - - No steps - -Taskruns - - No taskruns -` - test.AssertOutput(t, expected, out) + golden.Assert(t, out, fmt.Sprintf("%s.golden", t.Name())) } func TestTaskDescribe_OnlyNameDiffNameSpace(t *testing.T) { @@ -148,30 +127,7 @@ func TestTaskDescribe_OnlyNameDiffNameSpace(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: task -Namespace: ns-2 - -Input Resources - - No input resources - -Output Resources - - No output resources - -Params - - No params - -Steps - - No steps - -Taskruns - - No taskruns -` - test.AssertOutput(t, expected, out) + golden.Assert(t, out, fmt.Sprintf("%s.golden", t.Name())) } func TestTaskDescribe_OnlyNameParams(t *testing.T) { @@ -209,33 +165,7 @@ func TestTaskDescribe_OnlyNameParams(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: task-1 -Namespace: ns - -Input Resources - - No input resources - -Output Resources - - No output resources - -Params - - NAME TYPE DEFAULT VALUE - myarg string - myprint string - myarray array - -Steps - - No steps - -Taskruns - - No taskruns -` - test.AssertOutput(t, expected, out) + golden.Assert(t, out, fmt.Sprintf("%s.golden", t.Name())) } func TestTaskDescribe_Full(t *testing.T) { @@ -304,43 +234,7 @@ func TestTaskDescribe_Full(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: task-1 -Namespace: ns - -Input Resources - - NAME TYPE - my-repo git - source-repo git - my-image image - -Output Resources - - NAME TYPE - artifact-image image - code-image image - -Params - - NAME TYPE DEFAULT VALUE - myarg string - myarray array - print string somethingdifferent - output array [booms booms booms] - -Steps - - hello - exit - -Taskruns - -NAME STARTED DURATION STATUS -tr-1 20 minutes ago 5 minutes Failed -tr-2 10 minutes ago 7 minutes Succeeded - -` - test.AssertOutput(t, expected, out) + golden.Assert(t, out, fmt.Sprintf("%s.golden", t.Name())) } func TestTaskDescribe_PipelineRunError(t *testing.T) { diff --git a/pkg/cmd/task/list_test.go b/pkg/cmd/task/list_test.go index 235a57d86..f2bcb6acf 100644 --- a/pkg/cmd/task/list_test.go +++ b/pkg/cmd/task/list_test.go @@ -15,17 +15,17 @@ package task import ( - "strings" + "fmt" "testing" "time" - "github.com/google/go-cmp/cmp" "github.com/jonboulle/clockwork" "github.com/tektoncd/cli/pkg/test" cb "github.com/tektoncd/cli/pkg/test/builder" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" pipelinetest "github.com/tektoncd/pipeline/test" tb "github.com/tektoncd/pipeline/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -91,16 +91,5 @@ func TestTaskList_Only_Tasks(t *testing.T) { t.Errorf("Unexpected error: %v", err) } - expected := []string{ - "NAME AGE", - "tomatoes 1 minute ago", - "mangoes 20 seconds ago", - "bananas 3 weeks ago", - "", - } - - text := strings.Join(expected, "\n") - if d := cmp.Diff(text, output); d != "" { - t.Errorf("Unexpected output mismatch: %s", d) - } + golden.Assert(t, output, fmt.Sprintf("%s.golden", t.Name())) } diff --git a/pkg/cmd/task/testdata/TestTaskDescribe_Full.golden b/pkg/cmd/task/testdata/TestTaskDescribe_Full.golden new file mode 100644 index 000000000..557b2051e --- /dev/null +++ b/pkg/cmd/task/testdata/TestTaskDescribe_Full.golden @@ -0,0 +1,35 @@ +Name: task-1 +Namespace: ns + +Input Resources + + NAME TYPE + my-repo git + source-repo git + my-image image + +Output Resources + + NAME TYPE + artifact-image image + code-image image + +Params + + NAME TYPE DEFAULT VALUE + myarg string + myarray array + print string somethingdifferent + output array [booms booms booms] + +Steps + + hello + exit + +Taskruns + +NAME STARTED DURATION STATUS +tr-1 20 minutes ago 5 minutes Failed +tr-2 10 minutes ago 7 minutes Succeeded + diff --git a/pkg/cmd/task/testdata/TestTaskDescribe_OnlyName.golden b/pkg/cmd/task/testdata/TestTaskDescribe_OnlyName.golden new file mode 100644 index 000000000..c8e24629c --- /dev/null +++ b/pkg/cmd/task/testdata/TestTaskDescribe_OnlyName.golden @@ -0,0 +1,22 @@ +Name: task-1 +Namespace: ns + +Input Resources + + No input resources + +Output Resources + + No output resources + +Params + + No params + +Steps + + No steps + +Taskruns + + No taskruns diff --git a/pkg/cmd/task/testdata/TestTaskDescribe_OnlyNameDiffNameSpace.golden b/pkg/cmd/task/testdata/TestTaskDescribe_OnlyNameDiffNameSpace.golden new file mode 100644 index 000000000..2f0f2bf50 --- /dev/null +++ b/pkg/cmd/task/testdata/TestTaskDescribe_OnlyNameDiffNameSpace.golden @@ -0,0 +1,22 @@ +Name: task +Namespace: ns-2 + +Input Resources + + No input resources + +Output Resources + + No output resources + +Params + + No params + +Steps + + No steps + +Taskruns + + No taskruns diff --git a/pkg/cmd/task/testdata/TestTaskDescribe_OnlyNameParams.golden b/pkg/cmd/task/testdata/TestTaskDescribe_OnlyNameParams.golden new file mode 100644 index 000000000..2dcd6c3de --- /dev/null +++ b/pkg/cmd/task/testdata/TestTaskDescribe_OnlyNameParams.golden @@ -0,0 +1,25 @@ +Name: task-1 +Namespace: ns + +Input Resources + + No input resources + +Output Resources + + No output resources + +Params + + NAME TYPE DEFAULT VALUE + myarg string + myprint string + myarray array + +Steps + + No steps + +Taskruns + + No taskruns diff --git a/pkg/cmd/task/testdata/TestTaskList_Only_Tasks.golden b/pkg/cmd/task/testdata/TestTaskList_Only_Tasks.golden new file mode 100644 index 000000000..d7b201f45 --- /dev/null +++ b/pkg/cmd/task/testdata/TestTaskList_Only_Tasks.golden @@ -0,0 +1,4 @@ +NAME AGE +tomatoes 1 minute ago +mangoes 20 seconds ago +bananas 3 weeks ago diff --git a/vendor/gotest.tools/v3/golden/golden.go b/vendor/gotest.tools/v3/golden/golden.go new file mode 100644 index 000000000..fd24119e1 --- /dev/null +++ b/vendor/gotest.tools/v3/golden/golden.go @@ -0,0 +1,153 @@ +/*Package golden provides tools for comparing large mutli-line strings. + +Golden files are files in the ./testdata/ subdirectory of the package under test. +*/ +package golden // import "gotest.tools/golden" + +import ( + "bytes" + "flag" + "fmt" + "io/ioutil" + "os" + "path/filepath" + + "gotest.tools/v3/assert" + "gotest.tools/v3/assert/cmp" + "gotest.tools/v3/internal/format" +) + +var flagUpdate = flag.Bool("test.update-golden", false, "update golden file") + +type helperT interface { + Helper() +} + +// Open opens the file in ./testdata +func Open(t assert.TestingT, filename string) *os.File { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + f, err := os.Open(Path(filename)) + assert.NilError(t, err) + return f +} + +// Get returns the contents of the file in ./testdata +func Get(t assert.TestingT, filename string) []byte { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + expected, err := ioutil.ReadFile(Path(filename)) + assert.NilError(t, err) + return expected +} + +// Path returns the full path to a file in ./testdata +func Path(filename string) string { + if filepath.IsAbs(filename) { + return filename + } + return filepath.Join("testdata", filename) +} + +func update(filename string, actual []byte, normalize normalize) error { + if *flagUpdate { + return ioutil.WriteFile(Path(filename), normalize(actual), 0644) + } + return nil +} + +type normalize func([]byte) []byte + +func removeCarriageReturn(in []byte) []byte { + return bytes.Replace(in, []byte("\r\n"), []byte("\n"), -1) +} + +func exactBytes(in []byte) []byte { + return in +} + +// Assert compares the actual content to the expected content in the golden file. +// If the `-test.update-golden` flag is set then the actual content is written +// to the golden file. +// Returns whether the assertion was successful (true) or not (false). +// This is equivalent to assert.Check(t, String(actual, filename)) +func Assert(t assert.TestingT, actual string, filename string, msgAndArgs ...interface{}) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + assert.Assert(t, String(actual, filename), msgAndArgs...) +} + +// String compares actual to the contents of filename and returns success +// if the strings are equal. +// If the `-test.update-golden` flag is set then the actual content is written +// to the golden file. +// +// Any \r\n substrings in actual are converted to a single \n character +// before comparing it to the expected string. When updating the golden file the +// normalized version will be written to the file. This allows Windows to use +// the same golden files as other operating systems. +func String(actual string, filename string) cmp.Comparison { + return func() cmp.Result { + actualBytes := removeCarriageReturn([]byte(actual)) + result, expected := compare(actualBytes, filename, removeCarriageReturn) + if result != nil { + return result + } + diff := format.UnifiedDiff(format.DiffConfig{ + A: string(expected), + B: string(actualBytes), + From: "expected", + To: "actual", + }) + return cmp.ResultFailure("\n" + diff) + } +} + +// AssertBytes compares the actual result to the expected result in the golden +// file. If the `-test.update-golden` flag is set then the actual content is +// written to the golden file. +// Returns whether the assertion was successful (true) or not (false). +// This is equivalent to assert.Check(t, Bytes(actual, filename)) +func AssertBytes( + t assert.TestingT, + actual []byte, + filename string, + msgAndArgs ...interface{}, +) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + assert.Assert(t, Bytes(actual, filename), msgAndArgs...) +} + +// Bytes compares actual to the contents of filename and returns success +// if the bytes are equal. +// If the `-test.update-golden` flag is set then the actual content is written +// to the golden file. +func Bytes(actual []byte, filename string) cmp.Comparison { + return func() cmp.Result { + result, expected := compare(actual, filename, exactBytes) + if result != nil { + return result + } + msg := fmt.Sprintf("%v (actual) != %v (expected)", actual, expected) + return cmp.ResultFailure(msg) + } +} + +func compare(actual []byte, filename string, normalize normalize) (cmp.Result, []byte) { + if err := update(filename, actual, normalize); err != nil { + return cmp.ResultFromError(err), nil + } + expected, err := ioutil.ReadFile(Path(filename)) + if err != nil { + return cmp.ResultFromError(err), nil + } + if bytes.Equal(expected, actual) { + return cmp.ResultSuccess, nil + } + return nil, expected +} diff --git a/vendor/modules.txt b/vendor/modules.txt index ece4aaa65..6f14c1454 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -396,6 +396,7 @@ gopkg.in/yaml.v2 # gotest.tools/v3 v3.0.0 gotest.tools/v3/assert gotest.tools/v3/assert/cmp +gotest.tools/v3/golden gotest.tools/v3/internal/difflib gotest.tools/v3/internal/format gotest.tools/v3/internal/source From 5baa07800d5b4360e8e0064c0d003fd07d23d899 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 17 Jan 2020 12:16:23 +0100 Subject: [PATCH 02/11] =?UTF-8?q?Use=20golden=20package=20for=20unit=20tes?= =?UTF-8?q?t=20in=20TaskRun=20=F0=9F=90=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allow to remove the potential long expected output from the test code. See gotest.tools/v3/golden for documentation. Signed-off-by: Vincent Demeester --- pkg/cmd/taskrun/describe_test.go | 304 +----------------- pkg/cmd/taskrun/list_test.go | 113 ++----- .../TestListTaskRuns-all_in_namespace.golden | 6 + .../TestListTaskRuns-by_Task_name.golden | 2 + .../TestListTaskRuns-empty_list.golden | 1 + ...skRuns-error_from_invalid_namespace.golden | 1 + ..._taskruns_greater_than_maximum_case.golden | 6 + ...skRuns-limit_taskruns_negative_case.golden | 0 ...skRuns-limit_taskruns_returned_to_1.golden | 2 + ...limit_taskruns_with_output_flag_set.golden | 2 + .../TestListTaskRuns-print_by_template.golden | 5 + .../TestListTaskRuns_no_condition.golden | 2 + .../TestTaskRunDescribe_cancel_taskrun.golden | 27 ++ .../TestTaskRunDescribe_empty_taskrun.golden | 24 ++ .../TestTaskRunDescribe_failed.golden | 28 ++ .../TestTaskRunDescribe_no_resourceref.golden | 32 ++ .../TestTaskRunDescribe_no_taskref.golden | 27 ++ .../TestTaskRunDescribe_only_taskrun.golden | 32 ++ ...TaskRunDescribe_step_status_default.golden | 32 ++ ...TaskRunDescribe_step_status_pending.golden | 32 ++ ...TaskRunDescribe_step_status_running.golden | 32 ++ 21 files changed, 334 insertions(+), 376 deletions(-) create mode 100644 pkg/cmd/taskrun/testdata/TestListTaskRuns-all_in_namespace.golden create mode 100644 pkg/cmd/taskrun/testdata/TestListTaskRuns-by_Task_name.golden create mode 100644 pkg/cmd/taskrun/testdata/TestListTaskRuns-empty_list.golden create mode 100644 pkg/cmd/taskrun/testdata/TestListTaskRuns-error_from_invalid_namespace.golden create mode 100644 pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_greater_than_maximum_case.golden create mode 100644 pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_negative_case.golden create mode 100644 pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_returned_to_1.golden create mode 100644 pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_with_output_flag_set.golden create mode 100644 pkg/cmd/taskrun/testdata/TestListTaskRuns-print_by_template.golden create mode 100644 pkg/cmd/taskrun/testdata/TestListTaskRuns_no_condition.golden create mode 100644 pkg/cmd/taskrun/testdata/TestTaskRunDescribe_cancel_taskrun.golden create mode 100644 pkg/cmd/taskrun/testdata/TestTaskRunDescribe_empty_taskrun.golden create mode 100644 pkg/cmd/taskrun/testdata/TestTaskRunDescribe_failed.golden create mode 100644 pkg/cmd/taskrun/testdata/TestTaskRunDescribe_no_resourceref.golden create mode 100644 pkg/cmd/taskrun/testdata/TestTaskRunDescribe_no_taskref.golden create mode 100644 pkg/cmd/taskrun/testdata/TestTaskRunDescribe_only_taskrun.golden create mode 100644 pkg/cmd/taskrun/testdata/TestTaskRunDescribe_step_status_default.golden create mode 100644 pkg/cmd/taskrun/testdata/TestTaskRunDescribe_step_status_pending.golden create mode 100644 pkg/cmd/taskrun/testdata/TestTaskRunDescribe_step_status_running.golden diff --git a/pkg/cmd/taskrun/describe_test.go b/pkg/cmd/taskrun/describe_test.go index f398cbde8..8a125a23d 100644 --- a/pkg/cmd/taskrun/describe_test.go +++ b/pkg/cmd/taskrun/describe_test.go @@ -15,6 +15,7 @@ package taskrun import ( + "fmt" "testing" "time" @@ -25,6 +26,7 @@ import ( "github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources" pipelinetest "github.com/tektoncd/pipeline/test" tb "github.com/tektoncd/pipeline/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" @@ -114,33 +116,7 @@ func TestTaskRunDescribe_empty_taskrun(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: tr-1 -Namespace: ns -Task Ref: t1 - -Status - -STARTED DURATION STATUS ---- --- Succeeded - -Input Resources - -No resources - -Output Resources - -No resources - -Params - -No params - -Steps - -No steps -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestTaskRunDescribe_only_taskrun(t *testing.T) { @@ -194,41 +170,7 @@ func TestTaskRunDescribe_only_taskrun(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: tr-1 -Namespace: ns -Task Ref: t1 - -Status - -STARTED DURATION STATUS -9 minutes ago --- Succeeded - -Input Resources - - NAME RESOURCE REF - git git - image-input image - -Output Resources - - NAME RESOURCE REF - image-output image - image-output2 image - -Params - - NAME VALUE - input param - input2 param2 - -Steps - - NAME STATUS - step1 Completed - step2 Completed -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestTaskRunDescribe_failed(t *testing.T) { @@ -270,37 +212,7 @@ func TestTaskRunDescribe_failed(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: tr-1 -Namespace: ns -Task Ref: t1 - -Status - -STARTED DURATION STATUS -8 minutes ago 3 minutes Failed - -Message - -Testing tr failed - -Input Resources - -No resources - -Output Resources - -No resources - -Params - -No params - -Steps - -No steps -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestTaskRunDescribe_no_taskref(t *testing.T) { @@ -339,36 +251,7 @@ func TestTaskRunDescribe_no_taskref(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: tr-1 -Namespace: ns - -Status - -STARTED DURATION STATUS -8 minutes ago 3 minutes Failed - -Message - -Testing tr failed - -Input Resources - -No resources - -Output Resources - -No resources - -Params - -No params - -Steps - -No steps -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestTaskRunDescribe_no_resourceref(t *testing.T) { @@ -422,41 +305,7 @@ func TestTaskRunDescribe_no_resourceref(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: tr-1 -Namespace: ns -Task Ref: t1 - -Status - -STARTED DURATION STATUS -9 minutes ago --- Succeeded - -Input Resources - - NAME RESOURCE REF - git - image-input image - -Output Resources - - NAME RESOURCE REF - image-output - image-output2 - -Params - - NAME VALUE - input param - input2 param2 - -Steps - - NAME STATUS - step1 Completed - step2 Completed -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestTaskRunDescribe_step_status_default(t *testing.T) { @@ -509,41 +358,7 @@ func TestTaskRunDescribe_step_status_default(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: tr-1 -Namespace: ns -Task Ref: t1 - -Status - -STARTED DURATION STATUS -9 minutes ago --- Failed - -Input Resources - - NAME RESOURCE REF - git - image-input image - -Output Resources - - NAME RESOURCE REF - image-output - image-output2 - -Params - - NAME VALUE - input param - input2 param2 - -Steps - - NAME STATUS - step1 Error - step2 --- -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestTaskRunDescribe_step_status_pending(t *testing.T) { @@ -598,41 +413,7 @@ func TestTaskRunDescribe_step_status_pending(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: tr-1 -Namespace: ns -Task Ref: t1 - -Status - -STARTED DURATION STATUS -9 minutes ago --- Running - -Input Resources - - NAME RESOURCE REF - git - image-input image - -Output Resources - - NAME RESOURCE REF - image-output - image-output2 - -Params - - NAME VALUE - input param - input2 param2 - -Steps - - NAME STATUS - step1 PodInitializing - step2 PodInitializing -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestTaskRunDescribe_step_status_running(t *testing.T) { @@ -687,41 +468,7 @@ func TestTaskRunDescribe_step_status_running(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: tr-1 -Namespace: ns -Task Ref: t1 - -Status - -STARTED DURATION STATUS -9 minutes ago --- Running - -Input Resources - - NAME RESOURCE REF - git - image-input image - -Output Resources - - NAME RESOURCE REF - image-output - image-output2 - -Params - - NAME VALUE - input param - input2 param2 - -Steps - - NAME STATUS - step1 Running - step2 Running -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestTaskRunDescribe_cancel_taskrun(t *testing.T) { @@ -760,34 +507,5 @@ func TestTaskRunDescribe_cancel_taskrun(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: tr-1 -Namespace: ns - -Status - -STARTED DURATION STATUS -8 minutes ago 3 minutes Cancelled(TaskRunCancelled) - -Message - -TaskRun "tr-1" was cancelled - -Input Resources - -No resources - -Output Resources - -No resources - -Params - -No params - -Steps - -No steps -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } diff --git a/pkg/cmd/taskrun/list_test.go b/pkg/cmd/taskrun/list_test.go index 58505e4c6..7cfa7bf4b 100644 --- a/pkg/cmd/taskrun/list_test.go +++ b/pkg/cmd/taskrun/list_test.go @@ -27,6 +27,7 @@ import ( "github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources" pipelinetest "github.com/tektoncd/pipeline/test" tb "github.com/tektoncd/pipeline/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" @@ -112,108 +113,59 @@ func TestListTaskRuns(t *testing.T) { name string command *cobra.Command args []string - expected []string wantError bool }{ { - name: "by Task name", - command: command(t, trs, now, ns), - args: []string{"list", "bar", "-n", "foo"}, - expected: []string{ - "NAME STARTED DURATION STATUS ", - "tr1-1 1 hour ago 1 minute Succeeded ", - "", - }, + name: "by Task name", + command: command(t, trs, now, ns), + args: []string{"list", "bar", "-n", "foo"}, wantError: false, }, { - name: "all in namespace", - command: command(t, trs, now, ns), - args: []string{"list", "-n", "foo"}, - expected: []string{ - "NAME STARTED DURATION STATUS ", - "tr0-1 --- --- Succeeded ", - "tr3-1 --- --- Failed ", - "tr2-2 59 minutes ago 1 minute Failed ", - "tr1-1 1 hour ago 1 minute Succeeded ", - "tr2-1 1 hour ago --- Running ", - "", - }, + name: "all in namespace", + command: command(t, trs, now, ns), + args: []string{"list", "-n", "foo"}, wantError: false, }, { - name: "print by template", - command: command(t, trs, now, ns), - args: []string{"list", "-n", "foo", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"}, - expected: []string{ - "tr0-1", - "tr3-1", - "tr2-2", - "tr1-1", - "tr2-1", - "", - }, + name: "print by template", + command: command(t, trs, now, ns), + args: []string{"list", "-n", "foo", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"}, wantError: false, }, { - name: "empty list", - command: command(t, trs, now, ns), - args: []string{"list", "-n", "random"}, - expected: []string{emptyMsg, ""}, + name: "empty list", + command: command(t, trs, now, ns), + args: []string{"list", "-n", "random"}, }, { - name: "limit taskruns returned to 1", - command: command(t, trs, now, ns), - args: []string{"list", "-n", "foo", "--limit", fmt.Sprintf("%d", 1)}, - expected: []string{ - "NAME STARTED DURATION STATUS ", - "tr0-1 --- --- Succeeded ", - "", - }, + name: "limit taskruns returned to 1", + command: command(t, trs, now, ns), + args: []string{"list", "-n", "foo", "--limit", fmt.Sprintf("%d", 1)}, wantError: false, }, { - name: "limit taskruns negative case", - command: command(t, trs, now, ns), - args: []string{"list", "-n", "foo", "--limit", fmt.Sprintf("%d", -1)}, - expected: []string{ - "", - }, + name: "limit taskruns negative case", + command: command(t, trs, now, ns), + args: []string{"list", "-n", "foo", "--limit", fmt.Sprintf("%d", -1)}, wantError: false, }, { - name: "limit taskruns greater than maximum case", - command: command(t, trs, now, ns), - args: []string{"list", "-n", "foo", "--limit", fmt.Sprintf("%d", 7)}, - expected: []string{ - "NAME STARTED DURATION STATUS ", - "tr0-1 --- --- Succeeded ", - "tr3-1 --- --- Failed ", - "tr2-2 59 minutes ago 1 minute Failed ", - "tr1-1 1 hour ago 1 minute Succeeded ", - "tr2-1 1 hour ago --- Running ", - "", - }, + name: "limit taskruns greater than maximum case", + command: command(t, trs, now, ns), + args: []string{"list", "-n", "foo", "--limit", fmt.Sprintf("%d", 7)}, wantError: false, }, { - name: "limit taskruns with output flag set", - command: command(t, trs, now, ns), - args: []string{"list", "-n", "foo", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}", "--limit", fmt.Sprintf("%d", 2)}, - expected: []string{ - "tr0-1", - "tr3-1", - "", - }, + name: "limit taskruns with output flag set", + command: command(t, trs, now, ns), + args: []string{"list", "-n", "foo", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}", "--limit", fmt.Sprintf("%d", 2)}, wantError: false, }, { - name: "error from invalid namespace", - command: command(t, trs, now, ns), - args: []string{"list", "-n", "invalid"}, - expected: []string{ - "Error: namespaces \"invalid\" not found\n", - }, + name: "error from invalid namespace", + command: command(t, trs, now, ns), + args: []string{"list", "-n", "invalid"}, wantError: true, }, } @@ -225,7 +177,7 @@ func TestListTaskRuns(t *testing.T) { if err != nil && !td.wantError { t.Errorf("Unexpected error: %v", err) } - test.AssertOutput(t, strings.Join(td.expected, "\n"), got) + golden.Assert(t, got, strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-")) }) } } @@ -258,12 +210,7 @@ func TestListTaskRuns_no_condition(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - - expected := `NAME STARTED DURATION STATUS -tr1-1 1 hour ago 1 minute --- -` - - test.AssertOutput(t, expected, got) + golden.Assert(t, got, fmt.Sprintf("%s.golden", t.Name())) } func command(t *testing.T, trs []*v1alpha1.TaskRun, now time.Time, ns []*corev1.Namespace) *cobra.Command { diff --git a/pkg/cmd/taskrun/testdata/TestListTaskRuns-all_in_namespace.golden b/pkg/cmd/taskrun/testdata/TestListTaskRuns-all_in_namespace.golden new file mode 100644 index 000000000..ee226a3c0 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestListTaskRuns-all_in_namespace.golden @@ -0,0 +1,6 @@ +NAME STARTED DURATION STATUS +tr0-1 --- --- Succeeded +tr3-1 --- --- Failed +tr2-2 59 minutes ago 1 minute Failed +tr1-1 1 hour ago 1 minute Succeeded +tr2-1 1 hour ago --- Running diff --git a/pkg/cmd/taskrun/testdata/TestListTaskRuns-by_Task_name.golden b/pkg/cmd/taskrun/testdata/TestListTaskRuns-by_Task_name.golden new file mode 100644 index 000000000..7c34e3620 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestListTaskRuns-by_Task_name.golden @@ -0,0 +1,2 @@ +NAME STARTED DURATION STATUS +tr1-1 1 hour ago 1 minute Succeeded diff --git a/pkg/cmd/taskrun/testdata/TestListTaskRuns-empty_list.golden b/pkg/cmd/taskrun/testdata/TestListTaskRuns-empty_list.golden new file mode 100644 index 000000000..b2a7d9a17 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestListTaskRuns-empty_list.golden @@ -0,0 +1 @@ +No TaskRuns found diff --git a/pkg/cmd/taskrun/testdata/TestListTaskRuns-error_from_invalid_namespace.golden b/pkg/cmd/taskrun/testdata/TestListTaskRuns-error_from_invalid_namespace.golden new file mode 100644 index 000000000..1ca4dafb1 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestListTaskRuns-error_from_invalid_namespace.golden @@ -0,0 +1 @@ +Error: namespaces "invalid" not found diff --git a/pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_greater_than_maximum_case.golden b/pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_greater_than_maximum_case.golden new file mode 100644 index 000000000..ee226a3c0 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_greater_than_maximum_case.golden @@ -0,0 +1,6 @@ +NAME STARTED DURATION STATUS +tr0-1 --- --- Succeeded +tr3-1 --- --- Failed +tr2-2 59 minutes ago 1 minute Failed +tr1-1 1 hour ago 1 minute Succeeded +tr2-1 1 hour ago --- Running diff --git a/pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_negative_case.golden b/pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_negative_case.golden new file mode 100644 index 000000000..e69de29bb diff --git a/pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_returned_to_1.golden b/pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_returned_to_1.golden new file mode 100644 index 000000000..81555e3e7 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_returned_to_1.golden @@ -0,0 +1,2 @@ +NAME STARTED DURATION STATUS +tr0-1 --- --- Succeeded diff --git a/pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_with_output_flag_set.golden b/pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_with_output_flag_set.golden new file mode 100644 index 000000000..3ccec5860 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestListTaskRuns-limit_taskruns_with_output_flag_set.golden @@ -0,0 +1,2 @@ +tr0-1 +tr3-1 diff --git a/pkg/cmd/taskrun/testdata/TestListTaskRuns-print_by_template.golden b/pkg/cmd/taskrun/testdata/TestListTaskRuns-print_by_template.golden new file mode 100644 index 000000000..6f4e57dbc --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestListTaskRuns-print_by_template.golden @@ -0,0 +1,5 @@ +tr0-1 +tr3-1 +tr2-2 +tr1-1 +tr2-1 diff --git a/pkg/cmd/taskrun/testdata/TestListTaskRuns_no_condition.golden b/pkg/cmd/taskrun/testdata/TestListTaskRuns_no_condition.golden new file mode 100644 index 000000000..8d8c3cf78 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestListTaskRuns_no_condition.golden @@ -0,0 +1,2 @@ +NAME STARTED DURATION STATUS +tr1-1 1 hour ago 1 minute --- diff --git a/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_cancel_taskrun.golden b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_cancel_taskrun.golden new file mode 100644 index 000000000..a7946e625 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_cancel_taskrun.golden @@ -0,0 +1,27 @@ +Name: tr-1 +Namespace: ns + +Status + +STARTED DURATION STATUS +8 minutes ago 3 minutes Cancelled(TaskRunCancelled) + +Message + +TaskRun "tr-1" was cancelled + +Input Resources + +No resources + +Output Resources + +No resources + +Params + +No params + +Steps + +No steps diff --git a/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_empty_taskrun.golden b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_empty_taskrun.golden new file mode 100644 index 000000000..3c1147408 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_empty_taskrun.golden @@ -0,0 +1,24 @@ +Name: tr-1 +Namespace: ns +Task Ref: t1 + +Status + +STARTED DURATION STATUS +--- --- Succeeded + +Input Resources + +No resources + +Output Resources + +No resources + +Params + +No params + +Steps + +No steps diff --git a/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_failed.golden b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_failed.golden new file mode 100644 index 000000000..2cc327b02 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_failed.golden @@ -0,0 +1,28 @@ +Name: tr-1 +Namespace: ns +Task Ref: t1 + +Status + +STARTED DURATION STATUS +8 minutes ago 3 minutes Failed + +Message + +Testing tr failed + +Input Resources + +No resources + +Output Resources + +No resources + +Params + +No params + +Steps + +No steps diff --git a/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_no_resourceref.golden b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_no_resourceref.golden new file mode 100644 index 000000000..c5e252f57 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_no_resourceref.golden @@ -0,0 +1,32 @@ +Name: tr-1 +Namespace: ns +Task Ref: t1 + +Status + +STARTED DURATION STATUS +9 minutes ago --- Succeeded + +Input Resources + + NAME RESOURCE REF + git + image-input image + +Output Resources + + NAME RESOURCE REF + image-output + image-output2 + +Params + + NAME VALUE + input param + input2 param2 + +Steps + + NAME STATUS + step1 Completed + step2 Completed diff --git a/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_no_taskref.golden b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_no_taskref.golden new file mode 100644 index 000000000..f1e2136ed --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_no_taskref.golden @@ -0,0 +1,27 @@ +Name: tr-1 +Namespace: ns + +Status + +STARTED DURATION STATUS +8 minutes ago 3 minutes Failed + +Message + +Testing tr failed + +Input Resources + +No resources + +Output Resources + +No resources + +Params + +No params + +Steps + +No steps diff --git a/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_only_taskrun.golden b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_only_taskrun.golden new file mode 100644 index 000000000..159cdcedf --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_only_taskrun.golden @@ -0,0 +1,32 @@ +Name: tr-1 +Namespace: ns +Task Ref: t1 + +Status + +STARTED DURATION STATUS +9 minutes ago --- Succeeded + +Input Resources + + NAME RESOURCE REF + git git + image-input image + +Output Resources + + NAME RESOURCE REF + image-output image + image-output2 image + +Params + + NAME VALUE + input param + input2 param2 + +Steps + + NAME STATUS + step1 Completed + step2 Completed diff --git a/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_step_status_default.golden b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_step_status_default.golden new file mode 100644 index 000000000..1bf06776f --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_step_status_default.golden @@ -0,0 +1,32 @@ +Name: tr-1 +Namespace: ns +Task Ref: t1 + +Status + +STARTED DURATION STATUS +9 minutes ago --- Failed + +Input Resources + + NAME RESOURCE REF + git + image-input image + +Output Resources + + NAME RESOURCE REF + image-output + image-output2 + +Params + + NAME VALUE + input param + input2 param2 + +Steps + + NAME STATUS + step1 Error + step2 --- diff --git a/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_step_status_pending.golden b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_step_status_pending.golden new file mode 100644 index 000000000..605134497 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_step_status_pending.golden @@ -0,0 +1,32 @@ +Name: tr-1 +Namespace: ns +Task Ref: t1 + +Status + +STARTED DURATION STATUS +9 minutes ago --- Running + +Input Resources + + NAME RESOURCE REF + git + image-input image + +Output Resources + + NAME RESOURCE REF + image-output + image-output2 + +Params + + NAME VALUE + input param + input2 param2 + +Steps + + NAME STATUS + step1 PodInitializing + step2 PodInitializing diff --git a/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_step_status_running.golden b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_step_status_running.golden new file mode 100644 index 000000000..d383d7626 --- /dev/null +++ b/pkg/cmd/taskrun/testdata/TestTaskRunDescribe_step_status_running.golden @@ -0,0 +1,32 @@ +Name: tr-1 +Namespace: ns +Task Ref: t1 + +Status + +STARTED DURATION STATUS +9 minutes ago --- Running + +Input Resources + + NAME RESOURCE REF + git + image-input image + +Output Resources + + NAME RESOURCE REF + image-output + image-output2 + +Params + + NAME VALUE + input param + input2 param2 + +Steps + + NAME STATUS + step1 Running + step2 Running From fe1a5e5ce0c493afa76ffff339b538e15c47d3ea Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 17 Jan 2020 12:21:46 +0100 Subject: [PATCH 03/11] =?UTF-8?q?Use=20golden=20package=20for=20unit=20tes?= =?UTF-8?q?t=20for=20Pipeline=20=F0=9F=90=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allow to remove the potential long expected output from the test code. See gotest.tools/v3/golden for documentation. Signed-off-by: Vincent Demeester --- pkg/cmd/pipeline/describe_test.go | 117 ++---------------- pkg/cmd/pipeline/list_test.go | 45 +------ .../TestPipelineList_only_pipelines.golden | 4 + .../TestPipelinesDescribe_empty.golden | 18 +++ ...th_multiple_resource_param_task_run.golden | 29 +++++ ...scribe_with_resource_param_task_run.golden | 22 ++++ .../TestPipelinesDescribe_with_run.golden | 19 +++ ...TestPipelinesDescribe_with_task_run.golden | 20 +++ .../TestPipelinesList_latest_run.golden | 2 + .../TestPipelinesList_with_single_run.golden | 2 + 10 files changed, 129 insertions(+), 149 deletions(-) create mode 100644 pkg/cmd/pipeline/testdata/TestPipelineList_only_pipelines.golden create mode 100644 pkg/cmd/pipeline/testdata/TestPipelinesDescribe_empty.golden create mode 100644 pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_multiple_resource_param_task_run.golden create mode 100644 pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_resource_param_task_run.golden create mode 100644 pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_run.golden create mode 100644 pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_task_run.golden create mode 100644 pkg/cmd/pipeline/testdata/TestPipelinesList_latest_run.golden create mode 100644 pkg/cmd/pipeline/testdata/TestPipelinesList_with_single_run.golden diff --git a/pkg/cmd/pipeline/describe_test.go b/pkg/cmd/pipeline/describe_test.go index 640c7cda3..3efed1026 100644 --- a/pkg/cmd/pipeline/describe_test.go +++ b/pkg/cmd/pipeline/describe_test.go @@ -15,11 +15,10 @@ package pipeline import ( - "strings" + "fmt" "testing" "time" - "github.com/google/go-cmp/cmp" "github.com/jonboulle/clockwork" "github.com/tektoncd/cli/pkg/test" cb "github.com/tektoncd/cli/pkg/test/builder" @@ -27,6 +26,7 @@ import ( "github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources" pipelinetest "github.com/tektoncd/pipeline/test" tb "github.com/tektoncd/pipeline/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" @@ -92,24 +92,7 @@ func TestPipelinesDescribe_empty(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - - expected := []string{ - "Name: pipeline", - "Namespace: ns\n", - "Resources\n", - " No resources\n", - "Params\n", - " No params\n", - "Tasks\n", - " No tasks\n", - "PipelineRuns\n", - " No pipelineruns\n", - } - - text := strings.Join(expected, "\n") - if d := cmp.Diff(text, got); d != "" { - t.Errorf("Unexpected output mismatch: \n%s\n", d) - } + golden.Assert(t, got, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelinesDescribe_with_run(t *testing.T) { @@ -162,25 +145,7 @@ func TestPipelinesDescribe_with_run(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - - expected := []string{ - "Name: pipeline", - "Namespace: ns\n", - "Resources\n", - " No resources\n", - "Params\n", - " No params\n", - "Tasks\n", - " No tasks\n", - "PipelineRuns\n", - " NAME STARTED DURATION STATUS", - " pipeline-run-1 15 minutes ago 10 minutes Succeeded\n", - } - - text := strings.Join(expected, "\n") - if d := cmp.Diff(text, got); d != "" { - t.Errorf("Unexpected output mismatch: \n%s\n", d) - } + golden.Assert(t, got, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelinesDescribe_with_task_run(t *testing.T) { @@ -237,26 +202,7 @@ func TestPipelinesDescribe_with_task_run(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - - expected := []string{ - "Name: pipeline", - "Namespace: ns\n", - "Resources\n", - " No resources\n", - "Params\n", - " No params\n", - "Tasks\n", - " NAME TASKREF RUNAFTER", - " task taskref one, two\n", - "PipelineRuns\n", - " NAME STARTED DURATION STATUS", - " pipeline-run-1 15 minutes ago 10 minutes Succeeded\n", - } - - text := strings.Join(expected, "\n") - if d := cmp.Diff(text, got); d != "" { - t.Errorf("Unexpected output mismatch: \n%s\n", d) - } + golden.Assert(t, got, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelinesDescribe_with_resource_param_task_run(t *testing.T) { @@ -316,28 +262,7 @@ func TestPipelinesDescribe_with_resource_param_task_run(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - - expected := []string{ - "Name: pipeline", - "Namespace: ns\n", - "Resources\n", - " NAME TYPE", - " name git\n", - "Params\n", - " NAME TYPE DEFAULT VALUE", - " pipeline-param string somethingdifferent\n", - "Tasks\n", - " NAME TASKREF RUNAFTER", - " task taskref one, two\n", - "PipelineRuns\n", - " NAME STARTED DURATION STATUS", - " pipeline-run-1 15 minutes ago 10 minutes Succeeded\n", - } - - text := strings.Join(expected, "\n") - if d := cmp.Diff(text, got); d != "" { - t.Errorf("Unexpected output mismatch: \n%s\n", d) - } + golden.Assert(t, got, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelinesDescribe_with_multiple_resource_param_task_run(t *testing.T) { @@ -404,33 +329,5 @@ func TestPipelinesDescribe_with_multiple_resource_param_task_run(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - - expected := []string{ - "Name: pipeline", - "Namespace: ns\n", - "Resources\n", - " NAME TYPE", - " code git", - " name git", - " repo git", - " artifact-image image", - " code-image image\n", - "Params\n", - " NAME TYPE DEFAULT VALUE", - " pipeline-param string somethingdifferent", - " rev-param array [booms booms booms]", - " pipeline-param2 string ", - " rev-param2 array \n", - "Tasks\n", - " NAME TASKREF RUNAFTER", - " task taskref one, two\n", - "PipelineRuns\n", - " NAME STARTED DURATION STATUS", - " pipeline-run-1 15 minutes ago 10 minutes Succeeded\n", - } - - text := strings.Join(expected, "\n") - if d := cmp.Diff(text, got); d != "" { - t.Errorf("Unexpected output mismatch: \n%s\n", d) - } + golden.Assert(t, got, fmt.Sprintf("%s.golden", t.Name())) } diff --git a/pkg/cmd/pipeline/list_test.go b/pkg/cmd/pipeline/list_test.go index d8b012dce..01ee1a889 100644 --- a/pkg/cmd/pipeline/list_test.go +++ b/pkg/cmd/pipeline/list_test.go @@ -15,22 +15,21 @@ package pipeline import ( - "strings" + "fmt" "testing" "time" - "github.com/google/go-cmp/cmp" "github.com/jonboulle/clockwork" "github.com/tektoncd/cli/pkg/test" cb "github.com/tektoncd/cli/pkg/test/builder" - "knative.dev/pkg/apis" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" "github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources" pipelinetest "github.com/tektoncd/pipeline/test" tb "github.com/tektoncd/pipeline/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "knative.dev/pkg/apis" ) func TestPipelinesList_invalid_namespace(t *testing.T) { @@ -102,19 +101,7 @@ func TestPipelineList_only_pipelines(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - - expected := []string{ - "NAME AGE LAST RUN STARTED DURATION STATUS", - "tomatoes 1 minute ago --- --- --- ---", - "mangoes 20 seconds ago --- --- --- ---", - "bananas 3 weeks ago --- --- --- ---", - "", - } - - text := strings.Join(expected, "\n") - if d := cmp.Diff(text, output); d != "" { - t.Errorf("Unexpected output mismatch: %s", d) - } + golden.Assert(t, output, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelinesList_with_single_run(t *testing.T) { @@ -167,17 +154,7 @@ func TestPipelinesList_with_single_run(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - - expected := []string{ - "NAME AGE LAST RUN STARTED DURATION STATUS", - "pipeline 20 minutes ago pipeline-run-1 15 minutes ago 10 minutes Succeeded", - "", - } - - text := strings.Join(expected, "\n") - if d := cmp.Diff(text, got); d != "" { - t.Errorf("Unexpected output mismatch: \n%s\n", d) - } + golden.Assert(t, got, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelinesList_latest_run(t *testing.T) { @@ -258,17 +235,7 @@ func TestPipelinesList_latest_run(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - - expected := []string{ - "NAME AGE LAST RUN STARTED DURATION STATUS", - "pipeline 35 minutes ago pipeline-run-2 18 minutes ago 5 minutes Succeeded", - "", - } - - text := strings.Join(expected, "\n") - if d := cmp.Diff(text, got); d != "" { - t.Errorf("Unexpected output mismatch: \n%s\n", d) - } + golden.Assert(t, got, fmt.Sprintf("%s.golden", t.Name())) } type pipelineDetails struct { diff --git a/pkg/cmd/pipeline/testdata/TestPipelineList_only_pipelines.golden b/pkg/cmd/pipeline/testdata/TestPipelineList_only_pipelines.golden new file mode 100644 index 000000000..55150acfc --- /dev/null +++ b/pkg/cmd/pipeline/testdata/TestPipelineList_only_pipelines.golden @@ -0,0 +1,4 @@ +NAME AGE LAST RUN STARTED DURATION STATUS +tomatoes 1 minute ago --- --- --- --- +mangoes 20 seconds ago --- --- --- --- +bananas 3 weeks ago --- --- --- --- diff --git a/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_empty.golden b/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_empty.golden new file mode 100644 index 000000000..aaf3bcb40 --- /dev/null +++ b/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_empty.golden @@ -0,0 +1,18 @@ +Name: pipeline +Namespace: ns + +Resources + + No resources + +Params + + No params + +Tasks + + No tasks + +PipelineRuns + + No pipelineruns diff --git a/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_multiple_resource_param_task_run.golden b/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_multiple_resource_param_task_run.golden new file mode 100644 index 000000000..94fd77f82 --- /dev/null +++ b/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_multiple_resource_param_task_run.golden @@ -0,0 +1,29 @@ +Name: pipeline +Namespace: ns + +Resources + + NAME TYPE + code git + name git + repo git + artifact-image image + code-image image + +Params + + NAME TYPE DEFAULT VALUE + pipeline-param string somethingdifferent + rev-param array [booms booms booms] + pipeline-param2 string + rev-param2 array + +Tasks + + NAME TASKREF RUNAFTER + task taskref one, two + +PipelineRuns + + NAME STARTED DURATION STATUS + pipeline-run-1 15 minutes ago 10 minutes Succeeded diff --git a/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_resource_param_task_run.golden b/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_resource_param_task_run.golden new file mode 100644 index 000000000..18737ccbe --- /dev/null +++ b/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_resource_param_task_run.golden @@ -0,0 +1,22 @@ +Name: pipeline +Namespace: ns + +Resources + + NAME TYPE + name git + +Params + + NAME TYPE DEFAULT VALUE + pipeline-param string somethingdifferent + +Tasks + + NAME TASKREF RUNAFTER + task taskref one, two + +PipelineRuns + + NAME STARTED DURATION STATUS + pipeline-run-1 15 minutes ago 10 minutes Succeeded diff --git a/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_run.golden b/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_run.golden new file mode 100644 index 000000000..f54f4dc71 --- /dev/null +++ b/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_run.golden @@ -0,0 +1,19 @@ +Name: pipeline +Namespace: ns + +Resources + + No resources + +Params + + No params + +Tasks + + No tasks + +PipelineRuns + + NAME STARTED DURATION STATUS + pipeline-run-1 15 minutes ago 10 minutes Succeeded diff --git a/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_task_run.golden b/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_task_run.golden new file mode 100644 index 000000000..ed281be8e --- /dev/null +++ b/pkg/cmd/pipeline/testdata/TestPipelinesDescribe_with_task_run.golden @@ -0,0 +1,20 @@ +Name: pipeline +Namespace: ns + +Resources + + No resources + +Params + + No params + +Tasks + + NAME TASKREF RUNAFTER + task taskref one, two + +PipelineRuns + + NAME STARTED DURATION STATUS + pipeline-run-1 15 minutes ago 10 minutes Succeeded diff --git a/pkg/cmd/pipeline/testdata/TestPipelinesList_latest_run.golden b/pkg/cmd/pipeline/testdata/TestPipelinesList_latest_run.golden new file mode 100644 index 000000000..77996d290 --- /dev/null +++ b/pkg/cmd/pipeline/testdata/TestPipelinesList_latest_run.golden @@ -0,0 +1,2 @@ +NAME AGE LAST RUN STARTED DURATION STATUS +pipeline 35 minutes ago pipeline-run-2 18 minutes ago 5 minutes Succeeded diff --git a/pkg/cmd/pipeline/testdata/TestPipelinesList_with_single_run.golden b/pkg/cmd/pipeline/testdata/TestPipelinesList_with_single_run.golden new file mode 100644 index 000000000..b1045413e --- /dev/null +++ b/pkg/cmd/pipeline/testdata/TestPipelinesList_with_single_run.golden @@ -0,0 +1,2 @@ +NAME AGE LAST RUN STARTED DURATION STATUS +pipeline 20 minutes ago pipeline-run-1 15 minutes ago 10 minutes Succeeded From 01dd43a7b4fc355540ee55aca7f476723894d263 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 17 Jan 2020 12:29:36 +0100 Subject: [PATCH 04/11] =?UTF-8?q?Use=20golden=20package=20for=20unit=20tes?= =?UTF-8?q?t=20for=20PipelineRun=20=F0=9F=90=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allow to remove the potential long expected output from the test code. See gotest.tools/v3/golden for documentation. Signed-off-by: Vincent Demeester --- pkg/cmd/pipelinerun/describe_test.go | 293 +----------------- pkg/cmd/pipelinerun/list_test.go | 49 +-- ...tListPipelineRuns-Invalid_namespace.golden | 1 + ...stListPipelineRuns-all_in_namespace.golden | 6 + ...stListPipelineRuns-by_pipeline_name.golden | 2 + .../TestListPipelineRuns-by_template.golden | 5 + ...elineruns_greater_than_maximum_case.golden | 6 + ...ns-limit_pipelineruns_negative_case.golden | 0 ...ns-limit_pipelineruns_returned_to_1.golden | 2 + ...t_pipelineruns_with_output_flag_set.golden | 2 + ...neRunDescribe_cancelled_pipelinerun.golden | 25 ++ .../TestPipelineRunDescribe_failed.golden | 26 ++ ...nDescribe_failed_withoutPRCondition.golden | 22 ++ ...nDescribe_failed_withoutTRCondition.golden | 26 ++ ...nDescribe_multiple_taskrun_ordering.golden | 22 ++ ...tPipelineRunDescribe_no_resourceref.golden | 24 ++ ...estPipelineRunDescribe_only_taskrun.golden | 21 ++ ...eRunDescribe_with_resources_taskrun.golden | 24 ++ ...lineRunDescribe_without_pipelineref.golden | 19 ++ ...elineRunDescribe_without_start_time.golden | 20 ++ ...neRunDescribe_without_tr_start_time.golden | 21 ++ 21 files changed, 289 insertions(+), 327 deletions(-) create mode 100644 pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-Invalid_namespace.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-all_in_namespace.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-by_pipeline_name.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-by_template.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_greater_than_maximum_case.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_negative_case.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_returned_to_1.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_with_output_flag_set.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_cancelled_pipelinerun.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_failed.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_failed_withoutPRCondition.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_failed_withoutTRCondition.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_multiple_taskrun_ordering.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_no_resourceref.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_only_taskrun.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_with_resources_taskrun.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_without_pipelineref.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_without_start_time.golden create mode 100644 pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_without_tr_start_time.golden diff --git a/pkg/cmd/pipelinerun/describe_test.go b/pkg/cmd/pipelinerun/describe_test.go index e63e649d5..61a5f235a 100644 --- a/pkg/cmd/pipelinerun/describe_test.go +++ b/pkg/cmd/pipelinerun/describe_test.go @@ -15,6 +15,7 @@ package pipelinerun import ( + "fmt" "testing" "time" @@ -25,6 +26,7 @@ import ( "github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources" pipelinetest "github.com/tektoncd/pipeline/test" tb "github.com/tektoncd/pipeline/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" @@ -125,30 +127,7 @@ func TestPipelineRunDescribe_only_taskrun(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: pipeline-run -Namespace: ns -Pipeline Ref: pipeline - -Status - -STARTED DURATION STATUS -10 minutes ago 5 minutes Succeeded - -Resources - - No resources - -Params - - No params - -Taskruns - - NAME TASK NAME STARTED DURATION STATUS - tr-1 t-1 8 minutes ago 3 minutes Succeeded -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelineRunDescribe_multiple_taskrun_ordering(t *testing.T) { @@ -217,30 +196,7 @@ func TestPipelineRunDescribe_multiple_taskrun_ordering(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: pipeline-run -Namespace: ns -Pipeline Ref: pipeline - -Status - -STARTED DURATION STATUS -10 minutes ago 15 minutes Succeeded - -Resources - - No resources - -Params - - No params - -Taskruns - - NAME TASK NAME STARTED DURATION STATUS - tr-2 t-2 5 minutes ago 4 minutes Succeeded - tr-1 t-1 8 minutes ago 3 minutes Succeeded -` - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } @@ -301,35 +257,7 @@ func TestPipelineRunDescribe_failed(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: pipeline-run -Namespace: ns -Pipeline Ref: pipeline -Service Account: test-sa - -Status - -STARTED DURATION STATUS -10 minutes ago 5 minutes Failed(Resource not found) - -Message - -Resource test-resource not found in the pipelinerun (Testing tr failed) - -Resources - - No resources - -Params - - No params - -Taskruns - - NAME TASK NAME STARTED DURATION STATUS - tr-1 t-1 8 minutes ago 3 minutes Failed -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelineRunDescribe_failed_withoutTRCondition(t *testing.T) { @@ -384,35 +312,7 @@ func TestPipelineRunDescribe_failed_withoutTRCondition(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: pipeline-run -Namespace: ns -Pipeline Ref: pipeline -Service Account: test-sa - -Status - -STARTED DURATION STATUS -10 minutes ago 5 minutes Failed(Resource not found) - -Message - -Resource test-resource not found in the pipelinerun - -Resources - - No resources - -Params - - No params - -Taskruns - - NAME TASK NAME STARTED DURATION STATUS - tr-1 t-1 8 minutes ago 3 minutes --- -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelineRunDescribe_failed_withoutPRCondition(t *testing.T) { @@ -462,31 +362,7 @@ func TestPipelineRunDescribe_failed_withoutPRCondition(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: pipeline-run -Namespace: ns -Pipeline Ref: pipeline -Service Account: test-sa - -Status - -STARTED DURATION STATUS -10 minutes ago 5 minutes --- - -Resources - - No resources - -Params - - No params - -Taskruns - - NAME TASK NAME STARTED DURATION STATUS - tr-1 t-1 8 minutes ago 3 minutes --- -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelineRunDescribe_with_resources_taskrun(t *testing.T) { @@ -548,33 +424,7 @@ func TestPipelineRunDescribe_with_resources_taskrun(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: pipeline-run -Namespace: ns -Pipeline Ref: pipeline -Service Account: test-sa - -Status - -STARTED DURATION STATUS -10 minutes ago 5 minutes Succeeded - -Resources - - NAME RESOURCE REF - test-resource test-resource-ref - -Params - - NAME VALUE - test-param param-value - -Taskruns - - NAME TASK NAME STARTED DURATION STATUS - tr-1 t-1 8 minutes ago 3 minutes Succeeded -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelineRunDescribe_without_start_time(t *testing.T) { @@ -606,29 +456,7 @@ func TestPipelineRunDescribe_without_start_time(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: pipeline-run -Namespace: ns -Pipeline Ref: pipeline - -Status - -STARTED DURATION STATUS ---- --- --- - -Resources - - No resources - -Params - - No params - -Taskruns - - No taskruns -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelineRunDescribe_without_pipelineref(t *testing.T) { @@ -659,27 +487,7 @@ func TestPipelineRunDescribe_without_pipelineref(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: pipeline-run -Namespace: ns - -Status - -STARTED DURATION STATUS ---- --- --- - -Resources - - No resources - -Params - - No params - -Taskruns - - No taskruns -` - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelineRunDescribe_no_resourceref(t *testing.T) { @@ -739,33 +547,7 @@ func TestPipelineRunDescribe_no_resourceref(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: pipeline-run -Namespace: ns -Pipeline Ref: pipeline -Service Account: test-sa - -Status - -STARTED DURATION STATUS -10 minutes ago 5 minutes Succeeded - -Resources - - NAME RESOURCE REF - test-resource - -Params - - NAME VALUE - test-param param-value - -Taskruns - - NAME TASK NAME STARTED DURATION STATUS - tr-1 t-1 8 minutes ago 3 minutes Succeeded -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelineRunDescribe_cancelled_pipelinerun(t *testing.T) { @@ -822,34 +604,7 @@ func TestPipelineRunDescribe_cancelled_pipelinerun(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: pipeline-run -Namespace: ns -Pipeline Ref: pipeline - -Status - -STARTED DURATION STATUS -10 minutes ago 5 minutes Cancelled(PipelineRunCancelled) - -Message - -PipelineRun "pipeline-run" was cancelled - -Resources - - No resources - -Params - - No params - -Taskruns - - NAME TASK NAME STARTED DURATION STATUS - tr-1 t-1 8 minutes ago 3 minutes Succeeded -` - - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelineRunDescribe_without_tr_start_time(t *testing.T) { @@ -903,27 +658,5 @@ func TestPipelineRunDescribe_without_tr_start_time(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - expected := `Name: pipeline-run -Namespace: ns -Pipeline Ref: pipeline - -Status - -STARTED DURATION STATUS -10 minutes ago --- Running - -Resources - - No resources - -Params - - No params - -Taskruns - - NAME TASK NAME STARTED DURATION STATUS - tr-1 t-1 --- --- Running -` - test.AssertOutput(t, expected, actual) + golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } diff --git a/pkg/cmd/pipelinerun/list_test.go b/pkg/cmd/pipelinerun/list_test.go index d560c65c1..094b0d9c6 100644 --- a/pkg/cmd/pipelinerun/list_test.go +++ b/pkg/cmd/pipelinerun/list_test.go @@ -28,6 +28,7 @@ import ( "github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun/resources" pipelinetest "github.com/tektoncd/pipeline/test" tb "github.com/tektoncd/pipeline/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" @@ -97,100 +98,54 @@ func TestListPipelineRuns(t *testing.T) { command *cobra.Command args []string wantError bool - expected []string }{ { name: "Invalid namespace", command: command(t, prs, clock.Now(), ns), args: []string{"list", "-n", "invalid"}, wantError: true, - expected: []string{"Error: namespaces \"invalid\" not found\n"}, }, { name: "by pipeline name", command: command(t, prs, clock.Now(), ns), args: []string{"list", "pipeline", "-n", "namespace"}, wantError: false, - expected: []string{ - "NAME STARTED DURATION STATUS ", - "pr1-1 59 minutes ago 1 minute Succeeded ", - "", - }, }, { name: "all in namespace", command: command(t, prs, clock.Now(), ns), args: []string{"list", "-n", "namespace"}, wantError: false, - expected: []string{ - "NAME STARTED DURATION STATUS ", - "pr0-1 --- --- --- ", - "pr3-1 --- --- --- ", - "pr1-1 59 minutes ago 1 minute Succeeded ", - "pr2-2 2 hours ago 1 minute Failed ", - "pr2-1 3 hours ago --- Succeeded(Running) ", - "", - }, }, { name: "by template", command: command(t, prs, clock.Now(), ns), args: []string{"list", "-n", "namespace", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"}, wantError: false, - expected: []string{ - "pr0-1", - "pr3-1", - "pr1-1", - "pr2-2", - "pr2-1", - "", - }, }, { name: "limit pipelineruns returned to 1", command: command(t, prs, clock.Now(), ns), args: []string{"list", "-n", "namespace", "--limit", fmt.Sprintf("%d", 1)}, wantError: false, - expected: []string{ - "NAME STARTED DURATION STATUS ", - "pr0-1 --- --- --- ", - "", - }, }, { name: "limit pipelineruns negative case", command: command(t, prs, clock.Now(), ns), args: []string{"list", "-n", "namespace", "--limit", fmt.Sprintf("%d", -1)}, wantError: false, - expected: []string{ - "", - }, }, { name: "limit pipelineruns greater than maximum case", command: command(t, prs, clock.Now(), ns), args: []string{"list", "-n", "namespace", "--limit", fmt.Sprintf("%d", 7)}, wantError: false, - expected: []string{ - "NAME STARTED DURATION STATUS ", - "pr0-1 --- --- --- ", - "pr3-1 --- --- --- ", - "pr1-1 59 minutes ago 1 minute Succeeded ", - "pr2-2 2 hours ago 1 minute Failed ", - "pr2-1 3 hours ago --- Succeeded(Running) ", - "", - }, }, { name: "limit pipelineruns with output flag set", command: command(t, prs, clock.Now(), ns), args: []string{"list", "-n", "namespace", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}", "--limit", fmt.Sprintf("%d", 2)}, wantError: false, - expected: []string{ - "pr0-1", - "pr3-1", - "", - }, }, } @@ -201,7 +156,7 @@ func TestListPipelineRuns(t *testing.T) { if !td.wantError && err != nil { t.Errorf("Unexpected error: %v", err) } - test.AssertOutput(t, strings.Join(td.expected, "\n"), got) + golden.Assert(t, got, strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-")) }) } } diff --git a/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-Invalid_namespace.golden b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-Invalid_namespace.golden new file mode 100644 index 000000000..1ca4dafb1 --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-Invalid_namespace.golden @@ -0,0 +1 @@ +Error: namespaces "invalid" not found diff --git a/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-all_in_namespace.golden b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-all_in_namespace.golden new file mode 100644 index 000000000..c37b7030d --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-all_in_namespace.golden @@ -0,0 +1,6 @@ +NAME STARTED DURATION STATUS +pr0-1 --- --- --- +pr3-1 --- --- --- +pr1-1 59 minutes ago 1 minute Succeeded +pr2-2 2 hours ago 1 minute Failed +pr2-1 3 hours ago --- Succeeded(Running) diff --git a/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-by_pipeline_name.golden b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-by_pipeline_name.golden new file mode 100644 index 000000000..76796290c --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-by_pipeline_name.golden @@ -0,0 +1,2 @@ +NAME STARTED DURATION STATUS +pr1-1 59 minutes ago 1 minute Succeeded diff --git a/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-by_template.golden b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-by_template.golden new file mode 100644 index 000000000..4ab8893ec --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-by_template.golden @@ -0,0 +1,5 @@ +pr0-1 +pr3-1 +pr1-1 +pr2-2 +pr2-1 diff --git a/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_greater_than_maximum_case.golden b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_greater_than_maximum_case.golden new file mode 100644 index 000000000..c37b7030d --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_greater_than_maximum_case.golden @@ -0,0 +1,6 @@ +NAME STARTED DURATION STATUS +pr0-1 --- --- --- +pr3-1 --- --- --- +pr1-1 59 minutes ago 1 minute Succeeded +pr2-2 2 hours ago 1 minute Failed +pr2-1 3 hours ago --- Succeeded(Running) diff --git a/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_negative_case.golden b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_negative_case.golden new file mode 100644 index 000000000..e69de29bb diff --git a/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_returned_to_1.golden b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_returned_to_1.golden new file mode 100644 index 000000000..53a5d11a0 --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_returned_to_1.golden @@ -0,0 +1,2 @@ +NAME STARTED DURATION STATUS +pr0-1 --- --- --- diff --git a/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_with_output_flag_set.golden b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_with_output_flag_set.golden new file mode 100644 index 000000000..269e2c94e --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestListPipelineRuns-limit_pipelineruns_with_output_flag_set.golden @@ -0,0 +1,2 @@ +pr0-1 +pr3-1 diff --git a/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_cancelled_pipelinerun.golden b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_cancelled_pipelinerun.golden new file mode 100644 index 000000000..3c9b6559a --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_cancelled_pipelinerun.golden @@ -0,0 +1,25 @@ +Name: pipeline-run +Namespace: ns +Pipeline Ref: pipeline + +Status + +STARTED DURATION STATUS +10 minutes ago 5 minutes Cancelled(PipelineRunCancelled) + +Message + +PipelineRun "pipeline-run" was cancelled + +Resources + + No resources + +Params + + No params + +Taskruns + + NAME TASK NAME STARTED DURATION STATUS + tr-1 t-1 8 minutes ago 3 minutes Succeeded diff --git a/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_failed.golden b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_failed.golden new file mode 100644 index 000000000..254afc658 --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_failed.golden @@ -0,0 +1,26 @@ +Name: pipeline-run +Namespace: ns +Pipeline Ref: pipeline +Service Account: test-sa + +Status + +STARTED DURATION STATUS +10 minutes ago 5 minutes Failed(Resource not found) + +Message + +Resource test-resource not found in the pipelinerun (Testing tr failed) + +Resources + + No resources + +Params + + No params + +Taskruns + + NAME TASK NAME STARTED DURATION STATUS + tr-1 t-1 8 minutes ago 3 minutes Failed diff --git a/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_failed_withoutPRCondition.golden b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_failed_withoutPRCondition.golden new file mode 100644 index 000000000..2a37aa67d --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_failed_withoutPRCondition.golden @@ -0,0 +1,22 @@ +Name: pipeline-run +Namespace: ns +Pipeline Ref: pipeline +Service Account: test-sa + +Status + +STARTED DURATION STATUS +10 minutes ago 5 minutes --- + +Resources + + No resources + +Params + + No params + +Taskruns + + NAME TASK NAME STARTED DURATION STATUS + tr-1 t-1 8 minutes ago 3 minutes --- diff --git a/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_failed_withoutTRCondition.golden b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_failed_withoutTRCondition.golden new file mode 100644 index 000000000..e4ee1d4a7 --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_failed_withoutTRCondition.golden @@ -0,0 +1,26 @@ +Name: pipeline-run +Namespace: ns +Pipeline Ref: pipeline +Service Account: test-sa + +Status + +STARTED DURATION STATUS +10 minutes ago 5 minutes Failed(Resource not found) + +Message + +Resource test-resource not found in the pipelinerun + +Resources + + No resources + +Params + + No params + +Taskruns + + NAME TASK NAME STARTED DURATION STATUS + tr-1 t-1 8 minutes ago 3 minutes --- diff --git a/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_multiple_taskrun_ordering.golden b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_multiple_taskrun_ordering.golden new file mode 100644 index 000000000..4daf1ea04 --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_multiple_taskrun_ordering.golden @@ -0,0 +1,22 @@ +Name: pipeline-run +Namespace: ns +Pipeline Ref: pipeline + +Status + +STARTED DURATION STATUS +10 minutes ago 15 minutes Succeeded + +Resources + + No resources + +Params + + No params + +Taskruns + + NAME TASK NAME STARTED DURATION STATUS + tr-2 t-2 5 minutes ago 4 minutes Succeeded + tr-1 t-1 8 minutes ago 3 minutes Succeeded diff --git a/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_no_resourceref.golden b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_no_resourceref.golden new file mode 100644 index 000000000..11da0c43b --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_no_resourceref.golden @@ -0,0 +1,24 @@ +Name: pipeline-run +Namespace: ns +Pipeline Ref: pipeline +Service Account: test-sa + +Status + +STARTED DURATION STATUS +10 minutes ago 5 minutes Succeeded + +Resources + + NAME RESOURCE REF + test-resource + +Params + + NAME VALUE + test-param param-value + +Taskruns + + NAME TASK NAME STARTED DURATION STATUS + tr-1 t-1 8 minutes ago 3 minutes Succeeded diff --git a/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_only_taskrun.golden b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_only_taskrun.golden new file mode 100644 index 000000000..7ad939b53 --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_only_taskrun.golden @@ -0,0 +1,21 @@ +Name: pipeline-run +Namespace: ns +Pipeline Ref: pipeline + +Status + +STARTED DURATION STATUS +10 minutes ago 5 minutes Succeeded + +Resources + + No resources + +Params + + No params + +Taskruns + + NAME TASK NAME STARTED DURATION STATUS + tr-1 t-1 8 minutes ago 3 minutes Succeeded diff --git a/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_with_resources_taskrun.golden b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_with_resources_taskrun.golden new file mode 100644 index 000000000..fe1d2ec25 --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_with_resources_taskrun.golden @@ -0,0 +1,24 @@ +Name: pipeline-run +Namespace: ns +Pipeline Ref: pipeline +Service Account: test-sa + +Status + +STARTED DURATION STATUS +10 minutes ago 5 minutes Succeeded + +Resources + + NAME RESOURCE REF + test-resource test-resource-ref + +Params + + NAME VALUE + test-param param-value + +Taskruns + + NAME TASK NAME STARTED DURATION STATUS + tr-1 t-1 8 minutes ago 3 minutes Succeeded diff --git a/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_without_pipelineref.golden b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_without_pipelineref.golden new file mode 100644 index 000000000..6a9fb5bb4 --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_without_pipelineref.golden @@ -0,0 +1,19 @@ +Name: pipeline-run +Namespace: ns + +Status + +STARTED DURATION STATUS +--- --- --- + +Resources + + No resources + +Params + + No params + +Taskruns + + No taskruns diff --git a/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_without_start_time.golden b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_without_start_time.golden new file mode 100644 index 000000000..b45b09a7f --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_without_start_time.golden @@ -0,0 +1,20 @@ +Name: pipeline-run +Namespace: ns +Pipeline Ref: pipeline + +Status + +STARTED DURATION STATUS +--- --- --- + +Resources + + No resources + +Params + + No params + +Taskruns + + No taskruns diff --git a/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_without_tr_start_time.golden b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_without_tr_start_time.golden new file mode 100644 index 000000000..d8621f6fc --- /dev/null +++ b/pkg/cmd/pipelinerun/testdata/TestPipelineRunDescribe_without_tr_start_time.golden @@ -0,0 +1,21 @@ +Name: pipeline-run +Namespace: ns +Pipeline Ref: pipeline + +Status + +STARTED DURATION STATUS +10 minutes ago --- Running + +Resources + + No resources + +Params + + No params + +Taskruns + + NAME TASK NAME STARTED DURATION STATUS + tr-1 t-1 --- --- Running From 328209680421e63dc94523692824190ad64e2a21 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 17 Jan 2020 12:46:26 +0100 Subject: [PATCH 05/11] =?UTF-8?q?Use=20golden=20package=20for=20unit=20tes?= =?UTF-8?q?t=20for=20Condition=20=F0=9F=90=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allow to remove the potential long expected output from the test code. See gotest.tools/v3/golden for documentation. Signed-off-by: Vincent Demeester --- pkg/cmd/condition/list_test.go | 50 ++----------------- ...-Failed_to_list_condition_resources.golden | 2 + ..._resources_with_specify_output_flag.golden | 2 + .../TestConditionList-Found_conditions.golden | 4 ++ ...stConditionList-Found_no_conditions.golden | 1 + ...TestConditionList-Invalid_Namespace.golden | 1 + ...stConditionList-Specify_output_flag.golden | 28 +++++++++++ 7 files changed, 41 insertions(+), 47 deletions(-) create mode 100644 pkg/cmd/condition/testdata/TestConditionList-Failed_to_list_condition_resources.golden create mode 100644 pkg/cmd/condition/testdata/TestConditionList-Failed_to_list_condition_resources_with_specify_output_flag.golden create mode 100644 pkg/cmd/condition/testdata/TestConditionList-Found_conditions.golden create mode 100644 pkg/cmd/condition/testdata/TestConditionList-Found_no_conditions.golden create mode 100644 pkg/cmd/condition/testdata/TestConditionList-Invalid_Namespace.golden create mode 100644 pkg/cmd/condition/testdata/TestConditionList-Specify_output_flag.golden diff --git a/pkg/cmd/condition/list_test.go b/pkg/cmd/condition/list_test.go index dafe188a4..927d08b0b 100644 --- a/pkg/cmd/condition/list_test.go +++ b/pkg/cmd/condition/list_test.go @@ -16,6 +16,7 @@ package condition import ( "errors" + "fmt" "strings" "testing" "time" @@ -26,6 +27,7 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" pipelinetest "github.com/tektoncd/pipeline/test" tb "github.com/tektoncd/pipeline/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -75,85 +77,42 @@ func TestConditionList(t *testing.T) { command []string input pipelinetest.Clients wantError bool - want []string }{ { name: "Invalid Namespace", command: []string{"ls", "-n", "invalid"}, input: seeds[0], wantError: true, - want: []string{"namespaces \"invalid\" not found"}, }, { name: "Found no conditions", command: []string{"ls", "-n", "empty"}, input: seeds[0], wantError: false, - want: []string{"No conditions found", ""}, }, { name: "Found conditions", command: []string{"ls", "-n", "ns"}, input: seeds[0], wantError: false, - want: []string{ - "NAME AGE", - "condition1 1 minute ago", - "condition2 20 seconds ago", - "condition3 3 weeks ago", - "", - }, }, { name: "Specify output flag", command: []string{"ls", "-n", "ns", "--output", "yaml"}, input: seeds[0], wantError: false, - want: []string{ - "apiVersion: tekton.dev/v1alpha1", - "items:", - "- metadata:", - " creationTimestamp: \"1984-04-03T23:59:00Z\"", - " name: condition1", - " namespace: ns", - " spec:", - " check:", - " name: \"\"", - " resources: {}", - "- metadata:", - " creationTimestamp: \"1984-04-03T23:59:40Z\"", - " name: condition2", - " namespace: ns", - " spec:", - " check:", - " name: \"\"", - " resources: {}", - "- metadata:", - " creationTimestamp: \"1984-03-13T16:00:00Z\"", - " name: condition3", - " namespace: ns", - " spec:", - " check:", - " name: \"\"", - " resources: {}", - "kind: ConditionList", - "metadata: {}", - "", - }, }, { name: "Failed to list condition resources", command: []string{"ls", "-n", "ns"}, input: seeds[1], wantError: true, - want: []string{"test error"}, }, { name: "Failed to list condition resources with specify output flag", command: []string{"ls", "-n", "ns", "--output", "yaml"}, input: seeds[1], wantError: true, - want: []string{"test error"}, }, } @@ -162,20 +121,17 @@ func TestConditionList(t *testing.T) { p := &test.Params{Tekton: tp.input.Pipeline, Kube: tp.input.Kube} pipelineResource := Command(p) - want := strings.Join(tp.want, "\n") - out, err := test.ExecuteCommand(pipelineResource, tp.command...) if tp.wantError { if err == nil { t.Errorf("Error expected here") } - test.AssertOutput(t, want, err.Error()) } else { if err != nil { t.Errorf("Unexpected Error") } - test.AssertOutput(t, want, out) } + golden.Assert(t, out, strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-")) }) } } diff --git a/pkg/cmd/condition/testdata/TestConditionList-Failed_to_list_condition_resources.golden b/pkg/cmd/condition/testdata/TestConditionList-Failed_to_list_condition_resources.golden new file mode 100644 index 000000000..e51bc920e --- /dev/null +++ b/pkg/cmd/condition/testdata/TestConditionList-Failed_to_list_condition_resources.golden @@ -0,0 +1,2 @@ +Failed to list conditions from ns namespace +Error: test error diff --git a/pkg/cmd/condition/testdata/TestConditionList-Failed_to_list_condition_resources_with_specify_output_flag.golden b/pkg/cmd/condition/testdata/TestConditionList-Failed_to_list_condition_resources_with_specify_output_flag.golden new file mode 100644 index 000000000..e51bc920e --- /dev/null +++ b/pkg/cmd/condition/testdata/TestConditionList-Failed_to_list_condition_resources_with_specify_output_flag.golden @@ -0,0 +1,2 @@ +Failed to list conditions from ns namespace +Error: test error diff --git a/pkg/cmd/condition/testdata/TestConditionList-Found_conditions.golden b/pkg/cmd/condition/testdata/TestConditionList-Found_conditions.golden new file mode 100644 index 000000000..d868c6c78 --- /dev/null +++ b/pkg/cmd/condition/testdata/TestConditionList-Found_conditions.golden @@ -0,0 +1,4 @@ +NAME AGE +condition1 1 minute ago +condition2 20 seconds ago +condition3 3 weeks ago diff --git a/pkg/cmd/condition/testdata/TestConditionList-Found_no_conditions.golden b/pkg/cmd/condition/testdata/TestConditionList-Found_no_conditions.golden new file mode 100644 index 000000000..d4af05f35 --- /dev/null +++ b/pkg/cmd/condition/testdata/TestConditionList-Found_no_conditions.golden @@ -0,0 +1 @@ +No conditions found diff --git a/pkg/cmd/condition/testdata/TestConditionList-Invalid_Namespace.golden b/pkg/cmd/condition/testdata/TestConditionList-Invalid_Namespace.golden new file mode 100644 index 000000000..1ca4dafb1 --- /dev/null +++ b/pkg/cmd/condition/testdata/TestConditionList-Invalid_Namespace.golden @@ -0,0 +1 @@ +Error: namespaces "invalid" not found diff --git a/pkg/cmd/condition/testdata/TestConditionList-Specify_output_flag.golden b/pkg/cmd/condition/testdata/TestConditionList-Specify_output_flag.golden new file mode 100644 index 000000000..764177a1a --- /dev/null +++ b/pkg/cmd/condition/testdata/TestConditionList-Specify_output_flag.golden @@ -0,0 +1,28 @@ +apiVersion: tekton.dev/v1alpha1 +items: +- metadata: + creationTimestamp: "1984-04-03T23:59:00Z" + name: condition1 + namespace: ns + spec: + check: + name: "" + resources: {} +- metadata: + creationTimestamp: "1984-04-03T23:59:40Z" + name: condition2 + namespace: ns + spec: + check: + name: "" + resources: {} +- metadata: + creationTimestamp: "1984-03-13T16:00:00Z" + name: condition3 + namespace: ns + spec: + check: + name: "" + resources: {} +kind: ConditionList +metadata: {} From f27c267860897cda9c7acd1e4378e859b087a5f3 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 17 Jan 2020 12:50:42 +0100 Subject: [PATCH 06/11] =?UTF-8?q?Use=20golden=20package=20for=20unit=20tes?= =?UTF-8?q?t=20for=20ClusterTask=20=F0=9F=90=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allow to remove the potential long expected output from the test code. See gotest.tools/v3/golden for documentation. Signed-off-by: Vincent Demeester --- pkg/cmd/clustertask/list_test.go | 18 +++--------------- .../TestClusterTaskListOnlyClusterTasks.golden | 4 ++++ 2 files changed, 7 insertions(+), 15 deletions(-) create mode 100644 pkg/cmd/clustertask/testdata/TestClusterTaskListOnlyClusterTasks.golden diff --git a/pkg/cmd/clustertask/list_test.go b/pkg/cmd/clustertask/list_test.go index 952f5acbd..cd88be0e7 100644 --- a/pkg/cmd/clustertask/list_test.go +++ b/pkg/cmd/clustertask/list_test.go @@ -15,17 +15,17 @@ package clustertask import ( - "strings" + "fmt" "testing" "time" - "github.com/google/go-cmp/cmp" "github.com/jonboulle/clockwork" "github.com/tektoncd/cli/pkg/test" cb "github.com/tektoncd/cli/pkg/test/builder" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" pipelinetest "github.com/tektoncd/pipeline/test" tb "github.com/tektoncd/pipeline/test/builder" + "gotest.tools/v3/golden" ) func TestClusterTaskList_Empty(t *testing.T) { @@ -59,17 +59,5 @@ func TestClusterTaskListOnlyClusterTasks(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - - expected := []string{ - "NAME AGE", - "guavas 1 minute ago", - "avocados 20 seconds ago", - "pineapple 3 weeks ago", - "", - } - - text := strings.Join(expected, "\n") - if d := cmp.Diff(text, output); d != "" { - t.Errorf("Unexpected output mismatch: %s", d) - } + golden.Assert(t, output, fmt.Sprintf("%s.golden", t.Name())) } diff --git a/pkg/cmd/clustertask/testdata/TestClusterTaskListOnlyClusterTasks.golden b/pkg/cmd/clustertask/testdata/TestClusterTaskListOnlyClusterTasks.golden new file mode 100644 index 000000000..51c8aa37a --- /dev/null +++ b/pkg/cmd/clustertask/testdata/TestClusterTaskListOnlyClusterTasks.golden @@ -0,0 +1,4 @@ +NAME AGE +guavas 1 minute ago +avocados 20 seconds ago +pineapple 3 weeks ago From a6b37fe0b1afbb53d201afc299d689086f955e82 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 17 Jan 2020 12:57:42 +0100 Subject: [PATCH 07/11] =?UTF-8?q?Use=20golden=20package=20for=20unit=20tes?= =?UTF-8?q?t=20for=20TriggerBinding=20=F0=9F=90=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allow to remove the potential long expected output from the test code. See gotest.tools/v3/golden for documentation. Signed-off-by: Vincent Demeester --- pkg/cmd/triggerbinding/list_test.go | 50 ++++++------------- ...istTriggerBinding-Invalid_namespace.golden | 1 + ...ggerBinding-Multiple_TriggerBinding.golden | 5 ++ ...e_TriggerBinding_with_output_format.golden | 4 ++ ...istTriggerBinding-No_TriggerBinding.golden | 1 + 5 files changed, 26 insertions(+), 35 deletions(-) create mode 100644 pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-Invalid_namespace.golden create mode 100644 pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-Multiple_TriggerBinding.golden create mode 100644 pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-Multiple_TriggerBinding_with_output_format.golden create mode 100644 pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-No_TriggerBinding.golden diff --git a/pkg/cmd/triggerbinding/list_test.go b/pkg/cmd/triggerbinding/list_test.go index a04cccd5c..5a27ff625 100644 --- a/pkg/cmd/triggerbinding/list_test.go +++ b/pkg/cmd/triggerbinding/list_test.go @@ -15,6 +15,7 @@ package triggerbinding import ( + "fmt" "strings" "testing" "time" @@ -26,6 +27,7 @@ import ( "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" triggertest "github.com/tektoncd/triggers/test" tb "github.com/tektoncd/triggers/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -57,52 +59,30 @@ func TestListTriggerBinding(t *testing.T) { name string command *cobra.Command args []string - expected []string wantError bool }{ { - name: "Invalid namespace", - command: command(t, tbs, now, ns), - args: []string{"list", "-n", "default"}, - expected: []string{ - "Error: namespaces \"default\" not found\n", - }, + name: "Invalid namespace", + command: command(t, tbs, now, ns), + args: []string{"list", "-n", "default"}, wantError: true, }, { - name: "No TriggerBinding", - command: command(t, tbs, now, ns), - args: []string{"list", "-n", "random"}, - expected: []string{ - "No triggerbindings found\n", - }, + name: "No TriggerBinding", + command: command(t, tbs, now, ns), + args: []string{"list", "-n", "random"}, wantError: false, }, { - name: "Multiple TriggerBinding", - command: command(t, tbs, now, ns), - args: []string{"list", "-n", "foo"}, - expected: []string{ - "NAME AGE", - "tb1 2 minutes ago", - "tb2 30 seconds ago", - "tb3 1 week ago", - "tb4 ---", - "", - }, + name: "Multiple TriggerBinding", + command: command(t, tbs, now, ns), + args: []string{"list", "-n", "foo"}, wantError: false, }, { - name: "Multiple TriggerBinding with output format", - command: command(t, tbs, now, ns), - args: []string{"list", "-n", "foo", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"}, - expected: []string{ - "tb1", - "tb2", - "tb3", - "tb4", - "", - }, + name: "Multiple TriggerBinding with output format", + command: command(t, tbs, now, ns), + args: []string{"list", "-n", "foo", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"}, wantError: false, }, } @@ -114,7 +94,7 @@ func TestListTriggerBinding(t *testing.T) { if err != nil && !td.wantError { t.Errorf("Unexpected error: %v", err) } - test.AssertOutput(t, strings.Join(td.expected, "\n"), got) + golden.Assert(t, got, strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-")) }) } } diff --git a/pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-Invalid_namespace.golden b/pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-Invalid_namespace.golden new file mode 100644 index 000000000..93f0371f2 --- /dev/null +++ b/pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-Invalid_namespace.golden @@ -0,0 +1 @@ +Error: namespaces "default" not found diff --git a/pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-Multiple_TriggerBinding.golden b/pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-Multiple_TriggerBinding.golden new file mode 100644 index 000000000..75a1393bc --- /dev/null +++ b/pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-Multiple_TriggerBinding.golden @@ -0,0 +1,5 @@ +NAME AGE +tb1 2 minutes ago +tb2 30 seconds ago +tb3 1 week ago +tb4 --- diff --git a/pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-Multiple_TriggerBinding_with_output_format.golden b/pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-Multiple_TriggerBinding_with_output_format.golden new file mode 100644 index 000000000..9a7705f99 --- /dev/null +++ b/pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-Multiple_TriggerBinding_with_output_format.golden @@ -0,0 +1,4 @@ +tb1 +tb2 +tb3 +tb4 diff --git a/pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-No_TriggerBinding.golden b/pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-No_TriggerBinding.golden new file mode 100644 index 000000000..a71fb6aa8 --- /dev/null +++ b/pkg/cmd/triggerbinding/testdata/TestListTriggerBinding-No_TriggerBinding.golden @@ -0,0 +1 @@ +No triggerbindings found From 2470f1430fd7fc3379bf6f6008f8df6e9f89c082 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 17 Jan 2020 13:00:34 +0100 Subject: [PATCH 08/11] =?UTF-8?q?Use=20golden=20package=20for=20unit=20tes?= =?UTF-8?q?t=20for=20TriggerTemplate=20=F0=9F=90=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allow to remove the potential long expected output from the test code. See gotest.tools/v3/golden for documentation. Signed-off-by: Vincent Demeester --- pkg/cmd/triggertemplate/list_test.go | 50 ++++++------------- ...stTriggerTemplate-Invalid_namespace.golden | 1 + ...rTemplate-Multiple_TriggerTemplates.golden | 5 ++ ...TriggerTemplates_with_output_format.golden | 4 ++ ...tTriggerTemplate-No_TriggerTemplate.golden | 1 + 5 files changed, 26 insertions(+), 35 deletions(-) create mode 100644 pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-Invalid_namespace.golden create mode 100644 pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-Multiple_TriggerTemplates.golden create mode 100644 pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-Multiple_TriggerTemplates_with_output_format.golden create mode 100644 pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-No_TriggerTemplate.golden diff --git a/pkg/cmd/triggertemplate/list_test.go b/pkg/cmd/triggertemplate/list_test.go index 3aae19c04..6e2cd1da9 100644 --- a/pkg/cmd/triggertemplate/list_test.go +++ b/pkg/cmd/triggertemplate/list_test.go @@ -15,6 +15,7 @@ package triggertemplate import ( + "fmt" "strings" "testing" "time" @@ -26,6 +27,7 @@ import ( "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" triggertest "github.com/tektoncd/triggers/test" tb "github.com/tektoncd/triggers/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -57,52 +59,30 @@ func TestListTriggerTemplate(t *testing.T) { name string command *cobra.Command args []string - expected []string wantError bool }{ { - name: "Invalid namespace", - command: command(t, tts, now, ns), - args: []string{"list", "-n", "default"}, - expected: []string{ - "Error: namespaces \"default\" not found\n", - }, + name: "Invalid namespace", + command: command(t, tts, now, ns), + args: []string{"list", "-n", "default"}, wantError: true, }, { - name: "No TriggerTemplate", - command: command(t, tts, now, ns), - args: []string{"list", "-n", "random"}, - expected: []string{ - "No triggertemplates found\n", - }, + name: "No TriggerTemplate", + command: command(t, tts, now, ns), + args: []string{"list", "-n", "random"}, wantError: false, }, { - name: "Multiple TriggerTemplates", - command: command(t, tts, now, ns), - args: []string{"list", "-n", "foo"}, - expected: []string{ - "NAME AGE", - "tt1 2 minutes ago", - "tt2 30 seconds ago", - "tt3 1 week ago", - "tt4 ---", - "", - }, + name: "Multiple TriggerTemplates", + command: command(t, tts, now, ns), + args: []string{"list", "-n", "foo"}, wantError: false, }, { - name: "Multiple TriggerTemplates with output format", - command: command(t, tts, now, ns), - args: []string{"list", "-n", "foo", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"}, - expected: []string{ - "tt1", - "tt2", - "tt3", - "tt4", - "", - }, + name: "Multiple TriggerTemplates with output format", + command: command(t, tts, now, ns), + args: []string{"list", "-n", "foo", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"}, wantError: false, }, } @@ -114,7 +94,7 @@ func TestListTriggerTemplate(t *testing.T) { if err != nil && !td.wantError { t.Errorf("Unexpected error: %v", err) } - test.AssertOutput(t, strings.Join(td.expected, "\n"), got) + golden.Assert(t, got, strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-")) }) } } diff --git a/pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-Invalid_namespace.golden b/pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-Invalid_namespace.golden new file mode 100644 index 000000000..93f0371f2 --- /dev/null +++ b/pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-Invalid_namespace.golden @@ -0,0 +1 @@ +Error: namespaces "default" not found diff --git a/pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-Multiple_TriggerTemplates.golden b/pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-Multiple_TriggerTemplates.golden new file mode 100644 index 000000000..a16e08e43 --- /dev/null +++ b/pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-Multiple_TriggerTemplates.golden @@ -0,0 +1,5 @@ +NAME AGE +tt1 2 minutes ago +tt2 30 seconds ago +tt3 1 week ago +tt4 --- diff --git a/pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-Multiple_TriggerTemplates_with_output_format.golden b/pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-Multiple_TriggerTemplates_with_output_format.golden new file mode 100644 index 000000000..09667897b --- /dev/null +++ b/pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-Multiple_TriggerTemplates_with_output_format.golden @@ -0,0 +1,4 @@ +tt1 +tt2 +tt3 +tt4 diff --git a/pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-No_TriggerTemplate.golden b/pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-No_TriggerTemplate.golden new file mode 100644 index 000000000..f06372316 --- /dev/null +++ b/pkg/cmd/triggertemplate/testdata/TestListTriggerTemplate-No_TriggerTemplate.golden @@ -0,0 +1 @@ +No triggertemplates found From b1d361764e8dfc746a2ed16b6ff39bf5e1d1a152 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 17 Jan 2020 13:03:07 +0100 Subject: [PATCH 09/11] =?UTF-8?q?Use=20golden=20package=20for=20unit=20tes?= =?UTF-8?q?t=20for=20EventListener=20=F0=9F=90=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allow to remove the potential long expected output from the test code. See gotest.tools/v3/golden for documentation. Signed-off-by: Vincent Demeester --- pkg/cmd/eventlistener/list_test.go | 50 ++++++------------- ...ListEventListener-Invalid_namespace.golden | 1 + ...ventListener-Multiple_EventListener.golden | 5 ++ ...le_EventListener_with_output_format.golden | 4 ++ ...tListEventListener-No_EventListener.golden | 1 + 5 files changed, 26 insertions(+), 35 deletions(-) create mode 100644 pkg/cmd/eventlistener/testdata/TestListEventListener-Invalid_namespace.golden create mode 100644 pkg/cmd/eventlistener/testdata/TestListEventListener-Multiple_EventListener.golden create mode 100644 pkg/cmd/eventlistener/testdata/TestListEventListener-Multiple_EventListener_with_output_format.golden create mode 100644 pkg/cmd/eventlistener/testdata/TestListEventListener-No_EventListener.golden diff --git a/pkg/cmd/eventlistener/list_test.go b/pkg/cmd/eventlistener/list_test.go index 215c46d46..fe7e727eb 100644 --- a/pkg/cmd/eventlistener/list_test.go +++ b/pkg/cmd/eventlistener/list_test.go @@ -15,6 +15,7 @@ package eventlistener import ( + "fmt" "strings" "testing" "time" @@ -26,6 +27,7 @@ import ( "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" triggertest "github.com/tektoncd/triggers/test" tb "github.com/tektoncd/triggers/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -57,52 +59,30 @@ func TestListEventListener(t *testing.T) { name string command *cobra.Command args []string - expected []string wantError bool }{ { - name: "Invalid namespace", - command: command(t, els, now, ns), - args: []string{"list", "-n", "default"}, - expected: []string{ - "Error: namespaces \"default\" not found\n", - }, + name: "Invalid namespace", + command: command(t, els, now, ns), + args: []string{"list", "-n", "default"}, wantError: true, }, { - name: "No EventListener", - command: command(t, els, now, ns), - args: []string{"list", "-n", "random"}, - expected: []string{ - "No eventlisteners found\n", - }, + name: "No EventListener", + command: command(t, els, now, ns), + args: []string{"list", "-n", "random"}, wantError: false, }, { - name: "Multiple EventListener", - command: command(t, els, now, ns), - args: []string{"list", "-n", "foo"}, - expected: []string{ - "NAME AGE", - "tb1 2 minutes ago", - "tb2 30 seconds ago", - "tb3 1 week ago", - "tb4 ---", - "", - }, + name: "Multiple EventListener", + command: command(t, els, now, ns), + args: []string{"list", "-n", "foo"}, wantError: false, }, { - name: "Multiple EventListener with output format", - command: command(t, els, now, ns), - args: []string{"list", "-n", "foo", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"}, - expected: []string{ - "tb1", - "tb2", - "tb3", - "tb4", - "", - }, + name: "Multiple EventListener with output format", + command: command(t, els, now, ns), + args: []string{"list", "-n", "foo", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"}, wantError: false, }, } @@ -114,7 +94,7 @@ func TestListEventListener(t *testing.T) { if err != nil && !td.wantError { t.Errorf("Unexpected error: %v", err) } - test.AssertOutput(t, strings.Join(td.expected, "\n"), got) + golden.Assert(t, got, strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-")) }) } } diff --git a/pkg/cmd/eventlistener/testdata/TestListEventListener-Invalid_namespace.golden b/pkg/cmd/eventlistener/testdata/TestListEventListener-Invalid_namespace.golden new file mode 100644 index 000000000..93f0371f2 --- /dev/null +++ b/pkg/cmd/eventlistener/testdata/TestListEventListener-Invalid_namespace.golden @@ -0,0 +1 @@ +Error: namespaces "default" not found diff --git a/pkg/cmd/eventlistener/testdata/TestListEventListener-Multiple_EventListener.golden b/pkg/cmd/eventlistener/testdata/TestListEventListener-Multiple_EventListener.golden new file mode 100644 index 000000000..75a1393bc --- /dev/null +++ b/pkg/cmd/eventlistener/testdata/TestListEventListener-Multiple_EventListener.golden @@ -0,0 +1,5 @@ +NAME AGE +tb1 2 minutes ago +tb2 30 seconds ago +tb3 1 week ago +tb4 --- diff --git a/pkg/cmd/eventlistener/testdata/TestListEventListener-Multiple_EventListener_with_output_format.golden b/pkg/cmd/eventlistener/testdata/TestListEventListener-Multiple_EventListener_with_output_format.golden new file mode 100644 index 000000000..9a7705f99 --- /dev/null +++ b/pkg/cmd/eventlistener/testdata/TestListEventListener-Multiple_EventListener_with_output_format.golden @@ -0,0 +1,4 @@ +tb1 +tb2 +tb3 +tb4 diff --git a/pkg/cmd/eventlistener/testdata/TestListEventListener-No_EventListener.golden b/pkg/cmd/eventlistener/testdata/TestListEventListener-No_EventListener.golden new file mode 100644 index 000000000..e55277535 --- /dev/null +++ b/pkg/cmd/eventlistener/testdata/TestListEventListener-No_EventListener.golden @@ -0,0 +1 @@ +No eventlisteners found From dffc628960a0b08415cbae4ae2375592b20af67e Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 17 Jan 2020 13:07:29 +0100 Subject: [PATCH 10/11] =?UTF-8?q?Use=20golden=20package=20for=20unit=20tes?= =?UTF-8?q?t=20for=20PipelineResource=20=F0=9F=90=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allow to remove the potential long expected output from the test code. See gotest.tools/v3/golden for documentation. Signed-off-by: Vincent Demeester --- pkg/cmd/pipelineresource/describe_test.go | 37 ++------------- pkg/cmd/pipelineresource/list_test.go | 45 ++----------------- ...PipelineResourceDescribe_WithParams.golden | 12 +++++ ...neResourceDescribe_WithSecretParams.golden | 14 ++++++ ...estPipelineResourceList-By_template.golden | 5 +++ ...ist-Empty_Pipeline_Resource_by_type.golden | 1 + ...elineResourceList-Invalid_namespace.golden | 1 + ...-Multiple_Pipeline_Resource_by_type.golden | 3 ++ ...rceList-Multiple_pipeline_resources.golden | 6 +++ ...st-Single_Pipeline_Resource_by_type.golden | 2 + ...sourceList-Single_pipeline_resource.golden | 2 + 11 files changed, 53 insertions(+), 75 deletions(-) create mode 100644 pkg/cmd/pipelineresource/testdata/TestPipelineResourceDescribe_WithParams.golden create mode 100644 pkg/cmd/pipelineresource/testdata/TestPipelineResourceDescribe_WithSecretParams.golden create mode 100644 pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-By_template.golden create mode 100644 pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Empty_Pipeline_Resource_by_type.golden create mode 100644 pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Invalid_namespace.golden create mode 100644 pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Multiple_Pipeline_Resource_by_type.golden create mode 100644 pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Multiple_pipeline_resources.golden create mode 100644 pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Single_Pipeline_Resource_by_type.golden create mode 100644 pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Single_pipeline_resource.golden diff --git a/pkg/cmd/pipelineresource/describe_test.go b/pkg/cmd/pipelineresource/describe_test.go index 770ec3d3b..d4f93786a 100644 --- a/pkg/cmd/pipelineresource/describe_test.go +++ b/pkg/cmd/pipelineresource/describe_test.go @@ -15,13 +15,14 @@ package pipelineresource import ( - "strings" + "fmt" "testing" "github.com/tektoncd/cli/pkg/test" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" pipelinetest "github.com/tektoncd/pipeline/test" tb "github.com/tektoncd/pipeline/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -91,21 +92,7 @@ func TestPipelineResourceDescribe_WithParams(t *testing.T) { p := &test.Params{Tekton: cs.Pipeline, Kube: cs.Kube} pipelineresource := Command(p) out, _ := test.ExecuteCommand(pipelineresource, "desc", "test-1", "-n", "test-ns-1") - expected := []string{ - "Name: test-1", - "Namespace: test-ns-1", - "PipelineResource Type: image", - "", - "Params\n", - " NAME VALUE", - " URL quay.io/tekton/controller", - "", - "Secret Params\n", - " No secret params", - "", - } - - test.AssertOutput(t, strings.Join(expected, "\n"), out) + golden.Assert(t, out, fmt.Sprintf("%s.golden", t.Name())) } func TestPipelineResourceDescribe_WithSecretParams(t *testing.T) { @@ -131,21 +118,5 @@ func TestPipelineResourceDescribe_WithSecretParams(t *testing.T) { p := &test.Params{Tekton: cs.Pipeline, Kube: cs.Kube} pipelineresource := Command(p) out, _ := test.ExecuteCommand(pipelineresource, "desc", "test-1", "-n", "test-ns-1") - expected := []string{ - "Name: test-1", - "Namespace: test-ns-1", - "PipelineResource Type: image", - "", - "Params\n", - " NAME VALUE", - " URL quay.io/tekton/controller", - " TAG latest", - "", - "Secret Params\n", - " FIELDNAME SECRETNAME", - " githubToken github-secrets", - "", - } - - test.AssertOutput(t, strings.Join(expected, "\n"), out) + golden.Assert(t, out, fmt.Sprintf("%s.golden", t.Name())) } diff --git a/pkg/cmd/pipelineresource/list_test.go b/pkg/cmd/pipelineresource/list_test.go index 166755dc0..8c33a3b26 100644 --- a/pkg/cmd/pipelineresource/list_test.go +++ b/pkg/cmd/pipelineresource/list_test.go @@ -15,6 +15,7 @@ package pipelineresource import ( + "fmt" "strings" "testing" @@ -23,6 +24,7 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" pipelinetest "github.com/tektoncd/pipeline/test" tb "github.com/tektoncd/pipeline/test/builder" + "gotest.tools/v3/golden" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -77,7 +79,6 @@ func TestPipelineResourceList(t *testing.T) { name string command *cobra.Command args []string - expected []string wantError bool }{ { @@ -85,82 +86,42 @@ func TestPipelineResourceList(t *testing.T) { command: command(t, pres, ns), args: []string{"list", "-n", "invalid"}, wantError: true, - expected: []string{ - "Error: namespaces \"invalid\" not found\n", - }, }, { name: "Multiple pipeline resources", command: command(t, pres, ns), args: []string{"list", "-n", "test-ns-1"}, wantError: false, - expected: []string{ - "NAME TYPE DETAILS", - "test-5 cloudEvent targetURI: http://sink", - "test git url: git@github.com:tektoncd/cli-new.git", - "test-2 git url: git@github.com:tektoncd/cli.git", - "test-1 image URL: quey.io/tekton/controller", - "test-3 image ---", - "", - }, }, { name: "Single pipeline resource", command: command(t, pres, ns), args: []string{"list", "-n", "test-ns-2"}, wantError: false, - expected: []string{ - "NAME TYPE DETAILS", - "test-4 image URL: quey.io/tekton/webhook", - "", - }, }, { name: "Single Pipeline Resource by type", command: command(t, pres, ns), args: []string{"list", "-n", "test-ns-2", "-t", "image"}, wantError: false, - expected: []string{ - "NAME TYPE DETAILS", - "test-4 image URL: quey.io/tekton/webhook", - "", - }, }, { name: "Multiple Pipeline Resource by type", command: command(t, pres, ns), args: []string{"list", "-n", "test-ns-1", "-t", "image"}, wantError: false, - expected: []string{ - "NAME TYPE DETAILS", - "test-1 image URL: quey.io/tekton/controller", - "test-3 image ---", - "", - }, }, { name: "Empty Pipeline Resource by type", command: command(t, pres, ns), args: []string{"list", "-n", "test-ns-1", "-t", "storage"}, wantError: false, - expected: []string{ - "No pipelineresources found.", - "", - }, }, { name: "By template", command: command(t, pres, ns), args: []string{"list", "-n", "test-ns-1", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"}, wantError: false, - expected: []string{ - "test-5", - "test", - "test-2", - "test-1", - "test-3", - "", - }, }, } @@ -171,7 +132,7 @@ func TestPipelineResourceList(t *testing.T) { if !td.wantError && err != nil { t.Errorf("Unexpected error: %v", err) } - test.AssertOutput(t, strings.Join(td.expected, "\n"), out) + golden.Assert(t, out, strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-")) }) } diff --git a/pkg/cmd/pipelineresource/testdata/TestPipelineResourceDescribe_WithParams.golden b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceDescribe_WithParams.golden new file mode 100644 index 000000000..ad619961b --- /dev/null +++ b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceDescribe_WithParams.golden @@ -0,0 +1,12 @@ +Name: test-1 +Namespace: test-ns-1 +PipelineResource Type: image + +Params + + NAME VALUE + URL quay.io/tekton/controller + +Secret Params + + No secret params diff --git a/pkg/cmd/pipelineresource/testdata/TestPipelineResourceDescribe_WithSecretParams.golden b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceDescribe_WithSecretParams.golden new file mode 100644 index 000000000..b4a8cfdbc --- /dev/null +++ b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceDescribe_WithSecretParams.golden @@ -0,0 +1,14 @@ +Name: test-1 +Namespace: test-ns-1 +PipelineResource Type: image + +Params + + NAME VALUE + URL quay.io/tekton/controller + TAG latest + +Secret Params + + FIELDNAME SECRETNAME + githubToken github-secrets diff --git a/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-By_template.golden b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-By_template.golden new file mode 100644 index 000000000..1eb04db49 --- /dev/null +++ b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-By_template.golden @@ -0,0 +1,5 @@ +test-5 +test +test-2 +test-1 +test-3 diff --git a/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Empty_Pipeline_Resource_by_type.golden b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Empty_Pipeline_Resource_by_type.golden new file mode 100644 index 000000000..375eb7872 --- /dev/null +++ b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Empty_Pipeline_Resource_by_type.golden @@ -0,0 +1 @@ +No pipelineresources found. diff --git a/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Invalid_namespace.golden b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Invalid_namespace.golden new file mode 100644 index 000000000..1ca4dafb1 --- /dev/null +++ b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Invalid_namespace.golden @@ -0,0 +1 @@ +Error: namespaces "invalid" not found diff --git a/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Multiple_Pipeline_Resource_by_type.golden b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Multiple_Pipeline_Resource_by_type.golden new file mode 100644 index 000000000..0a820426e --- /dev/null +++ b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Multiple_Pipeline_Resource_by_type.golden @@ -0,0 +1,3 @@ +NAME TYPE DETAILS +test-1 image URL: quey.io/tekton/controller +test-3 image --- diff --git a/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Multiple_pipeline_resources.golden b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Multiple_pipeline_resources.golden new file mode 100644 index 000000000..486a27953 --- /dev/null +++ b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Multiple_pipeline_resources.golden @@ -0,0 +1,6 @@ +NAME TYPE DETAILS +test-5 cloudEvent targetURI: http://sink +test git url: git@github.com:tektoncd/cli-new.git +test-2 git url: git@github.com:tektoncd/cli.git +test-1 image URL: quey.io/tekton/controller +test-3 image --- diff --git a/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Single_Pipeline_Resource_by_type.golden b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Single_Pipeline_Resource_by_type.golden new file mode 100644 index 000000000..acc801d40 --- /dev/null +++ b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Single_Pipeline_Resource_by_type.golden @@ -0,0 +1,2 @@ +NAME TYPE DETAILS +test-4 image URL: quey.io/tekton/webhook diff --git a/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Single_pipeline_resource.golden b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Single_pipeline_resource.golden new file mode 100644 index 000000000..acc801d40 --- /dev/null +++ b/pkg/cmd/pipelineresource/testdata/TestPipelineResourceList-Single_pipeline_resource.golden @@ -0,0 +1,2 @@ +NAME TYPE DETAILS +test-4 image URL: quey.io/tekton/webhook From cd7c83ea76a44dcd4425249439a1b1ce9a90a64c Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 17 Jan 2020 14:20:01 +0100 Subject: [PATCH 11/11] =?UTF-8?q?Add=20`test-unit-update-golden`=20target?= =?UTF-8?q?=20to=20update=20golden=20files=20=F0=9F=90=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Errors should be ignored when running this 👼 Signed-off-by: Vincent Demeester --- Makefile | 5 +++++ hack/update-golden.sh | 5 +++++ 2 files changed, 10 insertions(+) create mode 100755 hack/update-golden.sh diff --git a/Makefile b/Makefile index dc8a169f1..1769be9a3 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,11 @@ test-unit: ./vendor ## run unit tests @echo "Running unit tests..." @go test -failfast -v -cover ./... +.PHONY: test-unit-update-golden +test-unit-update-golden: ./vendor ## run unit tests (updating golden files) + @echo "Running unit tests updating golden files..." + @./hack/update-golden.sh + .PHONY: test-e2e test-e2e: bin/tkn ## run e2e tests @echo "Running e2e tests..." diff --git a/hack/update-golden.sh b/hack/update-golden.sh new file mode 100755 index 000000000..96cf48212 --- /dev/null +++ b/hack/update-golden.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +# This will run `go test a/package -test.update-golden=true` on all packages that are importing `gotest.tools/v3/golden` +# This will update the golden files with the current output. +# Run this only when you are sure the output is meant to change. +go test $(go list -f '{{ .ImportPath }} {{ .TestImports }}' ./... | grep gotest.tools/v3/golden | awk '{print $1}' | tr '\n' ' ') -test.update-golden=true