github.com/zaquestion/lab@v0.25.1/cmd/snippet_create_test.go (about) 1 package cmd 2 3 import ( 4 "io/ioutil" 5 "os/exec" 6 "path/filepath" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func Test_snippetCreate(t *testing.T) { 14 t.Parallel() 15 repo := copyTestRepo(t) 16 cmd := exec.Command(labBinaryPath, "snippet", "create", "-p", "lab-testing", 17 "-m", "snippet title", 18 "-m", "snippet description") 19 cmd.Dir = repo 20 21 rc, err := cmd.StdinPipe() 22 if err != nil { 23 t.Fatal(err) 24 } 25 26 _, err = rc.Write([]byte("snippet contents")) 27 if err != nil { 28 t.Fatal(err) 29 } 30 err = rc.Close() 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 b, err := cmd.CombinedOutput() 36 if err != nil { 37 t.Log(string(b)) 38 t.Fatal(err) 39 } 40 41 require.Contains(t, string(b), "https://gitlab.com/lab-testing/test/-/snippets/") 42 } 43 44 func Test_snippetCreate_file(t *testing.T) { 45 t.Parallel() 46 repo := copyTestRepo(t) 47 t.Log(filepath.Join(repo, "testfile")) 48 err := ioutil.WriteFile(filepath.Join(repo, "testfile"), []byte("test file contents"), 0644) 49 if err != nil { 50 t.Fatal(err) 51 } 52 53 cmd := exec.Command(labBinaryPath, "snippet", "-p", "lab-testing", "testfile", 54 "-m", "snippet title", 55 "-m", "snippet description") 56 cmd.Dir = repo 57 58 b, err := cmd.CombinedOutput() 59 if err != nil { 60 t.Log(string(b)) 61 t.Fatal(err) 62 } 63 64 require.Contains(t, string(b), "https://gitlab.com/lab-testing/test/-/snippets/") 65 } 66 67 func Test_snippetCreate_Global(t *testing.T) { 68 t.Parallel() 69 repo := copyTestRepo(t) 70 71 cmd := exec.Command(labBinaryPath, "snippet", "create", "-p", "-g", 72 "-m", "personal snippet title", 73 "-m", "personal snippet description") 74 75 cmd.Dir = repo 76 rc, err := cmd.StdinPipe() 77 if err != nil { 78 t.Fatal(err) 79 } 80 81 _, err = rc.Write([]byte("personal snippet contents")) 82 if err != nil { 83 t.Fatal(err) 84 } 85 err = rc.Close() 86 if err != nil { 87 t.Fatal(err) 88 } 89 90 b, err := cmd.CombinedOutput() 91 if err != nil { 92 t.Log(string(b)) 93 t.Fatal(err) 94 } 95 96 require.Contains(t, string(b), "https://gitlab.com/-/snippets/") 97 } 98 99 func Test_snipMsg(t *testing.T) { 100 msgs, err := snippetCreateCmd.Flags().GetStringArray("message") 101 if err != nil { 102 t.Fatal(err) 103 } 104 title, desc := snipMsg(msgs) 105 assert.Equal(t, "-", title) 106 assert.Equal(t, "", desc) 107 } 108 109 func Test_snipCode(t *testing.T) { 110 err := ioutil.WriteFile("./testfile", []byte("test file contents"), 0644) 111 if err != nil { 112 t.Fatal(err) 113 } 114 115 tests := []struct { 116 Name string 117 Path string 118 ExpectedCode string 119 }{ 120 { 121 Name: "From File", 122 Path: "./testfile", 123 ExpectedCode: "test file contents", 124 }, 125 { 126 Name: "From Editor", 127 Path: "", 128 ExpectedCode: "\n\n", 129 }, 130 } 131 for _, test := range tests { 132 t.Run(test.Name, func(t *testing.T) { 133 test := test 134 t.Parallel() 135 code, err := snipCode(test.Path) 136 if err != nil { 137 t.Fatal(err) 138 } 139 require.Equal(t, test.ExpectedCode, code) 140 }) 141 } 142 } 143 144 func Test_snipText(t *testing.T) { 145 var tmpl = "foo" + ` 146 {{.CommentChar}} In this mode you are writing a snippet from scratch 147 {{.CommentChar}} The first block is the title and the rest is the contents.` 148 text, err := snipText(tmpl) 149 if err != nil { 150 t.Fatal(err) 151 } 152 require.Equal(t, `foo 153 # In this mode you are writing a snippet from scratch 154 # The first block is the title and the rest is the contents.`, text) 155 156 }