-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add golden file tests for the 'add' and the 'init' command.
- Loading branch information
Showing
10 changed files
with
325 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ build: | |
test: | ||
go test -v ./... | ||
|
||
golden: | ||
go test -v ./... -update | ||
|
||
lint: | ||
golangci-lint run | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters