-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from screwdriver-cd/git
Added a basic git.Clone helper for checking out code
- Loading branch information
Showing
4 changed files
with
119 additions
and
1 deletion.
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 |
---|---|---|
@@ -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 | ||
} |
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 |
---|---|---|
@@ -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) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} |