github.com/motemen/ghq@v1.0.3/url_test.go (about)

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