added io to git clone command run

This commit is contained in:
Shreyas Goenka 2023-06-29 18:47:56 +02:00
parent fb6247bc16
commit 105a948915
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 63 additions and 7 deletions

View File

@ -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{

View File

@ -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)

View File

@ -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