// setupTest creates a setup for testing the review tool. func setupTest(t *testing.T, installHook bool) (ctx *tool.Context, cwd string, root *project.FakeJiriRoot, repoPath, originPath, gerritPath string) { ctx = tool.NewDefaultContext() cwd, err := os.Getwd() if err != nil { t.Fatalf("Getwd() failed: %v", err) } root, err = project.NewFakeJiriRoot(ctx) if err != nil { t.Fatalf("%v", err) } repoPath, originPath, gerritPath = createTestRepos(t, ctx, root.Dir) if installHook == true { for _, path := range []string{repoPath, originPath, gerritPath} { installCommitMsgHook(t, ctx, path) } } chdir(t, ctx, repoPath) return }
func TestConfigSerialization(t *testing.T) { ctx := tool.NewDefaultContext() 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) } }() oldRoot, err := project.JiriRoot() if err := os.Setenv("JIRI_ROOT", root.Dir); err != nil { t.Fatalf("%v", err) } defer os.Setenv("JIRI_ROOT", oldRoot) config := NewConfig( APICheckProjectsOpt(apiCheckProjects), CopyrightCheckProjectsOpt(copyrightCheckProjects), GoWorkspacesOpt(goWorkspaces), JenkinsMatrixJobsOpt(jenkinsMatrixJobs), ProjectTestsOpt(projectTests), TestDependenciesOpt(testDependencies), TestGroupsOpt(testGroups), TestPartsOpt(testParts), VDLWorkspacesOpt(vdlWorkspaces), ) if err := SaveConfig(ctx, config); err != nil { t.Fatalf("%v", err) } gotConfig, err := LoadConfig(ctx) if err != nil { t.Fatalf("%v", err) } testConfigAPI(t, gotConfig) }
func TestList(t *testing.T) { 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) } }() oldRoot, err := project.JiriRoot() if err := os.Setenv("JIRI_ROOT", root.Dir); err != nil { t.Fatalf("%v", err) } defer os.Setenv("JIRI_ROOT", oldRoot) remoteSnapshotDir, err := project.RemoteSnapshotDir() if err != nil { t.Fatalf("%v", err) } localSnapshotDir, err := project.LocalSnapshotDir() if err != nil { t.Fatalf("%v", err) } // Create a test suite. tests := []config{ config{ remote: false, dir: localSnapshotDir, }, config{ remote: true, dir: remoteSnapshotDir, }, } labels := []label{ label{ name: "beta", snapshots: []string{"beta-1", "beta-2", "beta-3"}, }, label{ name: "stable", snapshots: []string{"stable-1", "stable-2", "stable-3"}, }, } for _, test := range tests { remoteFlag = test.remote // Create the snapshots directory and populate it with the // data specified by the test suite. for _, label := range labels { createLabelDir(t, ctx, test.dir, label.name, label.snapshots) } // Check that running "jiri snapshot list" with no arguments // returns the expected output. var stdout bytes.Buffer env := &cmdline.Env{Stdout: &stdout} if err != nil { t.Fatalf("%v", err) } if err := runSnapshotList(env, nil); err != nil { t.Fatalf("%v", err) } got, want := stdout.String(), generateOutput(labels) if got != want { t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v\n", got, want) } // Check that running "jiri snapshot list" with one argument // returns the expected output. stdout.Reset() if err := runSnapshotList(env, []string{"stable"}); err != nil { t.Fatalf("%v", err) } got, want = stdout.String(), generateOutput(labels[1:]) if got != want { t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v\n", got, want) } // Check that running "jiri snapshot list" with // multiple arguments returns the expected output. stdout.Reset() if err := runSnapshotList(env, []string{"beta", "stable"}); err != nil { t.Fatalf("%v", err) } got, want = stdout.String(), generateOutput(labels) if got != want { t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v\n", got, want) } } }
func TestCreate(t *testing.T) { ctx := tool.NewDefaultContext() // Setup a fake JIRI_ROOT instance. 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) } }() // Setup the initial remote and local projects. numProjects, remoteProjects := 2, []string{} for i := 0; i < numProjects; i++ { if err := root.CreateRemoteProject(ctx, remoteProjectName(i)); err != nil { t.Fatalf("%v", err) } if err := root.AddProject(ctx, project.Project{ Name: remoteProjectName(i), Path: localProjectName(i), Remote: root.Projects[remoteProjectName(i)], }); err != nil { t.Fatalf("%v", err) } } // Point the JIRI_ROOT environment variable to the fake. oldRoot, err := project.JiriRoot() if err := os.Setenv("JIRI_ROOT", root.Dir); err != nil { t.Fatalf("%v", err) } defer os.Setenv("JIRI_ROOT", oldRoot) // Create initial commits in the remote projects and use // UpdateUniverse() to mirror them locally. for i := 0; i < numProjects; i++ { writeReadme(t, ctx, root.Projects[remoteProjectName(i)], "revision 1") } if err := project.UpdateUniverse(ctx, true); err != nil { t.Fatalf("%v", err) } // Create a local snapshot. var stdout bytes.Buffer env := &cmdline.Env{Stdout: &stdout} remoteFlag = false if err := runSnapshotCreate(env, []string{"test-local"}); err != nil { t.Fatalf("%v", err) } // Remove the local project repositories. for i, _ := range remoteProjects { localProject := filepath.Join(root.Dir, localProjectName(i)) if err := ctx.Run().RemoveAll(localProject); err != nil { t.Fatalf("%v", err) } } // Check that invoking the UpdateUniverse() with the local // snapshot restores the local repositories. snapshotDir, err := project.LocalSnapshotDir() if err != nil { t.Fatalf("%v", err) } snapshotFile := filepath.Join(snapshotDir, "test-local") localCtx := ctx.Clone(tool.ContextOpts{ Manifest: &snapshotFile, }) if err := project.UpdateUniverse(localCtx, true); err != nil { t.Fatalf("%v", err) } for i, _ := range remoteProjects { localProject := filepath.Join(root.Dir, localProjectName(i)) checkReadme(t, ctx, localProject, "revision 1") } // Create a remote snapshot. remoteFlag = true root.EnableRemoteManifestPush(ctx) if err := runSnapshotCreate(env, []string{"test-remote"}); err != nil { t.Fatalf("%v", err) } // Remove the local project repositories. for i, _ := range remoteProjects { localProject := filepath.Join(root.Dir, localProjectName(i)) if err := ctx.Run().RemoveAll(localProject); err != nil { t.Fatalf("%v", err) } } // Check that invoking the UpdateUniverse() with the remote snapshot // restores the local repositories. manifest := "snapshot/test-remote" remoteCtx := ctx.Clone(tool.ContextOpts{ Manifest: &manifest, }) if err := project.UpdateUniverse(remoteCtx, true); err != nil { t.Fatalf("%v", err) } for i, _ := range remoteProjects { localProject := filepath.Join(root.Dir, localProjectName(i)) checkReadme(t, ctx, localProject, "revision 1") } }
func TestOncall(t *testing.T) { ctx := tool.NewDefaultContext() 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) } }() oldRoot, err := project.JiriRoot() if err != nil { t.Fatalf("%v", err) } if err := os.Setenv("JIRI_ROOT", root.Dir); err != nil { t.Fatalf("%v", err) } defer os.Setenv("JIRI_ROOT", oldRoot) // Create a oncall.v1.xml file. createOncallFile(t, ctx) type testCase struct { targetTime time.Time expectedShift *OncallShift } testCases := []testCase{ testCase{ targetTime: time.Date(2013, time.November, 5, 12, 0, 0, 0, time.Local), expectedShift: nil, }, testCase{ targetTime: time.Date(2014, time.November, 5, 12, 0, 0, 0, time.Local), expectedShift: &OncallShift{ Primary: "spetrovic", Secondary: "suharshs", Date: "Nov 5, 2014 12:00:00 PM", }, }, testCase{ targetTime: time.Date(2014, time.November, 5, 14, 0, 0, 0, time.Local), expectedShift: &OncallShift{ Primary: "spetrovic", Secondary: "suharshs", Date: "Nov 5, 2014 12:00:00 PM", }, }, testCase{ targetTime: time.Date(2014, time.November, 20, 14, 0, 0, 0, time.Local), expectedShift: &OncallShift{ Primary: "jsimsa", Secondary: "toddw", Date: "Nov 19, 2014 12:00:00 PM", }, }, } for _, test := range testCases { got, err := Oncall(ctx, test.targetTime) if err != nil { t.Fatalf("want no errors, got: %v", err) } if !reflect.DeepEqual(test.expectedShift, got) { t.Fatalf("want %#v, got %#v", test.expectedShift, got) } } }
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) } }