github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/internal/ghrepo/repo_test.go (about)

     1  package ghrepo
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/url"
     7  	"testing"
     8  )
     9  
    10  func Test_repoFromURL(t *testing.T) {
    11  	tests := []struct {
    12  		name   string
    13  		input  string
    14  		result string
    15  		host   string
    16  		err    error
    17  	}{
    18  		{
    19  			name:   "github.com URL",
    20  			input:  "https://github.com/monalisa/octo-cat.git",
    21  			result: "monalisa/octo-cat",
    22  			host:   "github.com",
    23  			err:    nil,
    24  		},
    25  		{
    26  			name:   "github.com URL with trailing slash",
    27  			input:  "https://github.com/monalisa/octo-cat/",
    28  			result: "monalisa/octo-cat",
    29  			host:   "github.com",
    30  			err:    nil,
    31  		},
    32  		{
    33  			name:   "www.github.com URL",
    34  			input:  "http://www.GITHUB.com/monalisa/octo-cat.git",
    35  			result: "monalisa/octo-cat",
    36  			host:   "github.com",
    37  			err:    nil,
    38  		},
    39  		{
    40  			name:   "too many path components",
    41  			input:  "https://github.com/monalisa/octo-cat/pulls",
    42  			result: "",
    43  			host:   "",
    44  			err:    errors.New("invalid path: /monalisa/octo-cat/pulls"),
    45  		},
    46  		{
    47  			name:   "non-GitHub hostname",
    48  			input:  "https://example.com/one/two",
    49  			result: "one/two",
    50  			host:   "example.com",
    51  			err:    nil,
    52  		},
    53  		{
    54  			name:   "filesystem path",
    55  			input:  "/path/to/file",
    56  			result: "",
    57  			host:   "",
    58  			err:    errors.New("no hostname detected"),
    59  		},
    60  		{
    61  			name:   "filesystem path with scheme",
    62  			input:  "file:///path/to/file",
    63  			result: "",
    64  			host:   "",
    65  			err:    errors.New("no hostname detected"),
    66  		},
    67  		{
    68  			name:   "github.com SSH URL",
    69  			input:  "ssh://github.com/monalisa/octo-cat.git",
    70  			result: "monalisa/octo-cat",
    71  			host:   "github.com",
    72  			err:    nil,
    73  		},
    74  		{
    75  			name:   "github.com HTTPS+SSH URL",
    76  			input:  "https+ssh://github.com/monalisa/octo-cat.git",
    77  			result: "monalisa/octo-cat",
    78  			host:   "github.com",
    79  			err:    nil,
    80  		},
    81  		{
    82  			name:   "github.com git URL",
    83  			input:  "git://github.com/monalisa/octo-cat.git",
    84  			result: "monalisa/octo-cat",
    85  			host:   "github.com",
    86  			err:    nil,
    87  		},
    88  	}
    89  
    90  	for _, tt := range tests {
    91  		t.Run(tt.name, func(t *testing.T) {
    92  			u, err := url.Parse(tt.input)
    93  			if err != nil {
    94  				t.Fatalf("got error %q", err)
    95  			}
    96  
    97  			repo, err := FromURL(u)
    98  			if err != nil {
    99  				if tt.err == nil {
   100  					t.Fatalf("got error %q", err)
   101  				} else if tt.err.Error() == err.Error() {
   102  					return
   103  				}
   104  				t.Fatalf("got error %q", err)
   105  			}
   106  
   107  			got := fmt.Sprintf("%s/%s", repo.RepoOwner(), repo.RepoName())
   108  			if tt.result != got {
   109  				t.Errorf("expected %q, got %q", tt.result, got)
   110  			}
   111  			if tt.host != repo.RepoHost() {
   112  				t.Errorf("expected %q, got %q", tt.host, repo.RepoHost())
   113  			}
   114  		})
   115  	}
   116  }
   117  
   118  func TestFromFullName(t *testing.T) {
   119  	tests := []struct {
   120  		name         string
   121  		input        string
   122  		hostOverride string
   123  		wantOwner    string
   124  		wantName     string
   125  		wantHost     string
   126  		wantErr      error
   127  	}{
   128  		{
   129  			name:      "OWNER/REPO combo",
   130  			input:     "OWNER/REPO",
   131  			wantHost:  "github.com",
   132  			wantOwner: "OWNER",
   133  			wantName:  "REPO",
   134  			wantErr:   nil,
   135  		},
   136  		{
   137  			name:    "too few elements",
   138  			input:   "OWNER",
   139  			wantErr: errors.New(`expected the "[HOST/]OWNER/REPO" format, got "OWNER"`),
   140  		},
   141  		{
   142  			name:    "too many elements",
   143  			input:   "a/b/c/d",
   144  			wantErr: errors.New(`expected the "[HOST/]OWNER/REPO" format, got "a/b/c/d"`),
   145  		},
   146  		{
   147  			name:    "blank value",
   148  			input:   "a/",
   149  			wantErr: errors.New(`expected the "[HOST/]OWNER/REPO" format, got "a/"`),
   150  		},
   151  		{
   152  			name:      "with hostname",
   153  			input:     "example.org/OWNER/REPO",
   154  			wantHost:  "example.org",
   155  			wantOwner: "OWNER",
   156  			wantName:  "REPO",
   157  			wantErr:   nil,
   158  		},
   159  		{
   160  			name:      "full URL",
   161  			input:     "https://example.org/OWNER/REPO.git",
   162  			wantHost:  "example.org",
   163  			wantOwner: "OWNER",
   164  			wantName:  "REPO",
   165  			wantErr:   nil,
   166  		},
   167  		{
   168  			name:      "SSH URL",
   169  			input:     "git@example.org:OWNER/REPO.git",
   170  			wantHost:  "example.org",
   171  			wantOwner: "OWNER",
   172  			wantName:  "REPO",
   173  			wantErr:   nil,
   174  		},
   175  		{
   176  			name:         "OWNER/REPO with default host override",
   177  			input:        "OWNER/REPO",
   178  			hostOverride: "override.com",
   179  			wantHost:     "override.com",
   180  			wantOwner:    "OWNER",
   181  			wantName:     "REPO",
   182  			wantErr:      nil,
   183  		},
   184  		{
   185  			name:         "HOST/OWNER/REPO with default host override",
   186  			input:        "example.com/OWNER/REPO",
   187  			hostOverride: "override.com",
   188  			wantHost:     "example.com",
   189  			wantOwner:    "OWNER",
   190  			wantName:     "REPO",
   191  			wantErr:      nil,
   192  		},
   193  	}
   194  	for _, tt := range tests {
   195  		t.Run(tt.name, func(t *testing.T) {
   196  			if tt.hostOverride != "" {
   197  				t.Setenv("GH_HOST", tt.hostOverride)
   198  			}
   199  			r, err := FromFullName(tt.input)
   200  			if tt.wantErr != nil {
   201  				if err == nil {
   202  					t.Fatalf("no error in result, expected %v", tt.wantErr)
   203  				} else if err.Error() != tt.wantErr.Error() {
   204  					t.Fatalf("expected error %q, got %q", tt.wantErr.Error(), err.Error())
   205  				}
   206  				return
   207  			}
   208  			if err != nil {
   209  				t.Fatalf("got error %v", err)
   210  			}
   211  			if r.RepoHost() != tt.wantHost {
   212  				t.Errorf("expected host %q, got %q", tt.wantHost, r.RepoHost())
   213  			}
   214  			if r.RepoOwner() != tt.wantOwner {
   215  				t.Errorf("expected owner %q, got %q", tt.wantOwner, r.RepoOwner())
   216  			}
   217  			if r.RepoName() != tt.wantName {
   218  				t.Errorf("expected name %q, got %q", tt.wantName, r.RepoName())
   219  			}
   220  		})
   221  	}
   222  }