From cc3072b95773654bf3e40261aa0f0c1eeffdd4ff Mon Sep 17 00:00:00 2001 From: Oncilla Date: Sat, 20 Jun 2020 21:53:10 +0200 Subject: [PATCH] test: add golden files (#10) Add golden file tests for the 'add' and the 'init' command. --- .github/workflows/go.yml | 4 ++ Makefile | 3 ++ cmd/boa/add_test.go | 68 +++++++++++++++++++++++++++++ cmd/boa/init_test.go | 64 +++++++++++++++++++++++++++ cmd/boa/testdata/add/serve.go | 48 ++++++++++++++++++++ cmd/boa/testdata/init/completion.go | 58 ++++++++++++++++++++++++ cmd/boa/testdata/init/server.go | 43 ++++++++++++++++++ cmd/boa/testdata/init/version.go | 33 ++++++++++++++ go.mod | 1 + go.sum | 3 ++ 10 files changed, 325 insertions(+) create mode 100644 cmd/boa/add_test.go create mode 100644 cmd/boa/init_test.go create mode 100644 cmd/boa/testdata/add/serve.go create mode 100644 cmd/boa/testdata/init/completion.go create mode 100644 cmd/boa/testdata/init/server.go create mode 100644 cmd/boa/testdata/init/version.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 2d3a42e..5512282 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -36,6 +36,10 @@ jobs: - name: Test run: make test + - name: Golden files + run: make golden + + lint: name: lint runs-on: ubuntu-latest diff --git a/Makefile b/Makefile index 1c04b4a..095de8f 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,9 @@ build: test: go test -v ./... +golden: + go test -v ./... -update + lint: golangci-lint run diff --git a/cmd/boa/add_test.go b/cmd/boa/add_test.go new file mode 100644 index 0000000..b8841bd --- /dev/null +++ b/cmd/boa/add_test.go @@ -0,0 +1,68 @@ +// Copyright 2020 oncilla +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "flag" + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/oncilla/boa/pkg/boa" +) + +var update = flag.Bool("update", false, "set to true to regenerate golden files") + +func TestAdd(t *testing.T) { + var dir string + if !*update { + var err error + dir, err = ioutil.TempDir("", "add") + require.NoError(t, err) + defer func() { + require.NoError(t, os.RemoveAll(dir)) + }() + } else { + dir = "testdata/add" + require.NoError(t, os.RemoveAll(dir)) + require.NoError(t, os.MkdirAll(dir, 0755)) + } + + cmd := newAdd(boa.Pather("parent path")) + cmd.SetArgs([]string{ + "--author", "my-name", + "--license", "apache", + "--path", dir, + "--flags", "addr:ip,port:uint16", + "serve", + }) + err := cmd.Execute() + require.NoError(t, err) + + files, err := filepath.Glob("testdata/add/*") + require.NoError(t, err) + + for _, file := range files { + t.Log("Checking:", file) + created, err := ioutil.ReadFile(filepath.Join(dir, filepath.Base(file))) + require.NoError(t, err) + golden, err := ioutil.ReadFile(file) + require.NoError(t, err) + assert.Equal(t, string(golden), string(created)) + } +} diff --git a/cmd/boa/init_test.go b/cmd/boa/init_test.go new file mode 100644 index 0000000..85de626 --- /dev/null +++ b/cmd/boa/init_test.go @@ -0,0 +1,64 @@ +// Copyright 2020 oncilla +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/oncilla/boa/pkg/boa" +) + +func TestInit(t *testing.T) { + var dir string + if !*update { + var err error + dir, err = ioutil.TempDir("", "init") + require.NoError(t, err) + defer func() { + require.NoError(t, os.RemoveAll(dir)) + }() + } else { + dir = "testdata/init" + require.NoError(t, os.RemoveAll(dir)) + require.NoError(t, os.MkdirAll(dir, 0755)) + } + + cmd := newInit(boa.Pather("parent path")) + cmd.SetArgs([]string{ + "--author", "my-name", + "--license", "apache", + "--path", dir, + "server", + }) + err := cmd.Execute() + require.NoError(t, err) + + files, err := filepath.Glob("testdata/init/*") + require.NoError(t, err) + + for _, file := range files { + t.Log("Checking:", file) + created, err := ioutil.ReadFile(filepath.Join(dir, filepath.Base(file))) + require.NoError(t, err) + golden, err := ioutil.ReadFile(file) + require.NoError(t, err) + assert.Equal(t, string(golden), string(created)) + } +} diff --git a/cmd/boa/testdata/add/serve.go b/cmd/boa/testdata/add/serve.go new file mode 100644 index 0000000..442a903 --- /dev/null +++ b/cmd/boa/testdata/add/serve.go @@ -0,0 +1,48 @@ +// Copyright 2020 my-name +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "fmt" + "net" + + "github.com/spf13/cobra" +) + +func newServe(pather CommandPather) *cobra.Command { + var flags struct { + addr net.IP + port uint16 + } + + var cmd = &cobra.Command{ + Use: "serve ", + Short: "serve does amazing work!", + Example: fmt.Sprintf(" %[1]s serve --sample", pather.CommandPath()), + RunE: func(cmd *cobra.Command, args []string) error { + // Add basic sanity checks, where the usage help message should be + // printed on error, before this line. After this line, the usage + // message is no longer printed on error. + cmd.SilenceUsage = true + + // TODO: Amazing work goes here! + return nil + }, + } + + cmd.Flags().IPVar(&flags.addr, "addr", nil, "addr description") + cmd.Flags().Uint16Var(&flags.port, "port", 0, "port description") + return cmd +} diff --git a/cmd/boa/testdata/init/completion.go b/cmd/boa/testdata/init/completion.go new file mode 100644 index 0000000..680fdc3 --- /dev/null +++ b/cmd/boa/testdata/init/completion.go @@ -0,0 +1,58 @@ +// Copyright 2020 my-name +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +func newCompletion(pather CommandPather) *cobra.Command { + var flags struct { + shell string + } + cmd := &cobra.Command{ + Use: "completion", + Short: "Generates shell completion scripts", + Long: fmt.Sprintf(`Outputs the autocomplete configuration for some shells. + +For example, you can add autocompletion for your current bash session using: + + . <( %[1]s completion ) + +To permanently add bash autocompletion, run: + + %[1]s completion > /etc/bash_completion.d/%[1]s +`, pather.CommandPath()), + RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + switch flags.shell { + case "bash": + return cmd.Root().GenBashCompletion(os.Stdout) + case "zsh": + return cmd.Root().GenZshCompletion(os.Stdout) + case "fish": + return cmd.Root().GenFishCompletion(os.Stdout, true) + default: + return fmt.Errorf("unknown shell: %s", flags.shell) + } + }, + } + + cmd.Flags().StringVar(&flags.shell, "shell", "bash", "Shell type (bash|zsh|fish)") + return cmd +} diff --git a/cmd/boa/testdata/init/server.go b/cmd/boa/testdata/init/server.go new file mode 100644 index 0000000..d2dfa5a --- /dev/null +++ b/cmd/boa/testdata/init/server.go @@ -0,0 +1,43 @@ +// Copyright 2020 my-name +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +// CommandPather returns the path to a command. +type CommandPather interface { + CommandPath() string +} + +func main() { + cmd := &cobra.Command{ + Use: "server", + Short: "server does amazing work!", + SilenceErrors: true, + } + cmd.AddCommand( + newCompletion(cmd), + newVersion(cmd), + ) + if err := cmd.Execute(); err != nil { + fmt.Fprintf(os.Stderr, "Error: %s", err) + os.Exit(1) + } +} diff --git a/cmd/boa/testdata/init/version.go b/cmd/boa/testdata/init/version.go new file mode 100644 index 0000000..4162448 --- /dev/null +++ b/cmd/boa/testdata/init/version.go @@ -0,0 +1,33 @@ +// Copyright 2020 my-name +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func newVersion(pather CommandPather) *cobra.Command { + var cmd = &cobra.Command{ + Use: "version", + Short: "Show the version information", + Example: fmt.Sprintf(" %[1]s version", pather.CommandPath()), + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("v0.1.0") + }, + } + return cmd +} diff --git a/go.mod b/go.mod index d18ebe5..6ed947b 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ go 1.13 require ( github.com/spf13/cobra v1.0.0 github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/testify v1.2.2 golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 ) diff --git a/go.sum b/go.sum index 1720fa2..f169a8f 100644 --- a/go.sum +++ b/go.sum @@ -34,6 +34,7 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= @@ -60,6 +61,7 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -89,6 +91,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=