From d0752a714dcfc6887e6f37934aa6156965df64e7 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sat, 2 May 2015 04:05:57 +0200 Subject: [PATCH] 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) + } }