From 105a948915bb8bd5466950a679541d6cfc4deb6f Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Thu, 29 Jun 2023 18:47:56 +0200 Subject: [PATCH] added io to git clone command run --- internal/git_clone_test.go | 37 +++++++++++++++++++++++++++++++++---- libs/cmdio/io.go | 15 +++++++++++++++ libs/git/clone.go | 18 +++++++++++++++--- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/internal/git_clone_test.go b/internal/git_clone_test.go index 10d3269af..84ce70099 100644 --- a/internal/git_clone_test.go +++ b/internal/git_clone_test.go @@ -6,14 +6,14 @@ import ( "path/filepath" "testing" + "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/git" "github.com/stretchr/testify/assert" ) -// TODO: add assertion that the git tool is not called, maybe by voiding PATH // TODO: add assertion for error if git CLI is not found -func TestGitClonePublicRepository(t *testing.T) { - // t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV")) +func TestAccGitClonePublicRepository(t *testing.T) { + t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV")) tmpDir := t.TempDir() ctx := context.Background() @@ -38,11 +38,40 @@ func TestGitClonePublicRepository(t *testing.T) { assert.Contains(t, string(b), "Copyright (2023) Databricks, Inc.") } -func TestAccGitClonePrivateRepository(t *testing.T) { +func TestAccGitClonePublicRepositoryForTagReference(t *testing.T) { // t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV")) tmpDir := t.TempDir() ctx := context.Background() + var err error + + // We unset PATH to ensure that git.Clone cannot rely on the git CLI + t.Setenv("PATH", "") + + err = git.Clone(ctx, git.CloneOptions{ + Provider: "github", + Organization: "databricks", + RepositoryName: "cli", + Reference: "snapshot", + TargetDir: tmpDir, + }) + + assert.NoError(t, err) + assert.DirExists(t, filepath.Join(tmpDir, "cli-snapshot")) + + b, err := os.ReadFile(filepath.Join(tmpDir, "cli-snapshot/NOTICE")) + assert.NoError(t, err) + assert.Contains(t, string(b), "Copyright (2023) Databricks, Inc.") +} + +// TODO(file an issue before merge): create a dedicated databricks private repository +// and test this for branches and tags +func TestAccGitClonePrivateRepository(t *testing.T) { + t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV")) + + tmpDir := t.TempDir() + cmdIO := cmdio.NewIO("text", os.Stdin, os.Stdout, os.Stderr, "") + ctx := cmdio.InContext(context.Background(), cmdIO) // This is a private repository only accessible to databricks employees err := git.Clone(ctx, git.CloneOptions{ diff --git a/libs/cmdio/io.go b/libs/cmdio/io.go index 8b40294b9..9a402d940 100644 --- a/libs/cmdio/io.go +++ b/libs/cmdio/io.go @@ -78,6 +78,21 @@ func IsErrTTY(ctx context.Context) bool { return IsTTY(c.err) } +func In(ctx context.Context) io.Reader { + c := fromContext(ctx) + return c.in +} + +func Out(ctx context.Context) io.Writer { + c := fromContext(ctx) + return c.out +} + +func Err(ctx context.Context) io.Writer { + c := fromContext(ctx) + return c.err +} + // IsTTY detects if stdout is a terminal. It assumes that stderr is terminal as well func (c *cmdIO) IsTTY() bool { f, ok := c.out.(*os.File) diff --git a/libs/git/clone.go b/libs/git/clone.go index 4da17c74d..d48518be1 100644 --- a/libs/git/clone.go +++ b/libs/git/clone.go @@ -10,6 +10,7 @@ import ( "os/exec" "path/filepath" + "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/zip" ) @@ -86,11 +87,23 @@ func download(ctx context.Context, url string, dest string) error { return err } -// TODO: check stdin / stdout works properly with git clone and requesting an ID/password func clonePrivate(ctx context.Context, opts CloneOptions) error { // TODO: test that the branch --branch flag works with tags cmd := exec.CommandContext(ctx, "git", "clone", opts.repoUrl(), opts.destination(), "--branch", opts.Reference) - return cmd.Run() + + // Redirect exec command output + cmd.Stderr = cmdio.Err(ctx) + cmd.Stdout = cmdio.Out(ctx) + cmd.Stdin = cmdio.In(ctx) + + // start git clone + err := cmd.Start() + if err != nil { + return err + } + + // wait for git clone to complete + return cmd.Wait() } func clonePublic(ctx context.Context, opts CloneOptions) error { @@ -123,7 +136,6 @@ func Clone(ctx context.Context, opts CloneOptions) error { // If a public repository was not found, we defer to the git CLI if errors.Is(err, errNotFound) { - return clonePrivate(ctx, opts) } return err