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", false}, {"x-b@2", "x-b@1", 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.OrderedTargets{} data := []string{"a-b@2", "x-y", "a-b@12", "a-b@3", "foo=a-b@3", "a-b@0", "x-y@3", "tag=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@3"; got != want { t.Fatalf("got %v, want %v", got, want) } if got, want := ol[len(ol)-1].String(), "tag=x-y@2"; got != want { t.Fatalf("got %v, want %v", got, want) } t2, _ := profiles.NewTarget("a-b@12") ol = profiles.RemoveTarget(ol, &t2) }
func addProfileAndTargets(t *testing.T, pdb *profiles.DB, name string) { t1, _ := profiles.NewTarget("cpu1-os1@1", "A=B,C=D") t2, _ := profiles.NewTarget("cpu2-os2@bar", "A=B,C=D") pdb.InstallProfile("test", name, "") if err := pdb.AddProfileTarget("test", name, t1); err != nil { t.Fatal(err) } t2.InstallationDir = "bar" if err := pdb.AddProfileTarget("test", name, t2); err != nil { t.Fatal(err) } }
// 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 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) } }
func TestWrite(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() filename := tmpFile() defer os.RemoveAll(filepath.Dir(filename)) // test for no version being set. t1, _ := profiles.NewTarget("cpu1-os1", "A=B,C=D") pdb.InstallProfile("test", "b", "") if err := pdb.AddProfileTarget("test", "b", t1); err != nil { t.Fatal(err) } if err := pdb.Write(jirix, "test", filename); err == nil || !strings.HasPrefix(err.Error(), "missing version for profile") { t.Fatalf("was expecing a missing version error, but got %v", err) } pdb.RemoveProfileTarget("test", "b", t1) addProfileAndTargets(t, pdb, "b") addProfileAndTargets(t, pdb, "a") if err := pdb.Write(jirix, "test", filename); err != nil { t.Fatal(err) } g, _ := ioutil.ReadFile(filename) w, _ := ioutil.ReadFile("./testdata/m1.xml") if got, want := removeDate(strings.TrimSpace(string(g))), strings.TrimSpace(string(w)); got != want { t.Fatalf("got %v, want %v", got, want) } }
func TestReadingV3AndV4(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() for i, c := range []struct { filename, prefix, variable string version profiles.Version }{ {"v3.xml", "", "", profiles.V3}, {"v4.xml", fake.X.Root, "${JIRI_ROOT}", profiles.V4}, } { pdb := profiles.NewDB() err := pdb.Read(fake.X, filepath.Join("testdata", c.filename)) if err != nil { t.Fatal(err) } if got, want := pdb.SchemaVersion(), c.version; got != want { t.Errorf("%d: got %v, want %v", i, got, want) } target, err := profiles.NewTarget("cpu1-os1@1", "") if err != nil { t.Fatal(err) } p := pdb.LookupProfile("", "a") // We need to expand the variable here for a V4 profile if we want // to get the full absolute path. if got, want := p.Root(), c.variable+"/an/absolute/root"; got != want { t.Errorf("%d: got %v, want %v", i, got, want) } lt := pdb.LookupProfileTarget("", "a", target) if got, want := lt.InstallationDir, c.variable+"/an/absolute/dir"; got != want { t.Errorf("%d: got %v, want %v", i, got, want) } } }
func TestWriteDir(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() dir, err := ioutil.TempDir("", "pdb-") if err != nil { t.Fatal(err) } t1, _ := profiles.NewTarget("cpu1-os1@1", "A=B,C=D") pdb.InstallProfile("i1", "b", "") if err := pdb.AddProfileTarget("i1", "b", t1); err != nil { t.Fatal(err) } t2, _ := profiles.NewTarget("cpu1-os1@2", "A=B,C=D") pdb.InstallProfile("i2", "b", "") if err := pdb.AddProfileTarget("i2", "b", t2); err != nil { t.Fatal(err) } // Write out i1 installer's data if err := pdb.Write(jirix, "i1", dir); err != nil { t.Fatal(err) } if f := filepath.Join(dir, "i1"); !exists(t, f) { t.Errorf("%s doesn't exist", f) } if f := filepath.Join(dir, "i2"); exists(t, f) { t.Errorf("%s exists", f) } // Write out i2 installer's data if err := pdb.Write(jirix, "i2", dir); err != nil { t.Fatal(err) } if f := filepath.Join(dir, "i1"); !exists(t, f) { t.Errorf("%s doesn't exist", f) } if f := filepath.Join(dir, "i2"); !exists(t, f) { t.Errorf("%s doesn't exist", f) } }
func TestCopyCommandLineEnv(t *testing.T) { env := "A=B,C=D" target, _ := profiles.NewTarget("a=a-o", env) clenv := target.CommandLineEnv() if got, want := strings.Join(clenv.Vars, ","), env; got != want { t.Errorf("got %v, want %v", got, want) } // Make sure we can't mutate the command line env stored in target. clenv.Vars[0] = "oops" clenv2 := target.CommandLineEnv() if got, want := strings.Join(clenv2.Vars, ","), env; got != want { t.Errorf("got %v, want %v", got, want) } }
func TestConfigHelper(t *testing.T) { ctx := tool.NewDefaultContext() ch, err := profiles.NewConfigHelper(ctx, profiles.UseProfiles, "release/go/src/v.io/jiri/profiles/testdata/m2.xml") if err != nil { t.Fatal(err) } ch.Vars = envvar.VarsFromOS() ch.Delete("CGO_CFLAGS") target, _ := profiles.NewTarget("native=") ch.SetEnvFromProfiles(profiles.CommonConcatVariables(), map[string]bool{}, "go,syncbase", target) if got, want := ch.Get("CGO_CFLAGS"), "-IX -IY -IA -IB"; got != want { t.Errorf("got %v, want %v", got, want) } }
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 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) } }