ronantakizawa/codereview-bench huggingface dataset:
Repository: spf13/cobra
File: command_test.go (Go)

Code before:
			cmd.SetContext(ctx)
		},
	}
	child := &Command{
		Use: "child",
		Run: func(cmd *Command, args []string) {
			key := cmd.Context().Value(key{})
			got, ok := key.(string)
			if !ok {
				t.Error("key not found in context")
			}
			if got != val {
				t.Errorf("Expected value: \n %v\nGot:\n %v\n", val, got)
			}
		},
	}
	root.AddCommand(child)
	root.SetArgs([]string{"child"})
	err := root.Execute()
	if err != nil {
		t.Error(err)
	}
}

const VERSION_FLAG = "--version"
const HELP_FLAG = "--help"

func TestNoRootRunCommandExecutedWithVersionSet(t *testing.T) {
	rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description"}
	rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})

	output, err := executeCommand(rootCmd)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	checkStringContains(t, output, rootCmd.Long)
	checkStringContains(t, output, HELP_FLAG)
	checkStringContains(t, output, VERSION_FLAG)
}

func TestNoRootRunCommandExecutedWithoutVersionSet(t *testing.T) {
	rootCmd := &Command{Use: "root", Long: "Long description"}
	rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})

	output, err := executeCommand(rootCmd)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	checkStringContains(t, output, rootCmd.Long)

Review comment (bug):
I just noticed the linter is failing on these two names, it says: "don't use ALL_CAPS in Go names; use CamelCase"

Code after:
			cmd.SetContext(ctx)
		},
	}
	child := &Command{
		Use: "child",
		Run: func(cmd *Command, args []string) {
			key := cmd.Context().Value(key{})
			got, ok := key.(string)
			if !ok {
				t.Error("key not found in context")
			}
			if got != val {
				t.Errorf("Expected value: \n %v\nGot:\n %v\n", val, got)
			}
		},
	}
	root.AddCommand(child)
	root.SetArgs([]string{"child"})
	err := root.Execute()
	if err != nil {
		t.Error(err)
	}
}

const VersionFlag = "--version"
const HelpFlag = "--help"

func TestNoRootRunCommandExecutedWithVersionSet(t *testing.T) {
	rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description"}
	rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})

	output, err := executeCommand(rootCmd)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	checkStringContains(t, output, rootCmd.Long)
	checkStringContains(t, output, HelpFlag)
	checkStringContains(t, output, VersionFlag)
}

func TestNoRootRunCommandExecutedWithoutVersionSet(t *testing.T) {
	rootCmd := &Command{Use: "root", Long: "Long description"}
	rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})

	output, err := executeCommand(rootCmd)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	checkStringContains(t, output, rootCmd.Long)
Repository: spf13/cobra
File: command.go (Go)

Code before:
			return cmd, nil
		}

		// If root command has SilenceErrors flagged,
		// all subcommands should respect it
		if !cmd.SilenceErrors && !c.SilenceErrors {
			c.PrintErrln("Error:", err.Error())
		}

		// If root command has SilenceUsage flagged,
		// all subcommands should respect it
		if !cmd.SilenceUsage && !c.SilenceUsage {
			c.Println(cmd.UsageString())
		}
	}
	return cmd, err
}

func (c *Command) ValidateArgs(args []string) error {
	if c.Args == nil {
		return ArbitraryArgs(c, args)
	}
	return c.Args(c, args)
}

// ValidateRequiredFlags validates the required flags logic and returns all unset required flags.
func (c *Command) ValidateRequiredFlags() error {
	if c.DisableFlagParsing {
		return nil
	}

	flags := c.Flags()
	missingFlagNames := []string{}
	flags.VisitAll(func(pflag *flag.Flag) {
		requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag]
		if !found {
			return
		}
		if (requiredAnnotation[0] == "true") && !pflag.Changed {
			missingFlagNames = append(missingFlagNames, pflag.Name)
		}
	})

	if len(missingFlagNames) > 0 {
		return fmt.Errorf(`required flag(s) "%s" not set`, strings.Join(missingFlagNames, `", "`))
	}
	return nil
}

