func TestRead(t *testing.T) { profiles.Clear() ctx := tool.NewDefaultContext() if err := profiles.Read(ctx, "./testdata/m1.xml"); err != nil { t.Fatal(err) } cmp := func(a, b []string) bool { if len(a) != len(b) { return false } for i, s := range a { if s != b[i] { return false } } return true } names := profiles.Profiles() sort.Strings(names) if got, want := names, []string{"a", "b"}; !cmp(got, want) { t.Errorf("got %v, want %v", got, want) } p := profiles.LookupProfile("a") if got, want := p.Targets()[0].Tag(), "t1"; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := p.Targets()[0].OS(), "os1"; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := p.Targets()[1].Version(), "bar"; got != want { t.Errorf("got %v, want %v", got, want) } }
func TestBackwardsCompatibility(t *testing.T) { profiles.Clear() getProfiles := func() []*profiles.Profile { db := []*profiles.Profile{} names := profiles.Profiles() sort.Strings(names) for _, name := range names { db = append(db, profiles.LookupProfile(name)) } return db } ctx := tool.NewDefaultContext() if err := profiles.Read(ctx, "./testdata/legacy.xml"); err != nil { t.Fatal(err) } if got, want := profiles.SchemaVersion(), profiles.Original; got != want { t.Errorf("got %v, want %v", got, want) } oprofiles := getProfiles() if got, want := len(oprofiles), 5; got != want { t.Errorf("got %v, want %v", got, want) } filename := tmpFile() defer os.RemoveAll(filepath.Dir(filename)) var t1 profiles.Target t1.Set("tag=cpu,os") profiles.AddProfileTarget("__first", t1) if err := profiles.Write(ctx, filename); err != nil { t.Fatal(err) } if err := profiles.Read(ctx, filename); err != nil { t.Fatal(err) } if got, want := profiles.SchemaVersion(), profiles.V3; got != want { t.Errorf("got %v, want %v", got, want) } nprofiles := getProfiles() if got, want := len(nprofiles), 6; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := nprofiles[0].Name, "__first"; got != want { t.Errorf("got %v, want %v", got, want) } for i, v := range nprofiles[1:] { if got, want := v.Name, oprofiles[i].Name; got != want { t.Errorf("got %v, want %v", got, want) } } }
func TestInstallProfile(t *testing.T) { profiles.Clear() profiles.InstallProfile("a", "root1") profiles.InstallProfile("a", "root2") profile := profiles.LookupProfile("a") if got, want := profile.Root, "root1"; got != want { t.Errorf("got %v, want %v", got, want) } }
func TestDuplicateTag(t *testing.T) { profiles.Clear() addProfileAndTargets(t, "b") t1 := &profiles.Target{} t1.Set("t2") err := profiles.AddProfileTarget("b", *t1) if got, want := err.Error(), "tag \"t2\" is already used"; !strings.Contains(got, want) { t.Fatalf("got %v doesn't contain %v", got, want) } }
func TestWrite(t *testing.T) { profiles.Clear() filename := tmpFile() defer os.RemoveAll(filepath.Dir(filename)) ctx := tool.NewDefaultContext() addProfileAndTargets(t, "b") addProfileAndTargets(t, "a") profiles.Write(ctx, filename) 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 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) } }
func ExampleManager() { myProfile := "myNewProfile" var target profiles.Target init := func() { profiles.Register(myProfile, newProfile(myProfile)) flags := flag.NewFlagSet("example", flag.ContinueOnError) profiles.RegisterTargetAndEnvFlags(flags, &target) flags.Parse([]string{"--target=myTarget=arm-linux", "--env=A=B,C=D", "--env=E=F"}) } init() mgr := profiles.LookupManager(myProfile) if mgr == nil { panic("manager not found for: " + myProfile) } ctx := tool.NewDefaultContext() // Install myNewProfile for target. if err := mgr.Install(ctx, target); err != nil { panic("failed to find manager for: " + myProfile) } fmt.Println(mgr.String()) filename := tmpFile() defer os.RemoveAll(filepath.Dir(filename)) if err := profiles.Write(ctx, filename); err != nil { panic(err) } // Clear the profile manifest information in order to mimic starting // a new process and reading the manifest file. profiles.Clear() // Read the profile manifest. profiles.Read(ctx, filename) mgr = profiles.LookupManager(myProfile) if mgr == nil { panic("manager not found for: " + myProfile) } fmt.Println(mgr.String()) mgr.Uninstall(ctx, target) fmt.Println(mgr.String()) fmt.Println(mgr.VersionInfo().Supported()) fmt.Println(mgr.VersionInfo().Default()) // Output: // Profile: myNewProfile: installed // [myTarget=arm-linux@] // // Profile: myNewProfile: installed // [myTarget=arm-linux@] // // Profile: myNewProfile: uninstalled // // [4 3 2] // 3 }
func testSetPathHelper(t *testing.T, name string) { profiles.Clear() ctx := tool.NewDefaultContext() // Setup a fake JIRI_ROOT. root, err := project.NewFakeJiriRoot(ctx) if err != nil { t.Fatalf("%v", err) } defer func() { if err := root.Cleanup(ctx); err != nil { t.Fatalf("%v", err) } }() // Create a test project and identify it as a Go workspace. if err := root.CreateRemoteProject(ctx, "test"); err != nil { t.Fatalf("%v", err) } if err := root.AddProject(ctx, project.Project{ Name: "test", Path: "test", Remote: root.Projects["test"], }); err != nil { t.Fatalf("%v", err) } if err := root.UpdateUniverse(ctx, false); err != nil { t.Fatalf("%v", err) } var config *util.Config switch name { case "GOPATH": config = util.NewConfig(util.GoWorkspacesOpt([]string{"test", "does/not/exist"})) case "VDLPATH": config = util.NewConfig(util.VDLWorkspacesOpt([]string{"test", "does/not/exist"})) } oldRoot, err := project.JiriRoot() if err := os.Setenv("JIRI_ROOT", root.Dir); err != nil { t.Fatalf("%v", err) } defer os.Setenv("JIRI_ROOT", oldRoot) if err := profiles.Write(ctx, filepath.Join(root.Dir, "profiles-manifest")); err != nil { t.Fatal(err) } if err := util.SaveConfig(ctx, config); err != nil { t.Fatalf("%v", err) } // Retrieve Jiri_ROOT through JiriRoot() to account for symlinks. jiriRoot, err := project.JiriRoot() if err != nil { t.Fatalf("%v", err) } ch, err := profiles.NewConfigHelper(ctx, profiles.UseProfiles, "profiles-manifest") if err != nil { t.Fatal(err) } ch.Vars = envvar.VarsFromOS() ch.Set(name, "") var want string switch name { case "GOPATH": want = filepath.Join(jiriRoot, "test") ch.SetGoPath() case "VDLPATH": // Make a fake src directory. want = filepath.Join(jiriRoot, "test", "src") if err := ctx.Run().MkdirAll(want, 0755); err != nil { t.Fatalf("%v", err) } ch.SetVDLPath() } if got := ch.Get(name); got != want { t.Fatalf("unexpected value: got %v, want %v", got, want) } }