Beispiel #1
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)
	}
}
Beispiel #2
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 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)
		}
	}
}
Beispiel #4
0
// RunProfile runs a coverage run according to the settings and return results.
func (c *Coverage) RunProfile(change scm.Change, options *Options) (profile CoverageProfile, err error) {
	// go test accepts packages, not files.
	var testPkgs []string
	if c.UseGlobalInference {
		testPkgs = change.All().TestPackages()
	} else {
		testPkgs = change.Indirect().TestPackages()
	}
	if len(testPkgs) == 0 {
		// Sir, there's no test.
		return nil, nil
	}

	tmpDir, err2 := ioutil.TempDir("", "pre-commit-go")
	if err2 != nil {
		return nil, err2
	}
	defer func() {
		err2 := internal.RemoveAll(tmpDir)
		if err == nil {
			err = err2
		}
	}()

	if c.UseGlobalInference {
		profile, err = c.RunGlobal(change, options, tmpDir)
	} else {
		profile, err = c.RunLocal(change, options, tmpDir)
	}
	if err != nil {
		return nil, err
	}

	if c.isGoverallsEnabled() {
		// Please send a pull request if the following doesn't work for you on your
		// favorite CI system.
		out, _, _, err2 := options.Capture(change.Repo(), "goveralls", "-coverprofile", filepath.Join(tmpDir, "profile.cov"))
		// Don't fail the build.
		if err2 != nil {
			fmt.Printf("%s", out)
		}
	}
	return profile, nil
}
Beispiel #5
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
}
Beispiel #6
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("."))
}
Beispiel #7
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)
}