github.com/zaquestion/lab@v0.25.1/cmd/project_create_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/pkg/errors"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  	"github.com/zaquestion/lab/internal/git"
    13  	lab "github.com/zaquestion/lab/internal/gitlab"
    14  )
    15  
    16  func Test_projectCreateCmd(t *testing.T) {
    17  	t.Parallel()
    18  	repo := copyTestRepo(t)
    19  	expectedPath := filepath.Base(repo)
    20  
    21  	// remove the .git/config so no remotes exist
    22  	os.Remove(filepath.Join(repo, ".git/config"))
    23  
    24  	t.Run("create", func(t *testing.T) {
    25  		// Depending on the test env the parent dirs can still contain a .git
    26  		// dir, meaning it's running inside a git repo and the test might fail.
    27  		if git.InsideGitRepo() {
    28  			t.Skip("inside git repo")
    29  		}
    30  
    31  		cmd := exec.Command(labBinaryPath, "project", "create", "-p", "-r", "origin")
    32  		cmd.Dir = repo
    33  
    34  		b, err := cmd.CombinedOutput()
    35  		if err != nil {
    36  			t.Log(string(b))
    37  			t.Fatal(err)
    38  		}
    39  
    40  		require.Contains(t, string(b), "https://gitlab.com/lab-testing/"+expectedPath+"\n")
    41  
    42  		gitCmd := git.New("remote", "get-url", "origin")
    43  		gitCmd.Dir = repo
    44  		gitCmd.Stdout = nil
    45  		gitCmd.Stderr = nil
    46  		remote, err := gitCmd.CombinedOutput()
    47  		if err != nil {
    48  			t.Fatal(err)
    49  		}
    50  		require.Equal(t, "git@gitlab.com:lab-testing/"+expectedPath+".git\n", string(remote))
    51  
    52  		p, err := lab.FindProject(expectedPath)
    53  		if err != nil {
    54  			t.Fatal(errors.Wrap(err, "failed to find project for cleanup"))
    55  		}
    56  		err = lab.ProjectDelete(p.ID)
    57  		if err != nil {
    58  			t.Fatal(errors.Wrap(err, "failed to delete project during cleanup"))
    59  		}
    60  	})
    61  }
    62  
    63  func Test_determineNamespacePath(t *testing.T) {
    64  	t.Parallel()
    65  	wd, err := os.Getwd()
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	tests := []struct {
    70  		desc              string
    71  		args              []string
    72  		expectedNamespace string
    73  		expectedPath      string
    74  	}{
    75  		{"arg", []string{"new_project"}, "", "new_project"},
    76  		{"git working dir", []string{}, "", filepath.Base(wd)},
    77  		{"namespace", []string{"group/new_project"}, "group", "new_project"},
    78  		{"namespace", []string{"company/group/new_project"}, "company/group", "new_project"},
    79  	}
    80  
    81  	for _, test := range tests {
    82  		test := test
    83  		t.Run(test.desc, func(t *testing.T) {
    84  			t.Parallel()
    85  			group, path := determineNamespacePath(test.args, "")
    86  			assert.Equal(t, test.expectedNamespace, group)
    87  			assert.Equal(t, test.expectedPath, path)
    88  		})
    89  	}
    90  }