// InitDefaultHelpFlag adds default help flag to c.
// It is called automatically by executing the c or by calling help and usage.

Review comment (suggestion):
Sorry to be picky but can you change the godoc to:

// ValidateRequiredFlags validates all required flags are present and returns an error otherwise

Code after:
			return cmd, nil
		}

		// If root command has SilenceErrors flagged,
		// all subcommands should respect it
		if !cmd.SilenceErrors && !c.SilenceErrors {
			c.PrintErrln("Error:", err.Error())
		}

		// If root command has SilenceUsage flagged,
		// all subcommands should respect it
		if !cmd.SilenceUsage && !c.SilenceUsage {
			c.Println(cmd.UsageString())
		}
	}
	return cmd, err
}

func (c *Command) ValidateArgs(args []string) error {
	if c.Args == nil {
		return ArbitraryArgs(c, args)
	}
	return c.Args(c, args)
}

// ValidateRequiredFlags validates all required flags are present and returns an error otherwise
func (c *Command) ValidateRequiredFlags() error {
	if c.DisableFlagParsing {
		return nil
	}

	flags := c.Flags()
	missingFlagNames := []string{}
	flags.VisitAll(func(pflag *flag.Flag) {
		requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag]
		if !found {
			return
		}
		if (requiredAnnotation[0] == "true") && !pflag.Changed {
			missingFlagNames = append(missingFlagNames, pflag.Name)
		}
	})

	if len(missingFlagNames) > 0 {
		return fmt.Errorf(`required flag(s) "%s" not set`, strings.Join(missingFlagNames, `", "`))
	}
	return nil
}

// InitDefaultHelpFlag adds default help flag to c.
// It is called automatically by executing the c or by calling help and usage.
Repository: spf13/cobra
File: command_test.go (Go)

Code before:
		{
			args:                []string{"alternative", "ALIAS"},
			failWithoutEnabling: false,
		},
		{
			args:                []string{"alternative", "alias"},
			failWithoutEnabling: true,
		},
		{
			args:                []string{"CHILD", "alias"},
			failWithoutEnabling: true,
		},
		{
			args:                []string{"CHIld", "aliAS"},
			failWithoutEnabling: true,
		},
	}

	for _, test := range tests {
		for _, enableCaseInsensitivity := range []bool{true, false} {
			EnableCaseInsensitive = enableCaseInsensitivity

			output, err := executeCommand(rootCmd, test.args...)
			expectedFailure := test.failWithoutEnabling && !enableCaseInsensitivity

			if !expectedFailure && output != "" {
				t.Errorf("Unexpected output: %v", output)
			}
			if !expectedFailure && err != nil {
				t.Errorf("Unexpected error: %v", err)
			}
		}
	}

	EnableCaseInsensitive = defaultCaseInsensitive
}

func TestRemoveCommand(t *testing.T) {
	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
	childCmd := &Command{Use: "child", Run: emptyRun}
	rootCmd.AddCommand(childCmd)
	rootCmd.RemoveCommand(childCmd)

	_, err := executeCommand(rootCmd, "child")
	if err == nil {
		t.Error("Expected error on calling removed command. Got nil.")
	}
}

func TestReplaceCommandWithRemove(t *testing.T) {
	childUsed := 0

Review comment (security):
I've learned that it's the thoroughness of our tests that help us accept changes more quickly.
So forgive me for being demanding.

These new tests cover the cases where we set `EnableCaseInsensitive=true`.  But they won't detect is `EnableCaseInsensitive=false` but no error message is received.  Could that be added?

Code after:
		{
			args:                []string{"alternative", "ALIAS"},
			failWithoutEnabling: false,
		},
		{
			args:                []string{"alternative", "alias"},
			failWithoutEnabling: true,
		},
		{
			args:                []string{"CHILD", "alias"},
			failWithoutEnabling: true,
		},
		{
			args:                []string{"CHIld", "aliAS"},
			failWithoutEnabling: true,
		},
	}

	for _, test := range tests {
		for _, enableCaseInsensitivity := range []bool{true, false} {
			EnableCaseInsensitive = enableCaseInsensitivity

			output, err := executeCommand(rootCmd, test.args...)
			expectedFailure := test.failWithoutEnabling && !enableCaseInsensitivity

			if !expectedFailure && output != "" {
				t.Errorf("Unexpected output: %v", output)
			}
			if !expectedFailure && err != nil {
				t.Errorf("Unexpected error: %v", err)
			}
		}
	}

	EnableCaseInsensitive = defaultCaseInsensitive
}

