Skip to content

Commit 7f7626f

Browse files
adding tests for the cobra function
Signed-off-by: Christopher Hein <[email protected]>
1 parent 228057b commit 7f7626f

File tree

7 files changed

+107
-55
lines changed

7 files changed

+107
-55
lines changed

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@ go:
66
env:
77
- GO111MODULE=on
88

9+
branches:
10+
only:
11+
- master
12+
13+
before_install:
14+
- go get -u golang.org/x/lint/golint
15+
916
script:
17+
- make test-fmt
18+
- make lint
1019
- make test

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
.PHONY: all
22
all: build-example test
33

4+
.PHONY: lint
5+
lint:
6+
@golint -set_exit_status ./...
7+
8+
test-fmt:
9+
./hack/test-fmt.sh
10+
411
.PHONY: test
512
test:
6-
go test -v ./...
13+
@go test -v -race ./...
714

815
.PHONY: build-example
916
build-example:

example/root.go

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,82 +16,77 @@ limitations under the License.
1616
package main
1717

1818
import (
19-
"fmt"
20-
"os"
21-
"github.com/spf13/cobra"
19+
"fmt"
20+
"os"
2221

23-
homedir "github.com/mitchellh/go-homedir"
24-
"github.com/spf13/viper"
22+
"github.com/spf13/cobra"
2523

24+
homedir "github.com/mitchellh/go-homedir"
25+
"github.com/spf13/viper"
2626
)
2727

28-
2928
var cfgFile string
3029

