github.com/nkprince007/lab@v0.6.2-0.20171218071646-19d68b56f403/cmd/root_test.go (about) 1 package cmd 2 3 import ( 4 "log" 5 "math/rand" 6 "os" 7 "os/exec" 8 "path" 9 "path/filepath" 10 "strconv" 11 "testing" 12 "time" 13 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 "github.com/zaquestion/lab/internal/git" 17 ) 18 19 func TestMain(m *testing.M) { 20 wd, err := git.WorkingDir() 21 if err != nil { 22 log.Fatal(err) 23 } 24 os.Chdir(wd) 25 err = exec.Command("go", "test", "-c", "-coverpkg", "./...", "-covermode", "count", "-o", "lab_bin").Run() 26 if err != nil { 27 log.Fatal(err) 28 } 29 rand.Seed(time.Now().UnixNano()) 30 os.Chdir(path.Join(wd, "testdata")) 31 code := m.Run() 32 os.Chdir(wd) 33 os.Remove("lab_bin") 34 testdirs, err := filepath.Glob("testdata-*") 35 if err != nil { 36 log.Fatal(err) 37 } 38 for _, dir := range testdirs { 39 err := os.RemoveAll(dir) 40 if err != nil { 41 log.Fatal(err) 42 } 43 } 44 45 os.Exit(code) 46 } 47 48 func copyTestRepo(t *testing.T) string { 49 dir := "../testdata-" + strconv.Itoa(int(rand.Uint64())) 50 t.Log(dir) 51 err := exec.Command("cp", "-r", "../testdata", dir).Run() 52 if err != nil { 53 t.Fatal(err) 54 } 55 wd, err := os.Getwd() 56 if err != nil { 57 t.Fatal(err) 58 } 59 dir = path.Join(wd, dir) 60 return dir 61 } 62 63 func TestRootCloneNoArg(t *testing.T) { 64 cmd := exec.Command("../lab_bin", "clone") 65 b, _ := cmd.CombinedOutput() 66 require.Contains(t, string(b), "You must specify a repository to clone.") 67 } 68 69 func TestRootGitCmd(t *testing.T) { 70 cmd := exec.Command("../lab_bin", "log", "-n", "1") 71 b, _ := cmd.CombinedOutput() 72 require.Contains(t, string(b), `commit cd64a7caea4f3ee5696a190379aff1a7f636e598 73 Author: Zaq? Wiedmann <zaquestion@gmail.com> 74 Date: Sat Sep 2 20:58:39 2017 -0700 75 76 Added additional commit for LastCommitMessage and meeting requirements for Log test (>1 commit)`) 77 } 78 79 func TestRootNoArg(t *testing.T) { 80 cmd := exec.Command("../lab_bin") 81 b, _ := cmd.CombinedOutput() 82 assert.Contains(t, string(b), "usage: git [--version] [--help] [-C <path>] [-c name=value]") 83 assert.Contains(t, string(b), `These GitLab commands are provided by lab: 84 85 fork Fork a remote repository on GitLab and add as remote`) 86 } 87 88 func Test_parseArgsRemote(t *testing.T) { 89 tests := []struct { 90 Name string 91 Args []string 92 ExpectedString string 93 ExpectedInt int64 94 ExpectedErr string 95 }{ 96 { 97 Name: "No Args", 98 Args: nil, 99 ExpectedString: "", 100 ExpectedInt: 0, 101 ExpectedErr: "", 102 }, 103 { 104 Name: "1 arg remote", 105 Args: []string{"origin"}, 106 ExpectedString: "origin", 107 ExpectedInt: 0, 108 ExpectedErr: "", 109 }, 110 { 111 Name: "1 arg non remote", 112 Args: []string{"foo"}, 113 ExpectedString: "", 114 ExpectedInt: 0, 115 ExpectedErr: "foo is not a valid remote or number", 116 }, 117 { 118 Name: "1 arg page", 119 Args: []string{"100"}, 120 ExpectedString: "", 121 ExpectedInt: 100, 122 ExpectedErr: "", 123 }, 124 { 125 Name: "1 arg invalid page", 126 Args: []string{"asdf100"}, 127 ExpectedString: "", 128 ExpectedInt: 0, 129 ExpectedErr: "asdf100 is not a valid remote or number", 130 }, 131 { 132 Name: "2 arg remote page", 133 Args: []string{"origin", "100"}, 134 ExpectedString: "origin", 135 ExpectedInt: 100, 136 ExpectedErr: "", 137 }, 138 { 139 Name: "2 arg invalid remote valid page", 140 Args: []string{"foo", "100"}, 141 ExpectedString: "", 142 ExpectedInt: 0, 143 ExpectedErr: "foo is not a valid remote", 144 }, 145 { 146 Name: "2 arg valid remote invalid page", 147 Args: []string{"foo", "asdf100"}, 148 ExpectedString: "", 149 ExpectedInt: 0, 150 ExpectedErr: "strconv.ParseInt: parsing \"asdf100\": invalid syntax", 151 }, 152 } 153 for _, test := range tests { 154 t.Run(test.Name, func(t *testing.T) { 155 test := test 156 t.Parallel() 157 s, i, err := parseArgsRemote(test.Args) 158 if err != nil { 159 assert.EqualError(t, err, test.ExpectedErr) 160 } 161 assert.Equal(t, test.ExpectedString, s) 162 assert.Equal(t, test.ExpectedInt, i) 163 }) 164 } 165 }