github.com/x-motemen/ghq@v1.6.1/cmd_create_test.go (about)

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