mirror of https://gitlab.com/gitlab-org/cli.git
Merge branch 'fix-trailing-slash-repo-create' into 'main'
fix(repo): Fix create repo with trailing slash Closes #1171 See merge request https://gitlab.com/gitlab-org/cli/-/merge_requests/1141 Merged-by: Tomas Vik <tvik@gitlab.com> Approved-by: Jay McCure <jmccure@gitlab.com> Approved-by: Tomas Vik <tvik@gitlab.com> Co-authored-by: Christoper Hans <4262799+ChrHan@users.noreply.github.com>
This commit is contained in:
commit
af3fae6e87
|
@ -255,7 +255,9 @@ func initialiseRepo(projectPath, remoteURL string) error {
|
|||
}
|
||||
|
||||
func projectPathFromArgs(args []string) (host, namespace, project string) {
|
||||
project = args[0]
|
||||
// sanitize input by removing trailing "/"
|
||||
project = strings.TrimSuffix(args[0], "/")
|
||||
|
||||
if strings.Contains(project, "/") {
|
||||
pp, _ := glrepo.FromFullName(project)
|
||||
host = pp.RepoHost()
|
||||
|
|
|
@ -2,96 +2,91 @@ package create
|
|||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/acarl005/stripansi"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/xanzy/go-gitlab"
|
||||
|
||||
"gitlab.com/gitlab-org/cli/api"
|
||||
|
||||
"gitlab.com/gitlab-org/cli/commands/cmdtest"
|
||||
"gitlab.com/gitlab-org/cli/commands/cmdutils"
|
||||
"gitlab.com/gitlab-org/cli/internal/config"
|
||||
"gitlab.com/gitlab-org/cli/pkg/iostreams"
|
||||
)
|
||||
|
||||
// TODO: test by mocking the appropriate api function
|
||||
func Test_projectCreateCmd(t *testing.T) {
|
||||
/*
|
||||
t.Parallel()
|
||||
repo := cmdtest.CopyTestRepo(t)
|
||||
expectedPath := fmt.Sprintf("glab-cli/%s", filepath.Base(repo))
|
||||
|
||||
// remove the .git/config so no remotes exist
|
||||
err := os.Remove(filepath.Join(repo, ".git/config"))
|
||||
if err != nil {
|
||||
t.Errorf("could not remove .git/config: %v", err)
|
||||
}
|
||||
_, err = api.DeleteProject(nil, expectedPath)
|
||||
if err != nil {
|
||||
t.Logf("unable to delete project %s: %v", expectedPath, err)
|
||||
}
|
||||
t.Run("create", func(t *testing.T) {
|
||||
cmd := exec.Command(cmdtest.GlabBinaryPath, "repo", "create", "-g", "glab-cli", "--public")
|
||||
cmd.Dir = repo
|
||||
|
||||
b, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Log(string(b))
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rp := strings.Split(expectedPath, "/")[1]
|
||||
require.Contains(t, string(b),
|
||||
"✓ Created repository glab / "+rp+" on GitLab: https://gitlab.com/"+expectedPath+"\n✓ Added remote git@gitlab.com:"+expectedPath+".git\n")
|
||||
|
||||
gitCmd := exec.Command("git", "remote", "get-url", "origin")
|
||||
gitCmd.Dir = repo
|
||||
gitCmd.Stdout = nil
|
||||
gitCmd.Stderr = nil
|
||||
remote, err := gitCmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cmdtest.Eq(t, string(remote), "git@gitlab.com:"+expectedPath+".git\n")
|
||||
})
|
||||
p, err := api.GetProject(nil, expectedPath)
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrap(err, "failed to find project for cleanup"))
|
||||
}
|
||||
_, err = api.DeleteProject(nil, p.ID)
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrap(err, "failed to delete project during cleanup"))
|
||||
}
|
||||
}
|
||||
|
||||
func Test_projectCreateCmdWithArgs(t *testing.T) {
|
||||
repo := cmdtest.CopyTestRepo(t)
|
||||
expectedPath := "glab-cli/unittest"
|
||||
|
||||
// remove the .git/config so no remotes exist
|
||||
err := os.Remove(filepath.Join(repo, ".git/config"))
|
||||
if err != nil {
|
||||
t.Errorf("could not remove .git/config: %v", err)
|
||||
}
|
||||
_, err = api.DeleteProject(nil, expectedPath)
|
||||
if err != nil {
|
||||
t.Logf("unable to delete project %s: %v", expectedPath, err)
|
||||
}
|
||||
t.Run("create_with_args", func(t *testing.T) {
|
||||
cmd := exec.Command(cmdtest.GlabBinaryPath, "repo", "create", expectedPath, "--public")
|
||||
cmd.Dir = repo
|
||||
|
||||
b, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Log(string(b))
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
require.Contains(t, string(b),
|
||||
"✓ Created repository glab / unittest on GitLab: https://gitlab.com/"+expectedPath+"\n")
|
||||
err = initialiseRepo(expectedPath, "git@gitlab.com:"+expectedPath+".git")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
p, err := api.GetProject(nil, expectedPath)
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrap(err, "failed to find project for cleanup"))
|
||||
}
|
||||
_, err = api.DeleteProject(nil, p.ID)
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrap(err, "failed to delete project during cleanup"))
|
||||
}
|
||||
|
||||
*/
|
||||
func TestMain(m *testing.M) {
|
||||
cmdtest.InitTest(m, "project_create_test")
|
||||
}
|
||||
|
||||
func Test_projectCreateCmd(t *testing.T) {
|
||||
t.Parallel()
|
||||
defer config.StubConfig(`---
|
||||
hosts:
|
||||
gitlab.com:
|
||||
username: monalisa
|
||||
token: OTOKEN
|
||||
no_prompt: true
|
||||
`, "")()
|
||||
|
||||
io, _, stdout, stderr := iostreams.Test()
|
||||
stubFactory, _ := cmdtest.StubFactoryWithConfig("")
|
||||
// to skip creation of local project directory, set prompt to false
|
||||
stubFactory.IO = io
|
||||
stubFactory.IO.IsaTTY = false
|
||||
stubFactory.IO.IsErrTTY = false
|
||||
|
||||
api.CreateProject = func(client *gitlab.Client, opts *gitlab.CreateProjectOptions) (*gitlab.Project, error) {
|
||||
return &gitlab.Project{
|
||||
ID: 1,
|
||||
Name: *opts.Name,
|
||||
NameWithNamespace: *opts.Name,
|
||||
}, nil
|
||||
}
|
||||
|
||||
api.CurrentUser = func(client *gitlab.Client) (*gitlab.User, error) {
|
||||
return &gitlab.User{
|
||||
ID: 1,
|
||||
Username: "username",
|
||||
Name: "name",
|
||||
}, nil
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
Name string
|
||||
Args []string
|
||||
ExpectedMsg []string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
Name: "Create project with only repo name",
|
||||
Args: []string{"reponame"},
|
||||
ExpectedMsg: []string{"✓ Created repository reponame on GitLab: \n"},
|
||||
},
|
||||
{
|
||||
Name: "Create project with only repo name and slash suffix",
|
||||
Args: []string{"reponame/"},
|
||||
ExpectedMsg: []string{"✓ Created repository reponame on GitLab: \n"},
|
||||
},
|
||||
}
|
||||
|
||||
cmd := NewCmdCreate(stubFactory)
|
||||
cmdutils.EnableRepoOverride(cmd, stubFactory)
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
cmd.SetArgs(tc.Args)
|
||||
_, err := cmd.ExecuteC()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
out := stripansi.Strip(stdout.String())
|
||||
|
||||
for _, msg := range tc.ExpectedMsg {
|
||||
assert.Contains(t, out, msg)
|
||||
assert.Equal(t, "", stderr.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue