Skip to content

Commit bb76012

Browse files
Merge pull request #11 from christopherhein/chore/pull-request-test
Adding tests for the cobra function
2 parents 228057b + 2fb52ec commit bb76012

File tree

9 files changed

+129
-55
lines changed

9 files changed

+129
-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

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ require (
66
github.com/mitchellh/go-homedir v1.1.0
77
github.com/spf13/cobra v0.0.5
88
github.com/spf13/viper v1.4.0
9+
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
10+
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 // indirect
11+
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
12+
golang.org/x/sys v0.0.0-20190812172437-4e8604ab3aff // indirect
13+
golang.org/x/text v0.3.2 // indirect
14+
golang.org/x/tools v0.0.0-20190812233024-afc3694995b6 // indirect
915
)

go.sum

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,46 @@ go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
107107
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
108108
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
109109
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
110+
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
110111
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
111112
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
113+
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 h1:QzoH/1pFpZguR8NrRHLcO6jKqfv2zpuSqZLgdm7ZmjI=
114+
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
112115
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
113116
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
114117
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
115118
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
119+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
116120
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
121+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
122+
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
117123
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
118124
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
119125
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
120126
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
127+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
121128
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
122129
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
123130
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
124131
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
125132
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
126133
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
127134
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
135+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
136+
golang.org/x/sys v0.0.0-20190812172437-4e8604ab3aff h1:u5LtynOOWSPG+jkEa3Y4ATlQ05vVeRvFjYSvbG0z6uw=
137+
golang.org/x/sys v0.0.0-20190812172437-4e8604ab3aff/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
128138
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
129139
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
140+
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
141+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
130142
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
131143
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
144+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
132145
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
133146
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
147+
golang.org/x/tools v0.0.0-20190812233024-afc3694995b6 h1:TDcU4GQ226bJxMRWvn7/cVmdet+ms7oAElM2dabOio0=
148+
golang.org/x/tools v0.0.0-20190812233024-afc3694995b6/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
149+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
134150
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
135151
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
136152
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=

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)