mirror of
https://github.com/grdl/git-get.git
synced 2026-02-08 09:54:19 +00:00
Simplify error checking in test helper functions
Helper functions check for errors and call Fatal if necessary on their own. This way we don't have to check for errors again in actual tests.
This commit is contained in:
@@ -25,63 +25,41 @@ func cleanupRepo(t *testing.T, repo *git.Repository) {
|
||||
}
|
||||
}
|
||||
|
||||
func newTestRepo() (*git.Repository, error) {
|
||||
func newTestRepo(t *testing.T) *git.Repository {
|
||||
dir, err := ioutil.TempDir("", "test-repo-")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed creating a temp repo")
|
||||
}
|
||||
checkFatal(t, errors.Wrap(err, "Failed creating test repo directory"))
|
||||
|
||||
repo, err := git.InitRepository(dir, false)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed initializing a temp repo")
|
||||
}
|
||||
checkFatal(t, errors.Wrap(err, "Failed initializing a temp repo"))
|
||||
|
||||
return repo, nil
|
||||
return repo
|
||||
}
|
||||
|
||||
func createFile(repo *git.Repository, name string) error {
|
||||
func createFile(t *testing.T, repo *git.Repository, name string) {
|
||||
err := ioutil.WriteFile(path.Join(repo.Workdir(), name), []byte("I'm a file"), 0644)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed writing a file")
|
||||
}
|
||||
|
||||
return nil
|
||||
checkFatal(t, errors.Wrap(err, "Failed writing a file"))
|
||||
}
|
||||
|
||||
func stageFile(repo *git.Repository, name string) error {
|
||||
func stageFile(t *testing.T, repo *git.Repository, name string) {
|
||||
index, err := repo.Index()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed getting repo index")
|
||||
}
|
||||
checkFatal(t, errors.Wrap(err, "Failed getting repo index"))
|
||||
|
||||
err = index.AddByPath(name)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed adding file to index")
|
||||
}
|
||||
checkFatal(t, errors.Wrap(err, "Failed adding file to index"))
|
||||
|
||||
err = index.Write()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed writing index")
|
||||
}
|
||||
|
||||
return nil
|
||||
checkFatal(t, errors.Wrap(err, "Failed writing index"))
|
||||
}
|
||||
|
||||
func createCommit(repo *git.Repository, message string) error {
|
||||
func createCommit(t *testing.T, repo *git.Repository, message string) {
|
||||
index, err := repo.Index()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed creating a temp repo")
|
||||
}
|
||||
checkFatal(t, errors.Wrap(err, "Failed getting repo index"))
|
||||
|
||||
treeId, err := index.WriteTree()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed building tree from index")
|
||||
}
|
||||
checkFatal(t, errors.Wrap(err, "Failed building tree from index"))
|
||||
|
||||
tree, err := repo.LookupTree(treeId)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed looking up tree")
|
||||
}
|
||||
checkFatal(t, errors.Wrap(err, "Failed looking up tree id"))
|
||||
|
||||
signature := &git.Signature{
|
||||
Name: "Some Guy",
|
||||
@@ -90,27 +68,19 @@ func createCommit(repo *git.Repository, message string) error {
|
||||
}
|
||||
|
||||
empty, err := repo.IsEmpty()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed cheching if repo is empty")
|
||||
}
|
||||
checkFatal(t, errors.Wrap(err, "Failed checking if repo is empty"))
|
||||
|
||||
if !empty {
|
||||
currentBranch, err := repo.Head()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed getting current branch")
|
||||
}
|
||||
checkFatal(t, errors.Wrap(err, "Failed getting current branch"))
|
||||
|
||||
currentTip, err := repo.LookupCommit(currentBranch.Target())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed getting current tip")
|
||||
}
|
||||
checkFatal(t, errors.Wrap(err, "Failed getting current tip"))
|
||||
|
||||
_, err = repo.CreateCommit("HEAD", signature, signature, message, tree, currentTip)
|
||||
} else {
|
||||
_, err = repo.CreateCommit("HEAD", signature, signature, message, tree)
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed creating commit")
|
||||
}
|
||||
|
||||
return nil
|
||||
checkFatal(t, errors.Wrap(err, "Failed creating a commit"))
|
||||
}
|
||||
|
||||
@@ -7,8 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func TestStatusWithEmptyRepo(t *testing.T) {
|
||||
repo, err := newTestRepo()
|
||||
checkFatal(t, err)
|
||||
repo := newTestRepo(t)
|
||||
defer cleanupRepo(t, repo)
|
||||
|
||||
entries, err := statusEntries(repo)
|
||||
@@ -26,19 +25,17 @@ func TestStatusWithEmptyRepo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatusWithSingleUnstagedFile(t *testing.T) {
|
||||
repo, err := newTestRepo()
|
||||
checkFatal(t, err)
|
||||
func TestStatusWithUntrackedFile(t *testing.T) {
|
||||
repo := newTestRepo(t)
|
||||
defer cleanupRepo(t, repo)
|
||||
|
||||
err = createFile(repo, "SomeFile")
|
||||
checkFatal(t, err)
|
||||
createFile(t, repo, "SomeFile")
|
||||
|
||||
entries, err := statusEntries(repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
if len(entries) != 1 {
|
||||
t.Errorf("Repo with single unstaged file should have only one status entry")
|
||||
t.Errorf("Repo with untracked file should have only one status entry")
|
||||
}
|
||||
|
||||
if entries[0].Status != git.StatusWtNew {
|
||||
@@ -49,25 +46,30 @@ func TestStatusWithSingleUnstagedFile(t *testing.T) {
|
||||
checkFatal(t, err)
|
||||
|
||||
if len(statuses) != 1 && statuses[0] != StatusUntrackedFiles {
|
||||
t.Errorf("Empty repo should have a single StatusUntrackedFiles status")
|
||||
t.Errorf("Repo with untracked file should have a single StatusUntrackedFiles status")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatusWithSingleStagedFile(t *testing.T) {
|
||||
repo, err := newTestRepo()
|
||||
checkFatal(t, err)
|
||||
func TestStatusWithUnstagedFile(t *testing.T) {
|
||||
//todo
|
||||
}
|
||||
|
||||
func TestStatusWithUntrackedButIgnoredFile(t *testing.T) {
|
||||
//todo
|
||||
}
|
||||
|
||||
func TestStatusWithStagedFile(t *testing.T) {
|
||||
repo := newTestRepo(t)
|
||||
defer cleanupRepo(t, repo)
|
||||
|
||||
err = createFile(repo, "SomeFile")
|
||||
checkFatal(t, err)
|
||||
err = stageFile(repo, "SomeFile")
|
||||
checkFatal(t, err)
|
||||
createFile(t, repo, "SomeFile")
|
||||
stageFile(t, repo, "SomeFile")
|
||||
|
||||
entries, err := statusEntries(repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
if len(entries) != 1 {
|
||||
t.Errorf("Repo with single staged file should have only one status entry")
|
||||
t.Errorf("Repo with staged file should have only one status entry")
|
||||
}
|
||||
|
||||
if entries[0].Status != git.StatusIndexNew {
|
||||
@@ -78,21 +80,17 @@ func TestStatusWithSingleStagedFile(t *testing.T) {
|
||||
checkFatal(t, err)
|
||||
|
||||
if len(statuses) != 1 && statuses[0] != StatusUncommittedChanges {
|
||||
t.Errorf("Empty repo should have a single SStatusUncommittedChange status")
|
||||
t.Errorf("Repo with staged file should have a single StatusUncommittedChange status")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatusWithSingleCommit(t *testing.T) {
|
||||
repo, err := newTestRepo()
|
||||
checkFatal(t, err)
|
||||
repo := newTestRepo(t)
|
||||
defer cleanupRepo(t, repo)
|
||||
|
||||
err = createFile(repo, "SomeFile")
|
||||
checkFatal(t, err)
|
||||
err = stageFile(repo, "SomeFile")
|
||||
checkFatal(t, err)
|
||||
err = createCommit(repo, "Initial commit")
|
||||
checkFatal(t, err)
|
||||
createFile(t, repo, "SomeFile")
|
||||
stageFile(t, repo, "SomeFile")
|
||||
createCommit(t, repo, "Initial commit")
|
||||
|
||||
entries, err := statusEntries(repo)
|
||||
checkFatal(t, err)
|
||||
@@ -110,23 +108,15 @@ func TestStatusWithSingleCommit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStatusWithMultipleCommits(t *testing.T) {
|
||||
repo, err := newTestRepo()
|
||||
checkFatal(t, err)
|
||||
repo := newTestRepo(t)
|
||||
defer cleanupRepo(t, repo)
|
||||
|
||||
err = createFile(repo, "SomeFile")
|
||||
checkFatal(t, err)
|
||||
err = stageFile(repo, "SomeFile")
|
||||
checkFatal(t, err)
|
||||
err = createCommit(repo, "Initial commit")
|
||||
checkFatal(t, err)
|
||||
|
||||
err = createFile(repo, "AnotherFile")
|
||||
checkFatal(t, err)
|
||||
err = stageFile(repo, "AnotherFile")
|
||||
checkFatal(t, err)
|
||||
err = createCommit(repo, "Second commit")
|
||||
checkFatal(t, err)
|
||||
createFile(t, repo, "SomeFile")
|
||||
stageFile(t, repo, "SomeFile")
|
||||
createCommit(t, repo, "Initial commit")
|
||||
createFile(t, repo, "AnotherFile")
|
||||
stageFile(t, repo, "AnotherFile")
|
||||
createCommit(t, repo, "Second commit")
|
||||
|
||||
entries, err := statusEntries(repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
Reference in New Issue
Block a user