File: remotes_controller_test.go

package info (click to toggle)
lazygit 0.57.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,748 kB
  • sloc: sh: 153; makefile: 76
file content (165 lines) | stat: -rw-r--r-- 3,760 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package controllers

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestReplaceForkUsername_SSH_OK(t *testing.T) {
	cases := []struct {
		name     string
		in       string
		forkUser string
		expected string
	}{
		{
			name:     "github ssh scp-like basic",
			in:       "git@github.com:old/repo.git",
			forkUser: "new",
			expected: "git@github.com:new/repo.git",
		},
		{
			name:     "ssh scp-like no .git",
			in:       "git@github.com:old/repo",
			forkUser: "new",
			expected: "git@github.com:new/repo",
		},
		{
			name:     "gitlab subgroup ssh scp-like",
			in:       "git@gitlab.com:group/sub/repo.git",
			forkUser: "alice",
			expected: "git@gitlab.com:alice/repo.git",
		},
		{
			name:     "ssh url style basic",
			in:       "ssh://git@github.com/old/repo.git",
			forkUser: "new",
			expected: "ssh://git@github.com/new/repo.git",
		},
		{
			name:     "ssh url style with port",
			in:       "ssh://git@github.com:2222/old/repo.git",
			forkUser: "bob",
			expected: "ssh://git@github.com:2222/bob/repo.git",
		},
		{
			name:     "ssh url style multi subgroup",
			in:       "ssh://git@gitlab.com/group/sub/repo.git",
			forkUser: "alice",
			expected: "ssh://git@gitlab.com/alice/repo.git",
		},
	}

	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got, err := replaceForkUsername(c.in, c.forkUser, false)
			assert.NoError(t, err)
			assert.Equal(t, c.expected, got)
		})
	}
}

func TestReplaceForkUsername_HTTPS_OK(t *testing.T) {
	cases := []struct {
		name     string
		in       string
		forkUser string
		expected string
	}{
		{
			name:     "github https basic",
			in:       "https://github.com/old/repo.git",
			forkUser: "new",
			expected: "https://github.com/new/repo.git",
		},
		{
			name:     "https no .git",
			in:       "https://github.com/old/repo",
			forkUser: "new",
			expected: "https://github.com/new/repo",
		},
		{
			name:     "https with port",
			in:       "https://git.example.com:8443/group/repo",
			forkUser: "me",
			expected: "https://git.example.com:8443/me/repo",
		},
		{
			name:     "gitlab multi subgroup https",
			in:       "https://gitlab.com/group/sub/sub2/repo",
			forkUser: "bob",
			expected: "https://gitlab.com/bob/repo",
		},
	}

	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got, err := replaceForkUsername(c.in, c.forkUser, false)
			assert.NoError(t, err)
			assert.Equal(t, c.expected, got)
		})
	}
}

func TestReplaceForkUsername_IntegrationTest_OK(t *testing.T) {
	got, err := replaceForkUsername("../origin", "bob", true)
	assert.NoError(t, err)
	assert.Equal(t, "../bob", got)
}

func TestReplaceForkUsername_Errors(t *testing.T) {
	cases := []struct {
		name     string
		in       string
		forkUser string
	}{
		{
			name:     "https host only",
			in:       "https://github.com",
			forkUser: "x",
		},
		{
			name:     "https host slash only",
			in:       "https://github.com/",
			forkUser: "x",
		},
		{
			name:     "https only repo (no owner)",
			in:       "https://github.com/repo.git",
			forkUser: "x",
		},
		{
			name:     "ssh missing path",
			in:       "git@github.com",
			forkUser: "x",
		},
		{
			name:     "ssh one segment only",
			in:       "git@github.com:repo.git",
			forkUser: "x",
		},
		{
			name:     "unsupported scheme",
			in:       "ftp://github.com/old/repo.git",
			forkUser: "x",
		},
		{
			name:     "empty url",
			in:       "",
			forkUser: "x",
		},
		{
			name:     "integration test URL outside of integration test",
			in:       "../origin",
			forkUser: "x",
		},
	}

	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			_, err := replaceForkUsername(c.in, c.forkUser, false)
			assert.EqualError(t, err, "unsupported or invalid remote URL: "+c.in)
		})
	}
}