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 TestReadDir(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() if err := pdb.Read(jirix, "./testdata/old_version"); err == nil || !strings.Contains(err.Error(), "files must be at version") { t.Fatalf("missing or wrong error: %v", err) } if err := pdb.Read(jirix, "./testdata/mismatched_versions"); err == nil || !strings.Contains(err.Error(), "files must have the same version") { t.Fatalf("missing or wrong error: %v", err) } if err := pdb.Read(jirix, "./testdata/db_dir"); err != nil { t.Fatal(err) } names := pdb.Names() if got, want := names, []string{"m1:a", "m1:b", "m2:a", "m2:b"}; !reflect.DeepEqual(got, want) { t.Fatalf("got %v, want %v", got, want) } profile := pdb.LookupProfile("m2", "a") if got, want := profile.Root(), "root"; got != want { t.Errorf("got %v, want %v", got, want) } profile = pdb.LookupProfile("m1", "b") if got, want := profile.Root(), "r1"; got != want { t.Errorf("got %v, want %v", got, want) } }
func TestProjectToFromFile(t *testing.T) { jirix, cleanup := jiritest.NewX(t) defer cleanup() tests := []struct { Project project.Project XML string }{ { // Default fields are dropped when marshaled, and added when unmarshaled. project.Project{ Name: "project1", Path: filepath.Join(jirix.Root, "path1"), Protocol: "git", Remote: "remote1", RemoteBranch: "master", Revision: "HEAD", }, `<project name="project1" path="path1" remote="remote1"/> `, }, { project.Project{ Name: "project2", Path: filepath.Join(jirix.Root, "path2"), GitHooks: filepath.Join(jirix.Root, "git-hooks"), RunHook: filepath.Join(jirix.Root, "run-hook"), Protocol: "git", Remote: "remote2", RemoteBranch: "branch2", Revision: "rev2", }, `<project name="project2" path="path2" remote="remote2" remotebranch="branch2" revision="rev2" githooks="git-hooks" runhook="run-hook"/> `, }, } for index, test := range tests { filename := filepath.Join(jirix.Root, fmt.Sprintf("test-%d", index)) if err := test.Project.ToFile(jirix, filename); err != nil { t.Errorf("%+v ToFile failed: %v", test.Project, err) } gotBytes, err := jirix.NewSeq().ReadFile(filename) if err != nil { t.Errorf("%+v ReadFile failed: %v", test.Project, err) } if got, want := string(gotBytes), test.XML; got != want { t.Errorf("%+v ToFile GOT\n%v\nWANT\n%v", test.Project, got, want) } project, err := project.ProjectFromFile(jirix, filename) if err != nil { t.Errorf("%+v FromFile failed: %v", test.Project, err) } if got, want := project, &test.Project; !reflect.DeepEqual(got, want) { t.Errorf("%+v FromFile got %#v, want %#v", test.Project, got, want) } } }
func TestReadingV0(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() if err := pdb.Read(jirix, "./testdata/legacy.xml"); err != nil { t.Fatal(err) } if got, want := pdb.SchemaVersion(), profiles.Original; got != want { t.Errorf("got %v, want %v", got, want) } oprofiles := pdb.Profiles() 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("cpu-os@1") pdb.InstallProfile("", "__first", "") if err := pdb.AddProfileTarget("", "__first", t1); err != nil { t.Fatal(err) } if err := pdb.Write(jirix, "", filename); err != nil { t.Fatal(err) } if err := pdb.Read(jirix, filename); err != nil { t.Fatal(err) } if got, want := pdb.SchemaVersion(), profiles.V5; got != want { t.Errorf("got %v, want %v", got, want) } nprofiles := pdb.Profiles() if got, want := len(nprofiles), 6; got != want { t.Fatalf("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 TestRead(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() if err := pdb.Read(jirix, "./testdata/m1.xml"); err != nil { t.Fatal(err) } names := pdb.Names() if got, want := names, []string{"test:a", "test:b"}; !reflect.DeepEqual(got, want) { t.Fatalf("got %v, want %v", got, want) } p := pdb.LookupProfile("test", "a") 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 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 TestReadDirWithPrevFiles(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() if err := pdb.Read(jirix, "./testdata/db_dir_with_prev"); err != nil { t.Fatal(err) } names := pdb.Names() if got, want := names, []string{"m1:a", "m2:b"}; !reflect.DeepEqual(got, want) { t.Fatalf("got %v, want %v", got, want) } profile := pdb.LookupProfile("m1", "a") if got, want := profile.Root(), "root"; got != want { t.Errorf("got %v, want %v", got, want) } profile = pdb.LookupProfile("m2", "b") if got, want := profile.Root(), "r2"; got != want { t.Errorf("got %v, want %v", got, want) } }
func TestWriteAndRename(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() filename := tmpFile() defer os.RemoveAll(filepath.Dir(filename)) addProfileAndTargets(t, pdb, "b") if exists(t, filename) { t.Fatalf("%q exists", filename) } if exists(t, filename+".prev") { t.Fatalf("%q exists", filename+".prev") } if err := pdb.Write(jirix, "test", filename); err != nil { t.Fatal(err) } if !exists(t, filename) { t.Fatalf("%q exists", filename) } if exists(t, filename+".prev") { t.Fatalf("%q exists", filename+".prev") } if err := pdb.Write(jirix, "test", filename); err != nil { t.Fatal(err) } if !exists(t, filename) { t.Fatalf("%q does not exist", filename) } if !exists(t, filename+".prev") { t.Fatalf("%q does not exist", filename+".prev") } }
func TestFileImportCycle(t *testing.T) { jirix, cleanup := jiritest.NewX(t) defer cleanup() // Set up the cycle .jiri_manifest -> A -> B -> A jiriManifest := project.Manifest{ LocalImports: []project.LocalImport{ {File: "A"}, }, } manifestA := project.Manifest{ LocalImports: []project.LocalImport{ {File: "B"}, }, } manifestB := project.Manifest{ LocalImports: []project.LocalImport{ {File: "A"}, }, } if err := jiriManifest.ToFile(jirix, jirix.JiriManifestFile()); err != nil { t.Fatal(err) } if err := manifestA.ToFile(jirix, filepath.Join(jirix.Root, "A")); err != nil { t.Fatal(err) } if err := manifestB.ToFile(jirix, filepath.Join(jirix.Root, "B")); err != nil { t.Fatal(err) } // The update should complain about the cycle. err := project.UpdateUniverse(jirix, false) if got, want := fmt.Sprint(err), "import cycle detected in local manifest files"; !strings.Contains(got, want) { t.Errorf("got error %v, want substr %v", got, want) } }
// TestLocalProjects tests the behavior of the LocalProjects method with // different ScanModes. func TestLocalProjects(t *testing.T) { jirix, cleanup := jiritest.NewX(t) defer cleanup() // Create some projects. numProjects, projectPaths := 3, []string{} for i := 0; i < numProjects; i++ { s := jirix.NewSeq() name := projectName(i) path := filepath.Join(jirix.Root, name) if err := s.MkdirAll(path, 0755).Done(); err != nil { t.Fatal(err) } // Initialize empty git repository. The commit is necessary, otherwise // "git rev-parse master" fails. git := gitutil.New(s, gitutil.RootDirOpt(path)) if err := git.Init(path); err != nil { t.Fatal(err) } if err := git.Commit(); err != nil { t.Fatal(err) } // Write project metadata. p := project.Project{ Path: path, Name: name, } if err := project.InternalWriteMetadata(jirix, p, path); err != nil { t.Fatalf("writeMetadata %v %v) failed: %v\n", p, path, err) } projectPaths = append(projectPaths, path) } // Create a latest update snapshot but only tell it about the first project. manifest := project.Manifest{ Projects: []project.Project{ { Name: projectName(0), Path: projectPaths[0], }, }, } if err := jirix.NewSeq().MkdirAll(jirix.UpdateHistoryDir(), 0755).Done(); err != nil { t.Fatalf("MkdirAll(%v) failed: %v", jirix.UpdateHistoryDir(), err) } if err := manifest.ToFile(jirix, jirix.UpdateHistoryLatestLink()); err != nil { t.Fatalf("manifest.ToFile(%v) failed: %v", jirix.UpdateHistoryLatestLink(), err) } // LocalProjects with scanMode = FastScan should only find the first // project. foundProjects, err := project.LocalProjects(jirix, project.FastScan) if err != nil { t.Fatalf("LocalProjects(%v) failed: %v", project.FastScan, err) } checkProjectsMatchPaths(t, foundProjects, projectPaths[:1]) // LocalProjects with scanMode = FullScan should find all projects. foundProjects, err = project.LocalProjects(jirix, project.FullScan) if err != nil { t.Fatalf("LocalProjects(%v) failed: %v", project.FastScan, err) } checkProjectsMatchPaths(t, foundProjects, projectPaths[:]) // Check that deleting a project forces LocalProjects to run a full scan, // even if FastScan is specified. if err := jirix.NewSeq().RemoveAll(projectPaths[0]).Done(); err != nil { t.Fatalf("RemoveAll(%v) failed: %v", projectPaths[0]) } foundProjects, err = project.LocalProjects(jirix, project.FastScan) if err != nil { t.Fatalf("LocalProjects(%v) failed: %v", project.FastScan, err) } checkProjectsMatchPaths(t, foundProjects, projectPaths[1:]) }
func TestTransitionBinDir(t *testing.T) { tests := []binDirTest{ { "No old dir", func(old, new string) error { return nil }, nil, "", false, }, { "Empty old dir", func(old, new string) error { return os.MkdirAll(old, 0777) }, nil, "", true, }, { "Populated old dir", func(old, new string) error { if err := os.MkdirAll(old, 0777); err != nil { return err } return ioutil.WriteFile(filepath.Join(old, "tool"), []byte("foo"), 0777) }, nil, "", true, }, { "Symlinked old dir", func(old, new string) error { if err := os.MkdirAll(filepath.Dir(old), 0777); err != nil { return err } return os.Symlink(new, old) }, nil, "", false, }, { "Symlinked old dir pointing elsewhere", func(old, new string) error { if err := os.MkdirAll(filepath.Dir(old), 0777); err != nil { return err } return os.Symlink(filepath.Dir(new), old) }, nil, "", true, }, { "Unreadable old dir parent", func(old, new string) error { if err := os.MkdirAll(old, 0777); err != nil { return err } return os.Chmod(filepath.Dir(old), 0222) }, func(old, new string) error { return os.Chmod(filepath.Dir(old), 0777) }, "Failed to stat old bin dir", false, }, { "Unwritable old dir", func(old, new string) error { if err := os.MkdirAll(old, 0777); err != nil { return err } return os.Chmod(old, 0444) }, func(old, new string) error { return os.Chmod(old, 0777) }, "Failed to backup old bin dir", false, }, { "Unreadable backup dir parent", func(old, new string) error { if err := os.MkdirAll(old, 0777); err != nil { return err } return os.Chmod(filepath.Dir(new), 0222) }, func(old, new string) error { return os.Chmod(filepath.Dir(new), 0777) }, "Failed to stat backup bin dir", false, }, { "Existing backup dir", func(old, new string) error { if err := os.MkdirAll(old, 0777); err != nil { return err } return os.MkdirAll(new+".BACKUP", 0777) }, nil, "Backup bin dir", false, }, } for _, test := range tests { jirix, cleanup := jiritest.NewX(t) if err := testTransitionBinDir(jirix, test); err != nil { t.Errorf("%s: %v", test.Name, err) } cleanup() } }