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

Commit

Permalink
Merge pull request #114 from guilhermef/master
Browse files Browse the repository at this point in the history
More getTree tests
  • Loading branch information
andrewsmedina committed Jul 3, 2014
2 parents 00779d9 + b38aece commit 7df38e1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
15 changes: 13 additions & 2 deletions repository/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (r *MockContentRetriever) GetArchive(repo, ref string, format ArchiveFormat
return r.ResultContents, nil
}

func CreateTestRepository(tmp_path string, repo string, file string, content string) (func(), error) {
func CreateTestRepository(tmp_path string, repo string, file string, content string, folders ...string) (func(), error) {
testPath := path.Join(tmp_path, repo+".git")
cleanup := func() {
os.RemoveAll(testPath)
Expand All @@ -71,7 +71,18 @@ func CreateTestRepository(tmp_path string, repo string, file string, content str
if err != nil {
return cleanup, err
}
cmd = exec.Command(gitPath, "add", file)
for _, folder := range folders {
folderPath := path.Join(testPath, folder)
err = os.MkdirAll(folderPath, 0777)
if err != nil {
return cleanup, err
}
err = ioutil.WriteFile(path.Join(folderPath, file), []byte(content), 0644)
if err != nil {
return cleanup, err
}
}
cmd = exec.Command(gitPath, "add", ".")
cmd.Dir = testPath
err = cmd.Run()
if err != nil {
Expand Down
58 changes: 56 additions & 2 deletions repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ func (s *S) TestGetFileContentWhenRefIsInvalid(c *gocheck.C) {
}()
c.Assert(errCreate, gocheck.IsNil)
_, err := GetFileContents(repo, "MuchMissing", file)
c.Assert(err.Error(), gocheck.Equals, "Error when trying to obtain file README on ref MuchMissing of repository gandalf-test-repo (exit status 128).")
c.Assert(err, gocheck.ErrorMatches, "^Error when trying to obtain file README on ref MuchMissing of repository gandalf-test-repo \\(exit status 128\\)\\.$")
}

func (s *S) TestGetFileContentWhenFileIsInvalid(c *gocheck.C) {
Expand All @@ -562,5 +562,59 @@ func (s *S) TestGetFileContentWhenFileIsInvalid(c *gocheck.C) {
}()
c.Assert(errCreate, gocheck.IsNil)
_, err := GetFileContents(repo, "master", "Such file")
c.Assert(err.Error(), gocheck.Equals, "Error when trying to obtain file Such file on ref master of repository gandalf-test-repo (exit status 128).")
c.Assert(err, gocheck.ErrorMatches, "^Error when trying to obtain file Such file on ref master of repository gandalf-test-repo \\(exit status 128\\)\\.$")
}

func (s *S) TestGetTreeIntegration(c *gocheck.C) {
oldBare := bare
bare = "/tmp"
repo := "gandalf-test-repo"
file := "README"
content := "much WOW"
cleanUp, errCreate := CreateTestRepository(bare, repo, file, content, []string{"such", "folder", "much", "magic"}...)
defer func() {
cleanUp()
bare = oldBare
}()
c.Assert(errCreate, gocheck.IsNil)
tree, err := GetTree(repo, "master", "much/README")
c.Assert(err, gocheck.IsNil)
c.Assert(tree[0]["path"], gocheck.Equals, "much/README")
c.Assert(tree[0]["rawPath"], gocheck.Equals, "much/README")
}

func (s *S) TestGetTreeIntegrationWithEscapedFileName(c *gocheck.C) {
oldBare := bare
bare = "/tmp"
repo := "gandalf-test-repo"
file := "such\tREADME"
content := "much WOW"
cleanUp, errCreate := CreateTestRepository(bare, repo, file, content, []string{"such", "folder", "much", "magic"}...)
defer func() {
cleanUp()
bare = oldBare
}()
c.Assert(errCreate, gocheck.IsNil)
tree, err := GetTree(repo, "master", "much/such\tREADME")
c.Assert(err, gocheck.IsNil)
c.Assert(tree[0]["path"], gocheck.Equals, "much/such\\tREADME")
c.Assert(tree[0]["rawPath"], gocheck.Equals, "\"much/such\\tREADME\"")
}

func (s *S) TestGetTreeIntegrationWithFileNameWithSpace(c *gocheck.C) {
oldBare := bare
bare = "/tmp"
repo := "gandalf-test-repo"
file := "much README"
content := "much WOW"
cleanUp, errCreate := CreateTestRepository(bare, repo, file, content, []string{"such", "folder", "much", "magic"}...)
defer func() {
cleanUp()
bare = oldBare
}()
c.Assert(errCreate, gocheck.IsNil)
tree, err := GetTree(repo, "master", "much/much README")
c.Assert(err, gocheck.IsNil)
c.Assert(tree[0]["path"], gocheck.Equals, "much/much README")
c.Assert(tree[0]["rawPath"], gocheck.Equals, "much/much README")
}

0 comments on commit 7df38e1

Please sign in to comment.