github.com/zaquestion/lab@v0.25.1/cmd/snippet_test.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os/exec" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func Test_snippetCmd_personal(t *testing.T) { 13 t.Parallel() 14 repo := copyTestRepo(t) 15 var snipID string 16 t.Run("create_personal", func(t *testing.T) { 17 cmd := exec.Command(labBinaryPath, "snippet", "-g", 18 "-m", "personal snippet title", 19 "-m", "personal 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("personal 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 out := string(b) 43 require.Contains(t, out, "https://gitlab.com/-/snippets/") 44 45 i := strings.Index(out, "\n") 46 snipID = strings.TrimPrefix(out[:i], "https://gitlab.com/-/snippets/") 47 t.Log(snipID) 48 }) 49 t.Run("list_personal", func(t *testing.T) { 50 // Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/43361 51 t.Skip("borked") 52 if snipID == "" { 53 t.Skip("snipID is empty, create likely failed") 54 } 55 cmd := exec.Command(labBinaryPath, "snippet", "-l", "-g") 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 snips := strings.Split(string(b), "\n") 65 t.Log(snips) 66 require.Contains(t, snips, fmt.Sprintf("#%s personal snippet title", snipID)) 67 }) 68 t.Run("delete_personal", func(t *testing.T) { 69 if snipID == "" { 70 t.Skip("snipID is empty, create likely failed") 71 } 72 cmd := exec.Command(labBinaryPath, "snippet", "-g", "-d", snipID) 73 cmd.Dir = repo 74 75 b, err := cmd.CombinedOutput() 76 if err != nil { 77 t.Log(string(b)) 78 t.Fatal(err) 79 } 80 require.Contains(t, string(b), fmt.Sprintf("Snippet #%s deleted", snipID)) 81 }) 82 } 83 84 func Test_snippetCmd_noArgs(t *testing.T) { 85 repo := copyTestRepo(t) 86 cmd := exec.Command(labBinaryPath, "snippet") 87 cmd.Dir = repo 88 89 b, err := cmd.CombinedOutput() 90 if err != nil { 91 t.Log(string(b)) 92 t.Fatal(err) 93 } 94 require.Contains(t, string(b), `Usage: 95 lab snippet [flags] 96 lab snippet [command]`) 97 }