diff --git a/repository/mocks.go b/repository/mocks.go index f002ed7..730153a 100644 --- a/repository/mocks.go +++ b/repository/mocks.go @@ -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 { diff --git a/repository/repository_test.go b/repository/repository_test.go index c01fc68..55175ff 100644 --- a/repository/repository_test.go +++ b/repository/repository_test.go @@ -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!.*`) } @@ -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) { @@ -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)) }