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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
package repository
import (
"testing"
"github.com/cli/go-gh/v2/pkg/config"
"github.com/stretchr/testify/assert"
)
func TestParse(t *testing.T) {
stubConfig(t, "")
tests := []struct {
name string
input string
hostOverride string
wantOwner string
wantName string
wantHost string
wantErr string
}{
{
name: "OWNER/REPO combo",
input: "OWNER/REPO",
wantHost: "github.com",
wantOwner: "OWNER",
wantName: "REPO",
},
{
name: "too few elements",
input: "OWNER",
wantErr: `expected the "[HOST/]OWNER/REPO" format, got "OWNER"`,
},
{
name: "too many elements",
input: "a/b/c/d",
wantErr: `expected the "[HOST/]OWNER/REPO" format, got "a/b/c/d"`,
},
{
name: "blank value",
input: "a/",
wantErr: `expected the "[HOST/]OWNER/REPO" format, got "a/"`,
},
{
name: "with hostname",
input: "example.org/OWNER/REPO",
wantHost: "example.org",
wantOwner: "OWNER",
wantName: "REPO",
},
{
name: "full URL",
input: "https://example.org/OWNER/REPO.git",
wantHost: "example.org",
wantOwner: "OWNER",
wantName: "REPO",
},
{
name: "SSH URL",
input: "git@example.org:OWNER/REPO.git",
wantHost: "example.org",
wantOwner: "OWNER",
wantName: "REPO",
},
{
name: "OWNER/REPO with default host override",
input: "OWNER/REPO",
hostOverride: "override.com",
wantHost: "override.com",
wantOwner: "OWNER",
wantName: "REPO",
},
{
name: "HOST/OWNER/REPO with default host override",
input: "example.com/OWNER/REPO",
hostOverride: "override.com",
wantHost: "example.com",
wantOwner: "OWNER",
wantName: "REPO",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("GH_CONFIG_DIR", "nonexistant")
if tt.hostOverride != "" {
t.Setenv("GH_HOST", tt.hostOverride)
}
r, err := Parse(tt.input)
if tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.wantHost, r.Host)
assert.Equal(t, tt.wantOwner, r.Owner)
assert.Equal(t, tt.wantName, r.Name)
})
}
}
func TestParse_hostFromConfig(t *testing.T) {
var cfgStr = `
hosts:
enterprise.com:
user: user2
oauth_token: yyyyyyyyyyyyyyyyyyyy
git_protocol: https
`
stubConfig(t, cfgStr)
r, err := Parse("OWNER/REPO")
assert.NoError(t, err)
assert.Equal(t, "enterprise.com", r.Host)
assert.Equal(t, "OWNER", r.Owner)
assert.Equal(t, "REPO", r.Name)
}
func TestParseWithHost(t *testing.T) {
tests := []struct {
name string
input string
host string
wantOwner string
wantName string
wantHost string
wantErr string
}{
{
name: "OWNER/REPO combo",
input: "OWNER/REPO",
host: "github.com",
wantHost: "github.com",
wantOwner: "OWNER",
wantName: "REPO",
},
{
name: "too few elements",
input: "OWNER",
host: "github.com",
wantErr: `expected the "[HOST/]OWNER/REPO" format, got "OWNER"`,
},
{
name: "too many elements",
input: "a/b/c/d",
host: "github.com",
wantErr: `expected the "[HOST/]OWNER/REPO" format, got "a/b/c/d"`,
},
{
name: "blank value",
input: "a/",
host: "github.com",
wantErr: `expected the "[HOST/]OWNER/REPO" format, got "a/"`,
},
{
name: "with hostname",
input: "example.org/OWNER/REPO",
host: "github.com",
wantHost: "example.org",
wantOwner: "OWNER",
wantName: "REPO",
},
{
name: "full URL",
input: "https://example.org/OWNER/REPO.git",
host: "github.com",
wantHost: "example.org",
wantOwner: "OWNER",
wantName: "REPO",
},
{
name: "SSH URL",
input: "git@example.org:OWNER/REPO.git",
host: "github.com",
wantHost: "example.org",
wantOwner: "OWNER",
wantName: "REPO",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r, err := ParseWithHost(tt.input, tt.host)
if tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.wantHost, r.Host)
assert.Equal(t, tt.wantOwner, r.Owner)
assert.Equal(t, tt.wantName, r.Name)
})
}
}
func stubConfig(t *testing.T, cfgStr string) {
t.Helper()
old := config.Read
config.Read = func(_ *config.Config) (*config.Config, error) {
return config.ReadFromString(cfgStr), nil
}
t.Cleanup(func() {
config.Read = old
})
}
|