Beispiel #1
0
func findProfileSubcommands(jirix *jiri.X) []string {
	if !runSubcommands {
		return nil
	}
	cmds, _ := lookpath.LookPrefix(jirix.Env(), "jiri-profile-", nil)
	return cmds
}
Beispiel #2
0
func TestLookPrefix(t *testing.T) {
	tmpDir, cleanup := initTmpDir(t)
	defer cleanup()
	dirA, dirB := mkdir(t, tmpDir, "a"), mkdir(t, tmpDir, "b")
	aFoo, aBar := mkfile(t, dirA, "foo", 0755), mkfile(t, dirA, "bar", 0755)
	bBar, bBaz := mkfile(t, dirB, "bar", 0755), mkfile(t, dirB, "baz", 0755)
	aBzz, bBaa := mkfile(t, dirA, "bzz", 0755), mkfile(t, dirB, "baa", 0755)
	aExe, bExe := mkfile(t, dirA, "exe", 0644), mkfile(t, dirB, "exe", 0755)
	tests := []struct {
		Env    map[string]string
		Prefix string
		Names  map[string]bool
		Want   []string
	}{
		{nil, "", nil, nil},
		{nil, "foo", nil, nil},
		{pathEnv(dirA), "foo", nil, []string{aFoo}},
		{pathEnv(dirA), "bar", nil, []string{aBar}},
		{pathEnv(dirA), "baz", nil, nil},
		{pathEnv(dirA), "f", nil, []string{aFoo}},
		{pathEnv(dirA), "b", nil, []string{aBar, aBzz}},
		{pathEnv(dirB), "foo", nil, nil},
		{pathEnv(dirB), "bar", nil, []string{bBar}},
		{pathEnv(dirB), "baz", nil, []string{bBaz}},
		{pathEnv(dirB), "f", nil, nil},
		{pathEnv(dirB), "b", nil, []string{bBaa, bBar, bBaz}},
		{pathEnv(dirA, dirB), "foo", nil, []string{aFoo}},
		{pathEnv(dirA, dirB), "bar", nil, []string{aBar}},
		{pathEnv(dirA, dirB), "baz", nil, []string{bBaz}},
		{pathEnv(dirA, dirB), "f", nil, []string{aFoo}},
		{pathEnv(dirA, dirB), "b", nil, []string{bBaa, aBar, bBaz, aBzz}},
		// Don't find baz, since it's already provided.
		{pathEnv(dirA, dirB), "b", map[string]bool{"baz": true}, []string{bBaa, aBar, aBzz}},
		// Make sure we find bExe, since aExe isn't executable.
		{pathEnv(dirA, dirB), "exe", nil, []string{bExe}},
		{pathEnv(dirA, dirB), "e", nil, []string{bExe}},
		// Absolute prefix lookups.
		{nil, dirA, nil, nil},
		{nil, dirB, nil, nil},
		{nil, aFoo, nil, []string{aFoo}},
		{nil, aBar, nil, []string{aBar}},
		{nil, bBar, nil, []string{bBar}},
		{nil, bBaz, nil, []string{bBaz}},
		{nil, aBzz, nil, []string{aBzz}},
		{nil, bBaa, nil, []string{bBaa}},
		{nil, filepath.Join(dirA, "f"), nil, []string{aFoo}},
		{nil, filepath.Join(dirA, "b"), nil, []string{aBar, aBzz}},
		{nil, filepath.Join(dirB, "f"), nil, nil},
		{nil, filepath.Join(dirB, "b"), nil, []string{bBaa, bBar, bBaz}},
		{nil, filepath.Join(dirB, "b"), map[string]bool{"baz": true}, []string{bBaa, bBar}},
		{nil, aExe, nil, nil},
		{nil, filepath.Join(dirA, "e"), nil, nil},
		{nil, bExe, nil, []string{bExe}},
		{nil, filepath.Join(dirB, "e"), nil, []string{bExe}},
	}
	for _, test := range tests {
		hdr := fmt.Sprintf("env=%v prefix=%v names=%v", test.Env, test.Prefix, test.Names)
		look, err := lookpath.LookPrefix(test.Env, test.Prefix, test.Names)
		if got, want := look, test.Want; !reflect.DeepEqual(got, want) {
			t.Errorf("%s got %v, want %v", hdr, got, want)
		}
		if (look == nil) == (err == nil) {
			t.Errorf("%s got mismatched look=%v err=%v", hdr, look, err)
		}
		if err != nil && !isNotFoundError(err, test.Prefix+"*") {
			t.Errorf("%s got wrong error %v", hdr, err)
		}
	}
}