github.com/kazu/ghq@v0.8.1-0.20180818162325-dedd532b4440/url_test.go (about)

     1  package main
     2  
     3  import (
     4  	. "github.com/onsi/gomega"
     5  	"net/url"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  func TestNewURL(t *testing.T) {
    11  	RegisterTestingT(t)
    12  
    13  	// Does nothing whent the URL has scheme part
    14  	httpsUrl, err := NewURL("https://github.com/motemen/pusheen-explorer")
    15  	Expect(httpsUrl.String()).To(Equal("https://github.com/motemen/pusheen-explorer"))
    16  	Expect(httpsUrl.Host).To(Equal("github.com"))
    17  	Expect(err).To(BeNil())
    18  
    19  	// Convert SCP-like URL to SSH URL
    20  	scpUrl, err := NewURL("git@github.com:motemen/pusheen-explorer.git")
    21  	Expect(scpUrl.String()).To(Equal("ssh://git@github.com/motemen/pusheen-explorer.git"))
    22  	Expect(scpUrl.Host).To(Equal("github.com"))
    23  	Expect(err).To(BeNil())
    24  
    25  	scpUrlWithRoot, err := NewURL("git@github.com:/motemen/pusheen-explorer.git")
    26  	Expect(scpUrlWithRoot.String()).To(Equal("ssh://git@github.com/motemen/pusheen-explorer.git"))
    27  	Expect(scpUrlWithRoot.Host).To(Equal("github.com"))
    28  	Expect(err).To(BeNil())
    29  
    30  	scpUrlWithoutUser, err := NewURL("github.com:motemen/pusheen-explorer.git")
    31  	Expect(scpUrlWithoutUser.String()).To(Equal("ssh://github.com/motemen/pusheen-explorer.git"))
    32  	Expect(scpUrlWithoutUser.Host).To(Equal("github.com"))
    33  	Expect(err).To(BeNil())
    34  
    35  	differentNameRepository, err := NewURL("motemen/ghq")
    36  	Expect(differentNameRepository.String()).To(Equal("https://github.com/motemen/ghq"))
    37  	Expect(differentNameRepository.Host).To(Equal("github.com"))
    38  	Expect(err).To(BeNil())
    39  
    40  	os.Setenv("GITHUB_USER", "ghq-test")
    41  	sameNameRepository, err := NewURL("same-name-ghq")
    42  	Expect(sameNameRepository.String()).To(Equal("https://github.com/ghq-test/same-name-ghq"))
    43  	Expect(sameNameRepository.Host).To(Equal("github.com"))
    44  	Expect(err).To(BeNil())
    45  }
    46  
    47  func TestConvertGitURLHTTPToSSH(t *testing.T) {
    48  	RegisterTestingT(t)
    49  
    50  	var (
    51  		httpsURL, sshURL *url.URL
    52  		err              error
    53  	)
    54  
    55  	httpsURL, err = NewURL("https://github.com/motemen/pusheen-explorer")
    56  	sshURL, err = ConvertGitURLHTTPToSSH(httpsURL)
    57  	Expect(err).To(BeNil())
    58  	Expect(sshURL.String()).To(Equal("ssh://git@github.com/motemen/pusheen-explorer"))
    59  
    60  	httpsURL, err = NewURL("https://ghe.example.com/motemen/pusheen-explorer")
    61  	sshURL, err = ConvertGitURLHTTPToSSH(httpsURL)
    62  	Expect(err).To(BeNil())
    63  	Expect(sshURL.String()).To(Equal("ssh://git@ghe.example.com/motemen/pusheen-explorer"))
    64  }