github.com/x-motemen/ghq@v1.6.1/commands_test.go (about) 1 package main 2 3 import ( 4 "net/url" 5 "path/filepath" 6 "sync" 7 "testing" 8 ) 9 10 type _cloneArgs struct { 11 remote *url.URL 12 local string 13 shallow bool 14 branch string 15 recursive bool 16 bare bool 17 } 18 19 type _updateArgs struct { 20 local string 21 } 22 23 func withFakeGitBackend(t *testing.T, block func(*testing.T, string, *_cloneArgs, *_updateArgs)) { 24 tmpRoot := newTempDir(t) 25 26 defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots) 27 _localRepositoryRoots = []string{tmpRoot} 28 29 var cloneArgs _cloneArgs 30 var updateArgs _updateArgs 31 32 var originalGitBackend = GitBackend 33 tmpBackend := &VCSBackend{ 34 Clone: func(vg *vcsGetOption) error { 35 cloneArgs = _cloneArgs{ 36 remote: vg.url, 37 local: filepath.FromSlash(vg.dir), 38 shallow: vg.shallow, 39 branch: vg.branch, 40 recursive: vg.recursive, 41 bare: vg.bare, 42 } 43 return nil 44 }, 45 Update: func(vg *vcsGetOption) error { 46 updateArgs = _updateArgs{ 47 local: vg.dir, 48 } 49 return nil 50 }, 51 } 52 defer func(orig string) { _home = orig }(_home) 53 _home = "" 54 homeOnce = &sync.Once{} 55 56 GitBackend = tmpBackend 57 vcsContentsMap[".git"] = tmpBackend 58 defer func() { GitBackend = originalGitBackend; vcsContentsMap[".git"] = originalGitBackend }() 59 block(t, tmpRoot, &cloneArgs, &updateArgs) 60 }