Skip to content

Commit

Permalink
Merge pull request #25 from screwdriver-cd/git
Browse files Browse the repository at this point in the history
Added a basic git.Clone helper for checking out code
  • Loading branch information
stjohnjohnson authored Aug 4, 2016
2 parents d2ad958 + d2e8407 commit 47a4cce
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
24 changes: 24 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package git

import (
"fmt"
"os"
"os/exec"
)

var execCommand = exec.Command

// Clone clones a git repo into a destination directory
func Clone(repo, destination string) error {
cmd := execCommand("git", "clone", repo, destination)
cmd.Stdout = os.Stdout
err := cmd.Start()
if err != nil {
return fmt.Errorf("starting git clone command: %v", err)
}
err = cmd.Wait()
if err != nil {
return fmt.Errorf("cloning git repo: %v", err)
}
return nil
}
71 changes: 71 additions & 0 deletions git/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package git

import (
"os"
"os/exec"
"testing"
)

type execFunc func(command string, args ...string) *exec.Cmd

func getFakeExecCommand(validator func(string, ...string)) execFunc {
return func(command string, args ...string) *exec.Cmd {
validator(command, args...)
return fakeExecCommand(command, args...)
}
}

func fakeExecCommand(command string, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--", command}
cs = append(cs, args...)
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
return cmd
}

func TestCheckout(t *testing.T) {
wantRepo := "testrepo"
wantDest := "testdest"
execCommand = getFakeExecCommand(func(cmd string, args ...string) {
want := []string{
"clone", wantRepo, wantDest,
}
if len(args) != len(want) {
t.Errorf("Incorrect args sent to git: %q, want %q", args, want)
}
for i, arg := range args {
if arg != want[i] {
t.Errorf("args[%d] = %q, want %q", i, arg, want[i])
}
}
})

err := Clone(wantRepo, wantDest)
if err != nil {
t.Errorf("Unexpected error from git clone: %v", err)
}
}

func TestHelperProcess(*testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
defer os.Exit(0)

args := os.Args[:]
for i, val := range os.Args { // Should become something lke ["git", "clone"]
args = os.Args[i:]
if val == "--" {
args = args[1:]
break
}
}

if len(args) > 1 {
switch args[1] {
case "clone":
return
}
}
os.Exit(255)
}
8 changes: 7 additions & 1 deletion launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path"
"regexp"

"github.com/screwdriver-cd/launcher/git"
"github.com/screwdriver-cd/launcher/screwdriver"
"github.com/urfave/cli"
)
Expand All @@ -22,6 +23,7 @@ type scmPath struct {

var mkdirAll = os.MkdirAll
var stat = os.Stat
var gitClone = git.Clone

func (s scmPath) String() string {
return fmt.Sprintf("%s:%s/%s#%s", s.Host, s.Org, s.Repo, s.Branch)
Expand Down Expand Up @@ -103,11 +105,15 @@ func launch(api screwdriver.API, buildID string) error {
}

scm, err := parseScmURL(p.ScmURL)
_, err = createWorkspace(scm.Org, scm.Repo)
workspace, err := createWorkspace(scm.Org, scm.Repo)
if err != nil {
return err
}

err = gitClone(p.ScmURL, workspace.Src)
if err != nil {
return err
}
return nil
}

Expand Down
17 changes: 17 additions & 0 deletions launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,20 @@ func TestCreateWorkspace(t *testing.T) {
}
}
}

func TestClone(t *testing.T) {
testBuildID := "BUILDID"
testJobID := "JOBID"
testSCMURL := "[email protected]:screwdriver-cd/launcher#master"
api := mockAPI(t, testBuildID, testJobID, "")
api.pipelineFromID = func(pipelineID string) (screwdriver.Pipeline, error) {
return screwdriver.Pipeline(FakePipeline{ScmURL: testSCMURL}), nil
}
gitClone = func(repo, dest string) error {
if repo != testSCMURL {
t.Errorf("Git clone was called with repo %q, want %q", repo, testSCMURL)
}
return nil
}
launch(screwdriver.API(api), testBuildID)
}

0 comments on commit 47a4cce

Please sign in to comment.