31-
3230
// rootCmd represents the base command when called without any subcommands
3331
var rootCmd = &cobra.Command{
34-
Use: "example",
35-
Short: "A brief description of your application",
36-
Long: `A longer description that spans multiple lines and likely contains
32+
Use: "example",
33+
Short: "A brief description of your application",
34+
Long: `A longer description that spans multiple lines and likely contains
3735
examples and usage of using your application. For example:
3836
3937
Cobra is a CLI library for Go that empowers applications.
4038
This application is a tool to generate the needed files
4139
to quickly create a Cobra application.`,
42-
// Uncomment the following line if your bare application
43-
// has an action associated with it:
44-
// Run: func(cmd *cobra.Command, args []string) { },
40+
// Uncomment the following line if your bare application
41+
// has an action associated with it:
42+
// Run: func(cmd *cobra.Command, args []string) { },
4543
}
4644

4745
// Execute adds all child commands to the root command and sets flags appropriately.
4846
// This is called by main.main(). It only needs to happen once to the rootCmd.
4947
func Execute() {
50-
if err := rootCmd.Execute(); err != nil {
51-
fmt.Println(err)
52-
os.Exit(1)
53-
}
48+
if err := rootCmd.Execute(); err != nil {
49+
fmt.Println(err)
50+
os.Exit(1)
51+
}
5452
}
5553

5654
func init() {
57-
cobra.OnInitialize(initConfig)
58-
59-
// Here you will define your flags and configuration settings.
60-
// Cobra supports persistent flags, which, if defined here,
61-
// will be global for your application.
55+
cobra.OnInitialize(initConfig)
6256

63-
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.example.yaml)")
57+
// Here you will define your flags and configuration settings.
58+
// Cobra supports persistent flags, which, if defined here,
59+
// will be global for your application.
6460

61+
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.example.yaml)")
6562

66-
// Cobra also supports local flags, which will only run
67-
// when this action is called directly.
68-
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
63+
// Cobra also supports local flags, which will only run
64+
// when this action is called directly.
65+
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
6966
}
7067

71-
7268
// initConfig reads in config file and ENV variables if set.
7369
func initConfig() {
74-
if cfgFile != "" {
75-
// Use config file from the flag.
76-
viper.SetConfigFile(cfgFile)
77-
} else {
78-
// Find home directory.
79-
home, err := homedir.Dir()
80-
if err != nil {
81-
fmt.Println(err)
82-
os.Exit(1)
83-
}
84-
85-
// Search config in home directory with name ".example" (without extension).
86-
viper.AddConfigPath(home)
87-
viper.SetConfigName(".example")
88-
}
89-
90-
viper.AutomaticEnv() // read in environment variables that match
91-
92-
// If a config file is found, read it in.
93-
if err := viper.ReadInConfig(); err == nil {
94-
fmt.Println("Using config file:", viper.ConfigFileUsed())
95-
}
70+
if cfgFile != "" {
71+
// Use config file from the flag.
72+
viper.SetConfigFile(cfgFile)
73+
} else {
74+
// Find home directory.
75+
home, err := homedir.Dir()
76+
if err != nil {
77+
fmt.Println(err)
78+
os.Exit(1)
79+
}
80+
81+
// Search config in home directory with name ".example" (without extension).
82+
viper.AddConfigPath(home)
83+
viper.SetConfigName(".example")
84+
}
85+
86+
viper.AutomaticEnv() // read in environment variables that match
87+
88+
// If a config file is found, read it in.
89+
if err := viper.ReadInConfig(); err == nil {
90+
fmt.Println("Using config file:", viper.ConfigFileUsed())
91+
}
9692
}
97-

example/version.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616
package main
1717

1818
import (
19+
"os"
20+
1921
"github.com/spf13/cobra"
2022
goVersion "go.hein.dev/go-version"
2123
)
@@ -29,7 +31,7 @@ var (
2931
Use: "version",
3032
Short: "Version will output the current build information",
3133
Long: ``,
32-
Run: goVersion.Func(shortened, version, commit, date),
34+
Run: goVersion.Func(os.Stdout, shortened, version, commit, date),
3335
}
3436
)
3537

hack/test-fmt.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
test -z $(gofmt -l .)

version.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package version // import "go.hein.dev/go-version"
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"strings"
78

89
"github.com/spf13/cobra"
@@ -25,7 +26,7 @@ func New(version string, commit string, date string) *Info {
2526
}
2627

2728
// Func will add the versioning code
28-
func Func(shortened bool, version, commit, date string) func(*cobra.Command, []string) {
29+
func Func(out io.Writer, shortened bool, version, commit, date string) func(*cobra.Command, []string) {
2930
return func(_ *cobra.Command, _ []string) {
3031
var response string
3132
versionOutput := New(version, commit, date)
@@ -35,7 +36,7 @@ func Func(shortened bool, version, commit, date string) func(*cobra.Command, []s
3536
} else {
3637
response = versionOutput.ToJSON()
3738
}
38-
fmt.Printf("%+v", response)
39+
fmt.Fprintf(out, "%+v", response)
3940
return
4041
}
4142
}

version_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package version
22

3-
import "testing"
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/spf13/cobra"
8+
)
49

510
func TestToJSON(t *testing.T) {
611
v := New("dev", "fee8dd1f7c24da23508e26347694be0acce5631b", "Fri Jun 21 10:50:15 PDT 2019")
@@ -25,3 +30,33 @@ Date: Fri Jun 21 10:50:15 PDT 2019
2530
t.Errorf("Expected shortened %s to equal %s", short, expected)
2631
}
2732
}
33+
34+
type testcase struct {
35+
expected string
36+
shortened bool
37+
}
38+
39+
func TestFunc(t *testing.T) {
40+
cases := []testcase{
41+
testcase{
42+
expected: "{\"Version\":\"dev\",\"Commit\":\"fee8dd1f7c24da23508e26347694be0acce5631b\",\"Date\":\"Fri Jun 21 10:50:15 PDT 2019\"}\n",
43+
shortened: false,
44+
},
45+
testcase{
46+
expected: `Version: dev
47+
Commit: fee8dd1f7c24da23508e26347694be0acce5631b
48+
Date: Fri Jun 21 10:50:15 PDT 2019
49+
`,
50+
shortened: true,
51+
},
52+
}
53+
54+
for _, c := range cases {
55+
buffer := &bytes.Buffer{}
56+
Func(buffer, c.shortened, "dev", "fee8dd1f7c24da23508e26347694be0acce5631b", "Fri Jun 21 10:50:15 PDT 2019")(&cobra.Command{}, []string{})
57+
resp := buffer.String()
58+
if resp != c.expected {
59+
t.Errorf("got %q want %q", resp, c.expected)
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)