2017-02-13 23:05:37 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain_cliArgsFromEnv(t *testing.T) {
|
|
|
|
// Setup the state. This test really messes with the environment and
|
|
|
|
// global state so we set things up to be restored.
|
|
|
|
|
|
|
|
// Restore original CLI args
|
|
|
|
oldArgs := os.Args
|
|
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
|
|
|
|
// Setup test command and restore that
|
2017-09-02 00:58:38 +02:00
|
|
|
Commands = make(map[string]cli.CommandFactory)
|
|
|
|
defer func() {
|
|
|
|
Commands = nil
|
|
|
|
}()
|
2017-02-13 23:05:37 +01:00
|
|
|
testCommandName := "unit-test-cli-args"
|
|
|
|
testCommand := &testCommandCLI{}
|
|
|
|
Commands[testCommandName] = func() (cli.Command, error) {
|
|
|
|
return testCommand, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Args []string
|
|
|
|
Value string
|
|
|
|
Expected []string
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"no env",
|
|
|
|
[]string{testCommandName, "foo", "bar"},
|
|
|
|
"",
|
|
|
|
[]string{"foo", "bar"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"both env var and CLI",
|
|
|
|
[]string{testCommandName, "foo", "bar"},
|
|
|
|
"-foo bar",
|
|
|
|
[]string{"-foo", "bar", "foo", "bar"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"only env var",
|
|
|
|
[]string{testCommandName},
|
|
|
|
"-foo bar",
|
|
|
|
[]string{"-foo", "bar"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2017-02-13 23:53:50 +01:00
|
|
|
{
|
|
|
|
"cli string has blank values",
|
|
|
|
[]string{testCommandName, "bar", "", "baz"},
|
|
|
|
"-foo bar",
|
|
|
|
[]string{"-foo", "bar", "bar", "", "baz"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"cli string has blank values before the command",
|
|
|
|
[]string{"", testCommandName, "bar"},
|
|
|
|
"-foo bar",
|
|
|
|
[]string{"-foo", "bar", "bar"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2017-02-13 23:05:37 +01:00
|
|
|
{
|
|
|
|
// this should fail gracefully, this is just testing
|
|
|
|
// that we don't panic with our slice arithmetic
|
|
|
|
"no command",
|
|
|
|
[]string{},
|
|
|
|
"-foo bar",
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"single quoted strings",
|
|
|
|
[]string{testCommandName, "foo"},
|
|
|
|
"-foo 'bar baz'",
|
|
|
|
[]string{"-foo", "bar baz", "foo"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"double quoted strings",
|
|
|
|
[]string{testCommandName, "foo"},
|
|
|
|
`-foo "bar baz"`,
|
|
|
|
[]string{"-foo", "bar baz", "foo"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"double quoted single quoted strings",
|
|
|
|
[]string{testCommandName, "foo"},
|
|
|
|
`-foo "'bar baz'"`,
|
|
|
|
[]string{"-foo", "'bar baz'", "foo"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
|
|
|
|
os.Unsetenv(EnvCLI)
|
2017-02-14 00:12:29 +01:00
|
|
|
defer os.Unsetenv(EnvCLI)
|
2017-02-13 23:05:37 +01:00
|
|
|
|
|
|
|
// Set the env var value
|
|
|
|
if tc.Value != "" {
|
|
|
|
if err := os.Setenv(EnvCLI, tc.Value); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the args
|
|
|
|
args := make([]string, len(tc.Args)+1)
|
|
|
|
args[0] = oldArgs[0] // process name
|
|
|
|
copy(args[1:], tc.Args)
|
|
|
|
|
|
|
|
// Run it!
|
|
|
|
os.Args = args
|
|
|
|
testCommand.Args = nil
|
|
|
|
exit := wrappedMain()
|
|
|
|
if (exit != 0) != tc.Err {
|
|
|
|
t.Fatalf("bad: %d", exit)
|
|
|
|
}
|
|
|
|
if tc.Err {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
if !reflect.DeepEqual(testCommand.Args, tc.Expected) {
|
|
|
|
t.Fatalf("bad: %#v", testCommand.Args)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 00:12:29 +01:00
|
|
|
// This test just has more options than the test above. Use this for
|
|
|
|
// more control over behavior at the expense of more complex test structures.
|
|
|
|
func TestMain_cliArgsFromEnvAdvanced(t *testing.T) {
|
|
|
|
// Restore original CLI args
|
|
|
|
oldArgs := os.Args
|
|
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
|
2017-09-02 00:58:38 +02:00
|
|
|
// Setup test command and restore that
|
|
|
|
Commands = make(map[string]cli.CommandFactory)
|
|
|
|
defer func() {
|
|
|
|
Commands = nil
|
|
|
|
}()
|
|
|
|
|
2017-02-14 00:12:29 +01:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Command string
|
|
|
|
EnvVar string
|
|
|
|
Args []string
|
|
|
|
Value string
|
|
|
|
Expected []string
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"targeted to another command",
|
|
|
|
"command",
|
|
|
|
EnvCLI + "_foo",
|
|
|
|
[]string{"command", "foo", "bar"},
|
|
|
|
"-flag",
|
|
|
|
[]string{"foo", "bar"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"targeted to this command",
|
|
|
|
"command",
|
|
|
|
EnvCLI + "_command",
|
|
|
|
[]string{"command", "foo", "bar"},
|
|
|
|
"-flag",
|
|
|
|
[]string{"-flag", "foo", "bar"},
|
|
|
|
false,
|
|
|
|
},
|
2017-02-14 00:18:50 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
"targeted to a command with a hyphen",
|
|
|
|
"command-name",
|
|
|
|
EnvCLI + "_command_name",
|
|
|
|
[]string{"command-name", "foo", "bar"},
|
|
|
|
"-flag",
|
|
|
|
[]string{"-flag", "foo", "bar"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"targeted to a command with a space",
|
|
|
|
"command name",
|
|
|
|
EnvCLI + "_command_name",
|
|
|
|
[]string{"command", "name", "foo", "bar"},
|
|
|
|
"-flag",
|
|
|
|
[]string{"-flag", "foo", "bar"},
|
|
|
|
false,
|
|
|
|
},
|
2017-02-14 00:12:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
|
|
|
|
// Setup test command and restore that
|
|
|
|
testCommandName := tc.Command
|
|
|
|
testCommand := &testCommandCLI{}
|
|
|
|
defer func() { delete(Commands, testCommandName) }()
|
|
|
|
Commands[testCommandName] = func() (cli.Command, error) {
|
|
|
|
return testCommand, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Unsetenv(tc.EnvVar)
|
|
|
|
defer os.Unsetenv(tc.EnvVar)
|
|
|
|
|
|
|
|
// Set the env var value
|
|
|
|
if tc.Value != "" {
|
|
|
|
if err := os.Setenv(tc.EnvVar, tc.Value); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the args
|
|
|
|
args := make([]string, len(tc.Args)+1)
|
|
|
|
args[0] = oldArgs[0] // process name
|
|
|
|
copy(args[1:], tc.Args)
|
|
|
|
|
|
|
|
// Run it!
|
|
|
|
os.Args = args
|
|
|
|
testCommand.Args = nil
|
|
|
|
exit := wrappedMain()
|
|
|
|
if (exit != 0) != tc.Err {
|
2017-09-02 00:58:38 +02:00
|
|
|
t.Fatalf("unexpected exit status %d; want 0", exit)
|
2017-02-14 00:12:29 +01:00
|
|
|
}
|
|
|
|
if tc.Err {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
if !reflect.DeepEqual(testCommand.Args, tc.Expected) {
|
|
|
|
t.Fatalf("bad: %#v", testCommand.Args)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 23:05:37 +01:00
|
|
|
type testCommandCLI struct {
|
|
|
|
Args []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testCommandCLI) Run(args []string) int {
|
|
|
|
c.Args = args
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testCommandCLI) Synopsis() string { return "" }
|
|
|
|
func (c *testCommandCLI) Help() string { return "" }
|