func TestReader(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() rd, err := profilesreader.NewReader(fake.X, profilesreader.UseProfiles, filepath.Join("testdata", "m2.xml")) if err != nil { t.Fatal(err) } rd.Vars = envvar.VarsFromOS() rd.Delete("CGO_CFLAGS") native, err := profiles.NewTarget("amd64-darwin", "") if err != nil { t.Fatal(err) } rd.MergeEnvFromProfiles(profilesreader.JiriMergePolicies(), native, "test::go", "test::syncbase") if got, want := rd.Get("CGO_CFLAGS"), "-IX -IY -IA -IB"; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := rd.DebugString(), "Root: "+fake.X.Root+` Path: testdata/m2.xml test:go: [email protected] dir: --env= envvars:[CGO_ENABLED=1 GOARCH=amd64 GOOS=darwin GOROOT=/goroot CGO_CFLAGS=-IX -IY] test:syncbase: amd64-darwin@1 dir: --env= envvars:[CGO_CFLAGS=-IA -IB CGO_LDFLAGS=-LA -LB] `; got != want { t.Errorf("got %v, want %v", got, want) } }
// Test using a fake jiri root. func TestJiriFakeRoot(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() profilesDBDir := filepath.Join(fake.X.Root, jiri.ProfilesDBDir) pdb := profiles.NewDB() t1, err := profiles.NewTarget("cpu1-os1@1", "A=B,C=D") if err != nil { t.Fatal(err) } pdb.InstallProfile("test", "b", "") if err := pdb.AddProfileTarget("test", "b", t1); err != nil { t.Fatal(err) } if err := pdb.Write(fake.X, "test", profilesDBDir); err != nil { t.Fatal(err) } rd, err := profilesreader.NewReader(fake.X, profilesreader.UseProfiles, profilesDBDir) if err != nil { t.Fatal(err) } if got, want := rd.ProfileNames(), []string{"test:b"}; !reflect.DeepEqual(got, want) { t.Errorf("got %v, want %v", got, want) } dir, sh := buildJiri(t), gosh.NewShell(t) sh.Vars["JIRI_ROOT"] = fake.X.Root sh.Vars["PATH"] = envvar.PrependUniqueToken(sh.Vars["PATH"], ":", dir) run(sh, dir, "jiri", "profile", "list", "-v") }
func TestEnvFromTarget(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() pdb := profiles.NewDB() pdb.InstallProfile("test", "a", "root") pdb.InstallProfile("test", "b", "root") t1, t2 := &profiles.Target{}, &profiles.Target{} t1.Set("cpu1-os1@1") t1.Env.Set("A=B C=D,B=C Z=Z") t2.Set("cpu1-os1@1") t2.Env.Set("A=Z,B=Z,Z=Z1") pdb.AddProfileTarget("test", "a", *t1) pdb.AddProfileTarget("test", "b", *t2) pdb.Write(fake.X, "test", "profile-manifest") filename := filepath.Join(fake.X.Root, "profile-manifest") if err := pdb.Write(fake.X, "test", filename); err != nil { t.Fatal(err) } rd, err := profilesreader.NewReader(fake.X, profilesreader.UseProfiles, filename) if err != nil { t.Fatal(err) } rd.Vars = envvar.VarsFromSlice([]string{}) t1Target, err := profiles.NewTarget("cpu1-os1@1", "") if err != nil { t.Fatal(err) } rd.MergeEnvFromProfiles(map[string]profilesreader.MergePolicy{ "A": profilesreader.AppendFlag, "B": profilesreader.UseLast, "Z": profilesreader.IgnoreBaseUseLast}, t1Target, "test::a", "test::b") vars := rd.ToMap() if got, want := len(vars), 3; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := rd.Get("A"), "B C=D Z"; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := rd.Get("B"), "Z"; got != want { t.Errorf("got %v, want %v", got, want) } }
func RunMojoShell(mojoUrl, configFile string, configAliases map[string]string, argsFor map[string][]string, target profiles.Target) *exec.Cmd { // ensure the profiles are loaded jirix, err := jiri.NewX(cmdline.EnvFromOS()) if err != nil { panic(err) } rd, err := profilesreader.NewReader(jirix, profilesreader.UseProfiles, filepath.Join(jirix.Root, ".jiri_root", "profile_db")) if err != nil { panic(err) } envslice := rd.EnvFromProfile(mojoProfileName(), target) env := envvar.VarsFromSlice(envslice) jiri.ExpandEnv(jirix, env) var mojoDevtools, mojoShell, mojoServices string for _, e := range env.ToSlice() { parts := strings.SplitN(e, "=", 2) switch parts[0] { case "MOJO_DEVTOOLS": mojoDevtools = parts[1] case "MOJO_SHELL": mojoShell = parts[1] case "MOJO_SERVICES": mojoServices = parts[1] } } args := []string{ mojoUrl, "--config-file", configFile, "--shell-path", mojoShell, "--enable-multiprocess"} if target.OS() == "android" { args = append(args, "--android") args = append(args, "--origin", mojoServices) } for alias, value := range configAliases { args = append(args, "--config-alias", fmt.Sprintf("%s=%s", alias, value)) } for key, value := range argsFor { args = append(args, fmt.Sprintf("--args-for=%s %s", key, strings.Join(value, " "))) } return exec.Command(filepath.Join(mojoDevtools, "mojo_run"), args...) }
func runEnv(jirix *jiri.X, args []string) error { if len(envFlags.Profiles) == 0 { return fmt.Errorf("no profiles were specified using --profiles") } rd, err := profilesreader.NewReader(jirix, envFlags.ProfilesMode, envFlags.DBFilename) if err != nil { return err } profileNames := strings.Split(envFlags.Profiles, ",") if err := rd.ValidateRequestedProfilesAndTarget(profileNames, envFlags.Target); err != nil { return err } rd.MergeEnvFromProfiles(envFlags.MergePolicies, envFlags.Target, profileNames...) out := fmtVars(rd.ToMap(), args) if len(out) > 0 { fmt.Fprintln(jirix.Stdout(), out) } return nil }
func runp(jirix *jiri.X, cmd *cmdline.Command, args []string) error { hasUntrackedSet := profilescmdline.IsFlagSet(cmd.ParsedFlags, "has-untracked") hasUncommitedSet := profilescmdline.IsFlagSet(cmd.ParsedFlags, "has-uncommitted") hasGerritSet := profilescmdline.IsFlagSet(cmd.ParsedFlags, "has-gerrit-message") if runpFlags.interactive { runpFlags.collateOutput = false } var keysRE, branchRE *regexp.Regexp var err error if profilescmdline.IsFlagSet(cmd.ParsedFlags, "projects") { re := "" for _, pre := range strings.Split(runpFlags.projectKeys, ",") { re += pre + "|" } re = strings.TrimRight(re, "|") keysRE, err = regexp.Compile(re) if err != nil { return fmt.Errorf("failed to compile projects regexp: %q: %v", runpFlags.projectKeys, err) } } if profilescmdline.IsFlagSet(cmd.ParsedFlags, "has-branch") { branchRE, err = regexp.Compile(runpFlags.hasBranch) if err != nil { return fmt.Errorf("failed to compile has-branch regexp: %q: %v", runpFlags.hasBranch, err) } } for _, f := range []string{"show-key-prefix", "show-name-prefix"} { if profilescmdline.IsFlagSet(cmd.ParsedFlags, f) { if runpFlags.interactive && profilescmdline.IsFlagSet(cmd.ParsedFlags, "interactive") { fmt.Fprintf(jirix.Stderr(), "WARNING: interactive mode being disabled because %s was set\n", f) } runpFlags.interactive = false runpFlags.collateOutput = true break } } git := gitutil.New(jirix.NewSeq()) homeBranch, err := git.CurrentBranchName() if err != nil { // jiri was run from outside of a project. Let's assume we'll // use all projects if none have been specified via the projects flag. if keysRE == nil { keysRE = regexp.MustCompile(".*") } } dirty := false if hasUntrackedSet || hasUncommitedSet { dirty = true } states, err := project.GetProjectStates(jirix, dirty) if err != nil { return err } mapInputs := map[project.ProjectKey]*mapInput{} var keys project.ProjectKeys for key, state := range states { if keysRE != nil { if !keysRE.MatchString(string(key)) { continue } } else { if state.CurrentBranch != homeBranch { continue } } if branchRE != nil { found := false for _, br := range state.Branches { if branchRE.MatchString(br.Name) { found = true break } } if !found { continue } } if hasUntrackedSet && (state.HasUntracked != runpFlags.hasUntracked) { continue } if hasUncommitedSet && (state.HasUncommitted != runpFlags.hasUncommitted) { continue } if hasGerritSet { hasMsg := false for _, br := range state.Branches { if (state.CurrentBranch == br.Name) && br.HasGerritMessage { hasMsg = true break } } if hasMsg != runpFlags.hasGerritMessage { continue } } mapInputs[key] = &mapInput{ ProjectState: state, jirix: jirix, key: key, } keys = append(keys, key) } total := len(mapInputs) index := 1 for _, mi := range mapInputs { mi.index = index mi.total = total index++ } if runpFlags.verbose { fmt.Fprintf(jirix.Stdout(), "Project Names: %s\n", strings.Join(stateNames(mapInputs), " ")) fmt.Fprintf(jirix.Stdout(), "Project Keys: %s\n", strings.Join(stateKeys(mapInputs), " ")) } reader, err := profilesreader.NewReader(jirix, runpFlags.ProfilesMode, runpFlags.DBFilename) runner := &runner{ reader: reader, args: args, } mr := simplemr.MR{} if runpFlags.interactive { // Run one mapper at a time. mr.NumMappers = 1 sort.Sort(keys) } in, out := make(chan *simplemr.Record, len(mapInputs)), make(chan *simplemr.Record, len(mapInputs)) sigch := make(chan os.Signal) signal.Notify(sigch, os.Interrupt) go func() { <-sigch; mr.Cancel() }() go mr.Run(in, out, runner, runner) for _, key := range keys { in <- &simplemr.Record{string(key), []interface{}{mapInputs[key]}} } close(in) <-out return mr.Error() }
func runList(jirix *jiri.X, args []string) error { if listFlags.Verbose { fmt.Fprintf(jirix.Stdout(), "Profiles Database Path: %s\n", listFlags.DBFilename) } rd, err := profilesreader.NewReader(jirix, listFlags.ProfilesMode, listFlags.DBFilename) if err != nil { return err } profileNames := []string{} for _, a := range args { if a != "" { profileNames = append(profileNames, a) } } if len(args) == 0 { if IsFlagSet(cmdList.ParsedFlags, "profiles") { profileNames = strings.Split(listFlags.Profiles, ",") } else { profileNames = rd.ProfileNames() } } if listFlags.Verbose { fmt.Fprintf(jirix.Stdout(), "Installed Profiles: ") fmt.Fprintf(jirix.Stdout(), "%s\n", strings.Join(rd.ProfileNames(), ", ")) for _, name := range profileNames { profile := rd.LookupProfile(name) if profile == nil { continue } fmt.Fprintf(jirix.Stdout(), "Profile: %s @ %s\n", profile.Name(), profile.Root()) for _, target := range matchingTargets(rd, profile) { fmt.Fprintf(jirix.Stdout(), "\t%s\n", target.DebugString()) } } return nil } if listFlags.info == "" { matchingNames := []string{} for _, name := range profileNames { profile := rd.LookupProfile(name) if profile == nil { continue } if len(matchingTargets(rd, profile)) > 0 { matchingNames = append(matchingNames, name) } } if len(matchingNames) > 0 { fmt.Fprintln(jirix.Stdout(), strings.Join(matchingNames, ", ")) } else { if IsFlagSet(cmdList.ParsedFlags, "target") { return fmt.Errorf("no matching targets for %s", listFlags.Target) } } return nil } // Handle --info found := false for _, name := range profileNames { profile := rd.LookupProfile(name) if profile == nil { continue } targets := matchingTargets(rd, profile) out := &bytes.Buffer{} printHeader := len(profileNames) > 1 || len(targets) > 1 || len(listFlags.info) == 0 for _, target := range targets { if printHeader { out.WriteString(fmtHeader(name, target)) out.WriteString(" ") } r, err := fmtInfo(jirix, listFlags.info, rd, profile, target) if err != nil { return err } out.WriteString(r) if printHeader { out.WriteString("\n") } found = true } fmt.Fprint(jirix.Stdout(), out.String()) } if !found && IsFlagSet(cmdList.ParsedFlags, "target") { return fmt.Errorf("no matching targets for %s", listFlags.Target) } return nil }