diff --git a/arguments.go b/arguments.go index d429a0c..92b72a9 100644 --- a/arguments.go +++ b/arguments.go @@ -41,8 +41,8 @@ type arguments struct { verbose bool } -func getArguments() (arguments, error) { - args, err := docopt.Parse(fmt.Sprintf(usage, defaultConcurrency), nil, true, "liche", true) +func getArguments(argv []string) (arguments, error) { + args, err := docopt.Parse(fmt.Sprintf(usage, defaultConcurrency), argv, true, "liche", true) if err != nil { return arguments{}, err @@ -54,12 +54,12 @@ func getArguments() (arguments, error) { return arguments{}, err } - t := 0.0 - if args["--document-root"] == nil { args["--document-root"] = "" } + t := 0.0 + if args["--timeout"] != nil { t, err = strconv.ParseFloat(args["--timeout"].(string), 64) diff --git a/arguments_test.go b/arguments_test.go new file mode 100644 index 0000000..a7cb642 --- /dev/null +++ b/arguments_test.go @@ -0,0 +1,65 @@ +package main + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestGetArguments(t *testing.T) { + for _, c := range []struct { + argv []string + args arguments + }{ + { + argv: []string{"file"}, + args: arguments{[]string{"file"}, "", defaultConcurrency, 0, false, false}, + }, + { + argv: []string{"-c", "42", "file"}, + args: arguments{[]string{"file"}, "", 42, 0, false, false}, + }, + { + argv: []string{"--concurrency", "42", "file"}, + args: arguments{[]string{"file"}, "", 42, 0, false, false}, + }, + { + argv: []string{"-d", "directory", "file"}, + args: arguments{[]string{"file"}, "directory", defaultConcurrency, 0, false, false}, + }, + { + argv: []string{"--document-root", "directory", "file"}, + args: arguments{[]string{"file"}, "directory", defaultConcurrency, 0, false, false}, + }, + { + argv: []string{"-r", "file"}, + args: arguments{[]string{"file"}, "", defaultConcurrency, 0, true, false}, + }, + { + argv: []string{"--recursive", "file"}, + args: arguments{[]string{"file"}, "", defaultConcurrency, 0, true, false}, + }, + { + argv: []string{"-t", "42", "file"}, + args: arguments{[]string{"file"}, "", defaultConcurrency, 42 * time.Second, false, false}, + }, + { + argv: []string{"--timeout", "42", "file"}, + args: arguments{[]string{"file"}, "", defaultConcurrency, 42 * time.Second, false, false}, + }, + { + argv: []string{"-v", "file"}, + args: arguments{[]string{"file"}, "", defaultConcurrency, 0, false, true}, + }, + { + argv: []string{"--verbose", "file"}, + args: arguments{[]string{"file"}, "", defaultConcurrency, 0, false, true}, + }, + } { + args, err := getArguments(c.argv) + + assert.Equal(t, nil, err) + assert.Equal(t, args, c.args) + } +} diff --git a/main.go b/main.go index 9a9c83a..e7f939b 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import "os" func main() { - args, err := getArguments() + args, err := getArguments(nil) if err != nil { printToStderr(err.Error())