cli/commands/project/clone/repo_clone_test.go

264 lines
5.9 KiB
Go
Raw Normal View History

Isolate commands into separate sub-packages (#229) * refactor: organize commands into separate subfolders * Format code with gofmt This commit fixes the style issues introduced in d1f70d5 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/1ce57d1c-4f1e-48c2-a4d8-00c1bce7fb49/ * 🔥 sub-packaging * Format code with gofmt This commit fixes the style issues introduced in ca0bff4 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/11379011-98c6-43ea-8baa-4fefa9b775e4/ * 🔥 sub-packaging * Format code with gofmt This commit fixes the style issues introduced in 00e5936 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/c912f945-acf3-48d2-a550-0895c0153df0/ * Done with sub-packaging 🔥 * 🔥 tests * Format code with gofmt This commit fixes the style issues introduced in 56a8ff4 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/24498865-4980-479f-90c7-7ddd228c2bb4/ * fix bug risks * fix tests * fix tests * Format code with gofmt This commit fixes the style issues introduced in c947d02 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/cb2ac46a-f857-40d5-8508-eacc253b3614/ * Format code with gofmt This commit fixes the style issues introduced in 902ff7b according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/f60ba0d6-94d3-47c0-ae27-568a5cc5d9e5/ * fix tests * fix tests * 🔥 gookit/color * Format code with gofmt This commit fixes the style issues introduced in 5a129ea according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/8f1c8327-f5c6-4ff5-97d2-3c8a23f0a1e2/ * Autofix issues in 1 files Resolved issues in the following files via DeepSource Autofix: 1. commands/cmdtest/helper.go * add tests for MR * Format code with gofmt This commit fixes the style issues introduced in 758af28 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/d55a4867-aa23-4202-b2cc-a7ceafbaa53e/ * migrate old config to new config * fix bug risks * Format code with gofmt This commit fixes the style issues introduced in 0ea8eb2 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/f90cf664-8396-4b78-814d-3167ce898164/ * refactoring Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
2020-09-29 05:55:34 +00:00
package clone
import (
2020-12-21 18:50:57 +00:00
"bytes"
"fmt"
2020-12-04 01:59:53 +00:00
"os"
"strings"
"testing"
2022-09-19 20:23:45 +00:00
"gitlab.com/gitlab-org/cli/pkg/iostreams"
2020-12-21 18:50:57 +00:00
"github.com/spf13/cobra"
"github.com/google/shlex"
"github.com/stretchr/testify/require"
2022-09-19 20:23:45 +00:00
"gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/internal/config"
"gitlab.com/gitlab-org/cli/test"
"github.com/stretchr/testify/assert"
2022-09-19 20:23:45 +00:00
"gitlab.com/gitlab-org/cli/commands/cmdtest"
)
2020-12-04 01:59:53 +00:00
Isolate commands into separate sub-packages (#229) * refactor: organize commands into separate subfolders * Format code with gofmt This commit fixes the style issues introduced in d1f70d5 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/1ce57d1c-4f1e-48c2-a4d8-00c1bce7fb49/ * 🔥 sub-packaging * Format code with gofmt This commit fixes the style issues introduced in ca0bff4 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/11379011-98c6-43ea-8baa-4fefa9b775e4/ * 🔥 sub-packaging * Format code with gofmt This commit fixes the style issues introduced in 00e5936 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/c912f945-acf3-48d2-a550-0895c0153df0/ * Done with sub-packaging 🔥 * 🔥 tests * Format code with gofmt This commit fixes the style issues introduced in 56a8ff4 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/24498865-4980-479f-90c7-7ddd228c2bb4/ * fix bug risks * fix tests * fix tests * Format code with gofmt This commit fixes the style issues introduced in c947d02 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/cb2ac46a-f857-40d5-8508-eacc253b3614/ * Format code with gofmt This commit fixes the style issues introduced in 902ff7b according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/f60ba0d6-94d3-47c0-ae27-568a5cc5d9e5/ * fix tests * fix tests * 🔥 gookit/color * Format code with gofmt This commit fixes the style issues introduced in 5a129ea according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/8f1c8327-f5c6-4ff5-97d2-3c8a23f0a1e2/ * Autofix issues in 1 files Resolved issues in the following files via DeepSource Autofix: 1. commands/cmdtest/helper.go * add tests for MR * Format code with gofmt This commit fixes the style issues introduced in 758af28 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/d55a4867-aa23-4202-b2cc-a7ceafbaa53e/ * migrate old config to new config * fix bug risks * Format code with gofmt This commit fixes the style issues introduced in 0ea8eb2 according to the output from gofmt. Details: https://deepsource.io/gh/profclems/glab/transform/f90cf664-8396-4b78-814d-3167ce898164/ * refactoring Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
2020-09-29 05:55:34 +00:00
func TestMain(m *testing.M) {
cmdtest.InitTest(m, "repo_clone_test")
}
2020-12-21 18:50:57 +00:00
func runCommand(cmd *cobra.Command, cli string, stds ...*bytes.Buffer) (*test.CmdOut, error) {
var stdin *bytes.Buffer
var stderr *bytes.Buffer
var stdout *bytes.Buffer
for i, std := range stds {
if std != nil {
if i == 0 {
stdin = std
}
if i == 1 {
stdout = std
}
if i == 2 {
stderr = std
}
}
}
cmd.SetIn(stdin)
cmd.SetOut(stdout)
cmd.SetErr(stderr)
argv, err := shlex.Split(cli)
if err != nil {
return nil, err
}
cmd.SetArgs(argv)
_, err = cmd.ExecuteC()
return &test.CmdOut{
OutBuf: stdout,
ErrBuf: stderr,
}, err
}
func TestNewCmdClone(t *testing.T) {
testCases := []struct {
2020-12-04 01:59:53 +00:00
name string
args string
wantOpts CloneOptions
wantCtxOpts ContextOpts
2020-12-04 01:59:53 +00:00
wantErr string
}{
{
name: "no arguments",
args: "",
wantErr: "specify repo argument or use --group flag to specify a group to clone all repos from the group",
},
{
name: "repo argument",
args: "NAMESPACE/REPO",
wantOpts: CloneOptions{
2020-12-04 01:59:53 +00:00
GitFlags: []string{},
},
wantCtxOpts: ContextOpts{
Repo: "NAMESPACE/REPO",
},
},
{
name: "directory argument",
args: "NAMESPACE/REPO mydir",
wantOpts: CloneOptions{
2020-12-04 01:59:53 +00:00
GitFlags: []string{"mydir"},
},
wantCtxOpts: ContextOpts{
Repo: "NAMESPACE/REPO",
},
},
{
name: "git clone arguments",
args: "NAMESPACE/REPO -- --depth 1 --recurse-submodules",
wantOpts: CloneOptions{
2020-12-04 01:59:53 +00:00
GitFlags: []string{"--depth", "1", "--recurse-submodules"},
},
wantCtxOpts: ContextOpts{
Repo: "NAMESPACE/REPO",
},
},
{
name: "unknown argument",
args: "NAMESPACE/REPO --depth 1",
wantErr: "unknown flag: --depth\nSeparate git clone flags with '--'.",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
io, stdin, stdout, stderr := iostreams.Test()
fac := &cmdutils.Factory{IO: io}
var opts *CloneOptions
var ctxOpts *ContextOpts
cmd := NewCmdClone(fac, func(co *CloneOptions, cx *ContextOpts) error {
opts = co
ctxOpts = cx
return nil
})
argv, err := shlex.Split(tt.args)
require.NoError(t, err)
cmd.SetArgs(argv)
cmd.SetIn(stdin)
cmd.SetOut(stdout)
cmd.SetErr(stderr)
_, err = cmd.ExecuteC()
if err != nil {
assert.Equal(t, tt.wantErr, err.Error())
return
} else if tt.wantErr != "" {
t.Errorf("expected error %q, got nil", tt.wantErr)
}
assert.Equal(t, "", stdout.String())
assert.Equal(t, "", stderr.String())
assert.Equal(t, tt.wantCtxOpts.Repo, ctxOpts.Repo)
assert.Equal(t, tt.wantOpts.GitFlags, opts.GitFlags)
})
}
}
2020-09-02 15:27:53 +00:00
func Test_repoClone(t *testing.T) {
defer config.StubConfig(`
hosts:
gitlab.com:
token: qRC87Xg9Wd46RhB8J8sp
`, "")()
token := os.Getenv("GITLAB_TOKEN")
if token != "" {
_ = os.Setenv("GITLAB_TOKEN", "")
}
io, stdin, stdout, stderr := iostreams.Test()
fac := &cmdutils.Factory{
IO: io,
Config: func() (config.Config, error) {
return config.ParseConfig("config.yml")
},
}
cs, restore := test.InitCmdStubber()
// git clone
cs.Stub("")
// git remote add
cs.Stub("")
defer restore()
cmd := NewCmdClone(fac, nil)
2020-12-21 18:50:57 +00:00
out, err := runCommand(cmd, "test", stdin, stdout, stderr)
if err != nil {
t.Errorf("unexpected error: %q", err)
return
}
assert.Equal(t, "", out.String())
assert.Equal(t, "", out.Stderr())
assert.Equal(t, 1, cs.Count)
assert.Equal(t, "git clone git@gitlab.com:clemsbot/test.git", strings.Join(cs.Calls[0].Args, " "))
if token != "" {
_ = os.Setenv("GITLAB_TOKEN", token)
}
}
func Test_repoClone_group(t *testing.T) {
names := []string{"glab-cli/test", "glab-cli/test-pv"}
urls := []string{"git@gitlab.com:glab-cli/test.git", "git@gitlab.com:glab-cli/test-pv.git"}
repoCloneTest(t, names, urls, 0, false)
}
func Test_repoClone_group_single(t *testing.T) {
names := []string{"glab-cli/test"}
urls := []string{"git@gitlab.com:glab-cli/test.git"}
repoCloneTest(t, names, urls, 1, false)
}
func Test_repoClone_group_paginate(t *testing.T) {
names := []string{"glab-cli/test", "glab-cli/test-pv"}
urls := []string{"git@gitlab.com:glab-cli/test.git", "git@gitlab.com:glab-cli/test-pv.git"}
repoCloneTest(t, names, urls, 1, true)
}
func repoCloneTest(t *testing.T, expectedRepoNames []string, expectedRepoUrls []string, perPage int, paginate bool) {
assert.Equal(t, len(expectedRepoNames), len(expectedRepoUrls))
defer config.StubConfig(`
hosts:
gitlab.com:
token: qRC87Xg9Wd46RhB8J8sp
`, "")()
token := os.Getenv("GITLAB_TOKEN")
if token != "" {
_ = os.Setenv("GITLAB_TOKEN", "")
}
io, stdin, stdout, stderr := iostreams.Test()
fac := &cmdutils.Factory{
IO: io,
Config: func() (config.Config, error) {
return config.ParseConfig("config.yml")
},
}
cs, restore := test.InitCmdStubber()
for i := 0; i < len(expectedRepoUrls); i++ {
cs.Stub("")
}
defer restore()
cmd := NewCmdClone(fac, nil)
cli := "-g glab-cli"
if perPage != 0 {
cli += fmt.Sprintf(" --per-page %d", perPage)
}
if paginate {
cli += " --paginate"
}
// TODO: stub api.ListGroupProjects endpoint
out, err := runCommand(cmd, cli, stdin, stdout, stderr)
if err != nil {
t.Errorf("unexpected error: %q", err)
return
}
assert.Equal(t, "✓ "+strings.Join(expectedRepoNames, "\n✓ ")+"\n", out.String())
assert.Equal(t, "", out.Stderr())
assert.Equal(t, len(expectedRepoUrls), cs.Count)
for i := 0; i < len(expectedRepoUrls); i++ {
assert.Equal(t, fmt.Sprintf("git clone %s", expectedRepoUrls[i]), strings.Join(cs.Calls[i].Args, " "))
}
if token != "" {
_ = os.Setenv("GITLAB_TOKEN", token)
}
}