Skip to content

Commit

Permalink
test: add golden files (#10)
Browse files Browse the repository at this point in the history
Add golden file tests for the 'add' and the 'init' command.
  • Loading branch information
oncilla authored Jun 20, 2020
1 parent 690a6d1 commit cc3072b
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ jobs:
- name: Test
run: make test

- name: Golden files
run: make golden


lint:
name: lint
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ build:
test:
go test -v ./...

golden:
go test -v ./... -update

lint:
golangci-lint run

68 changes: 68 additions & 0 deletions cmd/boa/add_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
64 changes: 64 additions & 0 deletions cmd/boa/init_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
48 changes: 48 additions & 0 deletions cmd/boa/testdata/add/serve.go
Original file line number Diff line number Diff line change
@@ -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 <arg>",
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
}
58 changes: 58 additions & 0 deletions cmd/boa/testdata/init/completion.go
Original file line number Diff line number Diff line change
@@ -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
}
43 changes: 43 additions & 0 deletions cmd/boa/testdata/init/server.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
33 changes: 33 additions & 0 deletions cmd/boa/testdata/init/version.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit cc3072b

Please sign in to comment.