github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/pkg/git/git_test.go (about) 1 package git 2 3 import ( 4 "errors" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "reflect" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestRepositoryURL(t *testing.T) { 15 t.Parallel() 16 17 actual := RepositoryURL("drycc.example.com", "app") 18 assert.Equal(t, actual, "ssh://git@drycc-builder.example.com:2222/app.git", "url") 19 actual = RepositoryURL("drycc.10.245.1.3.xip.io:31350", "velcro-underdog") 20 assert.Equal(t, actual, "ssh://git@drycc-builder.10.245.1.3.xip.io:2222/velcro-underdog.git", "url") 21 } 22 23 func TestGetRemotes(t *testing.T) { 24 t.Parallel() 25 26 expected := []remote{ 27 {"test", "ssh://test.com/test.git"}, 28 {"example", "ssh://example.com:2222/example.git"}, 29 } 30 31 actual, err := getRemotes(func(cmd []string) (string, error) { 32 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 33 return `test ssh://test.com/test.git (fetch) 34 test ssh://test.com/test.git (push) 35 example ssh://example.com:2222/example.git (fetch) 36 example ssh://example.com:2222/example.git (push) 37 `, nil 38 }) 39 40 assert.NoError(t, err) 41 assert.Equal(t, actual, expected, "remotes") 42 43 _, err = getRemotes(func(cmd []string) (string, error) { 44 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 45 return `fakeoutput(push) 46 `, nil 47 }) 48 49 assert.Error(t, err, ErrInvalidRepositoryList) 50 51 testErr := errors.New("test error") 52 53 _, err = getRemotes(func(cmd []string) (string, error) { 54 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 55 return "", testErr 56 }) 57 58 assert.Error(t, err, testErr) 59 } 60 61 func TestRemoteURL(t *testing.T) { 62 t.Parallel() 63 64 url, err := RemoteURL(func(cmd []string) (string, error) { 65 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 66 return `test ssh://test.com/test.git (fetch) 67 test ssh://test.com/test.git (push) 68 example ssh://example.com:2222/example.git (fetch) 69 example ssh://example.com:2222/example.git (push) 70 `, nil 71 }, "test") 72 73 assert.NoError(t, err) 74 assert.Equal(t, url, "ssh://test.com/test.git", "remote url") 75 76 _, err = RemoteURL(func(cmd []string) (string, error) { 77 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 78 return `test ssh://test.com/test.git (fetch) 79 test ssh://test.com/test.git (push) 80 example ssh://example.com:2222/example.git (fetch) 81 example ssh://example.com:2222/example.git (push) 82 `, nil 83 }, "foo") 84 85 assert.Error(t, err, ErrRemoteNotFound) 86 87 testErr := errors.New("test error") 88 89 _, err = RemoteURL(func(cmd []string) (string, error) { 90 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 91 return "", testErr 92 }, "test") 93 94 assert.Error(t, err, testErr) 95 } 96 97 func TestFindRemoteURL(t *testing.T) { 98 t.Parallel() 99 100 url, err := findRemote(func(cmd []string) (string, error) { 101 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 102 return `test ssh://test.com/test.git (fetch) 103 test ssh://test.com/test.git (push) 104 drycc ssh://git@drycc-builder.example.com:2222/test.git (fetch) 105 drycc ssh://git@drycc-builder.example.com:2222/test.git (push) 106 `, nil 107 }, "drycc.example.com") 108 109 assert.NoError(t, err) 110 assert.Equal(t, url, "ssh://git@drycc-builder.example.com:2222/test.git", "remote url") 111 112 _, err = findRemote(func(cmd []string) (string, error) { 113 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 114 return `test ssh://test.com/test.git (fetch) 115 test ssh://test.com/test.git (push) 116 example ssh://example.com:2222/example.git (fetch) 117 example ssh://example.com:2222/example.git (push) 118 `, nil 119 }, "drycc.test.com") 120 121 assert.Error(t, err, ErrRemoteNotFound) 122 123 testErr := errors.New("test error") 124 125 _, err = findRemote(func(cmd []string) (string, error) { 126 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 127 return "", testErr 128 }, "drycc.test.com") 129 130 assert.Error(t, err, testErr) 131 } 132 133 func TestDetectAppName(t *testing.T) { 134 t.Parallel() 135 136 app, err := DetectAppName(func(cmd []string) (string, error) { 137 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 138 return `drycc ssh://git@drycc-builder.example.com:2222/test.git (fetch) 139 drycc ssh://git@drycc-builder.example.com:2222/test.git (push) 140 `, nil 141 }, "drycc.example.com") 142 143 assert.NoError(t, err) 144 assert.Equal(t, app, "test", "app") 145 146 app, err = DetectAppName(func(cmd []string) (string, error) { 147 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 148 return "", errors.New("test error") 149 }, "drycc.test.com") 150 assert.NoError(t, err) 151 wd, err := os.Getwd() 152 assert.NoError(t, err) 153 assert.Equal(t, app, filepath.Base(wd), "app") 154 } 155 156 func TestRemoteNamesFromAppID(t *testing.T) { 157 t.Parallel() 158 159 apps, err := remoteNamesFromAppID(func(cmd []string) (string, error) { 160 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 161 return `test ssh://test.com/test.git (fetch) 162 test ssh://test.com/test.git (push) 163 drycc ssh://git@drycc-builder.example.com:2222/test.git (fetch) 164 drycc ssh://git@drycc-builder.example.com:2222/test.git (push) 165 two ssh://git@drycc-builder.example.com:2222/test.git (fetch) 166 two ssh://git@drycc-builder.example.com:2222/test.git (push) 167 `, nil 168 }, "drycc.example.com", "test") 169 170 assert.NoError(t, err) 171 assert.Equal(t, apps, []string{"drycc", "two"}, "remote url") 172 173 _, err = remoteNamesFromAppID(func(cmd []string) (string, error) { 174 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 175 return `test ssh://test.com/test.git (fetch) 176 test ssh://test.com/test.git (push) 177 drycc ssh://git@drycc-builder.example.com:2222/test.git (fetch) 178 drycc ssh://git@drycc-builder.example.com:2222/test.git (push) 179 two ssh://git@drycc-builder.example.com:2222/test.git (fetch) 180 two ssh://git@drycc-builder.example.com:2222/test.git (push) 181 `, nil 182 }, "drycc.test.com", "other") 183 184 assert.Error(t, err, ErrRemoteNotFound) 185 186 testErr := errors.New("test error") 187 188 _, err = remoteNamesFromAppID(func(cmd []string) (string, error) { 189 assert.Equal(t, cmd, []string{"remote", "-v"}, "args") 190 return "", testErr 191 }, "drycc.test.com", "test") 192 193 assert.Error(t, err, testErr) 194 } 195 196 func TestDeleteRemote(t *testing.T) { 197 t.Parallel() 198 199 err := DeleteRemote(func(cmd []string) (string, error) { 200 assert.Equal(t, cmd, []string{"remote", "remove", "test"}, "args") 201 return "", nil 202 }, "test") 203 assert.NoError(t, err) 204 } 205 206 func TestDeleteAppRemotes(t *testing.T) { 207 t.Parallel() 208 209 err := DeleteAppRemotes(func(cmd []string) (string, error) { 210 if reflect.DeepEqual(cmd, []string{"remote", "-v"}) { 211 return `drycc ssh://git@drycc-builder.example.com:2222/test.git (push) 212 `, nil 213 } else if reflect.DeepEqual(cmd, []string{"remote", "remove", "drycc"}) { 214 return "", nil 215 } 216 t.Errorf("unexpected command %v", cmd) 217 return "", nil 218 }, "drycc.example.com", "test") 219 220 assert.NoError(t, err) 221 222 testErr := errors.New("test error") 223 224 err = DeleteAppRemotes(func([]string) (string, error) { 225 return "", testErr 226 }, "drycc.example.com", "test") 227 228 assert.Error(t, testErr, err) 229 230 err = DeleteAppRemotes(func(cmd []string) (string, error) { 231 if reflect.DeepEqual(cmd, []string{"remote", "-v"}) { 232 return `drycc ssh://git@drycc-builder.example.com:2222/test.git (push) 233 `, nil 234 } else if reflect.DeepEqual(cmd, []string{"remote", "remove", "drycc"}) { 235 return "", testErr 236 } 237 t.Errorf("unexpected command %v", cmd) 238 return "", nil 239 }, "drycc.example.com", "test") 240 241 assert.Error(t, testErr, err) 242 } 243 244 func TestInit(t *testing.T) { 245 t.Parallel() 246 247 err := Init(func(cmd []string) (string, error) { 248 assert.Equal(t, cmd, []string{"init"}, "args") 249 return "", nil 250 }) 251 assert.NoError(t, err) 252 } 253 254 func TestCreateRemote(t *testing.T) { 255 t.Parallel() 256 257 err := CreateRemote(func(cmd []string) (string, error) { 258 assert.Equal(t, cmd, []string{"remote", "add", "drycc", "ssh://git@drycc-builder.example.com:2222/testing.git"}, "args") 259 return "", nil 260 }, "drycc.example.com", "drycc", "testing") 261 assert.NoError(t, err) 262 } 263 264 func TestGitError(t *testing.T) { 265 t.Parallel() 266 267 exitErr := exec.ExitError{Stderr: []byte("fake error")} 268 assert.Equal(t, gitError(&exitErr, []string{"fake"}).Error(), `Error when running 'git fake' 269 fake error`, "error") 270 }