github.com/motemen/ghq@v1.0.3/cmd_create_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "os/exec" 6 "path/filepath" 7 "reflect" 8 "runtime" 9 "strings" 10 "sync" 11 "testing" 12 13 "github.com/motemen/ghq/cmdutil" 14 ) 15 16 func TestDoCreate(t *testing.T) { 17 defer func(orig func(cmd *exec.Cmd) error) { 18 cmdutil.CommandRunner = orig 19 }(cmdutil.CommandRunner) 20 var lastCmd *exec.Cmd 21 cmdutil.CommandRunner = func(cmd *exec.Cmd) error { 22 lastCmd = cmd 23 return nil 24 } 25 defer func(orig string) { _home = orig }(_home) 26 _home = "" 27 homeOnce = &sync.Once{} 28 tmpd := newTempDir(t) 29 defer os.RemoveAll(tmpd) 30 defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots) 31 defer tmpEnv(envGhqRoot, tmpd)() 32 _localRepositoryRoots = nil 33 localRepoOnce = &sync.Once{} 34 35 testCases := []struct { 36 name string 37 input []string 38 want []string 39 wantDir string 40 errStr string 41 setup func() func() 42 skipOnWin bool 43 }{{ 44 name: "simple", 45 input: []string{"create", "motemen/ghqq"}, 46 want: []string{"git", "init"}, 47 wantDir: filepath.Join(tmpd, "github.com/motemen/ghqq"), 48 }, { 49 name: "empty directory exists", 50 input: []string{"create", "motemen/ghqqq"}, 51 want: []string{"git", "init"}, 52 setup: func() func() { 53 os.MkdirAll(filepath.Join(tmpd, "github.com/motemen/ghqqq"), 0755) 54 return func() {} 55 }, 56 wantDir: filepath.Join(tmpd, "github.com/motemen/ghqqq"), 57 }, { 58 name: "Mercurial", 59 input: []string{"create", "--vcs=hg", "motemen/ghq-hg"}, 60 want: []string{"hg", "init"}, 61 wantDir: filepath.Join(tmpd, "github.com/motemen/ghq-hg"), 62 }, { 63 name: "Darcs", 64 input: []string{"create", "--vcs=darcs", "motemen/ghq-darcs"}, 65 want: []string{"darcs", "init"}, 66 wantDir: filepath.Join(tmpd, "github.com/motemen/ghq-darcs"), 67 }, { 68 name: "Bazzar", 69 input: []string{"create", "--vcs=bzr", "motemen/ghq-bzr"}, 70 want: []string{"bzr", "init"}, 71 wantDir: filepath.Join(tmpd, "github.com/motemen/ghq-bzr"), 72 }, { 73 name: "Fossil", 74 input: []string{"create", "--vcs=fossil", "motemen/ghq-fossil"}, 75 want: []string{"fossil", "open", fossilRepoName}, 76 wantDir: filepath.Join(tmpd, "github.com/motemen/ghq-fossil"), 77 }, { 78 name: "unsupported VCS", 79 input: []string{"create", "--vcs=svn", "motemen/ghq-svn"}, 80 errStr: "unsupported VCS", 81 }, { 82 name: "not permitted", 83 input: []string{"create", "motemen/ghq-notpermitted"}, 84 setup: func() func() { 85 f := filepath.Join(tmpd, "github.com/motemen/ghq-notpermitted") 86 os.MkdirAll(f, 0) 87 return func() { 88 os.Chmod(f, 0755) 89 } 90 }, 91 errStr: "permission denied", 92 skipOnWin: true, 93 }, { 94 name: "not empty", 95 input: []string{"create", "motemen/ghq-notempty"}, 96 setup: func() func() { 97 f := filepath.Join(tmpd, "github.com/motemen/ghq-notempty", "dummy") 98 os.MkdirAll(f, 0755) 99 return func() {} 100 }, 101 errStr: "already exists and not empty", 102 }} 103 104 for _, tc := range testCases { 105 t.Run(tc.name, func(t *testing.T) { 106 if tc.skipOnWin && runtime.GOOS == "windows" { 107 t.SkipNow() 108 } 109 lastCmd = nil 110 if tc.setup != nil { 111 teardown := tc.setup() 112 defer teardown() 113 } 114 115 var err error 116 out, _, _ := capture(func() { 117 err = newApp().Run(append([]string{""}, tc.input...)) 118 }) 119 out = strings.TrimSpace(out) 120 121 if tc.errStr == "" { 122 if err != nil { 123 t.Errorf("error should be nil, but: %s", err) 124 } 125 } else { 126 if e, g := tc.errStr, err.Error(); !strings.Contains(g, e) { 127 t.Errorf("err.Error() should contains %q, but not: %q", e, g) 128 } 129 } 130 131 if len(tc.want) > 0 { 132 if !reflect.DeepEqual(lastCmd.Args, tc.want) { 133 t.Errorf("cmd.Args = %v, want: %v", lastCmd.Args, tc.want) 134 } 135 136 if lastCmd.Dir != tc.wantDir { 137 t.Errorf("cmd.Dir = %q, want: %q", lastCmd.Dir, tc.wantDir) 138 } 139 } 140 141 if tc.errStr == "" { 142 if out != tc.wantDir { 143 t.Errorf("cmd.Dir = %q, want: %q", out, tc.wantDir) 144 } 145 } else { 146 if out != "" { 147 t.Errorf("output should be empty but: %s", out) 148 } 149 } 150 }) 151 } 152 }