github.com/vanadium-archive/go.jiri@v0.0.0-20160715023856-abfb8b131290/profiles/profilescmdline/commandline_test.go (about) 1 // Copyright 2015 The Vanadium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package profilescmdline_test 6 7 import ( 8 "fmt" 9 "testing" 10 11 "v.io/jiri/profiles/profilescmdline" 12 "v.io/jiri/profiles/profilesreader" 13 "v.io/x/lib/cmdline" 14 ) 15 16 var parent = cmdline.Command{ 17 Name: "test", 18 Short: "test", 19 Long: "test", 20 ArgsName: "test", 21 ArgsLong: "test", 22 } 23 24 func TestReaderParent(t *testing.T) { 25 profilescmdline.Reset() 26 p := parent 27 args := []string{"--profiles-db=foo", "--skip-profiles"} 28 var rf profilescmdline.ReaderFlagValues 29 // If RegisterReaderCommandsUsingParent is called, the common reader 30 // flags are hosted by the parent command. 31 profilescmdline.RegisterReaderCommandsUsingParent(&p, &rf, "", "") 32 if got, want := len(p.Children), 2; got != want { 33 t.Errorf("got %v, want %v", got, want) 34 } 35 if err := p.Children[0].Flags.Parse(args); err == nil { 36 t.Errorf("this should have failed") 37 } 38 if err := p.Flags.Parse(args); err != nil { 39 t.Error(err) 40 } 41 if got, want := rf.DBFilename, "foo"; got != want { 42 t.Errorf("got %v, want %v", got, want) 43 } 44 if got, want := rf.ProfilesMode, profilesreader.SkipProfiles; got != want { 45 t.Errorf("got %v, want %v", got, want) 46 } 47 48 profilescmdline.Reset() 49 p = parent 50 profilescmdline.RegisterReaderFlags(&p.Flags, &rf, "", "") 51 if got, want := len(p.Children), 0; got != want { 52 t.Errorf("got %v, want %v", got, want) 53 } 54 if err := p.Flags.Parse(args); err != nil { 55 t.Fatal(err) 56 } 57 if got, want := rf.DBFilename, "foo"; got != want { 58 t.Errorf("got %v, want %v", got, want) 59 } 60 61 profilescmdline.Reset() 62 p = parent 63 // If RegisterReaderCommands is not called, the common reader 64 // flags are hosted by the subcommands. 65 profilescmdline.RegisterReaderCommands(&p, "", "") 66 if err := p.Flags.Parse(args); err == nil { 67 t.Fatal(fmt.Errorf("this should have failed")) 68 } 69 if err := p.Children[0].Flags.Parse([]string{"--profiles=a,b"}); err != nil { 70 t.Fatal(err) 71 } 72 // NOTE, that we can't access the actual values of the flags when they 73 // are hosted by the subcommands. 74 } 75 76 func TestSubcommandFlags(t *testing.T) { 77 profilescmdline.Reset() 78 p := parent 79 var rf profilescmdline.ReaderFlagValues 80 profilescmdline.RegisterReaderCommandsUsingParent(&p, &rf, "", "") 81 if got, want := len(p.Children), 2; got != want { 82 t.Errorf("got %v, want %v", got, want) 83 } 84 args := []string{"--info", "Profile.Root"} 85 if err := p.Flags.Parse(args); err == nil { 86 t.Error("this should have failed") 87 } 88 if err := p.Children[0].Flags.Parse(args); err != nil { 89 t.Error(err) 90 } 91 92 profilescmdline.Reset() 93 p = parent 94 profilescmdline.RegisterReaderCommands(&p, "", "") 95 if got, want := len(p.Children), 2; got != want { 96 t.Errorf("got %v, want %v", got, want) 97 } 98 if err := p.Flags.Parse(args); err == nil { 99 t.Error("this should have failed") 100 } 101 if err := p.Children[0].Flags.Parse(args); err != nil { 102 t.Error(err) 103 } 104 }