main: make prefixOArgs errors testable
By returning an error instead of calling os.Exit, error cases can be tested easily. Error cases were not tested until now.
This commit is contained in:
parent
98aa9cb176
commit
e29a81efc3
19
cli_args.go
19
cli_args.go
@ -42,16 +42,16 @@ var flagSet *flag.FlagSet
|
||||
// prefixOArgs transform options passed via "-o foo,bar" into regular options
|
||||
// like "-foo -bar" and prefixes them to the command line.
|
||||
// Testcases in TestPrefixOArgs().
|
||||
func prefixOArgs(osArgs []string) []string {
|
||||
func prefixOArgs(osArgs []string) ([]string, error) {
|
||||
// Need at least 3, example: gocryptfs -o foo,bar
|
||||
// ^ 0 ^ 1 ^ 2
|
||||
if len(osArgs) < 3 {
|
||||
return osArgs
|
||||
return osArgs, nil
|
||||
}
|
||||
// Passing "--" disables "-o" parsing. Ignore element 0 (program name).
|
||||
for _, v := range osArgs[1:] {
|
||||
if v == "--" {
|
||||
return osArgs
|
||||
return osArgs, nil
|
||||
}
|
||||
}
|
||||
// Find and extract "-o foo,bar"
|
||||
@ -60,8 +60,7 @@ func prefixOArgs(osArgs []string) []string {
|
||||
if osArgs[i] == "-o" {
|
||||
// Last argument?
|
||||
if i+1 >= len(osArgs) {
|
||||
tlog.Fatal.Printf("The \"-o\" option requires an argument")
|
||||
os.Exit(exitcodes.Usage)
|
||||
return nil, fmt.Errorf("The \"-o\" option requires an argument")
|
||||
}
|
||||
oOpts = strings.Split(osArgs[i+1], ",")
|
||||
// Skip over the arguments to "-o"
|
||||
@ -87,16 +86,20 @@ func prefixOArgs(osArgs []string) []string {
|
||||
}
|
||||
// Add other arguments
|
||||
newArgs = append(newArgs, otherArgs...)
|
||||
return newArgs
|
||||
return newArgs, nil
|
||||
}
|
||||
|
||||
// parseCliOpts - parse command line options (i.e. arguments that start with "-")
|
||||
func parseCliOpts() (args argContainer) {
|
||||
os.Args = prefixOArgs(os.Args)
|
||||
|
||||
var err error
|
||||
var opensslAuto string
|
||||
|
||||
os.Args, err = prefixOArgs(os.Args)
|
||||
if err != nil {
|
||||
tlog.Fatal.Println(err)
|
||||
os.Exit(exitcodes.Usage)
|
||||
}
|
||||
|
||||
flagSet = flag.NewFlagSet(tlog.ProgramName, flag.ContinueOnError)
|
||||
flagSet.Usage = helpShort
|
||||
flagSet.BoolVar(&args.debug, "d", false, "")
|
||||
|
@ -10,6 +10,8 @@ type testcase struct {
|
||||
i []string
|
||||
// o is the expected output
|
||||
o []string
|
||||
// Do we expect an error?
|
||||
e bool
|
||||
}
|
||||
|
||||
// TestPrefixOArgs checks that the "-o x,y,z" parsing works correctly.
|
||||
@ -61,11 +63,17 @@ func TestPrefixOArgs(t *testing.T) {
|
||||
i: []string{"gocryptfs", "--", "-o", "a"},
|
||||
o: []string{"gocryptfs", "--", "-o", "a"},
|
||||
},
|
||||
// This should error out
|
||||
{
|
||||
i: []string{"gocryptfs", "foo", "bar", "-o"},
|
||||
e: true,
|
||||
},
|
||||
}
|
||||
for _, tc := range testcases {
|
||||
o := prefixOArgs(tc.i)
|
||||
if !reflect.DeepEqual(o, tc.o) {
|
||||
t.Errorf("\n in=%q\nwant=%q\n got=%q", tc.i, tc.o, o)
|
||||
o, err := prefixOArgs(tc.i)
|
||||
e := (err != nil)
|
||||
if !reflect.DeepEqual(o, tc.o) || e != tc.e {
|
||||
t.Errorf("\n in=%q\nwant=%q err=%v\n got=%q err=%v", tc.i, tc.o, tc.e, o, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -448,3 +448,15 @@ func TestMultipleOperationFlags(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test that a missing argument to "-o" triggers exit code 1.
|
||||
// See also cli_args_test.go for comprehensive tests of "-o" parsing.
|
||||
func TestMissingOArg(t *testing.T) {
|
||||
cmd := exec.Command(test_helpers.GocryptfsBinary, "foo", "bar", "-o")
|
||||
err := cmd.Run()
|
||||
exitCode := test_helpers.ExtractCmdExitCode(err)
|
||||
if exitCode != exitcodes.Usage {
|
||||
t.Fatalf("this should have failed with code %d, but returned %d",
|
||||
exitcodes.Usage, exitCode)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user