This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/tsuru/gandalf
Conflicts: repository/mocks.go
- Loading branch information
Showing
3 changed files
with
131 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,12 @@ | |
package repository | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"time" | ||
) | ||
|
||
type MockContentRetriever struct { | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters