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 #130 from marcelometal/refactoring_create_repository
Browse files Browse the repository at this point in the history
Refactoring CreateTestRepository
  • Loading branch information
andrewsmedina committed Jul 25, 2014
2 parents f9857b6 + 26e84e7 commit d8c4ae6
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 37 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,80 +47,166 @@ 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 cleanup, err
return err
}
err = os.MkdirAll(testPath, 0777)
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 = AddAll(testPath)
if err != nil {
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 = CreateOrUpdateConfig(testPath, "user.email", "much@email.com")
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 = ioutil.WriteFile(path.Join(testPath, file), []byte(content), 0644)
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", "much@email.com")
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
}

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

0 comments on commit d8c4ae6

Please sign in to comment.