refactor: [test] make todo & version test works correctly

上级 b813d8e0
......@@ -40,12 +40,14 @@ func executeActionCommandC(cmd string) (*cobra.Command, string, error) {
if err != nil {
return nil, "", err
}
buf := new(bytes.Buffer)
command := NewRootCmd(buf)
rootCmd.SetOut(buf)
rootCmd.SetArgs(args)
command.SetOut(buf)
command.SetArgs(args)
c, err := rootCmd.ExecuteC()
c, err := command.ExecuteC()
return c, buf.String(), err
}
......
......@@ -2,11 +2,11 @@ package cmd
import (
"github.com/spf13/cobra"
"os"
"io"
)
var (
output = os.Stdout
output io.Writer
rootCmd = &cobra.Command{
Use: "coca",
Short: "A generator for Cobra based Applications",
......@@ -14,7 +14,8 @@ var (
}
)
func Execute() error {
rootCmd.SetOut(output)
return rootCmd.Execute()
func NewRootCmd(out io.Writer) *cobra.Command {
output = out
rootCmd.SetOut(out)
return rootCmd
}
Todos Count 0
+------+--------+----------+----------+------+
| DATE | AUTHOR | MESSAGES | FILENAME | LINE |
+------+--------+----------+----------+------+
+------+--------+----------+----------+------+
Coca Version: 1.3.0 -- HEAD
\ No newline at end of file
......@@ -7,7 +7,6 @@ import (
"github.com/phodal/coca/cmd/cmd_util"
"github.com/phodal/coca/core/context/todo"
"github.com/spf13/cobra"
"os"
"strings"
)
......@@ -32,7 +31,7 @@ var todoCmd = &cobra.Command{
simple, _ := json.MarshalIndent(todos, "", "\t")
cmd_util.WriteToCocaFile("simple-todos.json", string(simple))
fmt.Println("Todos Count", len(todos))
fmt.Fprintf(output, "Todos Count %d\n", len(todos))
if todoCmdConfig.WithGit {
gitTodos := app.BuildWithGitHistory(todos)
......@@ -40,7 +39,7 @@ var todoCmd = &cobra.Command{
cModel, _ := json.MarshalIndent(todos, "", "\t")
cmd_util.WriteToCocaFile("todos.json", string(cModel))
table := tablewriter.NewWriter(os.Stdout)
table := tablewriter.NewWriter(output)
table.SetHeader([]string{"Date", "Author", "Messages", "FileName", "Line"})
for _, todo := range gitTodos {
table.Append([]string{todo.Date, todo.Author, strings.Join(todo.Message, "\n"), todo.FileName, todo.Line})
......@@ -52,8 +51,9 @@ var todoCmd = &cobra.Command{
}
func init() {
rootCmd.AddCommand(todoCmd)
todoCmd.SetOut(output)
todoCmd.PersistentFlags().StringVarP(&todoCmdConfig.Path, "path", "p", ".", "path")
todoCmd.PersistentFlags().BoolVarP(&todoCmdConfig.WithGit, "git", "g", false, "path")
rootCmd.AddCommand(todoCmd)
}
......@@ -8,7 +8,7 @@ func TestTodo(t *testing.T) {
tests := []cmdTestCase{{
name: "todo",
cmd: "todo -p . -g",
golden: "",
golden: "testdata/todo_normal.txt",
}}
runTestCmd(t, tests)
}
\ No newline at end of file
......@@ -15,6 +15,6 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintf(output, "Coca Version: " + config.VERSION + " -- HEAD \n")
fmt.Fprintf(output, "Coca Version: " + config.VERSION + " -- HEAD")
},
}
\ No newline at end of file
......@@ -2,9 +2,12 @@ package main
import (
"github.com/phodal/coca/cmd"
"os"
)
func main() {
//defer profile.Refactoring().Stop()
cmd.Execute()
output := os.Stdout
rootCmd := cmd.NewRootCmd(output)
_ = rootCmd.Execute()
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册