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

Commit

Permalink
Merge branch 'master' of https://github.com/tsuru/gandalf
Browse files Browse the repository at this point in the history
Conflicts:
	repository/mocks.go
  • Loading branch information
joaopaulovieira committed Jul 25, 2014
2 parents 18a0b71 + d8c4ae6 commit 1b970b0
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 38 deletions.
160 changes: 124 additions & 36 deletions repository/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
package repository

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"time"
)

type MockContentRetriever struct {
Expand Down Expand Up @@ -45,61 +47,145 @@ func (r *MockContentRetriever) GetArchive(repo, ref string, format ArchiveFormat
return r.ResultContents, nil
}

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)
func CreateEmptyFile(tmpPath, repo, file string) error {
testPath := path.Join(tmpPath, repo+".git")
if file == "" {
file = fmt.Sprintf("README_%d", time.Now().UnixNano())
}
content := ""
return ioutil.WriteFile(path.Join(testPath, file), []byte(content), 0644)
}

func CreateFolder(tmpPath, repo, folder string) (string, error) {
testPath := path.Join(tmpPath, repo+".git")
folderPath := path.Join(testPath, folder)
err := os.MkdirAll(folderPath, 0777)
return folderPath, err
}

func CreateFile(testPath, file, content string) error {
now := time.Now().UnixNano()
if file == "" {
file = fmt.Sprintf("README_%d", now)
}
if content == "" {
content = fmt.Sprintf("much WOW %d", now)
}
return ioutil.WriteFile(path.Join(testPath, file), []byte(content), 0644)
}

func AddAll(testPath string) error {
gitPath, err := exec.LookPath("git")
if err != nil {
return err
}
cmd := exec.Command(gitPath, "add", "--all", ".")
cmd.Dir = testPath
return cmd.Run()
}

func MakeCommit(testPath, content string) error {
gitPath, err := exec.LookPath("git")
if err != nil {
return cleanup, err
return err
}
err = os.MkdirAll(testPath, 0777)
err = AddAll(testPath)
if err != nil {
return cleanup, err
return err
}
cmd := exec.Command(gitPath, "commit", "-m", content, "--allow-empty-message")
cmd.Dir = testPath
return cmd.Run()
}

func CreateCommit(tmpPath, repo, file, content string) error {
testPath := path.Join(tmpPath, repo+".git")
err := CreateFile(testPath, file, content)
if err != nil {
return err
}
return MakeCommit(testPath, content)
}

func InitRepository(testPath string) error {
gitPath, err := exec.LookPath("git")
if err != nil {
return err
}
cmd := exec.Command(gitPath, "init")
cmd.Dir = testPath
err = cmd.Run()
if err != nil {
return cleanup, err
return err
}
err = ioutil.WriteFile(path.Join(testPath, file), []byte(content), 0644)
err = CreateOrUpdateConfig(testPath, "user.email", "[email protected]")
if err != nil {
return err
}
return CreateOrUpdateConfig(testPath, "user.name", "doge")
}

func CreateEmptyTestRepository(tmpPath, repo string) (func(), error) {
testPath := path.Join(tmpPath, repo+".git")
cleanup := func() {
os.RemoveAll(testPath)
}
err := os.MkdirAll(testPath, 0777)
if err != nil {
return cleanup, err
}
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
}
err = InitRepository(testPath)
return cleanup, err
}

func CheckoutInNewBranch(testPath, branch string) error {
gitPath, err := exec.LookPath("git")
if err != nil {
return err
}
cmd = exec.Command(gitPath, "add", ".")
cmd := exec.Command(gitPath, "checkout", "-b", branch)
cmd.Dir = testPath
err = cmd.Run()
return cmd.Run()
}

