// 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 TestList(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() dir, sh := buildInstallers(t), gosh.NewShell(t) sh.Vars["JIRI_ROOT"] = fake.X.Root sh.Vars["PATH"] = envvar.PrependUniqueToken(os.Getenv("PATH"), ":", dir) run(sh, dir, "jiri", "profile", "install", "--target=arch-os@2", "i1:eg", "i2:eg") if got, want := run(sh, dir, "jiri", "profile", "list", "--target=arch-os"), "i1:eg, i2:eg\n"; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := run(sh, dir, "jiri", "profile", "list"), "i1:eg, i2:eg\n"; got != want { t.Errorf("got %v, want %v", got, want) } for _, arg := range []string{"", "--info=SchemaVersion"} { sh.ContinueOnError = true sh.Err = nil got := run(sh, dir, "jiri", "profile", "list", "--target=no-suchtarget", arg) if sh.Err == nil { t.Errorf("expected an error for: jiri profile list --target=no-suchtarget %s", arg) } if want := "ERROR: no matching targets for no-suchtarget@\n"; got != want { t.Errorf("got %v, want %v", got, want) } } }
func TestManagerInstallUninstall(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() dir, sh := buildInstallers(t), gosh.NewShell(t) createProfilesDB(t, fake.X) sh.Vars["JIRI_ROOT"] = fake.X.Root sh.Vars["PATH"] = envvar.PrependUniqueToken(sh.Vars["PATH"], ":", dir) run(sh, dir, "jiri", "profile", "list", "-v") i1 := filepath.Join(fake.X.Root, ".jiri_root/profile_db/i1") i2 := filepath.Join(fake.X.Root, ".jiri_root/profile_db/i2") run(sh, dir, "jiri", "profile", "install", "--target=arch-os", "i1:eg", "i2:eg") for _, installer := range []string{"i1", "i2"} { tdir := filepath.Join(fake.X.Root, jiri.ProfilesRootDir, installer, "eg", "arch_os") contains(t, filepath.Join(tdir, "version"), "3") contains(t, filepath.Join(tdir, "3"), "3") } cmpFiles(t, i1, filepath.Join("testdata", "i1a.xml")) cmpFiles(t, i2, filepath.Join("testdata", "i2a.xml")) run(sh, dir, "jiri", "profile", "install", "--target=arch-os@2", "i1:eg", "i2:eg") // Installs are idempotent. run(sh, dir, "jiri", "profile", "install", "--target=arch-os@2", "i1:eg", "i2:eg") for _, installer := range []string{"i1", "i2"} { tdir := filepath.Join(fake.X.Root, jiri.ProfilesRootDir, installer, "eg", "arch_os") contains(t, filepath.Join(tdir, "version"), "2") contains(t, filepath.Join(tdir, "3"), "3") contains(t, filepath.Join(tdir, "2"), "2") } cmpFiles(t, i1, filepath.Join("testdata", "i1b.xml")) cmpFiles(t, i2, filepath.Join("testdata", "i2b.xml")) run(sh, dir, "jiri", "profile", "uninstall", "--target=arch-os@2", "i1:eg", "i2:eg") for _, installer := range []string{"i1", "i2"} { tdir := filepath.Join(fake.X.Root, jiri.ProfilesRootDir, installer, "eg", "arch_os") contains(t, filepath.Join(tdir, "version"), "2") contains(t, filepath.Join(tdir, "3"), "3") if got, want := exists(filepath.Join(tdir, "2")), false; got != want { t.Errorf("%s: got %v, want %v", filepath.Join(tdir, "2"), got, want) } } cmpFiles(t, i1, filepath.Join("testdata", "i1c.xml")) cmpFiles(t, i2, filepath.Join("testdata", "i2c.xml")) // Put v2 back. run(sh, dir, "jiri", "profile", "list", "-v") run(sh, dir, "jiri", "profile", "install", "--target=arch-os@2", "i1:eg", "i2:eg") cmpFiles(t, i1, filepath.Join("testdata", "i1b.xml")) cmpFiles(t, i2, filepath.Join("testdata", "i2b.xml")) }
func buildJiri(t *testing.T) string { buildJiriOnce.Do(func() { binDir, err := ioutil.TempDir("", "") if err != nil { t.Fatal(err) } sh := gosh.NewShell(t) defer sh.Cleanup() gosh.BuildGoPkg(sh, binDir, "v.io/jiri/cmd/jiri", "-o", "jiri") buildJiriBinDir = binDir }) return buildJiriBinDir }
func testImport(t *testing.T, jiriTool string, test importTestCase) error { sh := gosh.NewShell(t) defer sh.Cleanup() tmpDir := sh.MakeTempDir() jiriRoot := filepath.Join(tmpDir, "root") if err := os.Mkdir(jiriRoot, 0755); err != nil { return err } sh.Pushd(jiriRoot) filename := test.Filename if filename == "" { filename = ".jiri_manifest" } // Set up manfile for the local file import tests. It should exist in both // the tmpDir (for ../manfile tests) and jiriRoot. for _, dir := range []string{tmpDir, jiriRoot} { if err := ioutil.WriteFile(filepath.Join(dir, "manfile"), nil, 0644); err != nil { return err } } // Set up an existing file if it was specified. if test.Exist != "" { if err := ioutil.WriteFile(filename, []byte(test.Exist), 0644); err != nil { return err } } // Run import and check the error. sh.Vars[jiri.RootEnv] = jiriRoot cmd := sh.Cmd(jiriTool, append([]string{"import"}, test.Args...)...) if test.Stderr != "" { cmd.ExitErrorIsOk = true } stdout, stderr := cmd.StdoutStderr() if got, want := stdout, test.Stdout; !strings.Contains(got, want) || (got != "" && want == "") { return fmt.Errorf("stdout got %q, want substr %q", got, want) } if got, want := stderr, test.Stderr; !strings.Contains(got, want) || (got != "" && want == "") { return fmt.Errorf("stderr got %q, want substr %q", got, want) } // Make sure the right file is generated. if test.Want != "" { data, err := ioutil.ReadFile(filename) if err != nil { return err } if got, want := string(data), test.Want; got != want { return fmt.Errorf("GOT\n%s\nWANT\n%s", got, want) } } return nil }
// TestMain must cleanup these directories created by this function. func buildInstallers(t *testing.T) string { buildInstallersOnce.Do(func() { binDir, err := ioutil.TempDir("", "") if err != nil { t.Fatal(err) } sh := gosh.NewShell(t) defer sh.Cleanup() prefix := "v.io/jiri/profiles/profilescmdline/internal/" gosh.BuildGoPkg(sh, binDir, "v.io/jiri/cmd/jiri", "-o", "jiri") gosh.BuildGoPkg(sh, binDir, prefix+"i1", "-o", "jiri-profile-i1") gosh.BuildGoPkg(sh, binDir, prefix+"i2", "-o", "jiri-profile-i2") buildInstallersBinDir = binDir }) return buildInstallersBinDir }
func TestManagerUpdate(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() dir, sh := buildInstallers(t), gosh.NewShell(t) createProfilesDB(t, fake.X) sh.Vars["JIRI_ROOT"] = fake.X.Root sh.Vars["PATH"] = envvar.PrependUniqueToken(sh.Vars["PATH"], ":", dir) i1 := filepath.Join(fake.X.ProfilesDBDir(), "i1") i2 := filepath.Join(fake.X.ProfilesDBDir(), "i2") run(sh, dir, "jiri", "profile", "install", "--target=arch-os@2", "i1:eg", "i2:eg") cmpFiles(t, i1, filepath.Join("testdata", "i1d.xml")) cmpFiles(t, i2, filepath.Join("testdata", "i2d.xml")) run(sh, dir, "jiri", "profile", "update") cmpFiles(t, i1, filepath.Join("testdata", "i1e.xml")) cmpFiles(t, i2, filepath.Join("testdata", "i2e.xml")) run(sh, dir, "jiri", "profile", "cleanup", "-gc") cmpFiles(t, i1, filepath.Join("testdata", "i1f.xml")) cmpFiles(t, i2, filepath.Join("testdata", "i2f.xml")) run(sh, dir, "jiri", "profile", "cleanup", "-rm-all") profiledir := filepath.Join(fake.X.Root, jiri.ProfilesRootDir) for _, dir := range []string{ fake.X.ProfilesDBDir(), filepath.Join(profiledir, "i1"), filepath.Join(profiledir, "i2"), } { _, err := os.Stat(dir) if !os.IsNotExist(err) { t.Errorf("%v still exists: %v", dir, err) } } // Start over, make sure update is idempotent. createProfilesDB(t, fake.X) run(sh, dir, "jiri", "profile", "install", "--target=arch-os@2", "i1:eg") run(sh, dir, "jiri", "profile", "update") run(sh, dir, "jiri", "profile", "update") cmpFiles(t, i1, filepath.Join("testdata", "i1g.xml")) run(sh, dir, "jiri", "profile", "install", "--target=arch-os@4", "i1:eg") run(sh, dir, "jiri", "profile", "update") cmpFiles(t, i1, filepath.Join("testdata", "i1h.xml")) }
func TestManagerAvailable(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() dir, sh := buildInstallers(t), gosh.NewShell(t) createProfilesDB(t, fake.X) sh.Vars["JIRI_ROOT"] = fake.X.Root sh.Vars["PATH"] = envvar.PrependUniqueToken(sh.Vars["PATH"], ":", dir) stdout := run(sh, dir, "jiri", "profile", "available", "-v") for _, installer := range []string{"i1", "i2"} { re := regexp.MustCompile("Available Subcommands:.*profile-" + installer + ".*\n") if got := stdout; !re.MatchString(got) { t.Errorf("%v does not match %v\n", got, re.String()) } if got, want := stdout, installer+":eg"; !strings.Contains(got, want) { t.Errorf("%v does not contain %v\n", got, want) } } }
func TestBootstrapJiriAlreadyExists(t *testing.T) { sh := gosh.NewShell(t) sh.PropagateChildOutput = true defer sh.Cleanup() bootstrap, err := filepath.Abs("../../scripts/bootstrap_jiri") if err != nil { t.Fatalf("couldn't determine absolute path to bootstrap_jiri script") } rootDir := sh.MakeTempDir() c := sh.Cmd(bootstrap, rootDir) c.ExitErrorIsOk = true stdout, stderr := c.StdoutStderr() if c.Err == nil { t.Errorf("error got %q, want nil", c.Err) } if got, want := stdout, ""; got != want { t.Errorf("stdout got %q, want %q", got, want) } if got, want := stderr, rootDir+" already exists"; !strings.Contains(got, want) { t.Errorf("stderr got %q, want substr %q", got, want) } }
// TestBuildJiriLocally checks that the jiri binary built in the bootstrap // script can be built locally. func TestBuildJiriLocally(t *testing.T) { sh := gosh.NewShell(t) sh.PropagateChildOutput = true defer sh.Cleanup() // Extract jiri package path from this line. // GOPATH="${tmp_dir}" go build -o "${bin_dir}/jiri" v.io/jiri/cmd/jiri bootstrap, err := filepath.Abs("../../scripts/bootstrap_jiri") if err != nil { t.Fatalf("couldn't determine absolute path to bootstrap_jiri script") } pkgRE := regexp.MustCompile(`.*go build.*\s([^\s]*)\n`) content, err := ioutil.ReadFile(bootstrap) if err != nil { t.Fatalf("couldn't read bootstrap script: %v", err) } matches := pkgRE.FindStringSubmatch(string(content)) if len(matches) <= 1 { t.Fatalf("couldn't find jiri package from the bootstrap_jiri script") } pkg := matches[1] sh.Cmd("jiri", "go", "build", "-o", filepath.Join(sh.MakeTempDir(), "jiri"), pkg).Run() }
func TestBootstrapJiri(t *testing.T) { sh := gosh.NewShell(t) sh.PropagateChildOutput = true defer sh.Cleanup() bootstrap, err := filepath.Abs("../../scripts/bootstrap_jiri") if err != nil { t.Fatalf("couldn't determine absolute path to bootstrap_jiri script") } rootDir := filepath.Join(sh.MakeTempDir(), "root") stdout, stderr := sh.Cmd(bootstrap, rootDir).StdoutStderr() if got, want := stdout, fmt.Sprintf("Please add %s to your PATH.\n", filepath.Join(rootDir, ".jiri_root", "scripts")); got != want { t.Errorf("stdout got %q, want %q", got, want) } if got, want := stderr, ""; got != want { t.Errorf("stderr got %q, want %q", got, want) } if _, err := os.Stat(filepath.Join(rootDir, ".jiri_root", "bin", "jiri")); err != nil { t.Error(err) } if _, err := os.Stat(filepath.Join(rootDir, ".jiri_root", "scripts", "jiri")); err != nil { t.Error(err) } }
func TestRunP(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() projects := addProjects(t, fake) dir, sh := buildJiri(t), gosh.NewShell(t) if got, want := len(projects), 5; got != want { t.Errorf("got %v, want %v", got, want) } cwd, err := os.Getwd() if err != nil { t.Fatal(err) } defer os.Chdir(cwd) chdir := func(dir string) { if err := os.Chdir(dir); err != nil { t.Fatal(err) } } manifestKey := strings.Replace(string(projects[0].Key()), "r.a", "manifest", -1) keys := []string{manifestKey} for _, p := range projects { keys = append(keys, string(p.Key())) } chdir(projects[0].Path) got := run(sh, dir, "jiri", "runp", "--show-name-prefix", "-v", "echo") hdr := "Project Names: manifest r.a r.b r.c r.t1 r.t2\n" hdr += "Project Keys: " + strings.Join(keys, " ") + "\n" if want := hdr + "manifest: \nr.a: \nr.b: \nr.c: \nr.t1: \nr.t2:"; got != want { t.Errorf("got %v, want %v", got, want) } got = run(sh, dir, "jiri", "runp", "-v", "--interactive=false", "basename", "$(", "jiri", "project", "info", "-f", "{{.Project.Path}}", ")") if want := hdr + "manifest\nr.a\nr.b\nr.c\nr.t1\nr.t2"; got != want { t.Errorf("got %v, want %v", got, want) } got = run(sh, dir, "jiri", "runp", "--interactive=false", "git", "rev-parse", "--abbrev-ref", "HEAD") if want := "master\nmaster\nmaster\nmaster\nmaster\nmaster"; got != want { t.Errorf("got %v, want %v", got, want) } got = run(sh, dir, "jiri", "runp", "-interactive=false", "--show-name-prefix=true", "git", "rev-parse", "--abbrev-ref", "HEAD") if want := "manifest: master\nr.a: master\nr.b: master\nr.c: master\nr.t1: master\nr.t2: master"; got != want { t.Errorf("got %v, want %v", got, want) } got = run(sh, dir, "jiri", "runp", "--interactive=false", "--show-key-prefix=true", "git", "rev-parse", "--abbrev-ref", "HEAD") if want := strings.Join(keys, ": master\n") + ": master"; got != want { t.Errorf("got %v, want %v", got, want) } uncollated := run(sh, dir, "jiri", "runp", "--interactive=false", "--collate-stdout=false", "--show-name-prefix=true", "git", "rev-parse", "--abbrev-ref", "HEAD") split := strings.Split(uncollated, "\n") sort.Strings(split) got = strings.TrimSpace(strings.Join(split, "\n")) if want := "manifest: master\nr.a: master\nr.b: master\nr.c: master\nr.t1: master\nr.t2: master"; got != want { t.Errorf("got %v, want %v", got, want) } got = run(sh, dir, "jiri", "runp", "--show-name-prefix", "--projects=r.t[12]", "echo") if want := "r.t1: \nr.t2:"; got != want { t.Errorf("got %v, want %v", got, want) } rb := projects[1].Path rc := projects[2].Path t1 := projects[3].Path s := fake.X.NewSeq() newfile := func(dir, file string) { testfile := filepath.Join(dir, file) _, err := s.Create(testfile) if err != nil { t.Errorf("failed to create %s: %v", testfile, err) } } git := func(dir string) *gitutil.Git { return gitutil.New(fake.X.NewSeq(), gitutil.RootDirOpt(dir)) } newfile(rb, "untracked.go") got = run(sh, dir, "jiri", "runp", "--has-untracked", "--show-name-prefix", "echo") if want := "r.b:"; got != want { t.Errorf("got %v, want %v", got, want) } got = run(sh, dir, "jiri", "runp", "--has-untracked=false", "--show-name-prefix", "echo") if want := "manifest: \nr.a: \nr.c: \nr.t1: \nr.t2:"; got != want { t.Errorf("got %v, want %v", got, want) } newfile(rc, "uncommitted.go") if err := git(rc).Add("uncommitted.go"); err != nil { t.Error(err) } got = run(sh, dir, "jiri", "runp", "--has-uncommitted", "--show-name-prefix", "echo") if want := "r.c:"; got != want { t.Errorf("got %v, want %v", got, want) } got = run(sh, dir, "jiri", "runp", "--has-uncommitted=false", "--show-name-prefix", "echo") if want := "manifest: \nr.a: \nr.b: \nr.t1: \nr.t2:"; got != want { t.Errorf("got %v, want %v", got, want) } // test ordering of has-<x> flags newfile(rc, "untracked.go") got = run(sh, dir, "jiri", "runp", "--has-untracked", "--has-uncommitted", "--show-name-prefix", "echo") if want := "r.c:"; got != want { t.Errorf("got %v, want %v", got, want) } got = run(sh, dir, "jiri", "runp", "--has-uncommitted", "--has-untracked", "--show-name-prefix", "echo") if want := "r.c:"; got != want { t.Errorf("got %v, want %v", got, want) } git(rb).CreateAndCheckoutBranch("a1") git(rb).CreateAndCheckoutBranch("b2") git(rc).CreateAndCheckoutBranch("b2") git(t1).CreateAndCheckoutBranch("a1") chdir(rc) // Just the projects with branch b2. got = run(sh, dir, "jiri", "runp", "--show-name-prefix", "echo") if want := "r.b: \nr.c:"; got != want { t.Errorf("got %v, want %v", got, want) } // All projects since --projects takes precendence over branches. got = run(sh, dir, "jiri", "runp", "--projects=.*", "--show-name-prefix", "echo") if want := "manifest: \nr.a: \nr.b: \nr.c: \nr.t1: \nr.t2:"; got != want { t.Errorf("got %v, want %v", got, want) } if err := s.MkdirAll(filepath.Join(rb, ".jiri", "a1"), os.FileMode(0755)).Done(); err != nil { t.Fatal(err) } newfile(rb, filepath.Join(".jiri", "a1", ".gerrit_commit_message")) git(rb).CheckoutBranch("a1") git(t1).CheckoutBranch("a1") chdir(t1) got = run(sh, dir, "jiri", "runp", "--has-gerrit-message", "--show-name-prefix", "echo") if want := "r.b:"; got != want { t.Errorf("got %v, want %v", got, want) } got = run(sh, dir, "jiri", "runp", "--has-gerrit-message=false", "--show-name-prefix", "echo") if want := "r.t1:"; got != want { t.Errorf("got %v, want %v", got, want) } }
func TestImport(t *testing.T) { tests := []importTestCase{ { Stderr: `wrong number of arguments`, }, { Args: []string{"a"}, Stderr: `wrong number of arguments`, }, { Args: []string{"a", "b", "c"}, Stderr: `wrong number of arguments`, }, // Remote imports, default append behavior { Args: []string{"-name=name", "-remote-branch=remotebranch", "-root=root", "foo", "https://github.com/new.git"}, Want: `<manifest> <imports> <import manifest="foo" name="name" remote="https://github.com/new.git" remotebranch="remotebranch" root="root"/> </imports> </manifest> `, }, { Args: []string{"foo", "https://github.com/new.git"}, Want: `<manifest> <imports> <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> </imports> </manifest> `, }, { Args: []string{"-out=file", "foo", "https://github.com/new.git"}, Filename: `file`, Want: `<manifest> <imports> <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> </imports> </manifest> `, }, { Args: []string{"-out=-", "foo", "https://github.com/new.git"}, Stdout: `<manifest> <imports> <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> </imports> </manifest> `, }, { Args: []string{"foo", "https://github.com/new.git"}, Exist: `<manifest> <imports> <import manifest="bar" name="manifest" remote="https://github.com/orig.git"/> </imports> </manifest> `, Want: `<manifest> <imports> <import manifest="bar" name="manifest" remote="https://github.com/orig.git"/> <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> </imports> </manifest> `, }, // Remote imports, explicit overwrite behavior { Args: []string{"-overwrite", "foo", "https://github.com/new.git"}, Want: `<manifest> <imports> <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> </imports> </manifest> `, }, { Args: []string{"-overwrite", "-out=file", "foo", "https://github.com/new.git"}, Filename: `file`, Want: `<manifest> <imports> <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> </imports> </manifest> `, }, { Args: []string{"-overwrite", "-out=-", "foo", "https://github.com/new.git"}, Stdout: `<manifest> <imports> <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> </imports> </manifest> `, }, { Args: []string{"-overwrite", "foo", "https://github.com/new.git"}, Exist: `<manifest> <imports> <import manifest="bar" name="manifest" remote="https://github.com/orig.git"/> </imports> </manifest> `, Want: `<manifest> <imports> <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> </imports> </manifest> `, }, } sh := gosh.NewShell(t) defer sh.Cleanup() jiriTool := gosh.BuildGoPkg(sh, sh.MakeTempDir(), "v.io/jiri/cmd/jiri") for _, test := range tests { if err := testImport(t, jiriTool, test); err != nil { t.Errorf("%v: %v", test.Args, err) } } }