github.com/chelnak/go-gh@v0.0.2/internal/git/remote_test.go (about)

     1  package git
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestRemotes(t *testing.T) {
    12  	tempDir := t.TempDir()
    13  	oldWd, _ := os.Getwd()
    14  	assert.NoError(t, os.Chdir(tempDir))
    15  	t.Cleanup(func() { _ = os.Chdir(oldWd) })
    16  	_, _, err := Exec("init", "--quiet")
    17  	assert.NoError(t, err)
    18  	gitDir := filepath.Join(tempDir, ".git")
    19  	remoteFile := filepath.Join(gitDir, "config")
    20  	remotes := `
    21  [remote "origin"]
    22  	url = git@example.com:monalisa/origin.git
    23  [remote "test"]
    24  	url = git://github.com/hubot/test.git
    25  	gh-resolved = other
    26  [remote "upstream"]
    27  	url = https://github.com/monalisa/upstream.git
    28  	gh-resolved = base
    29  [remote "github"]
    30  	url = git@github.com:hubot/github.git
    31  `
    32  	f, err := os.OpenFile(remoteFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0755)
    33  	assert.NoError(t, err)
    34  	_, err = f.Write([]byte(remotes))
    35  	assert.NoError(t, err)
    36  	err = f.Close()
    37  	assert.NoError(t, err)
    38  	rs, err := Remotes()
    39  	assert.NoError(t, err)
    40  	assert.Equal(t, 4, len(rs))
    41  	assert.Equal(t, "upstream", rs[0].Name)
    42  	assert.Equal(t, "base", rs[0].Resolved)
    43  	assert.Equal(t, "github", rs[1].Name)
    44  	assert.Equal(t, "", rs[1].Resolved)
    45  	assert.Equal(t, "origin", rs[2].Name)
    46  	assert.Equal(t, "", rs[2].Resolved)
    47  	assert.Equal(t, "test", rs[3].Name)
    48  	assert.Equal(t, "other", rs[3].Resolved)
    49  }
    50  
    51  func TestParseRemotes(t *testing.T) {
    52  	remoteList := []string{
    53  		"mona\tgit@github.com:monalisa/myfork.git (fetch)",
    54  		"origin\thttps://github.com/monalisa/octo-cat.git (fetch)",
    55  		"origin\thttps://github.com/monalisa/octo-cat-push.git (push)",
    56  		"upstream\thttps://example.com/nowhere.git (fetch)",
    57  		"upstream\thttps://github.com/hubot/tools (push)",
    58  		"zardoz\thttps://example.com/zed.git (push)",
    59  		"koke\tgit://github.com/koke/grit.git (fetch)",
    60  		"koke\tgit://github.com/koke/grit.git (push)",
    61  	}
    62  
    63  	r := parseRemotes(remoteList)
    64  	assert.Equal(t, 5, len(r))
    65  
    66  	assert.Equal(t, "mona", r[0].Name)
    67  	assert.Equal(t, "ssh://git@github.com/monalisa/myfork.git", r[0].FetchURL.String())
    68  	assert.Nil(t, r[0].PushURL)
    69  	assert.Equal(t, "github.com", r[0].Host)
    70  	assert.Equal(t, "monalisa", r[0].Owner)
    71  	assert.Equal(t, "myfork", r[0].Repo)
    72  
    73  	assert.Equal(t, "origin", r[1].Name)
    74  	assert.Equal(t, "/monalisa/octo-cat.git", r[1].FetchURL.Path)
    75  	assert.Equal(t, "/monalisa/octo-cat-push.git", r[1].PushURL.Path)
    76  	assert.Equal(t, "github.com", r[1].Host)
    77  	assert.Equal(t, "monalisa", r[1].Owner)
    78  	assert.Equal(t, "octo-cat", r[1].Repo)
    79  
    80  	assert.Equal(t, "upstream", r[2].Name)
    81  	assert.Equal(t, "example.com", r[2].FetchURL.Host)
    82  	assert.Equal(t, "github.com", r[2].PushURL.Host)
    83  	assert.Equal(t, "github.com", r[2].Host)
    84  	assert.Equal(t, "hubot", r[2].Owner)
    85  	assert.Equal(t, "tools", r[2].Repo)
    86  
    87  	assert.Equal(t, "zardoz", r[3].Name)
    88  	assert.Nil(t, r[3].FetchURL)
    89  	assert.Equal(t, "https://example.com/zed.git", r[3].PushURL.String())
    90  	assert.Equal(t, "", r[3].Host)
    91  	assert.Equal(t, "", r[3].Owner)
    92  	assert.Equal(t, "", r[3].Repo)
    93  
    94  	assert.Equal(t, "koke", r[4].Name)
    95  	assert.Equal(t, "/koke/grit.git", r[4].FetchURL.Path)
    96  	assert.Equal(t, "/koke/grit.git", r[4].PushURL.Path)
    97  	assert.Equal(t, "github.com", r[4].Host)
    98  	assert.Equal(t, "koke", r[4].Owner)
    99  	assert.Equal(t, "grit", r[4].Repo)
   100  }