github.com/azunymous/cdx@v0.0.0-20201122180449-fbb46cc4d252/test/e2e/gitsetup.go (about)

     1  //+build e2e
     2  
     3  package e2e
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  )
    10  
    11  func CreateTempGitDir() string {
    12  	fn, _ := ioutil.TempDir(os.TempDir(), "cdx-test-")
    13  	_ = os.Chdir(fn)
    14  	_, _ = exec.Command("git", "init").CombinedOutput()
    15  	_, _ = exec.Command("bash", "-c", "echo 'hello world' > file.txt").CombinedOutput()
    16  	_, _ = exec.Command("git", "add", "file.txt").CombinedOutput()
    17  	_, _ = exec.Command("git", "commit", "-m", `"Commit 1"`).CombinedOutput()
    18  	return fn
    19  }
    20  
    21  func CreateTag(dir, tag string) {
    22  	_ = os.Chdir(dir)
    23  	_, _ = exec.Command("git", "tag", tag).CombinedOutput()
    24  }
    25  
    26  func CreateCommit(dir, msg string) {
    27  	_ = os.Chdir(dir)
    28  	_, _ = exec.Command("bash", "-c", "echo '"+msg+"' > file.txt").CombinedOutput()
    29  	_, _ = exec.Command("git", "add", "file.txt").CombinedOutput()
    30  	_, _ = exec.Command("git", "commit", "-m", `"`+msg+`"`).CombinedOutput()
    31  }
    32  
    33  func CreateTempGitRemote(gitDir string) string {
    34  	remoteDir, _ := ioutil.TempDir(os.TempDir(), "cdx-remote-*.git")
    35  	_ = os.Chdir(remoteDir)
    36  	_, _ = exec.Command("git", "init", "--bare").CombinedOutput()
    37  
    38  	_ = os.Chdir(gitDir)
    39  	_, _ = exec.Command("git", "remote", "add", "origin", remoteDir).CombinedOutput()
    40  	_, _ = exec.Command("git", "push", "origin", "master").CombinedOutput()
    41  
    42  	return remoteDir
    43  }