// This test make sure we keep backwards-compatibility with respect
// to command names case sensitivity behavior.
func TestCaseSensitivityBackwardCompatibility(t *testing.T) {
	rootCmd := &Command{Use: "root", Run: emptyRun}
	childCmd := &Command{Use: "child", Run: emptyRun}

	rootCmd.AddCommand(childCmd)
	_, err := executeCommand(rootCmd, strings.ToUpper(childCmd.Use))
	if err == nil {
		t.Error("Expected error on calling a command in upper case while command names are case sensitive. Got nil.")
	}

}
Repository: spf13/cobra
File: completions.go (Go)

Code before:
)

var configEnvVarPrefixSubstRegexp = regexp.MustCompile(`[^A-Z0-9_]`)

// configEnvVar returns the name of the program-specific configuration environment
// variable.  It has the format <PROGRAM>_<SUFFIX> where <PROGRAM> is the name of the
// root command in upper case, with all non-ASCII-alphanumeric characters replaced by `_`.
func configEnvVar(name, suffix string) string {
	// This format should not be changed: users will be using it explicitly.
	v := strings.ToUpper(fmt.Sprintf("%s_%s", name, suffix))
	v = configEnvVarPrefixSubstRegexp.ReplaceAllString(v, "_")
	return v
}

// GetEnvConfig returns the value of the configuration environment variable
// <PROGRAM>_<SUFFIX> where <PROGRAM> is the name of the root command in upper
// case, with all non-ASCII-alphanumeric characters replaced by `_`.
// If the value is empty or not set, the value of the environment variable
// COBRA_<SUFFIX> is returned instead.
func GetEnvConfig(cmd *Command, suffix string) string {
	v := os.Getenv(configEnvVar(cmd.Root().Name(), suffix))
	if v == "" {
		v = os.Getenv(configEnvVar(configEnvVarGlobalPrefix, suffix))
	}
	return v
}


Review comment (suggestion):
I like your approach of prioritizing the `<PROGRAM>_<SUFFIX>` variable and only when not set to use the `COBRA_<SUFFIX>` one.

I think that would give more flexibility for ACTIVE_HELP also.  But let's do that in another PR.

Code after:

// These values should not be changed: users will be using them explicitly.
const (
	configEnvVarGlobalPrefix       = "COBRA"
	configEnvVarSuffixDescriptions = "COMPLETION_DESCRIPTIONS"
)

var configEnvVarPrefixSubstRegexp = regexp.MustCompile(`[^A-Z0-9_]`)

// configEnvVar returns the name of the program-specific configuration environment
// variable.  It has the format <PROGRAM>_<SUFFIX> where <PROGRAM> is the name of the
// root command in upper case, with all non-ASCII-alphanumeric characters replaced by `_`.
func configEnvVar(name, suffix string) string {
	// This format should not be changed: users will be using it explicitly.
	v := strings.ToUpper(fmt.Sprintf("%s_%s", name, suffix))
	v = configEnvVarPrefixSubstRegexp.ReplaceAllString(v, "_")
	return v
}

// getEnvConfig returns the value of the configuration environment variable
// <PROGRAM>_<SUFFIX> where <PROGRAM> is the name of the root command in upper
// case, with all non-ASCII-alphanumeric characters replaced by `_`.
// If the value is empty or not set, the value of the environment variable
// COBRA_<SUFFIX> is returned instead.
func getEnvConfig(cmd *Command, suffix string) string {
	v := os.Getenv(configEnvVar(cmd.Root().Name(), suffix))
	if v == "" {
		v = os.Getenv(configEnvVar(configEnvVarGlobalPrefix, suffix))
	}
	return v
}
Repository: spf13/cobra
File: completions_test.go (Go)

