From 2a5b2f2f4a21c77d9b7f518d0607875d8af220c8 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sat, 2 May 2015 22:35:10 +0200 Subject: [PATCH 1/7] parse_test: move helper functions License: MIT Signed-off-by: Christian Couder --- commands/cli/parse_test.go | 48 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/commands/cli/parse_test.go b/commands/cli/parse_test.go index edbca7f76..a1fbe13a3 100644 --- a/commands/cli/parse_test.go +++ b/commands/cli/parse_test.go @@ -7,6 +7,30 @@ import ( "github.com/ipfs/go-ipfs/commands" ) +type kvs map[string]interface{} +type words []string + +func sameWords(a words, b words) bool { + for i, w := range a { + if w != b[i] { + return false + } + } + return true +} + +func sameKVs(a kvs, b kvs) bool { + if len(a) != len(b) { + return false + } + for k, v := range a { + if v != b[k] { + return false + } + } + return true +} + func TestOptionParsing(t *testing.T) { subCmd := &commands.Command{} cmd := &commands.Command{ @@ -19,30 +43,6 @@ func TestOptionParsing(t *testing.T) { }, } - type kvs map[string]interface{} - type words []string - - sameWords := func(a words, b words) bool { - for i, w := range a { - if w != b[i] { - return false - } - } - return true - } - - sameKVs := func(a kvs, b kvs) bool { - if len(a) != len(b) { - return false - } - for k, v := range a { - if v != b[k] { - return false - } - } - return true - } - testHelper := func(args string, expectedOpts kvs, expectedWords words, expectErr bool) { _, opts, input, _, err := parseOpts(strings.Split(args, " "), cmd) if expectErr { From 3e4a06945f0af95f1ae99fe1a8478699be5ccc6d Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sat, 2 May 2015 22:47:03 +0200 Subject: [PATCH 2/7] parse_test: fix and test sameWords() License: MIT Signed-off-by: Christian Couder --- commands/cli/parse_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/commands/cli/parse_test.go b/commands/cli/parse_test.go index a1fbe13a3..286a39170 100644 --- a/commands/cli/parse_test.go +++ b/commands/cli/parse_test.go @@ -11,6 +11,9 @@ type kvs map[string]interface{} type words []string func sameWords(a words, b words) bool { + if len(a) != len(b) { + return false + } for i, w := range a { if w != b[i] { return false @@ -31,6 +34,33 @@ func sameKVs(a kvs, b kvs) bool { return true } +func TestSameWords(t *testing.T) { + a := []string{"v1", "v2"} + b := []string{"v1", "v2", "v3"} + c := []string{"v2", "v3"} + d := []string{"v2"} + e := []string{"v2", "v3"} + f := []string{"v2", "v1"} + + test := func(a words, b words, v bool) { + if sameWords(a, b) != v { + t.Errorf("sameWords('%v', '%v') != %v", a, b, v) + } + } + + test(a, b, false) + test(a, a, true) + test(a, c, false) + test(b, c, false) + test(c, d, false) + test(c, e, true) + test(b, e, false) + test(a, b, false) + test(a, f, false) + test(e, f, false) + test(f, f, true) +} + func TestOptionParsing(t *testing.T) { subCmd := &commands.Command{} cmd := &commands.Command{ From d0752a714dcfc6887e6f37934aa6156965df64e7 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sat, 2 May 2015 04:05:57 +0200 Subject: [PATCH 3/7] parse_test: add tests for stdin enabled arg Let's document how stdin enabled arguments currently work by adding some tests. License: MIT Signed-off-by: Christian Couder --- commands/cli/parse_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/commands/cli/parse_test.go b/commands/cli/parse_test.go index 286a39170..92dca144b 100644 --- a/commands/cli/parse_test.go +++ b/commands/cli/parse_test.go @@ -3,6 +3,9 @@ package cli import ( "strings" "testing" + "io" + "io/ioutil" + "os" "github.com/ipfs/go-ipfs/commands" ) @@ -147,6 +150,11 @@ func TestArgumentParsing(t *testing.T) { commands.StringArg("b", true, false, "another arg"), }, }, + "stdinenabled": &commands.Command{ + Arguments: []commands.Argument{ + commands.StringArg("a", true, true, "some arg").EnableStdin(), + }, + }, }, } @@ -219,4 +227,31 @@ func TestArgumentParsing(t *testing.T) { if err == nil { t.Error("Should have failed (provided too many args, only takes 1)") } + + // Use a temp file to simulate stdin + fstdin, err := ioutil.TempFile("", "") + if err != nil { + t.Fatal(err) + } + defer os.Remove(fstdin.Name()) + + if _, err := io.WriteString(fstdin, "stdin1"); err != nil { + t.Fatal(err) + } + + _, _, _, err = Parse([]string{"stdinenabled", "value1", "value2"}, nil, rootCmd) + if err != nil { + t.Error("Should have passed") + t.Fatal(err) + } + _, _, _, err = Parse([]string{"stdinenabled"}, fstdin, rootCmd) + if err != nil { + t.Error("Should have passed") + t.Fatal(err) + } + _, _, _, err = Parse([]string{"stdinenabled", "value1"}, fstdin, rootCmd) + if err != nil { + t.Error("Should have passed") + t.Fatal(err) + } } From 58126c1c6ccd733c15170a1c9ce8536eff1e46f6 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sat, 2 May 2015 04:50:42 +0200 Subject: [PATCH 4/7] parse_test: improve tests with stdin enabled arg Now also check that we get the right arguments from the parsing. License: MIT Signed-off-by: Christian Couder --- commands/cli/parse_test.go | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/commands/cli/parse_test.go b/commands/cli/parse_test.go index 92dca144b..15635be41 100644 --- a/commands/cli/parse_test.go +++ b/commands/cli/parse_test.go @@ -239,19 +239,23 @@ func TestArgumentParsing(t *testing.T) { t.Fatal(err) } - _, _, _, err = Parse([]string{"stdinenabled", "value1", "value2"}, nil, rootCmd) - if err != nil { - t.Error("Should have passed") - t.Fatal(err) - } - _, _, _, err = Parse([]string{"stdinenabled"}, fstdin, rootCmd) - if err != nil { - t.Error("Should have passed") - t.Fatal(err) - } - _, _, _, err = Parse([]string{"stdinenabled", "value1"}, fstdin, rootCmd) - if err != nil { - t.Error("Should have passed") - t.Fatal(err) + test := func(cmd words, f *os.File, res words) { + if f != nil { + if _, err := f.Seek(0, os.SEEK_SET); err != nil { + t.Fatal(err) + } + } + req, _, _, err := Parse(cmd, f, rootCmd) + if err != nil { + t.Error("Command '%v' should have passed parsing", cmd) + } + if !sameWords(req.Arguments(), res) { + t.Errorf("Arguments parsed from '%v' are not '%v'", cmd, res) + } } + + test([]string{"stdinenabled", "value1", "value2"}, nil, []string{"value1", "value2"}) + test([]string{"stdinenabled"}, fstdin, []string{"stdin1"}) + test([]string{"stdinenabled", "value1"}, fstdin, []string{"stdin1", "value1"}) + test([]string{"stdinenabled", "value1", "value2"}, fstdin, []string{"stdin1", "value1", "value2"}) } From c6dcfaaf5d706b20b20320d0c9872a8e751c63b0 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sun, 3 May 2015 10:14:56 +0200 Subject: [PATCH 5/7] parse_test: use fileToSimulateStdin() License: MIT Signed-off-by: Christian Couder --- commands/cli/parse_test.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/commands/cli/parse_test.go b/commands/cli/parse_test.go index 15635be41..27b1dfc5d 100644 --- a/commands/cli/parse_test.go +++ b/commands/cli/parse_test.go @@ -229,14 +229,17 @@ func TestArgumentParsing(t *testing.T) { } // Use a temp file to simulate stdin - fstdin, err := ioutil.TempFile("", "") - if err != nil { - t.Fatal(err) - } - defer os.Remove(fstdin.Name()) + fileToSimulateStdin := func(t *testing.T, content string) (*os.File) { + fstdin, err := ioutil.TempFile("", "") + if err != nil { + t.Fatal(err) + } + defer os.Remove(fstdin.Name()) - if _, err := io.WriteString(fstdin, "stdin1"); err != nil { - t.Fatal(err) + if _, err := io.WriteString(fstdin, content); err != nil { + t.Fatal(err) + } + return fstdin } test := func(cmd words, f *os.File, res words) { @@ -255,6 +258,9 @@ func TestArgumentParsing(t *testing.T) { } test([]string{"stdinenabled", "value1", "value2"}, nil, []string{"value1", "value2"}) + + fstdin := fileToSimulateStdin(t, "stdin1") + test([]string{"stdinenabled"}, fstdin, []string{"stdin1"}) test([]string{"stdinenabled", "value1"}, fstdin, []string{"stdin1", "value1"}) test([]string{"stdinenabled", "value1", "value2"}, fstdin, []string{"stdin1", "value1", "value2"}) From 47a88f84294f66faa9550114766df9433ce518ed Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sun, 3 May 2015 20:10:28 +0200 Subject: [PATCH 6/7] parse_test: improve existing tests License: MIT Signed-off-by: Christian Couder --- commands/cli/parse_test.go | 80 ++++++++++++++------------------------ 1 file changed, 29 insertions(+), 51 deletions(-) diff --git a/commands/cli/parse_test.go b/commands/cli/parse_test.go index 27b1dfc5d..c9b4a11bd 100644 --- a/commands/cli/parse_test.go +++ b/commands/cli/parse_test.go @@ -158,28 +158,37 @@ func TestArgumentParsing(t *testing.T) { }, } - _, _, _, err := Parse([]string{"noarg"}, nil, rootCmd) - if err != nil { - t.Error("Should have passed") + test := func(cmd words, f *os.File, res words) { + if f != nil { + if _, err := f.Seek(0, os.SEEK_SET); err != nil { + t.Fatal(err) + } + } + req, _, _, err := Parse(cmd, f, rootCmd) + if err != nil { + t.Errorf("Command '%v' should have passed parsing", cmd) + } + if !sameWords(req.Arguments(), res) { + t.Errorf("Arguments parsed from '%v' are not '%v'", cmd, res) + } } - _, _, _, err = Parse([]string{"noarg", "value!"}, nil, rootCmd) + + test([]string{"noarg"}, nil, []string{}) + + _, _, _, err := Parse([]string{"noarg", "value!"}, nil, rootCmd) if err == nil { t.Error("Should have failed (provided an arg, but command didn't define any)") } - _, _, _, err = Parse([]string{"onearg", "value!"}, nil, rootCmd) - if err != nil { - t.Error("Should have passed") - } + test([]string{"onearg", "value!"}, nil, []string{"value!"}) + _, _, _, err = Parse([]string{"onearg"}, nil, rootCmd) if err == nil { t.Error("Should have failed (didn't provide any args, arg is required)") } - _, _, _, err = Parse([]string{"twoargs", "value1", "value2"}, nil, rootCmd) - if err != nil { - t.Error("Should have passed") - } + test([]string{"twoargs", "value1", "value2"}, nil, []string{"value1", "value2"}) + _, _, _, err = Parse([]string{"twoargs", "value!"}, nil, rootCmd) if err == nil { t.Error("Should have failed (only provided 1 arg, needs 2)") @@ -189,36 +198,20 @@ func TestArgumentParsing(t *testing.T) { t.Error("Should have failed (didn't provide any args, 2 required)") } - _, _, _, err = Parse([]string{"variadic", "value!"}, nil, rootCmd) - if err != nil { - t.Error("Should have passed") - } - _, _, _, err = Parse([]string{"variadic", "value1", "value2", "value3"}, nil, rootCmd) - if err != nil { - t.Error("Should have passed") - } + test([]string{"variadic", "value!"}, nil, []string{"value!"}) + test([]string{"variadic", "value1", "value2", "value3"}, nil, []string{"value1", "value2", "value3"}) + _, _, _, err = Parse([]string{"variadic"}, nil, rootCmd) if err == nil { t.Error("Should have failed (didn't provide any args, 1 required)") } - _, _, _, err = Parse([]string{"optional", "value!"}, nil, rootCmd) - if err != nil { - t.Error("Should have passed") - } - _, _, _, err = Parse([]string{"optional"}, nil, rootCmd) - if err != nil { - t.Error("Should have passed") - } + test([]string{"optional", "value!"}, nil, []string{"value!"}) + test([]string{"optional"}, nil, []string{}) + + test([]string{"reversedoptional", "value1", "value2"}, nil, []string{"value1", "value2"}) + test([]string{"reversedoptional", "value!"}, nil, []string{"value!"}) - _, _, _, err = Parse([]string{"reversedoptional", "value1", "value2"}, nil, rootCmd) - if err != nil { - t.Error("Should have passed") - } - _, _, _, err = Parse([]string{"reversedoptional", "value!"}, nil, rootCmd) - if err != nil { - t.Error("Should have passed") - } _, _, _, err = Parse([]string{"reversedoptional"}, nil, rootCmd) if err == nil { t.Error("Should have failed (didn't provide any args, 1 required)") @@ -242,21 +235,6 @@ func TestArgumentParsing(t *testing.T) { return fstdin } - test := func(cmd words, f *os.File, res words) { - if f != nil { - if _, err := f.Seek(0, os.SEEK_SET); err != nil { - t.Fatal(err) - } - } - req, _, _, err := Parse(cmd, f, rootCmd) - if err != nil { - t.Error("Command '%v' should have passed parsing", cmd) - } - if !sameWords(req.Arguments(), res) { - t.Errorf("Arguments parsed from '%v' are not '%v'", cmd, res) - } - } - test([]string{"stdinenabled", "value1", "value2"}, nil, []string{"value1", "value2"}) fstdin := fileToSimulateStdin(t, "stdin1") From 93f253e00b11921f593f21b001acd5f4c597d005 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sun, 3 May 2015 20:18:40 +0200 Subject: [PATCH 7/7] parse_test: add testFail() to simplify tests License: MIT Signed-off-by: Christian Couder --- commands/cli/parse_test.go | 46 ++++++++++++-------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/commands/cli/parse_test.go b/commands/cli/parse_test.go index c9b4a11bd..5248b4a2a 100644 --- a/commands/cli/parse_test.go +++ b/commands/cli/parse_test.go @@ -173,38 +173,26 @@ func TestArgumentParsing(t *testing.T) { } } - test([]string{"noarg"}, nil, []string{}) - - _, _, _, err := Parse([]string{"noarg", "value!"}, nil, rootCmd) - if err == nil { - t.Error("Should have failed (provided an arg, but command didn't define any)") + testFail := func(cmd words, msg string) { + _, _, _, err := Parse(cmd, nil, rootCmd) + if err == nil { + t.Errorf("Should have failed: %v", msg) + } } + test([]string{"noarg"}, nil, []string{}) + testFail([]string{"noarg", "value!"}, "provided an arg, but command didn't define any") + test([]string{"onearg", "value!"}, nil, []string{"value!"}) - - _, _, _, err = Parse([]string{"onearg"}, nil, rootCmd) - if err == nil { - t.Error("Should have failed (didn't provide any args, arg is required)") - } + testFail([]string{"onearg"}, "didn't provide any args, arg is required") test([]string{"twoargs", "value1", "value2"}, nil, []string{"value1", "value2"}) - - _, _, _, err = Parse([]string{"twoargs", "value!"}, nil, rootCmd) - if err == nil { - t.Error("Should have failed (only provided 1 arg, needs 2)") - } - _, _, _, err = Parse([]string{"twoargs"}, nil, rootCmd) - if err == nil { - t.Error("Should have failed (didn't provide any args, 2 required)") - } + testFail([]string{"twoargs", "value!"}, "only provided 1 arg, needs 2") + testFail([]string{"twoargs"}, "didn't provide any args, 2 required") test([]string{"variadic", "value!"}, nil, []string{"value!"}) test([]string{"variadic", "value1", "value2", "value3"}, nil, []string{"value1", "value2", "value3"}) - - _, _, _, err = Parse([]string{"variadic"}, nil, rootCmd) - if err == nil { - t.Error("Should have failed (didn't provide any args, 1 required)") - } + testFail([]string{"variadic"}, "didn't provide any args, 1 required") test([]string{"optional", "value!"}, nil, []string{"value!"}) test([]string{"optional"}, nil, []string{}) @@ -212,14 +200,8 @@ func TestArgumentParsing(t *testing.T) { test([]string{"reversedoptional", "value1", "value2"}, nil, []string{"value1", "value2"}) test([]string{"reversedoptional", "value!"}, nil, []string{"value!"}) - _, _, _, err = Parse([]string{"reversedoptional"}, nil, rootCmd) - if err == nil { - t.Error("Should have failed (didn't provide any args, 1 required)") - } - _, _, _, err = Parse([]string{"reversedoptional", "value1", "value2", "value3"}, nil, rootCmd) - if err == nil { - t.Error("Should have failed (provided too many args, only takes 1)") - } + testFail([]string{"reversedoptional"}, "didn't provide any args, 1 required") + testFail([]string{"reversedoptional", "value1", "value2", "value3"}, "provided too many args, only takes 1") // Use a temp file to simulate stdin fileToSimulateStdin := func(t *testing.T, content string) (*os.File) {