@@ -4,13 +4,12 @@ import (
44 "context"
55 "crypto/ed25519"
66 "encoding/base64"
7- "fmt"
87 "io"
98 "net/http/httptest"
109 "net/url"
1110 "os"
1211 "path/filepath"
13- "regexp "
12+ "strings "
1413 "testing"
1514
1615 "github.com/coder/envbuilder/git"
@@ -76,6 +75,18 @@ func TestCloneRepo(t *testing.T) {
7675 password : "" ,
7776 expectClone : true ,
7877 },
78+ {
79+ name : "whitespace in URL" ,
80+ mungeURL : func (u * string ) {
81+ if u == nil {
82+ t .Errorf ("expected non-nil URL" )
83+ return
84+ }
85+ * u = " " + * u + " "
86+ t .Logf ("munged URL: %q" , * u )
87+ },
88+ expectClone : true ,
89+ },
7990 } {
8091 tc := tc
8192 t .Run (tc .name , func (t * testing.T ) {
@@ -87,6 +98,9 @@ func TestCloneRepo(t *testing.T) {
8798 _ = gittest .NewRepo (t , srvFS , gittest .Commit (t , "README.md" , "Hello, world!" , "Wow!" ))
8899 authMW := mwtest .BasicAuthMW (tc .srvUsername , tc .srvPassword )
89100 srv := httptest .NewServer (authMW (gittest .NewServer (srvFS )))
101+ if tc .mungeURL != nil {
102+ tc .mungeURL (& srv .URL )
103+ }
90104 clientFS := memfs .New ()
91105 // A repo already exists!
92106 _ = gittest .NewRepo (t , clientFS )
@@ -106,6 +120,9 @@ func TestCloneRepo(t *testing.T) {
106120 _ = gittest .NewRepo (t , srvFS , gittest .Commit (t , "README.md" , "Hello, world!" , "Wow!" ))
107121 authMW := mwtest .BasicAuthMW (tc .srvUsername , tc .srvPassword )
108122 srv := httptest .NewServer (authMW (gittest .NewServer (srvFS )))
123+ if tc .mungeURL != nil {
124+ tc .mungeURL (& srv .URL )
125+ }
109126 clientFS := memfs .New ()
110127
111128 cloned , err := git .CloneRepo (context .Background (), t .Logf , git.CloneRepoOptions {
@@ -129,7 +146,7 @@ func TestCloneRepo(t *testing.T) {
129146 require .Equal (t , "Hello, world!" , readme )
130147 gitConfig := mustRead (t , clientFS , "/workspace/.git/config" )
131148 // Ensure we do not modify the git URL that folks pass in.
132- require .Regexp (t , fmt . Sprintf ( `(?m)^\s+url\s+=\s+%s\s*$` , regexp . QuoteMeta (srv .URL )), gitConfig )
149+ require .Contains (t , gitConfig , strings . TrimSpace (srv .URL ))
133150 })
134151
135152 // In-URL-style auth e.g. http://user:password@host:port
@@ -139,15 +156,19 @@ func TestCloneRepo(t *testing.T) {
139156 _ = gittest .NewRepo (t , srvFS , gittest .Commit (t , "README.md" , "Hello, world!" , "Wow!" ))
140157 authMW := mwtest .BasicAuthMW (tc .srvUsername , tc .srvPassword )
141158 srv := httptest .NewServer (authMW (gittest .NewServer (srvFS )))
142-
143159 authURL , err := url .Parse (srv .URL )
144160 require .NoError (t , err )
145161 authURL .User = url .UserPassword (tc .username , tc .password )
162+ cloneURL := authURL .String ()
163+ if tc .mungeURL != nil {
164+ tc .mungeURL (& cloneURL )
165+ }
166+
146167 clientFS := memfs .New ()
147168
148169 cloned , err := git .CloneRepo (context .Background (), t .Logf , git.CloneRepoOptions {
149170 Path : "/workspace" ,
150- RepoURL : authURL . String () ,
171+ RepoURL : cloneURL ,
151172 Storage : clientFS ,
152173 })
153174 require .Equal (t , tc .expectClone , cloned )
@@ -162,7 +183,7 @@ func TestCloneRepo(t *testing.T) {
162183 require .Equal (t , "Hello, world!" , readme )
163184 gitConfig := mustRead (t , clientFS , "/workspace/.git/config" )
164185 // Ensure we do not modify the git URL that folks pass in.
165- require .Regexp (t , fmt . Sprintf ( `(?m)^\s+url\s+=\s+%s\s*$` , regexp . QuoteMeta ( authURL . String ())), gitConfig )
186+ require .Contains (t , gitConfig , strings . TrimSpace ( cloneURL ) )
166187 })
167188 })
168189 }
@@ -238,10 +259,9 @@ func TestShallowCloneRepo(t *testing.T) {
238259func TestCloneRepoSSH (t * testing.T ) {
239260 t .Parallel ()
240261
241- t .Run ("AuthSuccess " , func (t * testing.T ) {
262+ t .Run ("Success " , func (t * testing.T ) {
242263 t .Parallel ()
243264
244- // TODO: test the rest of the cloning flow. This just tests successful auth.
245265 tmpDir := t .TempDir ()
246266 srvFS := osfs .New (tmpDir , osfs .WithChrootOS ())
247267
@@ -264,10 +284,9 @@ func TestCloneRepoSSH(t *testing.T) {
264284 },
265285 },
266286 })
267- // TODO: ideally, we want to test the entire cloning flow.
268- // For now, this indicates successful ssh key auth.
269- require .ErrorContains (t , err , "repository not found" )
270- require .False (t , cloned )
287+ require .NoError (t , err )
288+ require .True (t , cloned )
289+ require .Equal (t , "Hello, world!" , mustRead (t , clientFS , "/workspace/README.md" ))
271290 })
272291
273292 t .Run ("AuthFailure" , func (t * testing.T ) {
@@ -399,12 +418,12 @@ func TestSetupRepoAuth(t *testing.T) {
399418 // Anything that is not https:// or http:// is treated as SSH.
400419 kPath := writeTestPrivateKey (t )
401420 opts := & options.Options {
402- GitURL : "git://git@host.tld:repo /path" ,
421+ GitURL : "git://git@host.tld:12345 /path" ,
403422 GitSSHPrivateKeyPath : kPath ,
404423 }
405424 auth := git .SetupRepoAuth (t .Logf , opts )
406425 _ , ok := auth .(* gitssh.PublicKeys )
407- require .True (t , ok )
426+ require .True (t , ok , "expected SSH auth for git:// URL" )
408427 })
409428
410429 t .Run ("SSH/GitUsername" , func (t * testing.T ) {
@@ -422,7 +441,7 @@ func TestSetupRepoAuth(t *testing.T) {
422441 t .Run ("SSH/PrivateKey" , func (t * testing.T ) {
423442 kPath := writeTestPrivateKey (t )
424443 opts := & options.Options {
425- GitURL : "ssh://git@host.tld: repo/path" ,
444+ GitURL : "ssh://git@host.tld/ repo/path" ,
426445 GitSSHPrivateKeyPath : kPath ,
427446 }
428447 auth := git .SetupRepoAuth (t .Logf , opts )
@@ -436,7 +455,7 @@ func TestSetupRepoAuth(t *testing.T) {
436455
437456 t .Run ("SSH/Base64PrivateKey" , func (t * testing.T ) {
438457 opts := & options.Options {
439- GitURL : "ssh://git@host.tld: repo/path" ,
458+ GitURL : "ssh://git@host.tld/ repo/path" ,
440459 GitSSHPrivateKeyBase64 : base64EncodeTestPrivateKey (),
441460 }
442461 auth := git .SetupRepoAuth (t .Logf , opts )
@@ -452,7 +471,7 @@ func TestSetupRepoAuth(t *testing.T) {
452471
453472 t .Run ("SSH/NoAuthMethods" , func (t * testing.T ) {
454473 opts := & options.Options {
455- GitURL : "ssh:// git@host.tld:repo/path" ,
474+ GitURL : "git@host.tld:repo/path" ,
456475 }
457476 auth := git .SetupRepoAuth (t .Logf , opts )
458477 require .Nil (t , auth ) // TODO: actually test SSH_AUTH_SOCK
@@ -481,6 +500,28 @@ func TestSetupRepoAuth(t *testing.T) {
481500 auth := git .SetupRepoAuth (t .Logf , opts )
482501 require .Nil (t , auth )
483502 })
503+
504+ t .Run ("Whitespace" , func (t * testing.T ) {
505+ kPath := writeTestPrivateKey (t )
506+ opts := & options.Options {
507+ GitURL : "ssh://git@host.tld/repo path" ,
508+ GitSSHPrivateKeyPath : kPath ,
509+ }
510+ auth := git .SetupRepoAuth (t .Logf , opts )
511+ _ , ok := auth .(* gitssh.PublicKeys )
512+ require .True (t , ok )
513+ })
514+
515+ t .Run ("LeadingTrailingWhitespace" , func (t * testing.T ) {
516+ kPath := writeTestPrivateKey (t )
517+ opts := & options.Options {
518+ GitURL : " ssh://git@host.tld/repo/path " ,
519+ GitSSHPrivateKeyPath : kPath ,
520+ }
521+ auth := git .SetupRepoAuth (t .Logf , opts )
522+ _ , ok := auth .(* gitssh.PublicKeys )
523+ require .True (t , ok )
524+ })
484525}
485526
486527func mustRead (t * testing.T , fs billy.Filesystem , path string ) string {
0 commit comments