mirror of
https://github.com/grdl/git-get.git
synced 2026-02-16 06:51:11 +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-")
|
dir, err := ioutil.TempDir("", "test-repo-")
|
||||||
if err != nil {
|
checkFatal(t, errors.Wrap(err, "Failed creating test repo directory"))
|
||||||
return nil, errors.Wrap(err, "failed creating a temp repo")
|
|
||||||
}
|
|
||||||
|
|
||||||
repo, err := git.InitRepository(dir, false)
|
repo, err := git.InitRepository(dir, false)
|
||||||
if err != nil {
|
checkFatal(t, errors.Wrap(err, "Failed initializing a temp repo"))
|
||||||
return nil, 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)
|
err := ioutil.WriteFile(path.Join(repo.Workdir(), name), []byte("I'm a file"), 0644)
|
||||||
if err != nil {
|
checkFatal(t, errors.Wrap(err, "Failed writing a file"))
|
||||||
return errors.Wrap(err, "failed writing a file")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func stageFile(repo *git.Repository, name string) error {
|
func stageFile(t *testing.T, repo *git.Repository, name string) {
|
||||||
index, err := repo.Index()
|
index, err := repo.Index()
|
||||||
if err != nil {
|
checkFatal(t, errors.Wrap(err, "Failed getting repo index"))
|
||||||
return errors.Wrap(err, "failed getting repo index")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = index.AddByPath(name)
|
err = index.AddByPath(name)
|
||||||
if err != nil {
|
checkFatal(t, errors.Wrap(err, "Failed adding file to index"))
|
||||||
return errors.Wrap(err, "failed adding file to index")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = index.Write()
|
err = index.Write()
|
||||||
if err != nil {
|
checkFatal(t, errors.Wrap(err, "Failed writing index"))
|
||||||
return errors.Wrap(err, "failed writing index")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createCommit(repo *git.Repository, message string) error {
|
func createCommit(t *testing.T, repo *git.Repository, message string) {
|
||||||
index, err := repo.Index()
|
index, err := repo.Index()
|
||||||
if err != nil {
|
checkFatal(t, errors.Wrap(err, "Failed getting repo index"))
|
||||||
return errors.Wrap(err, "failed creating a temp repo")
|
|
||||||
}
|
|
||||||
|
|
||||||
treeId, err := index.WriteTree()
|
treeId, err := index.WriteTree()
|
||||||
if err != nil {
|
checkFatal(t, errors.Wrap(err, "Failed building tree from index"))
|
||||||
return errors.Wrap(err, "failed building tree from index")
|
|
||||||
}
|
|
||||||
|
|
||||||
tree, err := repo.LookupTree(treeId)
|
tree, err := repo.LookupTree(treeId)
|
||||||
if err != nil {
|
checkFatal(t, errors.Wrap(err, "Failed looking up tree id"))
|
||||||
return errors.Wrap(err, "failed looking up tree")
|
|
||||||
}
|
|
||||||
|
|
||||||
signature := &git.Signature{
|
signature := &git.Signature{
|
||||||
Name: "Some Guy",
|
Name: "Some Guy",
|
||||||
@@ -90,27 +68,19 @@ func createCommit(repo *git.Repository, message string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
empty, err := repo.IsEmpty()
|
empty, err := repo.IsEmpty()
|
||||||
if err != nil {
|
checkFatal(t, errors.Wrap(err, "Failed checking if repo is empty"))
|
||||||
return errors.Wrap(err, "failed cheching if repo is empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !empty {
|
if !empty {
|
||||||
currentBranch, err := repo.Head()
|
currentBranch, err := repo.Head()
|
||||||
if err != nil {
|
checkFatal(t, errors.Wrap(err, "Failed getting current branch"))
|
||||||
return errors.Wrap(err, "failed getting current branch")
|
|
||||||
}
|
|
||||||
|
|
||||||
currentTip, err := repo.LookupCommit(currentBranch.Target())
|
currentTip, err := repo.LookupCommit(currentBranch.Target())
|
||||||
if err != nil {
|
checkFatal(t, errors.Wrap(err, "Failed getting current tip"))
|
||||||
return errors.Wrap(err, "failed getting current tip")
|
|
||||||
}
|
|
||||||
_, err = repo.CreateCommit("HEAD", signature, signature, message, tree, currentTip)
|
_, err = repo.CreateCommit("HEAD", signature, signature, message, tree, currentTip)
|
||||||
} else {
|
} else {
|
||||||
_, err = repo.CreateCommit("HEAD", signature, signature, message, tree)
|
_, 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) {
|
func TestStatusWithEmptyRepo(t *testing.T) {
|
||||||
repo, err := newTestRepo()
|
repo := newTestRepo(t)
|
||||||
checkFatal(t, err)
|
|
||||||
defer cleanupRepo(t, repo)
|
defer cleanupRepo(t, repo)
|
||||||
|
|
||||||
entries, err := statusEntries(repo)
|
entries, err := statusEntries(repo)
|
||||||
@@ -26,19 +25,17 @@ func TestStatusWithEmptyRepo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStatusWithSingleUnstagedFile(t *testing.T) {
|
func TestStatusWithUntrackedFile(t *testing.T) {
|
||||||
repo, err := newTestRepo()
|
repo := newTestRepo(t)
|
||||||
checkFatal(t, err)
|
|
||||||
defer cleanupRepo(t, repo)
|
defer cleanupRepo(t, repo)
|
||||||
|
|
||||||
err = createFile(repo, "SomeFile")
|
createFile(t, repo, "SomeFile")
|
||||||
checkFatal(t, err)
|
|
||||||
|
|
||||||
entries, err := statusEntries(repo)
|
entries, err := statusEntries(repo)
|
||||||
checkFatal(t, err)
|
checkFatal(t, err)
|
||||||
|
|
||||||
if len(entries) != 1 {
|
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 {
|
if entries[0].Status != git.StatusWtNew {
|
||||||
@@ -49,25 +46,30 @@ func TestStatusWithSingleUnstagedFile(t *testing.T) {
|
|||||||
checkFatal(t, err)
|
checkFatal(t, err)
|
||||||
|
|
||||||
if len(statuses) != 1 && statuses[0] != StatusUntrackedFiles {
|
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) {
|
func TestStatusWithUnstagedFile(t *testing.T) {
|
||||||
repo, err := newTestRepo()
|
//todo
|
||||||
checkFatal(t, err)
|
}
|
||||||
|
|
||||||
|
func TestStatusWithUntrackedButIgnoredFile(t *testing.T) {
|
||||||
|
//todo
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStatusWithStagedFile(t *testing.T) {
|
||||||
|
repo := newTestRepo(t)
|
||||||
defer cleanupRepo(t, repo)
|
defer cleanupRepo(t, repo)
|
||||||
|
|
||||||
err = createFile(repo, "SomeFile")
|
createFile(t, repo, "SomeFile")
|
||||||
checkFatal(t, err)
|
stageFile(t, repo, "SomeFile")
|
||||||
err = stageFile(repo, "SomeFile")
|
|
||||||
checkFatal(t, err)
|
|
||||||
|
|
||||||
entries, err := statusEntries(repo)
|
entries, err := statusEntries(repo)
|
||||||
checkFatal(t, err)
|
checkFatal(t, err)
|
||||||
|
|
||||||
if len(entries) != 1 {
|
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 {
|
if entries[0].Status != git.StatusIndexNew {
|
||||||
@@ -78,21 +80,17 @@ func TestStatusWithSingleStagedFile(t *testing.T) {
|
|||||||
checkFatal(t, err)
|
checkFatal(t, err)
|
||||||
|
|
||||||
if len(statuses) != 1 && statuses[0] != StatusUncommittedChanges {
|
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) {
|
func TestStatusWithSingleCommit(t *testing.T) {
|
||||||
repo, err := newTestRepo()
|
repo := newTestRepo(t)
|
||||||
checkFatal(t, err)
|
|
||||||
defer cleanupRepo(t, repo)
|
defer cleanupRepo(t, repo)
|
||||||
|
|
||||||
err = createFile(repo, "SomeFile")
|
createFile(t, repo, "SomeFile")
|
||||||
checkFatal(t, err)
|
stageFile(t, repo, "SomeFile")
|
||||||
err = stageFile(repo, "SomeFile")
|
createCommit(t, repo, "Initial commit")
|
||||||
checkFatal(t, err)
|
|
||||||
err = createCommit(repo, "Initial commit")
|
|
||||||
checkFatal(t, err)
|
|
||||||
|
|
||||||
entries, err := statusEntries(repo)
|
entries, err := statusEntries(repo)
|
||||||
checkFatal(t, err)
|
checkFatal(t, err)
|
||||||
@@ -110,23 +108,15 @@ func TestStatusWithSingleCommit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStatusWithMultipleCommits(t *testing.T) {
|
func TestStatusWithMultipleCommits(t *testing.T) {
|
||||||
repo, err := newTestRepo()
|
repo := newTestRepo(t)
|
||||||
checkFatal(t, err)
|
|
||||||
defer cleanupRepo(t, repo)
|
defer cleanupRepo(t, repo)
|
||||||
|
|
||||||
err = createFile(repo, "SomeFile")
|
createFile(t, repo, "SomeFile")
|
||||||
checkFatal(t, err)
|
stageFile(t, repo, "SomeFile")
|
||||||
err = stageFile(repo, "SomeFile")
|
createCommit(t, repo, "Initial commit")
|
||||||
checkFatal(t, err)
|
createFile(t, repo, "AnotherFile")
|
||||||
err = createCommit(repo, "Initial commit")
|
stageFile(t, repo, "AnotherFile")
|
||||||
checkFatal(t, err)
|
createCommit(t, repo, "Second commit")
|
||||||
|
|
||||||
err = createFile(repo, "AnotherFile")
|
|
||||||
checkFatal(t, err)
|
|
||||||
err = stageFile(repo, "AnotherFile")
|
|
||||||
checkFatal(t, err)
|
|
||||||
err = createCommit(repo, "Second commit")
|
|
||||||
checkFatal(t, err)
|
|
||||||
|
|
||||||
entries, err := statusEntries(repo)
|
entries, err := statusEntries(repo)
|
||||||
checkFatal(t, err)
|
checkFatal(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user