mirror of https://github.com/databricks/cli.git
added io to git clone command run
This commit is contained in:
parent
fb6247bc16
commit
105a948915
|
@ -6,14 +6,14 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/cli/libs/cmdio"
|
||||||
"github.com/databricks/cli/libs/git"
|
"github.com/databricks/cli/libs/git"
|
||||||
"github.com/stretchr/testify/assert"
|
"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
|
// TODO: add assertion for error if git CLI is not found
|
||||||
func TestGitClonePublicRepository(t *testing.T) {
|
func TestAccGitClonePublicRepository(t *testing.T) {
|
||||||
// t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
||||||
|
|
||||||
tmpDir := t.TempDir()
|
tmpDir := t.TempDir()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
@ -38,11 +38,40 @@ func TestGitClonePublicRepository(t *testing.T) {
|
||||||
assert.Contains(t, string(b), "Copyright (2023) Databricks, Inc.")
|
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"))
|
// t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
||||||
|
|
||||||
tmpDir := t.TempDir()
|
tmpDir := t.TempDir()
|
||||||
ctx := context.Background()
|
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
|
// This is a private repository only accessible to databricks employees
|
||||||
err := git.Clone(ctx, git.CloneOptions{
|
err := git.Clone(ctx, git.CloneOptions{
|
||||||
|
|
|
@ -78,6 +78,21 @@ func IsErrTTY(ctx context.Context) bool {
|
||||||
return IsTTY(c.err)
|
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
|
// IsTTY detects if stdout is a terminal. It assumes that stderr is terminal as well
|
||||||
func (c *cmdIO) IsTTY() bool {
|
func (c *cmdIO) IsTTY() bool {
|
||||||
f, ok := c.out.(*os.File)
|
f, ok := c.out.(*os.File)
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/databricks/cli/libs/cmdio"
|
||||||
"github.com/databricks/cli/libs/zip"
|
"github.com/databricks/cli/libs/zip"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -86,11 +87,23 @@ func download(ctx context.Context, url string, dest string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check stdin / stdout works properly with git clone and requesting an ID/password
|
|
||||||
func clonePrivate(ctx context.Context, opts CloneOptions) error {
|
func clonePrivate(ctx context.Context, opts CloneOptions) error {
|
||||||
// TODO: test that the branch --branch flag works with tags
|
// TODO: test that the branch --branch flag works with tags
|
||||||
cmd := exec.CommandContext(ctx, "git", "clone", opts.repoUrl(), opts.destination(), "--branch", opts.Reference)
|
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 {
|
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 a public repository was not found, we defer to the git CLI
|
||||||
if errors.Is(err, errNotFound) {
|
if errors.Is(err, errNotFound) {
|
||||||
|
|
||||||
return clonePrivate(ctx, opts)
|
return clonePrivate(ctx, opts)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue