Skip to content

Commit 5d76dbd

Browse files
committed
passing test
1 parent 981f7eb commit 5d76dbd

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

cli-files/go-cat/cmd/root.go

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,27 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
55
package cmd
66

77
import (
8+
"fmt"
89
"os"
910

1011
"github.com/spf13/cobra"
1112
)
1213

1314
// rootCmd represents the base command when called without any subcommands
14-
var rootCmd = &cobra.Command{
15-
Use: "go-cat",
16-
Short: "A brief description of your application",
17-
Long: `A longer description that spans multiple lines and likely contains
18-
examples and usage of using your application. For example:
19-
20-
Cobra is a CLI library for Go that empowers applications.
21-
This application is a tool to generate the needed files
22-
to quickly create a Cobra application.`,
23-
// Uncomment the following line if your bare application
24-
// has an action associated with it:
25-
// Run: func(cmd *cobra.Command, args []string) { },
15+
func NewRoodCmd() *cobra.Command {
16+
return &cobra.Command{
17+
Use: "go-cat",
18+
Short: "Go implementation of cat",
19+
Long: `Works like cat`,
20+
Run: func(cmd *cobra.Command, args []string) {
21+
},
22+
}
2623
}
2724

28-
// Execute adds all child commands to the root command and sets flags appropriately.
29-
// This is called by main.main(). It only needs to happen once to the rootCmd.
3025
func Execute() {
31-
err := rootCmd.Execute()
32-
if err != nil {
26+
rootCmd := NewRoodCmd()
27+
if err := rootCmd.Execute(); err != nil {
28+
fmt.Fprintln(os.Stderr, err)
3329
os.Exit(1)
3430
}
3531
}
36-
37-
func init() {
38-
// Here you will define your flags and configuration settings.
39-
// Cobra supports persistent flags, which, if defined here,
40-
// will be global for your application.
41-
42-
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.go-cat.yaml)")
43-
44-
// Cobra also supports local flags, which will only run
45-
// when this action is called directly.
46-
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
47-
}

cli-files/go-cat/cmd/root_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"testing"
7+
)
8+
9+
func TestRootCmd(t *testing.T) {
10+
cmd := NewRoodCmd()
11+
b := bytes.NewBufferString("")
12+
cmd.SetOut(b)
13+
cmd.SetArgs([]string{})
14+
cmd.Execute()
15+
out, err := ioutil.ReadAll(b)
16+
if err != nil {
17+
t.Fatal(err)
18+
}
19+
expected := ``
20+
if string(out) != expected {
21+
t.Fatalf("expected \"%s\" got \"%s\"", expected, string(out))
22+
}
23+
}

0 commit comments

Comments
 (0)