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 <chriscool@tuxfamily.org>
Christian Couder committed
May 2, 2015 at 04:05 UTC
d0752a714dcfc6887e6f37934aa6156965df64e7
1 file changed
+35
commands/cli/parse_test.go
+35
@@ -3,6 +3,9 @@ package cli
3
import (
4
"strings"
5
"testing"
6
+ "io"
7
+ "io/ioutil"
8
+ "os"
9
10
"github.com/ipfs/go-ipfs/commands"
11
)
@@ -147,6 +150,11 @@ func TestArgumentParsing(t *testing.T) {
150
commands.StringArg("b", true, false, "another arg"),
151
},
152
},
153
+ "stdinenabled": &commands.Command{
154
+ Arguments: []commands.Argument{
155
+ commands.StringArg("a", true, true, "some arg").EnableStdin(),
156
+ },
157
+ },
158
},
159
}
160
@@ -219,4 +227,31 @@ func TestArgumentParsing(t *testing.T) {
227
if err == nil {
228
t.Error("Should have failed (provided too many args, only takes 1)")
229
}
230
+
231
+ // Use a temp file to simulate stdin
232
+ fstdin, err := ioutil.TempFile("", "")
233
+ if err != nil {
234
+ t.Fatal(err)
235
+ }
236
+ defer os.Remove(fstdin.Name())
237
+
238
+ if _, err := io.WriteString(fstdin, "stdin1"); err != nil {
239
+ t.Fatal(err)
240
+ }
241
+
242
+ _, _, _, err = Parse([]string{"stdinenabled", "value1", "value2"}, nil, rootCmd)
243
+ if err != nil {
244
+ t.Error("Should have passed")
245
+ t.Fatal(err)
246
+ }
247
+ _, _, _, err = Parse([]string{"stdinenabled"}, fstdin, rootCmd)
248
+ if err != nil {
249
+ t.Error("Should have passed")
250
+ t.Fatal(err)
251
+ }
252
+ _, _, _, err = Parse([]string{"stdinenabled", "value1"}, fstdin, rootCmd)
253
+ if err != nil {
254
+ t.Error("Should have passed")
255
+ t.Fatal(err)
256
+ }
257
}