github.com/x-motemen/ghq@v1.6.1/url_test.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os/exec"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/Songmu/gitconfig"
    11  )
    12  
    13  func TestNewURL(t *testing.T) {
    14  	testCases := []struct {
    15  		name, url, expect, host string
    16  		setup                   func(t *testing.T)
    17  	}{{
    18  		name:   "https", // Does nothing when the URL has scheme part
    19  		url:    "https://github.com/motemen/pusheen-explorer",
    20  		expect: "https://github.com/motemen/pusheen-explorer",
    21  		host:   "github.com",
    22  	}, {
    23  		name:   "scp", // Convert SCP-like URL to SSH URL
    24  		url:    "git@github.com:motemen/pusheen-explorer.git",
    25  		expect: "ssh://git@github.com/motemen/pusheen-explorer.git",
    26  		host:   "github.com",
    27  	}, {
    28  		name:   "scp with root",
    29  		url:    "git@github.com:/motemen/pusheen-explorer.git",
    30  		expect: "ssh://git@github.com/motemen/pusheen-explorer.git",
    31  		host:   "github.com",
    32  	}, {
    33  		name:   "scp without user",
    34  		url:    "github.com:motemen/pusheen-explorer.git",
    35  		expect: "ssh://github.com/motemen/pusheen-explorer.git",
    36  		host:   "github.com",
    37  	}, {
    38  		name:   "different name repository",
    39  		url:    "motemen/ghq",
    40  		expect: "https://github.com/motemen/ghq",
    41  		host:   "github.com",
    42  	}, {
    43  		name:   "with authority repository",
    44  		url:    "github.com/motemen/gore",
    45  		expect: "https://github.com/motemen/gore",
    46  		host:   "github.com",
    47  	}, {
    48  		name:   "with authority repository and go-import",
    49  		url:    "golang.org/x/crypto",
    50  		expect: "https://golang.org/x/crypto",
    51  		host:   "golang.org",
    52  	}, {
    53  		name: "fill username",
    54  		setup: func(t *testing.T) {
    55  			setEnv(t, "GITHUB_USER", "ghq-test")
    56  		},
    57  		url:    "same-name-ghq",
    58  		expect: "https://github.com/ghq-test/same-name-ghq",
    59  		host:   "github.com",
    60  	}, {
    61  		name: "same name repository",
    62  		setup: func(t *testing.T) {
    63  			t.Cleanup(gitconfig.WithConfig(t, `[ghq]
    64  completeUser = false`))
    65  		},
    66  		url:    "peco",
    67  		expect: "https://github.com/peco/peco",
    68  		host:   "github.com",
    69  	}}
    70  
    71  	for _, tc := range testCases {
    72  		t.Run(tc.name, func(t *testing.T) {
    73  			if tc.setup != nil {
    74  				tc.setup(t)
    75  			}
    76  			repo, err := newURL(tc.url, false, false)
    77  			if err != nil {
    78  				t.Errorf("error should be nil but: %s", err)
    79  			}
    80  			if repo.String() != tc.expect {
    81  				t.Errorf("url: got: %s, expect: %s", repo.String(), tc.expect)
    82  			}
    83  			if repo.Host != tc.host {
    84  				t.Errorf("host: got: %s, expect: %s", repo.Host, tc.host)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestConvertGitURLHTTPToSSH(t *testing.T) {
    91  	testCases := []struct {
    92  		url, expect string
    93  	}{{
    94  		url:    "https://github.com/motemen/pusheen-explorer",
    95  		expect: "ssh://git@github.com/motemen/pusheen-explorer",
    96  	}, {
    97  		url:    "https://ghe.example.com/motemen/pusheen-explorer",
    98  		expect: "ssh://git@ghe.example.com/motemen/pusheen-explorer",
    99  	}, {
   100  		url:    "https://motemen@ghe.example.com/motemen/pusheen-explorer",
   101  		expect: "ssh://motemen@ghe.example.com/motemen/pusheen-explorer",
   102  	}}
   103  
   104  	for _, tc := range testCases {
   105  		t.Run(tc.url, func(t *testing.T) {
   106  			httpsURL, err := newURL(tc.url, false, false)
   107  			if err != nil {
   108  				t.Errorf("error should be nil but: %s", err)
   109  			}
   110  			sshURL, err := convertGitURLHTTPToSSH(httpsURL)
   111  			if err != nil {
   112  				t.Errorf("error should be nil but: %s", err)
   113  			}
   114  			if sshURL.String() != tc.expect {
   115  				t.Errorf("got: %s, expect: %s", sshURL.String(), tc.expect)
   116  			}
   117  		})
   118  	}
   119  }
   120  
   121  func TestNewURL_err(t *testing.T) {
   122  	invalidURL := "http://foo.com/?foo\nbar"
   123  	_, err := newURL(invalidURL, false, false)
   124  	const wantSub = "net/url: invalid control character in URL"
   125  	if got := fmt.Sprint(err); !strings.Contains(got, wantSub) {
   126  		t.Errorf("newURL(%q, false, false) error = %q; want substring %q", invalidURL, got, wantSub)
   127  	}
   128  	t.Cleanup(gitconfig.WithConfig(t, `[[[`))
   129  
   130  	var exitError *exec.ExitError
   131  	_, err = newURL("peco", false, false)
   132  	if !errors.As(err, &exitError) {
   133  		t.Errorf("error should be occurred but nil")
   134  	}
   135  }
   136  
   137  func TestFillUsernameToPath_err(t *testing.T) {
   138  	for _, envStr := range []string{"GITHUB_USER", "GITHUB_TOKEN", "USER", "USERNAME"} {
   139  		setEnv(t, envStr, "")
   140  	}
   141  	setEnv(t, "XDG_CONFIG_HOME", "/dummy/dummy")
   142  
   143  	usr, err := fillUsernameToPath("peco", false)
   144  	t.Log(usr)
   145  	const wantSub = "set ghq.user to your gitconfig"
   146  	if got := fmt.Sprint(err); !strings.Contains(got, wantSub) {
   147  		t.Errorf("fillUsernameToPath(peco, false) error = %q; want substring %q", got, wantSub)
   148  	}
   149  }