From 26e84e7bf4a3ea5d800faf73d11aff99b1d6ebea Mon Sep 17 00:00:00 2001 From: Marcelo Jorge Vieira Date: Thu, 24 Jul 2014 15:45:00 -0300 Subject: [PATCH] Refactoring CreateTestRepository Now each git command is a function --- repository/mocks.go | 160 ++++++++++++++++++++++++++-------- repository/repository_test.go | 7 +- 2 files changed, 130 insertions(+), 37 deletions(-) diff --git a/repository/mocks.go b/repository/mocks.go index 1cc0852..3299058 100644 --- a/repository/mocks.go +++ b/repository/mocks.go @@ -5,10 +5,12 @@ package repository import ( + "fmt" "io/ioutil" "os" "os/exec" "path" + "time" ) type MockContentRetriever struct { @@ -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 } diff --git a/repository/repository_test.go b/repository/repository_test.go index 72ff84a..c4e5144 100644 --- a/repository/repository_test.go +++ b/repository/repository_test.go @@ -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)