Cobra + Viper Golang 如何测试子命令?

Cobra + Viper Golang How to test subcommands?

我正在使用 Go 开发网络应用程序。到目前为止一切顺利,但现在我将 Wercker 集成为 CI 工具并开始关注测试。但是我的应用程序严重依赖 Cobra/Viper configuration/flags/environment_variables 方案,我不知道如何在 运行 我的测试套件之前正确初始化 Viper 值。任何帮助将非常感激。

当我使用 Cobra/Viper 或 CLI 助手的任何其他组合时,我这样做的方法是让 CLI 工具 运行 一个函数,其唯一目的是获取参数并传递它们到另一种方法谁会做实际的工作。

这是一个使用 Cobra 的简短(愚蠢)示例:

package main

import (
        "fmt"
        "os"

        "github.com/spf13/cobra"
)

func main() {
        var Cmd = &cobra.Command{
                Use:   "boom",
                Short: "Explode all the things!",
                Run:   Boom,
        }

        if err := Cmd.Execute(); err != nil {
                fmt.Println(err)
                os.Exit(-1)
        }
}

func Boom(cmd *cobra.Command, args []string) {
        boom(args...)
}

func boom(args ...string) {
        for _, arg := range args {
                println("boom " + arg)
        }
}

在这里,Boom函数很难测试,但是boom很容易。

你可以看到这个 here (and the correspond test here) 的另一个(非愚蠢的)例子。

我找到了一种使用多级子命令测试命令的简单方法,虽然不专业,但效果很好。

假设我们有这样的命令

RootCmd = &cobra.Command{
            Use:   "cliName",
            Short: "Desc",
    }

SubCmd = &cobra.Command{
            Use:   "subName",
            Short: "Desc",
    }

subOfSubCmd = &cobra.Command{
            Use:   "subOfSub",
            Short: "Desc",
            Run: Exec
    }

//commands relationship
RootCmd.AddCommand(SubCmd)
SubCmd.AddCommand(subOfSubCmd)

测试subOfSubCmd时我们可以这样做:

func TestCmd(t *testing.T) {
convey.Convey("test cmd", t, func() {
    args := []string{"subName", "subOfSub"}
    RootCmd.SetArgs(args)
    RootCmd.Execute()
    })
}