Code before:
			globalVal: "",
			expected:  "",
		},
		{
			use:       "foo.bar",
			suffix:    "test",
			cmdVar:    "FOO_BAR_TEST",
			globalVar: "COBRA_TEST",
			cmdVal:    "cmd",
			globalVal: "global",
			expected:  "cmd",
		},
		{
			use:       "quux-BAZ",
			suffix:    "test",
			cmdVar:    "QUUX_BAZ_TEST",
			globalVar: "COBRA_TEST",
			cmdVal:    "cmd",
			globalVal: "global",
			expected:  "cmd",
		},
	}

	for _, tc := range testCases {
		// Could make env handling cleaner with t.Setenv with Go >= 1.17
		func() {
			err := os.Setenv(tc.cmdVar, tc.cmdVal)
			defer assertNoErr(t, os.Unsetenv(tc.cmdVar))
			assertNoErr(t, err)
			err = os.Setenv(tc.globalVar, tc.globalVal)
			defer assertNoErr(t, os.Unsetenv(tc.globalVar))
			assertNoErr(t, err)
			cmd := &Command{Use: tc.use}
			got := GetEnvConfig(cmd, tc.suffix)
			if got != tc.expected {
				t.Errorf("expected: %q, got: %q", tc.expected, got)
			}
		}()
	}
}


Review comment (bug):
To be able to differentiate between the different tests upon a failure, you can replace `func()` with `t.Run(tc.name, func(t *testing.T)` and add a field `name` to your test struct.
Then you get (because I used `t.use` as the test case name because I was lazy):
```
=== RUN   TestGetEnvConfig/root
    completions_test.go:3591: expected: "cmd", got: ""
=== RUN   TestGetEnvConfig/root#01
    completions_test.go:3591: expected: "global", got: ""
=== RUN   TestGetEnvConfig/root#02
=== RUN   TestGetEnvConfig/foo.bar
    completions_test.go:3591: expected: "cmd", got: ""
=== RUN   TestGetEnvConfig/quux-BAZ
    completions_test.go:3591: expected: "cmd", got: ""
```

Code after:
			suffix:    "test",
			cmdVar:    "ROOT_TEST",
			globalVar: "COBRA_TEST",
			cmdVal:    "",
			globalVal: "",
			expected:  "",
		},
		{
			desc:      "Periods in command use transform to underscores in env var name",
			use:       "foo.bar",
			suffix:    "test",
			cmdVar:    "FOO_BAR_TEST",
			globalVar: "COBRA_TEST",
			cmdVal:    "cmd",
			globalVal: "global",
			expected:  "cmd",
		},
		{
			desc:      "Dashes in command use transform to underscores in env var name",
			use:       "quux-BAZ",
			suffix:    "test",
			cmdVar:    "QUUX_BAZ_TEST",
			globalVar: "COBRA_TEST",
			cmdVal:    "cmd",
			globalVal: "global",
			expected:  "cmd",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			// Could make env handling cleaner with t.Setenv with Go >= 1.17
			err := os.Setenv(tc.cmdVar, tc.cmdVal)
			defer func() {
				assertNoErr(t, os.Unsetenv(tc.cmdVar))
			}()
			assertNoErr(t, err)
			err = os.Setenv(tc.globalVar, tc.globalVal)
			defer func() {
				assertNoErr(t, os.Unsetenv(tc.globalVar))
			}()
			assertNoErr(t, err)
			cmd := &Command{Use: tc.use}
			got := getEnvConfig(cmd, tc.suffix)
			if got != tc.expected {
				t.Errorf("expected: %q, got: %q", tc.expected, got)
			}
		})
	}
}
Repository: spf13/cobra
File: completions_test.go (Go)

