Example #1
0
func TestSubcommandFlags(t *testing.T) {
	profilescmdline.Reset()
	p := parent
	var rf profilescmdline.ReaderFlagValues
	profilescmdline.RegisterReaderCommandsUsingParent(&p, &rf, "", "")
	if got, want := len(p.Children), 2; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	args := []string{"--info", "Profile.Root"}
	if err := p.Flags.Parse(args); err == nil {
		t.Error("this should have failed")
	}
	if err := p.Children[0].Flags.Parse(args); err != nil {
		t.Error(err)
	}

	profilescmdline.Reset()
	p = parent
	profilescmdline.RegisterReaderCommands(&p, "", "")
	if got, want := len(p.Children), 2; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	if err := p.Flags.Parse(args); err == nil {
		t.Error("this should have failed")
	}
	if err := p.Children[0].Flags.Parse(args); err != nil {
		t.Error(err)
	}
}
Example #2
0
func TestReaderParent(t *testing.T) {
	profilescmdline.Reset()
	p := parent
	args := []string{"--profiles-db=foo", "--skip-profiles"}
	var rf profilescmdline.ReaderFlagValues
	// If RegisterReaderCommandsUsingParent is called, the common reader
	// flags are hosted by the parent command.
	profilescmdline.RegisterReaderCommandsUsingParent(&p, &rf, "", "")
	if got, want := len(p.Children), 2; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	if err := p.Children[0].Flags.Parse(args); err == nil {
		t.Errorf("this should have failed")
	}
	if err := p.Flags.Parse(args); err != nil {
		t.Error(err)
	}
	if got, want := rf.DBFilename, "foo"; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	if got, want := rf.ProfilesMode, profilesreader.SkipProfiles; got != want {
		t.Errorf("got %v, want %v", got, want)
	}

	profilescmdline.Reset()
	p = parent
	profilescmdline.RegisterReaderFlags(&p.Flags, &rf, "", "")
	if got, want := len(p.Children), 0; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	if err := p.Flags.Parse(args); err != nil {
		t.Fatal(err)
	}
	if got, want := rf.DBFilename, "foo"; got != want {
		t.Errorf("got %v, want %v", got, want)
	}

	profilescmdline.Reset()
	p = parent
	// If RegisterReaderCommands is not called, the common reader
	// flags are hosted by the subcommands.
	profilescmdline.RegisterReaderCommands(&p, "", "")
	if err := p.Flags.Parse(args); err == nil {
		t.Fatal(fmt.Errorf("this should have failed"))
	}
	if err := p.Children[0].Flags.Parse([]string{"--profiles=a,b"}); err != nil {
		t.Fatal(err)
	}
	// NOTE, that we can't access the actual values of the flags when they
	// are hosted by the subcommands.
}
Example #3
0
func TestManagerArgs(t *testing.T) {
	profilescmdline.Reset()
	p := parent
	profilescmdline.RegisterManagementCommands(&p, false, "", "", jiri.ProfilesRootDir)
	if got, want := len(p.Children), 6; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	type cl struct {
		args string
		n    int
	}
	cls := map[string]cl{
		"install":   cl{"--profiles-db=db --profiles-dir=root --target=arch-os --env=a=b,c=d --force=false", 5},
		"uninstall": cl{"--profiles-db=db --profiles-dir=root --target=arch-os --all-targets --v", 5},
		"cleanup":   cl{"--profiles-db=db --profiles-dir=root --gc --rm-all --v", 5},
		"update":    cl{"--profiles-db=db --profiles-dir=root -v", 3},
		"available": cl{"-v", 1},
	}
	for _, c := range p.Children {
		args := cls[c.Name].args
		if err := c.Flags.Parse(strings.Split(args, " ")); err != nil {
			t.Errorf("failed to parse for %s: %s: %v", c.Name, args, err)
			continue
		}
		if got, want := c.Flags.NFlag(), cls[c.Name].n; got != want {
			t.Errorf("%s: got %v, want %v", c.Name, got, want)
		}
	}
}