...

Source file src/github.com/cli/go-gh/v2/pkg/repository/repository_test.go

Documentation: github.com/cli/go-gh/v2/pkg/repository

     1  package repository
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/cli/go-gh/v2/pkg/config"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestParse(t *testing.T) {
    11  	stubConfig(t, "")
    12  
    13  	tests := []struct {
    14  		name         string
    15  		input        string
    16  		hostOverride string
    17  		wantOwner    string
    18  		wantName     string
    19  		wantHost     string
    20  		wantErr      string
    21  	}{
    22  		{
    23  			name:      "OWNER/REPO combo",
    24  			input:     "OWNER/REPO",
    25  			wantHost:  "github.com",
    26  			wantOwner: "OWNER",
    27  			wantName:  "REPO",
    28  		},
    29  		{
    30  			name:    "too few elements",
    31  			input:   "OWNER",
    32  			wantErr: `expected the "[HOST/]OWNER/REPO" format, got "OWNER"`,
    33  		},
    34  		{
    35  			name:    "too many elements",
    36  			input:   "a/b/c/d",
    37  			wantErr: `expected the "[HOST/]OWNER/REPO" format, got "a/b/c/d"`,
    38  		},
    39  		{
    40  			name:    "blank value",
    41  			input:   "a/",
    42  			wantErr: `expected the "[HOST/]OWNER/REPO" format, got "a/"`,
    43  		},
    44  		{
    45  			name:      "with hostname",
    46  			input:     "example.org/OWNER/REPO",
    47  			wantHost:  "example.org",
    48  			wantOwner: "OWNER",
    49  			wantName:  "REPO",
    50  		},
    51  		{
    52  			name:      "full URL",
    53  			input:     "https://example.org/OWNER/REPO.git",
    54  			wantHost:  "example.org",
    55  			wantOwner: "OWNER",
    56  			wantName:  "REPO",
    57  		},
    58  		{
    59  			name:      "SSH URL",
    60  			input:     "git@example.org:OWNER/REPO.git",
    61  			wantHost:  "example.org",
    62  			wantOwner: "OWNER",
    63  			wantName:  "REPO",
    64  		},
    65  		{
    66  			name:         "OWNER/REPO with default host override",
    67  			input:        "OWNER/REPO",
    68  			hostOverride: "override.com",
    69  			wantHost:     "override.com",
    70  			wantOwner:    "OWNER",
    71  			wantName:     "REPO",
    72  		},
    73  		{
    74  			name:         "HOST/OWNER/REPO with default host override",
    75  			input:        "example.com/OWNER/REPO",
    76  			hostOverride: "override.com",
    77  			wantHost:     "example.com",
    78  			wantOwner:    "OWNER",
    79  			wantName:     "REPO",
    80  		},
    81  	}
    82  	for _, tt := range tests {
    83  		t.Run(tt.name, func(t *testing.T) {
    84  			t.Setenv("GH_CONFIG_DIR", "nonexistant")
    85  			if tt.hostOverride != "" {
    86  				t.Setenv("GH_HOST", tt.hostOverride)
    87  			}
    88  			r, err := Parse(tt.input)
    89  			if tt.wantErr != "" {
    90  				assert.EqualError(t, err, tt.wantErr)
    91  				return
    92  			}
    93  			assert.NoError(t, err)
    94  			assert.Equal(t, tt.wantHost, r.Host)
    95  			assert.Equal(t, tt.wantOwner, r.Owner)
    96  			assert.Equal(t, tt.wantName, r.Name)
    97  		})
    98  	}
    99  }
   100  
   101  func TestParse_hostFromConfig(t *testing.T) {
   102  	var cfgStr = `
   103  hosts:
   104    enterprise.com:
   105      user: user2
   106      oauth_token: yyyyyyyyyyyyyyyyyyyy
   107      git_protocol: https
   108  `
   109  	stubConfig(t, cfgStr)
   110  	r, err := Parse("OWNER/REPO")
   111  	assert.NoError(t, err)
   112  	assert.Equal(t, "enterprise.com", r.Host)
   113  	assert.Equal(t, "OWNER", r.Owner)
   114  	assert.Equal(t, "REPO", r.Name)
   115  }
   116  
   117  func TestParseWithHost(t *testing.T) {
   118  	tests := []struct {
   119  		name      string
   120  		input     string
   121  		host      string
   122  		wantOwner string
   123  		wantName  string
   124  		wantHost  string
   125  		wantErr   string
   126  	}{
   127  		{
   128  			name:      "OWNER/REPO combo",
   129  			input:     "OWNER/REPO",
   130  			host:      "github.com",
   131  			wantHost:  "github.com",
   132  			wantOwner: "OWNER",
   133  			wantName:  "REPO",
   134  		},
   135  		{
   136  			name:    "too few elements",
   137  			input:   "OWNER",
   138  			host:    "github.com",
   139  			wantErr: `expected the "[HOST/]OWNER/REPO" format, got "OWNER"`,
   140  		},
   141  		{
   142  			name:    "too many elements",
   143  			input:   "a/b/c/d",
   144  			host:    "github.com",
   145  			wantErr: `expected the "[HOST/]OWNER/REPO" format, got "a/b/c/d"`,
   146  		},
   147  		{
   148  			name:    "blank value",
   149  			input:   "a/",
   150  			host:    "github.com",
   151  			wantErr: `expected the "[HOST/]OWNER/REPO" format, got "a/"`,
   152  		},
   153  		{
   154  			name:      "with hostname",
   155  			input:     "example.org/OWNER/REPO",
   156  			host:      "github.com",
   157  			wantHost:  "example.org",
   158  			wantOwner: "OWNER",
   159  			wantName:  "REPO",
   160  		},
   161  		{
   162  			name:      "full URL",
   163  			input:     "https://example.org/OWNER/REPO.git",
   164  			host:      "github.com",
   165  			wantHost:  "example.org",
   166  			wantOwner: "OWNER",
   167  			wantName:  "REPO",
   168  		},
   169  		{
   170  			name:      "SSH URL",
   171  			input:     "git@example.org:OWNER/REPO.git",
   172  			host:      "github.com",
   173  			wantHost:  "example.org",
   174  			wantOwner: "OWNER",
   175  			wantName:  "REPO",
   176  		},
   177  	}
   178  	for _, tt := range tests {
   179  		t.Run(tt.name, func(t *testing.T) {
   180  			r, err := ParseWithHost(tt.input, tt.host)
   181  			if tt.wantErr != "" {
   182  				assert.EqualError(t, err, tt.wantErr)
   183  				return
   184  			}
   185  			assert.NoError(t, err)
   186  			assert.Equal(t, tt.wantHost, r.Host)
   187  			assert.Equal(t, tt.wantOwner, r.Owner)
   188  			assert.Equal(t, tt.wantName, r.Name)
   189  		})
   190  	}
   191  }
   192  
   193  func stubConfig(t *testing.T, cfgStr string) {
   194  	t.Helper()
   195  	old := config.Read
   196  	config.Read = func(_ *config.Config) (*config.Config, error) {
   197  		return config.ReadFromString(cfgStr), nil
   198  	}
   199  	t.Cleanup(func() {
   200  		config.Read = old
   201  	})
   202  }
   203  

View as plain text