Home > Software design >  Passing Persistant flags for Cobra CLI for testing
Passing Persistant flags for Cobra CLI for testing

Time:01-31

I have a CLI application written in Cobra. The app contains a root command RootCmd which contains two PersistentFlags. One of the persistent flags is called threads which has a default value of 1 and shorthand -h.

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

In the PersistentPreRun I have a check if the value is set to less than 1 to print the error message threads can't be less than 1 and exit.

The problem is I need to write some tests for the application and I can't find a way to set the flag. Currently, I test with os/exec, which is extremely annoying and doesn't provide code coverage.

I'm trying to do the following

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

This outputs the message Error: unknown shorthand flag: 't' in -t 12. Omitting the flag entirely and trying to use any subcommand shows the error message that the value can't be less than one (mind you I set a default value).

Is there a way to set flags other than SetArgs or a workaround?

CodePudding user response:

I can't reproduce this problem.

I have a simple Cobra application I've created by running cobra-cli init, and I've added a single subcommand, foo. That gives me this layout:

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

main.go is minimal:

package main

import "clitest/cmd"

func main() {
  cmd.Execute()
}

In cmd/root.go I add a single PersistentFlag, --threads (or -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")
}

In cmd/foo.go I define a single subcommand:

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)
}

With the above code in place, I can run:

$ ./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

I can write a test for the root command like this:

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)
    }
}

These tests succeed as expected:

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

You can find all the files referenced in this answer in this repository.

CodePudding user response:

I think I found the problem. Thanks to the example provided by Iarsks.

My previous root had


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)
    }
}

I split this function into:


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)
    }
}

And now it works fine.

  • Related