github.com/posener/gitfs@v1.2.2-0.20200410105819-ea4e48d73ab9/fsutil/tmpl_test.go (about)

     1  package fsutil
     2  
     3  import (
     4  	"bytes"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestTmplParse(t *testing.T) {
    13  	t.Parallel()
    14  	fs := http.Dir(".")
    15  	buf := bytes.NewBuffer(nil)
    16  
    17  	tmpl, err := TmplParse(fs, nil, "testdata/tmpl1.gotmpl", "testdata/tmpl2.gotmpl")
    18  	require.NoError(t, err)
    19  
    20  	buf.Reset()
    21  	require.NoError(t, tmpl.ExecuteTemplate(buf, "tmpl1.gotmpl", "foo"))
    22  	assert.Equal(t, "hello, foo", buf.String())
    23  
    24  	buf.Reset()
    25  	require.NoError(t, tmpl.ExecuteTemplate(buf, "tmpl2.gotmpl", "foo"))
    26  	assert.Equal(t, "hi, foo", buf.String())
    27  }
    28  
    29  func TestTmplParse_noSuchFile(t *testing.T) {
    30  	t.Parallel()
    31  	fs := http.Dir(".")
    32  	_, err := TmplParse(fs, nil, "testdata/tmpl1.gotmpl", "testdata/nosuchfile")
    33  	assert.Error(t, err)
    34  	_, err = TmplParse(fs, nil, "testdata/nosuchfile", "testdata/tmpl1.gotmpl")
    35  	assert.Error(t, err)
    36  }
    37  
    38  func TestTmplParse_emptyFileNames(t *testing.T) {
    39  	t.Parallel()
    40  	fs := http.Dir(".")
    41  	_, err := TmplParse(fs, nil)
    42  	assert.Error(t, err)
    43  }
    44  
    45  func TestTmplParseGlob(t *testing.T) {
    46  	t.Parallel()
    47  	buf := bytes.NewBuffer(nil)
    48  	fs := http.Dir(".")
    49  
    50  	// Match all files in the directory.
    51  	tmpl, err := TmplParseGlob(fs, nil, "testdata/*.gotmpl")
    52  	require.NoError(t, err)
    53  
    54  	buf.Reset()
    55  	require.NoError(t, tmpl.ExecuteTemplate(buf, "tmpl1.gotmpl", "foo"))
    56  	assert.Equal(t, "hello, foo", buf.String())
    57  
    58  	buf.Reset()
    59  	require.NoError(t, tmpl.ExecuteTemplate(buf, "tmpl2.gotmpl", "foo"))
    60  	assert.Equal(t, "hi, foo", buf.String())
    61  
    62  	// Match only one file.
    63  	tmpl, err = TmplParseGlob(fs, nil, "testdata/tmpl1.*")
    64  	require.NoError(t, err)
    65  
    66  	buf.Reset()
    67  	require.NoError(t, tmpl.ExecuteTemplate(buf, "tmpl1.gotmpl", "foo"))
    68  	assert.Equal(t, "hello, foo", buf.String())
    69  
    70  	buf.Reset()
    71  	assert.Error(t, tmpl.ExecuteTemplate(buf, "tmpl2.gotmpl", "foo"))
    72  }
    73  
    74  func TestTmplParseHTML(t *testing.T) {
    75  	t.Parallel()
    76  	fs := http.Dir(".")
    77  	tmpl, err := TmplParseHTML(fs, nil, "testdata/tmpl1.gotmpl", "testdata/tmpl2.gotmpl")
    78  	require.NoError(t, err)
    79  	buf := bytes.NewBuffer(nil)
    80  	require.NoError(t, tmpl.ExecuteTemplate(buf, "tmpl1.gotmpl", "foo"))
    81  	assert.Equal(t, "hello, foo", buf.String())
    82  
    83  	buf.Reset()
    84  	require.NoError(t, tmpl.ExecuteTemplate(buf, "tmpl2.gotmpl", "foo"))
    85  	assert.Equal(t, "hi, foo", buf.String())
    86  }
    87  
    88  func TestTmplParseGlobHTML(t *testing.T) {
    89  	t.Parallel()
    90  	buf := bytes.NewBuffer(nil)
    91  	fs := http.Dir(".")
    92  
    93  	// Match all files in the directory.
    94  	tmpl, err := TmplParseGlobHTML(fs, nil, "testdata/*.gotmpl")
    95  	require.NoError(t, err)
    96  
    97  	buf.Reset()
    98  	require.NoError(t, tmpl.ExecuteTemplate(buf, "tmpl1.gotmpl", "foo"))
    99  	assert.Equal(t, "hello, foo", buf.String())
   100  
   101  	buf.Reset()
   102  	require.NoError(t, tmpl.ExecuteTemplate(buf, "tmpl2.gotmpl", "foo"))
   103  	assert.Equal(t, "hi, foo", buf.String())
   104  
   105  	// Match only one file.
   106  	tmpl, err = TmplParseGlobHTML(fs, nil, "testdata/tmpl1.*")
   107  	require.NoError(t, err)
   108  
   109  	buf.Reset()
   110  	require.NoError(t, tmpl.ExecuteTemplate(buf, "tmpl1.gotmpl", "foo"))
   111  	assert.Equal(t, "hello, foo", buf.String())
   112  
   113  	buf.Reset()
   114  	assert.Error(t, tmpl.ExecuteTemplate(buf, "tmpl2.gotmpl", "foo"))
   115  }
   116  
   117  func TestTmplParseHTML_noSuchFile(t *testing.T) {
   118  	t.Parallel()
   119  	fs := http.Dir(".")
   120  	_, err := TmplParseHTML(fs, nil, "testdata/tmpl1.gotmpl", "testdata/nosuchfile")
   121  	assert.Error(t, err)
   122  	_, err = TmplParseHTML(fs, nil, "testdata/nosuchfile", "testdata/tmpl1.gotmpl")
   123  	assert.Error(t, err)
   124  }
   125  
   126  func TestTmplParseHTML_emptyFileNames(t *testing.T) {
   127  	t.Parallel()
   128  	fs := http.Dir(".")
   129  	_, err := TmplParseHTML(fs, nil)
   130  	assert.Error(t, err)
   131  }