github.com/motemen/ghq@v1.0.3/commands_test.go (about) 1 package main 2 3 import ( 4 "net/url" 5 "os" 6 "path/filepath" 7 "sync" 8 "testing" 9 ) 10 11 type _cloneArgs struct { 12 remote *url.URL 13 local string 14 shallow bool 15 branch string 16 recursive 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 defer os.RemoveAll(tmpRoot) 26 27 defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots) 28 _localRepositoryRoots = []string{tmpRoot} 29 30 var cloneArgs _cloneArgs 31 var updateArgs _updateArgs 32 33 var originalGitBackend = GitBackend 34 tmpBackend := &VCSBackend{ 35 Clone: func(vg *vcsGetOption) error { 36 cloneArgs = _cloneArgs{ 37 remote: vg.url, 38 local: filepath.FromSlash(vg.dir), 39 shallow: vg.shallow, 40 branch: vg.branch, 41 recursive: vg.recursive, 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 }