github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/cmd/pyroscope/command/exec_test.go (about)

     1  package command
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"testing"
     7  
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/pyroscope-io/pyroscope/pkg/cli"
    13  	"github.com/pyroscope-io/pyroscope/pkg/config"
    14  )
    15  
    16  type cmdArgsTestCase struct {
    17  	description  string
    18  	inputArgs    []string
    19  	expectedArgs []string
    20  	err          error
    21  }
    22  
    23  var appArgs = []string{"app", "-f", "1", "-b", "arg"}
    24  
    25  func execArgs(args ...string) []string { return append(args, appArgs...) }
    26  
    27  func TestExecCommand(t *testing.T) {
    28  	RegisterFailHandler(Fail)
    29  
    30  	testCases := []cmdArgsTestCase{
    31  		{
    32  			description: "no_arguments",
    33  			inputArgs:   []string{},
    34  		},
    35  		{
    36  			description: "help_flag",
    37  			inputArgs:   []string{"--help"},
    38  		},
    39  		{
    40  			description:  "delimiter",
    41  			inputArgs:    []string{cli.OptionsEnd},
    42  			expectedArgs: []string{},
    43  		},
    44  		{
    45  			description: "unknown_flag",
    46  			inputArgs:   []string{"--non-existing_flag"},
    47  			err:         errors.New("unknown flag: --non-existing_flag"),
    48  		},
    49  		{
    50  			description:  "exec_no_arguments",
    51  			inputArgs:    appArgs,
    52  			expectedArgs: appArgs,
    53  		},
    54  		{
    55  			description:  "exec_separated",
    56  			inputArgs:    execArgs("-spy-name", "debugspy", cli.OptionsEnd),
    57  			expectedArgs: appArgs,
    58  		},
    59  		{
    60  			description:  "exec_flags_mixed",
    61  			expectedArgs: appArgs,
    62  			inputArgs: execArgs(
    63  				"--spy-name=debugspy",
    64  				"--application-name", "app",
    65  				"--no-logging",
    66  				"-server-address=http://localhost:4040",
    67  				"-log-level", "debug",
    68  				"-no-logging",
    69  			),
    70  		},
    71  	}
    72  
    73  	for _, testCase := range testCases {
    74  		t.Run(testCase.description, func(t *testing.T) {
    75  			g := NewGomegaWithT(t)
    76  			cfg := new(config.Exec)
    77  			vpr := newViper()
    78  			var cmdArgs []string
    79  			cmd := &cobra.Command{
    80  				SilenceErrors:      true,
    81  				DisableFlagParsing: true,
    82  				RunE: cli.CreateCmdRunFn(cfg, vpr, func(_ *cobra.Command, args []string) error {
    83  					cmdArgs = args
    84  					return nil
    85  				}),
    86  			}
    87  
    88  			cmd.SetUsageFunc(printUsageMessage)
    89  			cmd.SetHelpFunc(printHelpMessage)
    90  			cmd.SetArgs(testCase.inputArgs)
    91  			cmd.SetOut(io.Discard)
    92  			cli.PopulateFlagSet(cfg, cmd.Flags(), vpr)
    93  
    94  			err := cmd.Execute()
    95  			if testCase.err == nil {
    96  				g.Expect(err).To(BeNil())
    97  				g.Expect(cmdArgs).To(Equal(testCase.expectedArgs))
    98  				return
    99  			}
   100  
   101  			g.Expect(err).To(Equal(testCase.err))
   102  		})
   103  	}
   104  }