示例#1
0
func matchingTargets(rd *profilesreader.Reader, profile *profiles.Profile) profiles.Targets {
	var targets profiles.Targets
	if IsFlagSet(cmdList.ParsedFlags, "target") {
		if t := rd.LookupProfileTarget(profile.Name(), listFlags.Target); t != nil {
			targets = profiles.Targets{t}
		}
	} else {
		targets = profile.Targets()
	}
	targets.Sort()
	return targets
}
示例#2
0
func TestOrderedTargets(t *testing.T) {
	for i, c := range []struct {
		a, b string
		r    bool
	}{
		{"x-b", "y-b", true},
		{"x-b", "w-b", false},
		{"x-b", "x-a", false},
		{"x-b", "x-c", true},
		{"x-b", "x-b@1", true},
		{"x-b@1", "x-b@", false},
		{"x-b@1", "x-b@2", false},
		{"x-b@12", "x-b@2", true},
		{"x-b@2", "x-b@1", true},
		{"[email protected]", "[email protected]", true},
		{"[email protected]", "[email protected]", true},
		{"[email protected]", "[email protected]", true},
		{"[email protected]", "[email protected]", true},
		{"[email protected]", "[email protected]", true},
		{"x-b", "x-b", false},
	} {
		a, err := profiles.NewTarget(c.a, "")
		if err != nil {
			t.Fatal(err)
		}
		b, _ := profiles.NewTarget(c.b, "")
		if err != nil {
			t.Fatal(err)
		}
		if got, want := a.Less(&b), c.r; got != want {
			t.Errorf("%d: got %v, want %v", i, got, want)
		}
	}

	ol := profiles.Targets{}
	data := []string{"a-b@2", "x-y", "a-b@12", "a-b@3", "a-b@0", "x-y@3", "x-y@2"}
	for _, s := range data {
		target, err := profiles.NewTarget(s)
		if err != nil {
			t.Fatal(err)
		}
		ol = profiles.InsertTarget(ol, &target)
	}
	if got, want := len(ol), len(data); got != want {
		t.Fatalf("got %v, want %v", got, want)
	}
	for i, _ := range ol[:len(ol)-1] {
		j := i + 1
		if !ol.Less(i, j) {
			t.Errorf("%v is not less than %v", ol[i], ol[j])
		}
	}
	if got, want := ol[0].String(), "a-b@12"; got != want {
		t.Fatalf("got %v, want %v", got, want)
	}
	if got, want := ol[len(ol)-1].String(), "x-y@2"; got != want {
		t.Fatalf("got %v, want %v", got, want)
	}
	t2, _ := profiles.NewTarget("a-b@12")
	ol = profiles.RemoveTarget(ol, &t2)
}