Code before:
		},
		{
			use:       "foo.bar",
			suffix:    "test",
			cmdVar:    "FOO_BAR_TEST",
			globalVar: "COBRA_TEST",
			cmdVal:    "cmd",
			globalVal: "global",
			expected:  "cmd",
		},
		{
			use:       "quux-BAZ",
			suffix:    "test",
			cmdVar:    "QUUX_BAZ_TEST",
			globalVar: "COBRA_TEST",
			cmdVal:    "cmd",
			globalVal: "global",
			expected:  "cmd",
		},
	}

	for _, tc := range testCases {
		// Could make env handling cleaner with t.Setenv with Go >= 1.17
		func() {
			err := os.Setenv(tc.cmdVar, tc.cmdVal)
			defer assertNoErr(t, os.Unsetenv(tc.cmdVar))
			assertNoErr(t, err)
			err = os.Setenv(tc.globalVar, tc.globalVal)
			defer assertNoErr(t, os.Unsetenv(tc.globalVar))
			assertNoErr(t, err)
			cmd := &Command{Use: tc.use}
			got := GetEnvConfig(cmd, tc.suffix)
			if got != tc.expected {
				t.Errorf("expected: %q, got: %q", tc.expected, got)
			}
		}()
	}
}


Review comment (bug):
I believe the tests fail because the call to ` os.Unsetenv()` is done right away, instead of in the `defer`.  Only `assertNoErr` is deferred.  If you want to achieve your intent you can do
```
defer func() {
   assertNoErr(t, os.Unsetenv(tc.cmdVar))
}()
```

Code after:
			globalVar: "COBRA_TEST",
			cmdVal:    "",
			globalVal: "",
			expected:  "",
		},
		{
			desc:      "Periods in command use transform to underscores in env var name",
			use:       "foo.bar",
			suffix:    "test",
			cmdVar:    "FOO_BAR_TEST",
			globalVar: "COBRA_TEST",
			cmdVal:    "cmd",
			globalVal: "global",
			expected:  "cmd",
		},
		{
			desc:      "Dashes in command use transform to underscores in env var name",
			use:       "quux-BAZ",
			suffix:    "test",
			cmdVar:    "QUUX_BAZ_TEST",
			globalVar: "COBRA_TEST",
			cmdVal:    "cmd",
			globalVal: "global",
			expected:  "cmd",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			// Could make env handling cleaner with t.Setenv with Go >= 1.17
			err := os.Setenv(tc.cmdVar, tc.cmdVal)
			defer func() {
				assertNoErr(t, os.Unsetenv(tc.cmdVar))
			}()
			assertNoErr(t, err)
			err = os.Setenv(tc.globalVar, tc.globalVal)
			defer func() {
				assertNoErr(t, os.Unsetenv(tc.globalVar))
			}()
			assertNoErr(t, err)
			cmd := &Command{Use: tc.use}
			got := getEnvConfig(cmd, tc.suffix)
			if got != tc.expected {
				t.Errorf("expected: %q, got: %q", tc.expected, got)
			}
		})
	}
}

func TestDisableDescriptions(t *testing.T) {
	rootCmd := &Command{
Repository: spf13/cobra
File: cobra_test.go (Go)

Code before:
			padding:     0,
			expected:    "cobra",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Act
			got := rpad(tt.inputString, tt.padding)

			// Assert
			if got != tt.expected {
				t.Errorf("Expected rpad: %v\nGot: %v", tt.expected, got)
			}
		})
	}
}

