1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
package issueutils
import (
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/cli/internal/glrepo"
)
func Test_issueMetadataFromURL(t *testing.T) {
tests := []struct {
name string
str string
want int
path string
}{
{
name: "valid URL",
str: "https://gitlab.com/namespace/repo/-/issues/1",
want: 1,
path: "https://gitlab.com/namespace/repo/",
},
{
name: "valid URL with nested subgroup",
str: "https://gitlab.com/namespace/project/subproject/repo/-/issues/100",
want: 100,
path: "https://gitlab.com/namespace/project/subproject/repo/",
},
{
name: "valid URL without dash",
str: "https://gitlab.com/namespace/project/subproject/repo/issues/1",
want: 1,
path: "https://gitlab.com/namespace/project/subproject/repo/",
},
{
name: "valid incident URL",
str: "https://gitlab.com/namespace/repo/-/issues/incident/1",
want: 1,
path: "https://gitlab.com/namespace/repo/",
},
{
name: "valid incident URL with nested subgroup",
str: "https://gitlab.com/namespace/project/subproject/repo/-/issues/incident/100",
want: 100,
path: "https://gitlab.com/namespace/project/subproject/repo/",
},
{
name: "valid incident URL without dash",
str: "https://gitlab.com/namespace/project/subproject/repo/issues/incident/1",
want: 1,
path: "https://gitlab.com/namespace/project/subproject/repo/",
},
{
name: "invalid URL with no issue number",
str: "https://gitlab.com/namespace/project/subproject/repo/issues",
want: 0,
path: "",
},
{
name: "invalid incident URL with no incident number",
str: "https://gitlab.com/namespace/project/subproject/repo/issues/incident",
want: 0,
path: "",
},
{
name: "invalid URL with only namespace, missing repo",
str: "https://gitlab.com/namespace/issues/100",
want: 0,
path: "",
},
{
name: "invalid incident URL with only namespace, missing repo",
str: "https://gitlab.com/namespace/issues/incident/100",
want: 0,
path: "",
},
{
name: "invalid issue URL",
str: "https://gitlab.com/namespace/repo",
want: 0,
path: "",
},
{
name: "invalid issue URL, missing issues path",
str: "https://gitlab.com/namespace/project/subproject/repo/10/",
want: 0,
path: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
id, repo := issueMetadataFromURL(tt.str)
require.Equal(t, tt.want, id)
if tt.want != 0 && tt.path != "" {
expectedRepo, err := glrepo.FromFullName(tt.path)
require.NoError(t, err)
require.Equal(t, expectedRepo, repo)
}
})
}
}
|