github.com/ianfoo/lab@v0.9.5-0.20180123060006-5ed79f2ccfc7/cmd/snippetCreate_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 := os.Rename(repo, "/tmp/testdata-6810350901254661225")
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	repo = "/tmp/testdata-6810350901254661225"
   113  	defer func() {
   114  		coveragePath, _ := filepath.Glob("/tmp/coverage-*")
   115  		os.Rename(coveragePath[0], "../coverage-6810350901254661225.out")
   116  		os.RemoveAll(repo)
   117  	}()
   118  
   119  	// Write the editor file here, since its tricky to get a file with
   120  	// contents in it otherwise. We need a file with contents to
   121  	// successfully create the snippet
   122  	err = ioutil.WriteFile("/tmp/SNIPCODE_EDITMSG", []byte("personal snippet title outside repo"), 0644)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	// Remove .git dir forcing the cmd to exec outside of a git repo
   128  	cmd := exec.Command("rm", "-rf", ".git")
   129  	cmd.Dir = repo
   130  	err = cmd.Run()
   131  	if err != nil {
   132  		t.Fatal(err)
   133  	}
   134  
   135  	cmd = exec.Command(os.ExpandEnv("$GOPATH/src/github.com/zaquestion/lab/lab_bin"), "snippet", "create", "-g")
   136  	cmd.Env = []string{"PATH=/usr/local/bin:/usr/bin:/bin", "EDITOR=test -f"}
   137  	cmd.Dir = repo
   138  
   139  	b, err := cmd.CombinedOutput()
   140  	if err != nil {
   141  		t.Log(string(b))
   142  		t.Fatal(err)
   143  	}
   144  
   145  	require.Contains(t, string(b), "https://gitlab.com/snippets/")
   146  }
   147  
   148  func Test_snipMsg(t *testing.T) {
   149  	msgs, err := snippetCreateCmd.Flags().GetStringSlice("message")
   150  	if err != nil {
   151  		t.Fatal(err)
   152  	}
   153  	title, desc := snipMsg(msgs)
   154  	assert.Equal(t, "-", title)
   155  	assert.Equal(t, "", desc)
   156  }
   157  
   158  func Test_snipCode(t *testing.T) {
   159  	err := ioutil.WriteFile("./testfile", []byte("test file contents"), 0644)
   160  	if err != nil {
   161  		t.Fatal(err)
   162  	}
   163  
   164  	tests := []struct {
   165  		Name         string
   166  		Path         string
   167  		ExpectedCode string
   168  	}{
   169  		{
   170  			Name:         "From File",
   171  			Path:         "./testfile",
   172  			ExpectedCode: "test file contents",
   173  		},
   174  		{
   175  			Name:         "From Editor",
   176  			Path:         "",
   177  			ExpectedCode: "\n\n",
   178  		},
   179  	}
   180  	for _, test := range tests {
   181  		t.Run(test.Name, func(t *testing.T) {
   182  			test := test
   183  			t.Parallel()
   184  			code, err := snipCode(test.Path)
   185  			if err != nil {
   186  				t.Fatal(err)
   187  			}
   188  			require.Equal(t, test.ExpectedCode, code)
   189  		})
   190  	}
   191  }
   192  
   193  func Test_snipText(t *testing.T) {
   194  	var tmpl = "foo" + `
   195  {{.CommentChar}} In this mode you are writing a snippet from scratch
   196  {{.CommentChar}} The first block is the title and the rest is the contents.`
   197  	text, err := snipText(tmpl)
   198  	if err != nil {
   199  		t.Fatal(err)
   200  	}
   201  	require.Equal(t, `foo
   202  # In this mode you are writing a snippet from scratch
   203  # The first block is the title and the rest is the contents.`, text)
   204  
   205  }