func TestMergeEnv(t *testing.T) { base := []string{"FS1=A", "IF=A", "A=B", "B=A", "C=D", "P=A", "V=A", "P1=A", "V1=A", "IA=A", "IB=A", "IC=A", "ID=A", "IE=A", "IG1=A"} b := []string{"FS1=B", "FS2=B", "IF=B", "A=B1", "B=B", "C=D1", "P=B", "V=B", "P1=B", "V1=B", "W=X", "Y=Z", "GP=X", "IA=B", "IB=B", "IC=B", "ID=B", "IE=B", "IG2=A"} c := []string{"FS1=C", "FS2=C", "FS3=C", "A=BL", "B=C", "C=DL", "P=C", "V=C", "P1=C", "V1=C", "Y=ZL", "GP=XL", "IA=C", "IB=C", "IC=C", "ID=C", "IE=C", "IG3=B"} env := envvar.VarsFromSlice(base) policies := map[string]profilesreader.MergePolicy{ "GP": profilesreader.UseLast, "P": profilesreader.PrependPath, "V": profilesreader.PrependFlag, "P1": profilesreader.AppendPath, "V1": profilesreader.AppendFlag, "A": profilesreader.IgnoreBaseUseLast, "B": profilesreader.UseBaseIgnoreProfiles, "IA": profilesreader.IgnoreBaseAppendPath, "IB": profilesreader.IgnoreBaseAppendFlag, "IC": profilesreader.IgnoreBasePrependPath, "ID": profilesreader.IgnoreBasePrependFlag, "IE": profilesreader.IgnoreBaseUseLast, "IF": profilesreader.IgnoreBaseUseFirst, "IG1": profilesreader.IgnoreVariable, "IG2": profilesreader.IgnoreVariable, "IG3": profilesreader.IgnoreVariable, "C": profilesreader.UseLast, "Y": profilesreader.UseLast, } profilesreader.MergeEnv(policies, env, b, c) expected := []string{"B=A", "A=BL", "C=DL", "GP=XL", "P1=A:B:C", "P=C:B:A", "V1=A B C", "V=C B A", "W=X", "Y=ZL", "IA=B:C", "IB=B C", "IC=C:B", "ID=C B", "IE=C", "FS1=A", "FS2=B", "FS3=C", "IF=B", } sort.Strings(expected) if got, want := env.ToSlice(), expected; len(got) != len(want) { sort.Strings(got) t.Errorf("got: %v", got) t.Errorf("want: %v", want) t.Errorf("got %v, want %v", len(got), len(want)) } for _, g := range env.ToSlice() { found := false for _, w := range expected { if g == w { found = true } } if !found { t.Errorf("failed to find %v in %v", g, expected) } } }
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 TestMergeEnv(t *testing.T) { a := []string{"A=B", "C=D"} b := []string{"W=X", "Y=Z", "GP=X"} env := envvar.VarsFromSlice(a) profiles.MergeEnv(map[string]string{}, map[string]bool{"GP": true}, env, b) if got, want := len(env.ToSlice()), 4; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := env.Get("W"), "X"; got != want { t.Errorf("got %v, want %v", got, want) } profiles.MergeEnv(map[string]string{"W": " "}, map[string]bool{"GP": true}, env, []string{"W=an option"}) if got, want := len(env.ToSlice()), 4; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := env.Get("W"), "X an option"; got != want { t.Errorf("got %v, want %v", got, want) } }
func TestEnvFromTarget(t *testing.T) { profiles.Clear() root, _ := project.JiriRoot() ctx := tool.NewDefaultContext() profiles.InstallProfile("a", "root") profiles.InstallProfile("b", "root") t1, t2 := &profiles.Target{}, &profiles.Target{} t1.Set("t1=cpu1-os1") t1.Env.Set("A=B C=D, B=C Z=Z") t2.Set("t1=cpu1-os1") t2.Env.Set("A=Z,B=Z,Z=Z") profiles.AddProfileTarget("a", *t1) profiles.AddProfileTarget("b", *t2) tmpdir, err := ioutil.TempDir(".", "pdb") if err != nil { t.Fatal(err) } filename := filepath.Join("release", "go", "src", "v.io", "jiri", "profiles", tmpdir, "manifest") if err := profiles.Write(ctx, filepath.Join(root, filename)); err != nil { t.Fatal(err) } defer os.RemoveAll(tmpdir) ch, err := profiles.NewConfigHelper(ctx, profiles.UseProfiles, filename) if err != nil { t.Fatal(err) } ch.Vars = envvar.VarsFromSlice([]string{}) target, _ := profiles.NewTarget("t1=") ch.SetEnvFromProfiles(map[string]string{"A": " "}, map[string]bool{"Z": true}, "a,b", target) vars := ch.ToMap() if got, want := len(vars), 3; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := ch.Get("A"), "B C=D Z"; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := ch.Get("B"), "Z"; got != want { t.Errorf("got %v, want %v", got, want) } }