Пример #1
0
// 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")
}
Пример #2
0
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)
		}
	}
}
Пример #3
0
// NewX returns a new execution environment, given a cmdline env.
// It also prepends $JIRI_ROOT/.jiri_root/bin to the PATH.
func NewX(env *cmdline.Env) (*X, error) {
	ctx := tool.NewContextFromEnv(env)
	root, err := findJiriRoot(ctx.Timer())
	if err != nil {
		return nil, err
	}
	x := &X{
		Context: ctx,
		Root:    root,
		Usage:   env.UsageErrorf,
	}
	if ctx.Env()[PreservePathEnv] == "" {
		// Prepend $JIRI_ROOT/.jiri_root/bin to the PATH, so execing a binary will
		// invoke the one in that directory, if it exists.  This is crucial for jiri
		// subcommands, where we want to invoke the binary that jiri installed, not
		// whatever is in the user's PATH.
		//
		// Note that we must modify the actual os env variable with os.SetEnv and
		// also the ctx.env, so that execing a binary through the os/exec package
		// and with ctx.Run both have the correct behavior.
		newPath := envvar.PrependUniqueToken(ctx.Env()["PATH"], string(os.PathListSeparator), x.BinDir())
		ctx.Env()["PATH"] = newPath
		if err := os.Setenv("PATH", newPath); err != nil {
			return nil, err
		}
	}
	return x, nil
}
Пример #4
0
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"))
}
Пример #5
0
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"))
}
Пример #6
0
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)
		}
	}
}