func CreateOrUpdateConfig(testPath, param, value string) error {
gitPath, err := exec.LookPath("git")
if err != nil {
return cleanup, err
return err
}
cmd = exec.Command(gitPath, "config", "user.email", "[email protected]")
cmd := exec.Command(gitPath, "config", param, value)
cmd.Dir = testPath
err = cmd.Run()
return cmd.Run()
}

func CreateTestRepository(tmpPath, repo, file, content string, folders ...string) (func(), error) {
testPath := path.Join(tmpPath, repo+".git")
cleanup := func() {
os.RemoveAll(testPath)
}
err := os.MkdirAll(testPath, 0777)
if err != nil {
return cleanup, err
}
cmd = exec.Command(gitPath, "config", "user.name", "doge")
cmd.Dir = testPath
err = cmd.Run()
err = InitRepository(testPath)
if err != nil {
return cleanup, err
}
cmd = exec.Command(gitPath, "commit", "-m", content, "--allow-empty-message")
cmd.Dir = testPath
err = cmd.Run()
err = CreateFile(testPath, file, content)
if err != nil {
return cleanup, err
}
for _, folder := range folders {
folderPath, err := CreateFolder(tmpPath, repo, folder)
if err != nil {
return cleanup, err
}
err = CreateFile(folderPath, file, content)
if err != nil {
return cleanup, err
}
}
err = MakeCommit(testPath, content)
return cleanup, err
}

Expand Down Expand Up @@ -134,22 +220,24 @@ func CreateCommitOnTestRepository(tmpPath, repo, file, content string) ([]byte,
return out, nil
}

func CreateBranchesOnTestRepository(tmp_path string, repo string, branches ...string) error {
testPath := path.Join(tmp_path, repo+".git")
func StatusRepository(testPath string) error {
gitPath, err := exec.LookPath("git")
if err != nil {
return err
}
cmd := exec.Command(gitPath, "status")
cmd.Dir = testPath
err = cmd.Run()
return cmd.Run()
}

func CreateBranchesOnTestRepository(tmpPath string, repo string, branches ...string) error {
testPath := path.Join(tmpPath, repo+".git")
err := StatusRepository(testPath)
if err != nil {
return err
}
for _, branch := range branches {
cmd = exec.Command(gitPath, "checkout", "-b", branch)
cmd.Dir = testPath
err = cmd.Run()
err = CheckoutInNewBranch(testPath, branch)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,17 @@ func (s *S) TestGetFileContentIntegrationEmptyContent(c *gocheck.C) {
repo := "gandalf-test-repo"
file := "README"
content := ""
cleanUp, errCreate := CreateTestRepository(bare, repo, file, content)
cleanUp, errCreate := CreateEmptyTestRepository(bare, repo)
defer func() {
cleanUp()
bare = oldBare
}()
c.Assert(errCreate, gocheck.IsNil)
err := CreateEmptyFile(bare, repo, file)
c.Assert(err, gocheck.IsNil)
testPath := path.Join(bare, repo+".git")
err = MakeCommit(testPath, "empty file content")
c.Assert(err, gocheck.IsNil)
contents, err := GetFileContents(repo, "master", file)
c.Assert(err, gocheck.IsNil)
c.Assert(string(contents), gocheck.Equals, content)
Expand Down
2 changes: 1 addition & 1 deletion webserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ For an example conf check gandalf/etc/gandalf.conf file.\n %s`
router.Get("/repository/:name/contents", http.HandlerFunc(api.GetFileContents))
router.Get("/repository/:name/tree/:path", http.HandlerFunc(api.GetTree))
router.Get("/repository/:name/tree", http.HandlerFunc(api.GetTree))
router.Get("/repository/:name/branch", http.HandlerFunc(api.GetTree))
router.Get("/repository/:name/branch", http.HandlerFunc(api.GetBranch))
router.Get("/healthcheck/", http.HandlerFunc(api.HealthCheck))
router.Post("/hook/:name", http.HandlerFunc(api.AddHook))

Expand Down

0 comments on commit 1b970b0

Please sign in to comment.