func TestDeadcodeElimination(t *testing.T) {
	// check that a simple program using cobra in its default configuration is
	// linked with deadcode elimination enabled.
	const (
		dirname  = "test_deadcode"
		progname = "test_deadcode_elimination"
	)
	os.Mkdir(dirname, 0770)
	filename := filepath.Join(dirname, progname+".go")
	err := os.WriteFile(filename, []byte(`package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Version: "1.0",
	Use:     "example_program",
	Short:   "example_program - test fixture to check that deadcode elimination is allowed",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("hello world")
	},
	Aliases: []string{"alias1", "alias2"},
	Example: "stringer --help",
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintf(os.Stderr, "Whoops. There was an error while executing your CLI '%s'", err)
		os.Exit(1)

Review comment (suggestion):
I'm getting a warning here.
Can you change line to `_ = os.Mkdir(dirname, 0770)`

Code after:
			inputString: "cobra",
			padding:     0,
			expected:    "cobra",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Act
			got := rpad(tt.inputString, tt.padding)

			// Assert
			if got != tt.expected {
				t.Errorf("Expected rpad: %v\nGot: %v", tt.expected, got)
			}
		})
	}
}

// TestDeadcodeElimination checks that a simple program using cobra in its
// default configuration is linked taking full advantage of the linker's
// deadcode elimination step.
//
// If reflect.Value.MethodByName/reflect.Value.Method are reachable the
// linker will not always be able to prove that exported methods are
// unreachable, making deadcode elimination less effective. Using
// text/template and html/template makes reflect.Value.MethodByName
// reachable.
// Since cobra can use text/template templates this test checks that in its
// default configuration that code path can be proven to be unreachable by
// the linker.
//
// See also: https://github.com/spf13/cobra/pull/1956
func TestDeadcodeElimination(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("go tool nm fails on windows")
	}

	// check that a simple program using cobra in its default configuration is
	// linked with deadcode elimination enabled.
	const (
		dirname  = "test_deadcode"
		progname = "test_deadcode_elimination"
	)
	_ = os.Mkdir(dirname, 0770)
	defer os.RemoveAll(dirname)
	filename := filepath.Join(dirname, progname+".go")
	err := os.WriteFile(filename, []byte(`package main

import (
	"fmt"
Repository: spf13/cobra
File: cobra_test.go (Go)

Code before:

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Version: "1.0",
	Use:     "example_program",
	Short:   "example_program - test fixture to check that deadcode elimination is allowed",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("hello world")
	},
	Aliases: []string{"alias1", "alias2"},
	Example: "stringer --help",
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintf(os.Stderr, "Whoops. There was an error while executing your CLI '%s'", err)
		os.Exit(1)
	}
}
`), 0660)
	if err != nil {
		t.Fatalf("could not write test program: %v", err)
	}
	defer os.RemoveAll(dirname)
	buf, err := exec.Command("go", "build", filename).CombinedOutput()
	if err != nil {
		t.Fatalf("could not compile test program: %s", string(buf))
	}
	defer os.Remove(progname)
	buf, err = exec.Command("go", "tool", "nm", progname).CombinedOutput()
	if err != nil {
		t.Fatalf("could not run go tool nm: %v", err)
	}
	if strings.Contains(string(buf), "MethodByName") {
		t.Error("compiled programs contains MethodByName symbol")
	}
}


Review comment (security):
I'm getting a warning here.
Can you change the permissions to `0600`

Code after:
// Since cobra can use text/template templates this test checks that in its
// default configuration that code path can be proven to be unreachable by
// the linker.
//
// See also: https://github.com/spf13/cobra/pull/1956
func TestDeadcodeElimination(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("go tool nm fails on windows")
	}

	// check that a simple program using cobra in its default configuration is
	// linked with deadcode elimination enabled.
	const (
		dirname  = "test_deadcode"
		progname = "test_deadcode_elimination"
	)
	_ = os.Mkdir(dirname, 0770)
	defer os.RemoveAll(dirname)
	filename := filepath.Join(dirname, progname+".go")
	err := os.WriteFile(filename, []byte(`package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Version: "1.0",
	Use:     "example_program",
	Short:   "example_program - test fixture to check that deadcode elimination is allowed",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("hello world")
	},
	Aliases: []string{"alias1", "alias2"},
	Example: "stringer --help",
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintf(os.Stderr, "Whoops. There was an error while executing your CLI '%s'", err)
		os.Exit(1)
	}
}
`), 0600)
	if err != nil {
		t.Fatalf("could not write test program: %v", err)
	}
	buf, err := exec.Command("go", "build", filename).CombinedOutput()
	if err != nil {