Go语言 传递Cobra CLI的持久性标志以进行测试

vcirk6k6  于 2023-02-06  发布在  Go
关注(0)|答案(2)|浏览(151)

我有一个用Cobra编写的CLI应用程序。该应用程序包含一个根命令RootCmd,其中包含两个PersistentFlags。其中一个持久性标志名为threads,默认值为1,缩写为-h

RootCmd.PersistentFlags().IntVarP(&threads, "threads", "t", 1, "Number of concurrent workers, using Stdin overrides this flag")

PersistentPreRun中,我检查该值是否设置为小于1,以打印错误消息threads can't be less than 1并退出。
问题是我需要为应用程序编写一些测试,但我找不到设置标记的方法,目前我使用os/exec进行测试,这非常烦人,而且不提供代码覆盖率。
我正在尝试做以下事情

func Test(t *testing.T) {
    root := cmd.RootCmd
    root.SetArgs([]string{"-t", "12"})
    // or even: root.SetArgs([]string{"-t 12"})
    root.Execute()
}

这将输出消息Error: unknown shorthand flag: 't' in -t 12。完全忽略该标志并尝试使用任何子命令都会显示错误消息,即该值不能小于1(请注意,我设置了默认值)。
是否有除SetArgs以外的方法设置标志或解决方法?

nc1teljy

nc1teljy1#

我不能重现这个问题。
我运行cobra-cli init创建了一个简单的Cobra应用程序,并添加了一个子命令foo,得到了以下布局:

.
├── cmd
│   ├── foo.go
│   └── root.go
├── go.mod
├── go.sum
└── main.go

main.go为最小值:

package main

import "clitest/cmd"

func main() {
  cmd.Execute()
}

cmd/root.go中,我添加了一个PersistentFlag--threads(或-t):

package cmd

import (
  "fmt"
  "os"

  "github.com/spf13/cobra"
)

var threads int

var rootCmd = &cobra.Command{
  Use:   "clitest",
  Short: "A simple cli test",
  RunE:  runRoot,
}

func runRoot(cmd *cobra.Command, args []string) error {
  fmt.Printf("This is the root command, threads=%d\n", threads)
  return nil
}

func Execute() {
  err := rootCmd.Execute()
  if err != nil {
    os.Exit(1)
  }
}

func init() {
  rootCmd.PersistentFlags().IntVarP(&threads, "threads", "t", 1, "Number of threads")
}

cmd/foo.go中,我定义了一个子命令:

package cmd

import (
  "fmt"

  "github.com/spf13/cobra"
)

var count int

var fooCmd = &cobra.Command{
  Use:   "foo",
  Short: "The foo command",
  RunE:  runFoo,
}

func runFoo(cmd *cobra.Command, args []string) (err error) {
  fmt.Printf("This is the foo command; count=%d\n", count)
  return nil
}

func init() {
  fooCmd.Flags().IntVarP(&count, "count", "c", 0, "Count of foo")
  rootCmd.AddCommand(fooCmd)
}

有了上面的代码,我可以运行:

$ ./clitest
This is the root command, threads=1
$ ./clitest -t 12
This is the root command, threads=12
$ ./clitest foo
This is the foo command; count=0, threads=1
$ ./clitest foo -t 12 -c 2
This is the foo command; count=2, threads=12

我可以像这样为root命令编写一个测试:

package cmd

import (
    "testing"
)

func TestRootCmdWithArgs(t *testing.T) {
    rootCmd.SetArgs([]string{"-t", "12"})
    if err := rootCmd.Execute(); err != nil {
        t.Errorf("failed to execute rootCmd")
    }

    if threads != 12 {
        t.Errorf("expected 12, got %d", threads)
    }
}

func TestRootCmdInvalidArgs(t *testing.T) {
    rootCmd.SetArgs([]string{"--arg-that-does-not-exist"})
    if err := rootCmd.Execute(); err == nil {
        t.Errorf("command succeeded when it should have failed")
    }
}

func TestFooCmdWithArgs(t *testing.T) {
    rootCmd.SetArgs([]string{"foo", "-c", "2"})
    if err := rootCmd.Execute(); err != nil {
        t.Errorf("failed to execute rootCmd")
    }

    if count != 2 {
        t.Errorf("execpted 2, got %d", count)
    }
}

这些测试按预期成功:

$ go test ./...
?       clitest [no test files]
ok      clitest/cmd (cached)

您可以在this repository中找到此答案中引用的所有文件。

fwzugrvs

fwzugrvs2#

我想我找到问题了,多亏了Iarsks提供的例子。
我以前的根

func Execute() {
    RootCmd.CompletionOptions.HiddenDefaultCmd = true
    RootCmd.PersistentFlags().IntVarP(&threads, "threads", "t", 1, "Number of concurrent workers, using Stdin overrides this flag")
    RootCmd.PersistentFlags().StringVarP(&delimiter, "delimiter", "d", ",", "Choose delimiter")
    if err := RootCmd.Execute(); err != nil {
        fmt.Fprintf(os.Stderr, "csvutil encountered an error while executing")
        os.Exit(1)
    }
}

我将此函数分解为:

func init() {
    RootCmd.CompletionOptions.HiddenDefaultCmd = true
    RootCmd.PersistentFlags().IntVarP(&threads, "threads", "t", 1, "Number of concurrent workers, using Stdin overrides this flag")
    RootCmd.PersistentFlags().StringVarP(&delimiter, "delimiter", "d", ",", "Choose delimiter")
}

func Execute() {
    if err := RootCmd.Execute(); err != nil {
        fmt.Fprintf(os.Stderr, "csvutil encountered an error while executing")
        os.Exit(1)
    }
}

而现在它工作得很好。

相关问题