Exemple #1
0
func TestRound(t *testing.T) {
	t.Parallel()
	ut.AssertEqual(t, 1500*time.Millisecond, round(1549*time.Millisecond, 100*time.Millisecond))
	ut.AssertEqual(t, 1600*time.Millisecond, round(1550*time.Millisecond, 100*time.Millisecond))
	ut.AssertEqual(t, -1500*time.Millisecond, round(-1549*time.Millisecond, 100*time.Millisecond))
	ut.AssertEqual(t, -1600*time.Millisecond, round(-1550*time.Millisecond, 100*time.Millisecond))
}
Exemple #2
0
func TestConfigYAMLBadMode(t *testing.T) {
	data, err := yaml.Marshal("foo")
	ut.AssertEqual(t, nil, err)
	v := PreCommit
	ut.AssertEqual(t, errors.New("invalid mode \"foo\""), yaml.Unmarshal(data, &v))
	ut.AssertEqual(t, PreCommit, v)
}
Exemple #3
0
func TestChangIgnore(t *testing.T) {
	t.Parallel()
	c := newChange(&dummyRepo{t, "<root>"}, nil, nil, IgnorePatterns{"*.pb.go"})
	ut.AssertEqual(t, false, c.IsIgnored("foo.go"))
	ut.AssertEqual(t, true, c.IsIgnored("foo.pb.go"))
	ut.AssertEqual(t, true, c.IsIgnored("bar/foo.pb.go"))
}
Exemple #4
0
func setup(t *testing.T, tmpDir string) {
	_, code, err := internal.Capture(tmpDir, nil, "git", "init")
	ut.AssertEqual(t, 0, code)
	ut.AssertEqual(t, nil, err)
	run(t, tmpDir, nil, "config", "user.email", "nobody@localhost")
	run(t, tmpDir, nil, "config", "user.name", "nobody")
}
Exemple #5
0
func TestConfigYAML(t *testing.T) {
	config := New("0.1")
	data, err := yaml.Marshal(config)
	ut.AssertEqual(t, nil, err)
	actual := &Config{}
	ut.AssertEqual(t, nil, yaml.Unmarshal(data, actual))
	ut.AssertEqual(t, config, actual)
}
Exemple #6
0
func TestGetRepoNoRepo(t *testing.T) {
	t.Parallel()
	tmpDir, err := ioutil.TempDir("", "pre-commit-go")
	defer func() {
		if err := internal.RemoveAll(tmpDir); err != nil {
			t.Errorf("%s", err)
		}
	}()

	r, err := GetRepo(tmpDir, "")
	ut.AssertEqual(t, errors.New("failed to find git checkout root"), err)
	ut.AssertEqual(t, nil, r)
}
func TestCoveragePrerequisites(t *testing.T) {
	// This test can't be parallel.
	if !IsContinuousIntegration() {
		old := os.Getenv("CI")
		defer func() {
			ut.ExpectEqual(t, nil, os.Setenv("CI", old))
		}()
		ut.AssertEqual(t, nil, os.Setenv("CI", "true"))
		ut.AssertEqual(t, true, IsContinuousIntegration())
	}
	c := Coverage{UseCoveralls: true}
	ut.AssertEqual(t, 1, len(c.GetPrerequisites()))
}
Exemple #8
0
func run(t *testing.T, tmpDir string, env []string, args ...string) string {
	internal := &git{root: tmpDir}
	out, code, err := internal.captureEnv(env, args...)
	ut.AssertEqualf(t, 0, code, "%s", out)
	ut.AssertEqual(t, nil, err)
	return out
}
Exemple #9
0
func assertHEAD(t *testing.T, r ReadOnlyRepo, expected Commit) Commit {
	if head := r.Eval(string(Head)); head != expected {
		t.Logf("%s", strings.Join(os.Environ(), "\n"))
		t.Logf("%s", run(t, r.Root(), nil, "log", "-p", "--format=fuller"))
		ut.AssertEqual(t, expected, head)
	}
	return expected
}
func TestChecksDescriptions(t *testing.T) {
	t.Parallel()
	for _, name := range getKnownChecks() {
		c := KnownChecks[name]()
		ut.AssertEqual(t, true, c.GetDescription() != "")
		c.GetPrerequisites()
	}
}
func TestCustom(t *testing.T) {
	t.Parallel()
	p := []CheckPrerequisite{
		{
			HelpCommand:      []string{"go", "version"},
			ExpectedExitCode: 0,
			URL:              "example.com.local",
		},
	}
	c := &Custom{
		Description:   "foo",
		Command:       []string{"go", "version"},
		Prerequisites: p,
	}
	ut.AssertEqual(t, "foo", c.GetDescription())
	ut.AssertEqual(t, p, c.GetPrerequisites())
}
Exemple #12
0
func TestRemoveAllMissing(t *testing.T) {
	td, err := ioutil.TempDir("", "pre-commit-go")
	ut.ExpectEqual(t, nil, err)

	foo := filepath.Join(td, "foo")
	err = ioutil.WriteFile(foo, []byte("yo"), 0600)
	ut.ExpectEqual(t, nil, err)
	ut.AssertEqual(t, nil, RemoveAll(td))
}
Exemple #13
0
func TestRangeToString(t *testing.T) {
	t.Parallel()
	ut.AssertEqual(t, "", rangeToString(nil))
	ut.AssertEqual(t, "1", rangeToString([]int{1}))
	ut.AssertEqual(t, "1-2", rangeToString([]int{1, 2}))
	ut.AssertEqual(t, "1-3", rangeToString([]int{1, 2, 3}))
	ut.AssertEqual(t, "1,3-4,6-8", rangeToString([]int{1, 3, 4, 6, 7, 8}))
	ut.AssertEqual(t, "1,3-4,6", rangeToString([]int{1, 3, 4, 6}))
}
func TestChecksSuccess(t *testing.T) {
	// Runs all checks, they should all pass.
	t.Parallel()
	if testing.Short() {
		t.SkipNow()
	}
	td, err := ioutil.TempDir("", "pre-commit-go")
	ut.AssertEqual(t, nil, err)
	defer func() {
		if err := internal.RemoveAll(td); err != nil {
			t.Fail()
		}
	}()
	change := setup(t, td, goodFiles)
	for _, name := range getKnownChecks() {
		c := KnownChecks[name]()
		switch name {
		case "custom":
			c = &Custom{
				Description:   "foo",
				Command:       []string{"go", "version"},
				CheckExitCode: true,
				Prerequisites: []CheckPrerequisite{
					{
						HelpCommand:      []string{"go", "version"},
						ExpectedExitCode: 0,
						URL:              "example.com.local",
					},
				},
			}
		case "copyright":
			cop := c.(*Copyright)
			cop.Header = "// Foo"
		case "coverage":
			cov := c.(*Coverage)
			cov.Global.MinCoverage = 100
			cov.Global.MaxCoverage = 100
			cov.PerDirDefault.MinCoverage = 100
			cov.PerDirDefault.MaxCoverage = 100
		}
		if l, ok := c.(sync.Locker); ok {
			l.Lock()
			l.Unlock()
		}
		if err := c.Run(change, &Options{MaxDuration: 1}); err != nil {
			t.Errorf("%s failed: %s", c.GetName(), err)
		}
	}
}
func setup(t *testing.T, td string, files map[string]string) scm.Change {
	fooDir := filepath.Join(td, "src", "foo")
	ut.AssertEqual(t, nil, os.MkdirAll(fooDir, 0700))
	for f, c := range files {
		p := filepath.Join(fooDir, f)
		ut.AssertEqual(t, nil, os.MkdirAll(filepath.Dir(p), 0700))
		ut.AssertEqual(t, nil, ioutil.WriteFile(p, []byte(c), 0600))
	}
	_, code, err := internal.Capture(fooDir, nil, "git", "init")
	ut.AssertEqual(t, 0, code)
	ut.AssertEqual(t, nil, err)
	// It's important to add the files to the index, otherwise they will be
	// ignored.
	_, code, err = internal.Capture(fooDir, nil, "git", "add", ".")
	ut.AssertEqual(t, 0, code)
	ut.AssertEqual(t, nil, err)

	repo, err := scm.GetRepo(fooDir, td)
	ut.AssertEqual(t, nil, err)
	change, err := repo.Between(scm.Current, scm.GitInitialCommit, nil)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, true, change != nil)
	return change
}
Exemple #16
0
func TestConfigNew(t *testing.T) {
	config := New("0.1")
	ut.AssertEqual(t, 3, len(config.Modes[PreCommit].Checks))
	ut.AssertEqual(t, 3, len(config.Modes[PrePush].Checks))
	ut.AssertEqual(t, 5, len(config.Modes[ContinuousIntegration].Checks))
	ut.AssertEqual(t, 3, len(config.Modes[Lint].Checks))
	checks, options := config.EnabledChecks([]Mode{PreCommit, PrePush, ContinuousIntegration, Lint})
	ut.AssertEqual(t, Options{MaxDuration: 120}, *options)
	ut.AssertEqual(t, 2+4+5+3, len(checks))
}
Exemple #17
0
// makeTree creates a temporary directory and creates the files in it.
//
// Returns the root directly, all files created and the cleanup function.
func makeTree(t *testing.T, files map[string]string) (string, []string, func()) {
	td, err := ioutil.TempDir("", "pre-commit-go")
	ut.AssertEqual(t, nil, err)
	cleanup := func() {
		if err := internal.RemoveAll(td); err != nil {
			t.Fail()
		}
	}
	allFiles := make([]string, 0, len(files))
	for f, c := range files {
		allFiles = append(allFiles, f)
		p := filepath.Join(td, f)
		if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil {
			cleanup()
			t.Fatal(err)
		}
		if err := ioutil.WriteFile(p, []byte(c), 0600); err != nil {
			cleanup()
			t.Fatal(err)
		}
	}
	sort.Strings(allFiles)
	return td, allFiles, cleanup
}
Exemple #18
0
func TestCoverageGlobal(t *testing.T) {
	t.Parallel()
	if testing.Short() {
		t.SkipNow()
	}
	td, err := ioutil.TempDir("", "pre-commit-go")
	ut.AssertEqual(t, nil, err)
	defer func() {
		if err := internal.RemoveAll(td); err != nil {
			t.Fail()
		}
	}()
	change := setup(t, td, coverageFiles)

	c := &Coverage{
		UseGlobalInference: true,
		UseCoveralls:       false,
		Global: CoverageSettings{
			MinCoverage: 50,
			MaxCoverage: 100,
		},
		PerDirDefault: CoverageSettings{
			MinCoverage: 50,
			MaxCoverage: 100,
		},
		PerDir: map[string]*CoverageSettings{},
	}
	profile, err := c.RunProfile(change, &Options{MaxDuration: 1})
	ut.AssertEqual(t, nil, err)
	expected := CoverageProfile{
		{
			Source:    "foo.go",
			Line:      3,
			SourceRef: "foo.go:3",
			Name:      "Type.Foo",
			Covered:   2,
			Missing:   []int{},
			Total:     2,
			Percent:   100,
		},
		{
			Source:    "bar/bar.go",
			Line:      2,
			SourceRef: "bar/bar.go:2",
			Name:      "Bar",
			Covered:   2,
			Missing:   []int{7, 8},
			Total:     4,
			Percent:   50,
		},
		{
			Source:    "bar/bar.go",
			Line:      11,
			SourceRef: "bar/bar.go:11",
			Name:      "Baz",
			Covered:   2,
			Missing:   []int{16, 17},
			Total:     4,
			Percent:   50,
		},
	}
	ut.AssertEqual(t, expected, profile)
	ut.AssertEqual(t, 60., profile.CoveragePercent())
	ut.AssertEqual(t, 2, profile.PartiallyCoveredFuncs())

	expected = CoverageProfile{
		{
			Source:    "bar.go",
			Line:      2,
			SourceRef: "bar/bar.go:2",
			Name:      "Bar",
			Covered:   2,
			Missing:   []int{7, 8},
			Total:     4,
			Percent:   50,
		},
		{
			Source:    "bar.go",
			Line:      11,
			SourceRef: "bar/bar.go:11",
			Name:      "Baz",
			Covered:   2,
			Missing:   []int{16, 17},
			Total:     4,
			Percent:   50,
		},
	}
	ut.AssertEqual(t, expected, profile.Subset("bar"))

	expected = CoverageProfile{
		{
			Source:    "foo.go",
			Line:      3,
			SourceRef: "foo.go:3",
			Name:      "Type.Foo",
			Covered:   2,
			Missing:   []int{},
			Total:     2,
			Percent:   100,
		},
	}
	ut.AssertEqual(t, expected, profile.Subset("."))
}
Exemple #19
0
func TestGetRepoGitSlowFailures(t *testing.T) {
	t.Parallel()
	tmpDir, err := ioutil.TempDir("", "pre-commit-go")
	defer func() {
		if err := internal.RemoveAll(tmpDir); err != nil {
			t.Errorf("%s", err)
		}
	}()

	setup(t, tmpDir)
	r, err := getRepo(tmpDir, tmpDir)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, tmpDir, r.Root())
	// Remove the .git directory after calling GetRepo().
	ut.AssertEqual(t, nil, internal.RemoveAll(filepath.Join(tmpDir, ".git")))

	p, err := r.HookPath()
	ut.AssertEqual(t, errors.New("failed to find .git dir: failed to find .git dir: failed to run \"git rev-parse --git-dir\""), err)
	ut.AssertEqual(t, "", p)

	ut.AssertEqual(t, []string(nil), r.untracked())
	ut.AssertEqual(t, []string(nil), r.unstaged())

	ut.AssertEqual(t, Commit(gitInitial), r.Eval(string(Head)))
	ut.AssertEqual(t, "", r.Ref(Head))

	done, err := r.Stash()
	ut.AssertEqual(t, errors.New("failed to get list of untracked files"), err)
	ut.AssertEqual(t, false, done)
	errStr := r.Restore().Error()
	if errStr != "git reset failed:\nfatal: Not a git repository: '.git'" && errStr != "git reset failed:\nfatal: Not a git repository (or any of the parent directories): .git" {
		t.Fatalf("Unexpected error: %s", errStr)
	}

	errStr = r.Checkout(string(Initial)).Error()
	if errStr != "checkout failed:\nfatal: Not a git repository: '.git'" && errStr != "checkout failed:\nfatal: Not a git repository (or any of the parent directories): .git" {
		t.Fatalf("Unexpected error: %s", errStr)
	}
}
func TestCheckPrerequisite(t *testing.T) {
	// Runs all checks, they should all pass.
	t.Parallel()
	ut.AssertEqual(t, true, (&CheckPrerequisite{HelpCommand: []string{"go", "version"}, ExpectedExitCode: 0}).IsPresent())
	ut.AssertEqual(t, false, (&CheckPrerequisite{HelpCommand: []string{"go", "version"}, ExpectedExitCode: 1}).IsPresent())
}
Exemple #21
0
func TestChangeAll(t *testing.T) {
	// All packages were affected, uses a slightly different (faster) code path.
	t.Parallel()
	if testing.Short() {
		t.SkipNow()
	}
	root, allFiles, cleanup := makeTree(t,
		map[string]string{
			"bar/bar.go":      "package bar\nfunc Bar() int { return 1}",
			"bar/bar_test.go": "package bar",
			"foo/foo.go":      "package foo\nfunc Foo() int { return 42}",
			"main.go":         "package main\nimport \"foo\"\nfunc main() { foo.Foo() }",
			"main_test.go":    "package main",
		})
	defer cleanup()
	r := &dummyRepo{t, root}
	c := newChange(r, []string{"bar/bar.go", "foo/foo.go", "main.go"}, allFiles, nil)
	ut.AssertEqual(t, r, c.Repo())
	ut.AssertEqual(t, "", c.Package())
	changed := c.Changed()
	ut.AssertEqual(t, []string{"bar/bar.go", "foo/foo.go", "main.go"}, changed.GoFiles())
	ut.AssertEqual(t, []string{".", "./bar", "./foo"}, changed.Packages())
	ut.AssertEqual(t, []string{".", "./bar"}, changed.TestPackages())
	indirect := c.Indirect()
	ut.AssertEqual(t, []string{"bar/bar.go", "foo/foo.go", "main.go"}, indirect.GoFiles())
	ut.AssertEqual(t, []string{".", "./bar", "./foo"}, indirect.Packages())
	ut.AssertEqual(t, []string{".", "./bar"}, indirect.TestPackages())
	all := c.All()
	ut.AssertEqual(t, []string{"bar/bar.go", "bar/bar_test.go", "foo/foo.go", "main.go", "main_test.go"}, all.GoFiles())
	ut.AssertEqual(t, []string{".", "./bar", "./foo"}, all.Packages())
	ut.AssertEqual(t, []string{".", "./bar"}, all.TestPackages())
}
Exemple #22
0
func TestInternalCheck(t *testing.T) {
	t.Parallel()
	d, err := os.Getwd()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, "scm", filepath.Base(d))
}
Exemple #23
0
func TestChangeIndirectReverse(t *testing.T) {
	// a_test.go is indirectly affect by z/z.go. Make sure the order files are
	// processed in does not affect the result.
	t.Parallel()
	if testing.Short() {
		t.SkipNow()
	}
	root, allFiles, cleanup := makeTree(t,
		map[string]string{
			// Is changed.
			"z/z.go": "package z\nfunc Bar() int { return 1}",
			// Directly affected.
			"z/z_test.go": "package z",
			// Indirectly affected.
			"y/y.go": "package y\nimport \"z\"\nfunc Bar() int { return z.Bar() }",
			// Not affected.
			"x/x.go": "package x\nfunc Foo() { return 42 }",
			// Indirectly affected by "y".
			"x/x_test.go": "package x\nimport (\n\"y\"\n\"testing\"\n)\nfunc TestFoo(t *testing.T) int { if y.Bar() != 1 { t.FailNow() } }",
			// Not affected even if it imports "x".
			"w/w.go": "package w\nimport \"x\"\nfunc Foo() { return x.Foo() }",
			// Indirectly affected by "b".
			"a_test.go": "package main\nimport \"y\"\nfunc main() { println(y.Bar()) }",
		})
	defer cleanup()
	r := &dummyRepo{t, root}
	c := newChange(r, []string{"z/z.go"}, allFiles, nil)
	ut.AssertEqual(t, r, c.Repo())
	ut.AssertEqual(t, "", c.Package())
	changed := c.Changed()
	ut.AssertEqual(t, []string{"z/z.go"}, changed.GoFiles())
	ut.AssertEqual(t, []string{"./z"}, changed.Packages())
	ut.AssertEqual(t, []string{"./z"}, changed.TestPackages())
	indirect := c.Indirect()
	ut.AssertEqual(t, []string{"z/z.go"}, indirect.GoFiles())
	ut.AssertEqual(t, []string{"./y", "./z"}, indirect.Packages())
	ut.AssertEqual(t, []string{".", "./x", "./z"}, indirect.TestPackages())
	all := c.All()
	ut.AssertEqual(t, allFiles, all.GoFiles())
	ut.AssertEqual(t, []string{".", "./w", "./x", "./y", "./z"}, all.Packages())
	ut.AssertEqual(t, []string{".", "./x", "./z"}, all.TestPackages())
}
Exemple #24
0
func TestCoverageEmpty(t *testing.T) {
	t.Parallel()
	ut.AssertEqual(t, 0., CoverageProfile{}.CoveragePercent())
	c := Coverage{PerDir: map[string]*CoverageSettings{"foo": nil}}
	ut.AssertEqual(t, &CoverageSettings{}, c.SettingsForPkg("foo"))
}
Exemple #25
0
func TestGetRepoGitSlowSuccess(t *testing.T) {
	// Make a repository and test behavior against it.
	t.Parallel()
	if isDrone() {
		t.Skipf("Give up on drone, it uses a weird go template which makes it not standard when using git init")
	}
	tmpDir, err := ioutil.TempDir("", "pre-commit-go")
	defer func() {
		if err := internal.RemoveAll(tmpDir); err != nil {
			t.Errorf("%s", err)
		}
	}()

	setup(t, tmpDir)
	r, err := getRepo(tmpDir, tmpDir)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, tmpDir, r.Root())
	p, err := r.HookPath()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, filepath.Join(tmpDir, ".git", "hooks"), p)
	ut.AssertEqual(t, Commit(gitInitial), r.Eval(string(Head)))
	ut.AssertEqual(t, nil, err)
	err = r.Checkout(string(Initial))
	ut.AssertEqual(t, errors.New("checkout failed:\nfatal: Cannot switch branch to a non-commit '4b825dc642cb6eb9a060e54bf8d69288fbee4904'"), err)

	ut.AssertEqual(t, []string{}, r.untracked())
	ut.AssertEqual(t, []string{}, r.unstaged())

	write(t, tmpDir, "src/foo/file1.go", "package foo\n")
	check(t, r, []string{"src/foo/file1.go"}, []string{})

	run(t, tmpDir, nil, "add", "src/foo/file1.go")
	check(t, r, []string{}, []string{})

	done, err := r.Stash()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, false, done)

	write(t, tmpDir, "src/foo/file1.go", "package foo\n// hello\n")
	check(t, r, []string{}, []string{"src/foo/file1.go"})

	done, err = r.Stash()
	ut.AssertEqual(t, errors.New("Can't stash until there's at least one commit"), err)
	ut.AssertEqual(t, false, done)

	deterministicCommit(t, tmpDir)
	ut.AssertEqual(t, "master", r.Ref(Head))
	ut.AssertEqual(t, "package foo\n// hello\n", read(t, tmpDir, "src/foo/file1.go"))
	commitInitial := assertHEAD(t, r, "f4edb8ac30289340040451b6f8c20d17614a9ae7")
	ut.AssertEqual(t, "master", r.Ref(Head))

	done, err = r.Stash()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, true, done)
	ut.AssertEqual(t, "package foo\n", read(t, tmpDir, "src/foo/file1.go"))
	ut.AssertEqual(t, nil, r.Restore())
	ut.AssertEqual(t, "package foo\n// hello\n", read(t, tmpDir, "src/foo/file1.go"))

	msg := "checkout failed:\nerror: pathspec 'invalid' did not match any file(s) known to git."
	ut.AssertEqual(t, errors.New(msg), r.Checkout("invalid"))
	ut.AssertEqual(t, "package foo\n// hello\n", read(t, tmpDir, "src/foo/file1.go"))
	ut.AssertEqual(t, "master", r.Ref(Head))
	ut.AssertEqual(t, nil, r.Checkout(string(commitInitial)))
	ut.AssertEqual(t, "package foo\n", read(t, tmpDir, "src/foo/file1.go"))
	ut.AssertEqual(t, commitInitial, r.Eval(string(Head)))
	ut.AssertEqual(t, nil, r.Checkout("master"))
	ut.AssertEqual(t, "package foo\n", read(t, tmpDir, "src/foo/file1.go"))
	ut.AssertEqual(t, "master", r.Ref(Head))
	ut.AssertEqual(t, commitInitial, r.Eval(string(Head)))

	ut.AssertEqual(t, Invalid, r.Eval(string(Upstream)))
	ut.AssertEqual(t, Invalid, r.Eval("HEAD~1000"))

	c, err := r.Between(commitInitial, Initial, nil)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string{"src/foo/file1.go"}, c.Changed().GoFiles())
	ut.AssertEqual(t, []string{"src/foo/file1.go"}, c.Indirect().GoFiles())
	ut.AssertEqual(t, []string{"src/foo/file1.go"}, c.All().GoFiles())

	c, err = r.Between(Current, Initial, nil)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string{"src/foo/file1.go"}, c.Changed().GoFiles())

	c, err = r.Between(Current, Initial, []string{"f*"})
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, nil, c)

	c, err = r.Between(Current, commitInitial, nil)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, nil, c)

	c, err = r.Between(commitInitial, Current, nil)
	ut.AssertEqual(t, errors.New("can't use Current as old commit"), err)
	ut.AssertEqual(t, nil, c)

	c, err = r.Between(commitInitial, Commit("foo"), nil)
	ut.AssertEqual(t, errors.New("invalid old commit"), err)
	ut.AssertEqual(t, nil, c)

	// Add a file then remove it. Make sure the file doesn't show up.
	check(t, r, []string{}, []string{})
	write(t, tmpDir, "src/foo/deleted/deleted.go", "package deleted\n")
	ut.AssertEqual(t, []string{}, r.staged())
	run(t, tmpDir, nil, "add", "src/foo/deleted/deleted.go")
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go"}, r.staged())
	deterministicCommit(t, tmpDir)
	commitWithDeleted := assertHEAD(t, r, "c9b5f312ec8eefb58beeaf8c3684bb832fdefef7")
	c, err = r.Between(commitWithDeleted, Initial, nil)
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go", "src/foo/file1.go"}, c.Changed().GoFiles())
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go", "src/foo/file1.go"}, c.Indirect().GoFiles())
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go", "src/foo/file1.go"}, c.All().GoFiles())
	c, err = r.Between(commitWithDeleted, commitInitial, nil)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go"}, c.Changed().GoFiles())
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go"}, c.Indirect().GoFiles())
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go", "src/foo/file1.go"}, c.All().GoFiles())
	ut.AssertEqual(t, nil, err)

	// Do the delete.
	run(t, tmpDir, nil, "rm", "src/foo/deleted/deleted.go")
	ut.AssertEqual(t, []string{}, r.staged())
	deterministicCommit(t, tmpDir)
	commitAfterDelete := assertHEAD(t, r, "8aacb7c27c4d012c56bd861d2a8bc4da8ea7ee73")
	c, err = r.Between(commitAfterDelete, Initial, nil)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string{"src/foo/file1.go"}, c.Changed().GoFiles())
	ut.AssertEqual(t, []string{"src/foo/file1.go"}, c.Indirect().GoFiles())
	ut.AssertEqual(t, []string{"src/foo/file1.go"}, c.All().GoFiles())
	c, err = r.Between(commitAfterDelete, commitWithDeleted, nil)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, nil, c)
	c, err = r.Between(commitWithDeleted, Initial, nil)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go", "src/foo/file1.go"}, c.Changed().GoFiles())
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go", "src/foo/file1.go"}, c.Indirect().GoFiles())
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go", "src/foo/file1.go"}, c.All().GoFiles())
	c, err = r.Between(commitWithDeleted, commitInitial, nil)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go"}, c.Changed().GoFiles())
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go"}, c.Indirect().GoFiles())
	ut.AssertEqual(t, []string{"src/foo/deleted/deleted.go", "src/foo/file1.go"}, c.All().GoFiles())
	c, err = r.Between(Current, commitInitial, nil)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, nil, c)
	c, err = r.Between(Current, commitAfterDelete, nil)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, nil, c)
	c, err = r.Between(Current, commitWithDeleted, nil)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, nil, c)
}
Exemple #26
0
func read(t *testing.T, tmpDir, name string) string {
	content, err := ioutil.ReadFile(filepath.Join(tmpDir, name))
	ut.AssertEqual(t, nil, err)
	return string(content)
}
Exemple #27
0
func write(t *testing.T, tmpDir, name string, content string) {
	if d := filepath.Dir(name); d != "" {
		ut.AssertEqual(t, nil, os.MkdirAll(filepath.Join(tmpDir, d), 0700))
	}
	ut.AssertEqual(t, nil, ioutil.WriteFile(filepath.Join(tmpDir, name), []byte(content), 0600))
}
Exemple #28
0
func TestChangeEmpty(t *testing.T) {
	t.Parallel()
	r := &dummyRepo{t, "<root>"}
	files := []string{}
	allFiles := []string{}
	c := newChange(r, files, allFiles, nil)
	ut.AssertEqual(t, r, c.Repo())
	ut.AssertEqual(t, "", c.Package())
	changed := c.Changed()
	ut.AssertEqual(t, []string(nil), changed.GoFiles())
	ut.AssertEqual(t, []string(nil), changed.Packages())
	ut.AssertEqual(t, []string(nil), changed.TestPackages())
	indirect := c.Indirect()
	ut.AssertEqual(t, []string(nil), indirect.GoFiles())
	ut.AssertEqual(t, []string(nil), indirect.Packages())
	ut.AssertEqual(t, []string(nil), indirect.TestPackages())
	all := c.All()
	ut.AssertEqual(t, []string(nil), all.GoFiles())
	ut.AssertEqual(t, []string(nil), all.Packages())
	ut.AssertEqual(t, []string(nil), all.TestPackages())
}
Exemple #29
0
func TestChangeIndirect(t *testing.T) {
	// main_test.go is indirectly affect by a/A.go.
	t.Parallel()
	if testing.Short() {
		t.SkipNow()
	}
	root, allFiles, cleanup := makeTree(t,
		map[string]string{
			// Is changed.
			"a/a.go": "package a\nfunc Bar() int { return 1}",
			// Directly affected.
			"a/a_test.go": "package a",
			// Indirectly affected.
			"b/b.go": "package b\nimport \"a\"\nfunc Bar() int { return a.Bar() }",
			// Not affected.
			"c/c.go": "package c\nfunc Foo() { return 42 }",
			// Indirectly affected by "b".
			"c/c_test.go": "package c\nimport (\n\"b\"\n\"testing\"\n)\nfunc TestFoo(t *testing.T) int { if b.Bar() != 1 { t.FailNow() } }",
			// Not affected even if it imports "c".
			"d/d.go": "package d\nimport \"c\"\nfunc Foo() { return c.Foo() }",
			// Indirectly affected by "b".
			"main_test.go": "package main\nimport \"b\"\nfunc main() { println(b.Bar()) }",
		})
	defer cleanup()
	r := &dummyRepo{t, root}
	c := newChange(r, []string{"a/a.go"}, allFiles, nil)
	ut.AssertEqual(t, r, c.Repo())
	ut.AssertEqual(t, "", c.Package())
	changed := c.Changed()
	ut.AssertEqual(t, []string{"a/a.go"}, changed.GoFiles())
	ut.AssertEqual(t, []string{"./a"}, changed.Packages())
	ut.AssertEqual(t, []string{"./a"}, changed.TestPackages())
	indirect := c.Indirect()
	ut.AssertEqual(t, []string{"a/a.go"}, indirect.GoFiles())
	ut.AssertEqual(t, []string{"./a", "./b"}, indirect.Packages())
	ut.AssertEqual(t, []string{".", "./a", "./c"}, indirect.TestPackages())
	all := c.All()
	ut.AssertEqual(t, allFiles, all.GoFiles())
	ut.AssertEqual(t, []string{".", "./a", "./b", "./c", "./d"}, all.Packages())
	ut.AssertEqual(t, []string{".", "./a", "./c"}, all.TestPackages())
}
Exemple #30
0
func check(t *testing.T, r repo, untracked []string, unstaged []string) {
	ut.AssertEqual(t, untracked, r.untracked())
	ut.AssertEqual(t, unstaged, r.unstaged())
}