gitlab.com/prarit/lab@v0.14.0/cmd/snippet_create_test.go (about) 1 package cmd 2 3 import ( 4 "io/ioutil" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func Test_snippetCreate(t *testing.T) { 15 t.Parallel() 16 repo := copyTestRepo(t) 17 cmd := exec.Command("../lab_bin", "snippet", "create", "lab-testing", 18 "-m", "snippet title", 19 "-m", "snippet description") 20 cmd.Dir = repo 21 22 rc, err := cmd.StdinPipe() 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 _, err = rc.Write([]byte("snippet contents")) 28 if err != nil { 29 t.Fatal(err) 30 } 31 err = rc.Close() 32 if err != nil { 33 t.Fatal(err) 34 } 35 36 b, err := cmd.CombinedOutput() 37 if err != nil { 38 t.Log(string(b)) 39 t.Fatal(err) 40 } 41 42 require.Contains(t, string(b), "https://gitlab.com/lab-testing/test/snippets/") 43 } 44 45 func Test_snippetCreate_file(t *testing.T) { 46 t.Parallel() 47 repo := copyTestRepo(t) 48 t.Log(filepath.Join(repo, "testfile")) 49 err := ioutil.WriteFile(filepath.Join(repo, "testfile"), []byte("test file contents"), 0644) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 cmd := exec.Command("../lab_bin", "snippet", "lab-testing", "testfile", 55 "-m", "snippet title", 56 "-m", "snippet description") 57 cmd.Dir = repo 58 59 b, err := cmd.CombinedOutput() 60 if err != nil { 61 t.Log(string(b)) 62 t.Fatal(err) 63 } 64 65 require.Contains(t, string(b), "https://gitlab.com/lab-testing/test/snippets/") 66 } 67 68 func Test_snippetCreate_Global(t *testing.T) { 69 t.Parallel() 70 repo := copyTestRepo(t) 71 72 cmd := exec.Command("../lab_bin", "snippet", "create", "-g", 73 "-m", "personal snippet title", 74 "-m", "personal snippet description") 75 76 cmd.Dir = repo 77 rc, err := cmd.StdinPipe() 78 if err != nil { 79 t.Fatal(err) 80 } 81 82 _, err = rc.Write([]byte("personal snippet contents")) 83 if err != nil { 84 t.Fatal(err) 85 } 86 err = rc.Close() 87 if err != nil { 88 t.Fatal(err) 89 } 90 91 b, err := cmd.CombinedOutput() 92 if err != nil { 93 t.Log(string(b)) 94 t.Fatal(err) 95 } 96 97 require.Contains(t, string(b), "https://gitlab.com/snippets/") 98 } 99 100 // This test is a little ridiculus, if we find it doesn't work well on other 101 // envionments, we can just remove it. Its sole purpose is to test that a 102 // personal snippet can be created (with the users git editor) outside of a git 103 // repo. issue #98 104 func Test_snippetCreate_Global_Editor(t *testing.T) { 105 t.Parallel() 106 repo := copyTestRepo(t) 107 108 err := exec.Command("cp", "-rf", repo, "/tmp/testdata-6810350901254661225").Run() 109 if err != nil { 110 t.Fatal(err) 111 } 112 repo = "/tmp/testdata-6810350901254661225" 113 defer func() { 114 coveragePath, _ := filepath.Glob("/tmp/coverage-*") 115 exec.Command("cp", coveragePath[0], "../coverage-6810350901254661225.out").Run() 116 os.Remove(coveragePath[0]) 117 os.RemoveAll(repo) 118 }() 119 120 // Write the editor file here, since its tricky to get a file with 121 // contents in it otherwise. We need a file with contents to 122 // successfully create the snippet 123 err = ioutil.WriteFile("/tmp/SNIPCODE_EDITMSG", []byte("personal snippet title outside repo"), 0644) 124 if err != nil { 125 t.Fatal(err) 126 } 127 128 // Remove .git dir forcing the cmd to exec outside of a git repo 129 cmd := exec.Command("rm", "-rf", ".git") 130 cmd.Dir = repo 131 err = cmd.Run() 132 if err != nil { 133 t.Fatal(err) 134 } 135 136 cmd = exec.Command(os.ExpandEnv("$GOPATH/src/github.com/zaquestion/lab/lab_bin"), "snippet", "create", "-g") 137 cmd.Env = []string{"PATH=/usr/local/bin:/usr/bin:/bin", "EDITOR=test -f"} 138 cmd.Dir = repo 139 140 b, err := cmd.CombinedOutput() 141 if err != nil { 142 t.Log(string(b)) 143 t.Fatal(err) 144 } 145 146 require.Contains(t, string(b), "https://gitlab.com/snippets/") 147 } 148 149 func Test_snipMsg(t *testing.T) { 150 msgs, err := snippetCreateCmd.Flags().GetStringSlice("message") 151 if err != nil { 152 t.Fatal(err) 153 } 154 title, desc := snipMsg(msgs) 155 assert.Equal(t, "-", title) 156 assert.Equal(t, "", desc) 157 } 158 159 func Test_snipCode(t *testing.T) { 160 err := ioutil.WriteFile("./testfile", []byte("test file contents"), 0644) 161 if err != nil { 162 t.Fatal(err) 163 } 164 165 tests := []struct { 166 Name string 167 Path string 168 ExpectedCode string 169 }{ 170 { 171 Name: "From File", 172 Path: "./testfile", 173 ExpectedCode: "test file contents", 174 }, 175 { 176 Name: "From Editor", 177 Path: "", 178 ExpectedCode: "\n\n", 179 }, 180 } 181 for _, test := range tests { 182 t.Run(test.Name, func(t *testing.T) { 183 test := test 184 t.Parallel() 185 code, err := snipCode(test.Path) 186 if err != nil { 187 t.Fatal(err) 188 } 189 require.Equal(t, test.ExpectedCode, code) 190 }) 191 } 192 } 193 194 func Test_snipText(t *testing.T) { 195 var tmpl = "foo" + ` 196 {{.CommentChar}} In this mode you are writing a snippet from scratch 197 {{.CommentChar}} The first block is the title and the rest is the contents.` 198 text, err := snipText(tmpl) 199 if err != nil { 200 t.Fatal(err) 201 } 202 require.Equal(t, `foo 203 # In this mode you are writing a snippet from scratch 204 # The first block is the title and the rest is the contents.`, text) 205 206 }