github.com/nkprince007/lab@v0.6.2-0.20171218071646-19d68b56f403/cmd/snippetCreate_test.go (about)

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