Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
refactored CreateCommitOnTestRepository to GetLastHashCommit and unit…
Browse files Browse the repository at this point in the history
… test
  • Loading branch information
joaopaulovieira committed Jul 25, 2014
1 parent 1b970b0 commit 545b190
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
20 changes: 2 additions & 18 deletions repository/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,29 +189,13 @@ func CreateTestRepository(tmpPath, repo, file, content string, folders ...string
return cleanup, err
}

func CreateCommitOnTestRepository(tmpPath, repo, file, content string) ([]byte, error) {
func GetLastHashCommit(tmpPath, repo string) ([]byte, error) {
testPath := path.Join(tmpPath, repo+".git")
gitPath, err := exec.LookPath("git")
if err != nil {
return nil, err
}
err = ioutil.WriteFile(path.Join(testPath, file), []byte(content), 0644)
if err != nil {
return nil, err
}
cmd := exec.Command(gitPath, "add", ".")
cmd.Dir = testPath
err = cmd.Run()
if err != nil {
return nil, err
}
cmd = exec.Command(gitPath, "commit", "-m", content, "--allow-empty-message")
cmd.Dir = testPath
err = cmd.Run()
if err != nil {
return nil, err
}
cmd = exec.Command(gitPath, "log", "--pretty=format:%H", "-1")
cmd := exec.Command(gitPath, "log", "--pretty=format:%H", "-1")
cmd.Dir = testPath
out, err := cmd.Output()
if err !=nil {
Expand Down
30 changes: 20 additions & 10 deletions repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,11 +1006,15 @@ func (s *S) TestGetDiffIntegration(c *gocheck.C) {
bare = oldBare
}()
c.Assert(errCreate, gocheck.IsNil)
firstCommit, err := CreateCommitOnTestRepository(bare, repo, file, object1)
errCreateCommit := CreateCommit(bare, repo, file, object1)
c.Assert(errCreateCommit, gocheck.IsNil)
firstHashCommit, err := GetLastHashCommit(bare, repo)
c.Assert(err, gocheck.IsNil)
secondCommit, err := CreateCommitOnTestRepository(bare, repo, file, object2)
errCreateCommit = CreateCommit(bare, repo, file, object2)
c.Assert(errCreateCommit, gocheck.IsNil)
secondHashCommit, err := GetLastHashCommit(bare, repo)
c.Assert(err, gocheck.IsNil)
diff, err := GetDiff(repo, string(firstCommit), string(secondCommit))
diff, err := GetDiff(repo, string(firstHashCommit), string(secondHashCommit))
c.Assert(err, gocheck.IsNil)
c.Assert(string(diff), gocheck.Matches, `(?s).*-You should read this README.*\+Seriously, read this file!.*`)
}
Expand All @@ -1029,12 +1033,16 @@ func (s *S) TestGetDiffIntegrationWhenInvalidRepo(c *gocheck.C) {
bare = oldBare
}()
c.Assert(errCreate, gocheck.IsNil)
firstCommit, err := CreateCommitOnTestRepository(bare, repo, file, object1)
errCreateCommit := CreateCommit(bare, repo, file, object1)
c.Assert(errCreateCommit, gocheck.IsNil)
firstHashCommit, err := GetLastHashCommit(bare, repo)
c.Assert(err, gocheck.IsNil)
secondCommit, err := CreateCommitOnTestRepository(bare, repo, file, object2)
errCreateCommit = CreateCommit(bare, repo, file, object2)
c.Assert(errCreateCommit, gocheck.IsNil)
secondHashCommit, err := GetLastHashCommit(bare, repo)
c.Assert(err, gocheck.IsNil)
_, err = GetDiff("invalid-repo", string(firstCommit), string(secondCommit))
c.Assert(err.Error(), gocheck.Equals, fmt.Sprintf("Error when trying to obtain diff with commits %s and %s of repository invalid-repo (Repository does not exist).", secondCommit, firstCommit))
_, err = GetDiff("invalid-repo", string(firstHashCommit), string(secondHashCommit))
c.Assert(err.Error(), gocheck.Equals, fmt.Sprintf("Error when trying to obtain diff with commits %s and %s of repository invalid-repo (Repository does not exist).", secondHashCommit, firstHashCommit))
}

func (s *S) TestGetDiffIntegrationWhenInvalidCommit(c *gocheck.C) {
Expand All @@ -1050,8 +1058,10 @@ func (s *S) TestGetDiffIntegrationWhenInvalidCommit(c *gocheck.C) {
bare = oldBare
}()
c.Assert(errCreate, gocheck.IsNil)
firstCommit, err := CreateCommitOnTestRepository(bare, repo, file, object1)
errCreateCommit := CreateCommit(bare, repo, file, object1)
c.Assert(errCreateCommit, gocheck.IsNil)
firstHashCommit, err := GetLastHashCommit(bare, repo)
c.Assert(err, gocheck.IsNil)
_, err = GetDiff(repo, "12beu23eu23923ey32eiyeg2ye", string(firstCommit))
c.Assert(err.Error(), gocheck.Equals, fmt.Sprintf("Error when trying to obtain diff with commits %s and 12beu23eu23923ey32eiyeg2ye of repository %s (exit status 128).", firstCommit, repo))
_, err = GetDiff(repo, "12beu23eu23923ey32eiyeg2ye", string(firstHashCommit))
c.Assert(err.Error(), gocheck.Equals, fmt.Sprintf("Error when trying to obtain diff with commits %s and 12beu23eu23923ey32eiyeg2ye of repository %s (exit status 128).", firstHashCommit, repo))
}

0 comments on commit 545b190

Please sign in to comment.