github.com/haraldLmueller/buffalo@v0.11.1/render/template_test.go (about)

     1  package render_test
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/gobuffalo/buffalo/render"
    12  	"github.com/gobuffalo/packr"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func Test_Template(t *testing.T) {
    17  	r := require.New(t)
    18  
    19  	tPath, err := ioutil.TempDir("", "")
    20  	r.NoError(err)
    21  	defer os.Remove(tPath)
    22  
    23  	tmpFile, err := os.Create(filepath.Join(tPath, "test"))
    24  	r.NoError(err)
    25  	defer os.Remove(tmpFile.Name())
    26  
    27  	_, err = tmpFile.Write([]byte("<%= name %>"))
    28  	r.NoError(err)
    29  
    30  	type ji func(string, ...string) render.Renderer
    31  
    32  	table := []ji{
    33  		render.New(render.Options{
    34  			TemplatesBox: packr.NewBox(tPath),
    35  		}).Template,
    36  	}
    37  
    38  	for _, j := range table {
    39  		re := j("foo/bar", filepath.Base(tmpFile.Name()))
    40  		r.Equal("foo/bar", re.ContentType())
    41  		bb := &bytes.Buffer{}
    42  		err = re.Render(bb, render.Data{"name": "Mark"})
    43  		r.NoError(err)
    44  		r.Equal("Mark", strings.TrimSpace(bb.String()))
    45  	}
    46  }
    47  
    48  func Test_Template_Partial(t *testing.T) {
    49  	r := require.New(t)
    50  
    51  	tPath, err := ioutil.TempDir("", "")
    52  	r.NoError(err)
    53  	defer os.Remove(tPath)
    54  
    55  	partFile, err := os.Create(filepath.Join(tPath, "_foo.html"))
    56  	r.NoError(err)
    57  
    58  	_, err = partFile.Write([]byte("Foo > <%= name %>"))
    59  	r.NoError(err)
    60  
    61  	tmpFile, err := os.Create(filepath.Join(tPath, "index.html"))
    62  	r.NoError(err)
    63  
    64  	_, err = tmpFile.Write([]byte(`<%= partial("foo.html") %>`))
    65  	r.NoError(err)
    66  
    67  	type ji func(string, ...string) render.Renderer
    68  
    69  	j := render.New(render.Options{
    70  		TemplatesBox: packr.NewBox(tPath),
    71  	}).Template
    72  
    73  	re := j("foo/bar", "index.html")
    74  	r.Equal("foo/bar", re.ContentType())
    75  	bb := &bytes.Buffer{}
    76  	err = re.Render(bb, render.Data{"name": "Mark"})
    77  	r.NoError(err)
    78  	r.Equal("Foo > Mark", strings.TrimSpace(bb.String()))
    79  }
    80  
    81  func Test_Template_Partial_Recursive_With_Global_And_Local_Context(t *testing.T) {
    82  	r := require.New(t)
    83  
    84  	tPath, err := ioutil.TempDir("", "")
    85  	r.NoError(err)
    86  	defer os.Remove(tPath)
    87  
    88  	partFile, err := os.Create(filepath.Join(tPath, "_foo.html"))
    89  	r.NoError(err)
    90  
    91  	_, err = partFile.Write([]byte(`<%= other %>|<%= name %>`))
    92  	r.NoError(err)
    93  
    94  	tmpFile, err := os.Create(filepath.Join(tPath, "index.html"))
    95  	r.NoError(err)
    96  
    97  	_, err = tmpFile.Write([]byte(`<%= partial("foo.html", {other: "Other"}) %>`))
    98  	r.NoError(err)
    99  
   100  	re := render.New(render.Options{
   101  		TemplatesBox: packr.NewBox(tPath),
   102  	}).Template("", "index.html")
   103  
   104  	bb := &bytes.Buffer{}
   105  	err = re.Render(bb, render.Data{"name": "Mark"})
   106  	r.NoError(err)
   107  	r.Equal("Other|Mark", strings.TrimSpace(bb.String()))
   108  }
   109  
   110  func Test_Template_Partial_WithoutExtension(t *testing.T) {
   111  	r := require.New(t)
   112  
   113  	tPath, err := ioutil.TempDir("", "")
   114  	r.NoError(err)
   115  	defer os.Remove(tPath)
   116  
   117  	partFile, err := os.Create(filepath.Join(tPath, "_foo.html"))
   118  	r.NoError(err)
   119  
   120  	_, err = partFile.Write([]byte("Foo > <%= name %>"))
   121  	r.NoError(err)
   122  
   123  	tmpFile, err := os.Create(filepath.Join(tPath, "index.html"))
   124  	r.NoError(err)
   125  
   126  	_, err = tmpFile.Write([]byte(`<%= partial("foo") %>`))
   127  	r.NoError(err)
   128  
   129  	type ji func(string, ...string) render.Renderer
   130  
   131  	j := render.New(render.Options{
   132  		TemplatesBox: packr.NewBox(tPath),
   133  	}).HTML
   134  
   135  	re := j("index.html")
   136  	bb := &bytes.Buffer{}
   137  	err = re.Render(bb, render.Data{"name": "Mark"})
   138  	r.NoError(err)
   139  	r.Equal("Foo > Mark", strings.TrimSpace(bb.String()))
   140  }
   141  
   142  func Test_AssetPath(t *testing.T) {
   143  	r := require.New(t)
   144  
   145  	cases := map[string]string{
   146  		"something.txt":         "/assets/something.txt",
   147  		"images/something.png":  "/assets/images/something.png",
   148  		"/images/something.png": "/assets/images/something.png",
   149  		"application.css":       "/assets/application.aabbc123.css",
   150  	}
   151  
   152  	tDir, err := ioutil.TempDir("", "templates")
   153  	if err != nil {
   154  		r.Fail("Could not set the templates dir")
   155  	}
   156  
   157  	aDir, err := ioutil.TempDir("", "assets")
   158  	if err != nil {
   159  		r.Fail("Could not set the assets dir")
   160  	}
   161  
   162  	re := render.New(render.Options{
   163  		TemplatesBox: packr.NewBox(tDir),
   164  		AssetsBox:    packr.NewBox(aDir),
   165  	}).Template
   166  
   167  	ioutil.WriteFile(filepath.Join(aDir, "manifest.json"), []byte(`{
   168  		"application.css": "application.aabbc123.css"
   169  	}`), 0644)
   170  
   171  	for original, expected := range cases {
   172  
   173  		tmpFile, err := os.Create(filepath.Join(tDir, "test.html"))
   174  		r.NoError(err)
   175  
   176  		_, err = tmpFile.Write([]byte("<%= assetPath(\"" + original + "\") %>"))
   177  		r.NoError(err)
   178  
   179  		result := re("text/html", filepath.Base(tmpFile.Name()))
   180  
   181  		bb := &bytes.Buffer{}
   182  		err = result.Render(bb, render.Data{})
   183  		r.NoError(err)
   184  		r.Equal(expected, strings.TrimSpace(bb.String()))
   185  
   186  		os.Remove(tmpFile.Name())
   187  	}
   188  }
   189  
   190  func Test_AssetPathNoManifest(t *testing.T) {
   191  	r := require.New(t)
   192  
   193  	cases := map[string]string{
   194  		"something.txt": "/assets/something.txt",
   195  	}
   196  
   197  	tDir, err := ioutil.TempDir("", "templates")
   198  	if err != nil {
   199  		r.Fail("Could not set the templates dir")
   200  	}
   201  
   202  	aDir, err := ioutil.TempDir("", "assets")
   203  	if err != nil {
   204  		r.Fail("Could not set the assets dir")
   205  	}
   206  
   207  	re := render.New(render.Options{
   208  		TemplatesBox: packr.NewBox(tDir),
   209  		AssetsBox:    packr.NewBox(aDir),
   210  	}).Template
   211  
   212  	for original, expected := range cases {
   213  
   214  		tmpFile, err := os.Create(filepath.Join(tDir, "test.html"))
   215  		r.NoError(err)
   216  
   217  		_, err = tmpFile.Write([]byte("<%= assetPath(\"" + original + "\") %>"))
   218  		r.NoError(err)
   219  
   220  		result := re("text/html", filepath.Base(tmpFile.Name()))
   221  
   222  		bb := &bytes.Buffer{}
   223  		err = result.Render(bb, render.Data{})
   224  		r.NoError(err)
   225  		r.Equal(expected, strings.TrimSpace(bb.String()))
   226  
   227  		os.Remove(tmpFile.Name())
   228  	}
   229  }
   230  func Test_AssetPathManifestCorrupt(t *testing.T) {
   231  	r := require.New(t)
   232  
   233  	cases := map[string]string{
   234  		"something.txt": "manifest.json is not correct",
   235  		"other.txt":     "manifest.json is not correct",
   236  	}
   237  
   238  	tDir, err := ioutil.TempDir("", "templates")
   239  	if err != nil {
   240  		r.Fail("Could not set the templates dir")
   241  	}
   242  
   243  	aDir, err := ioutil.TempDir("", "assets")
   244  	if err != nil {
   245  		r.Fail("Could not set the assets dir")
   246  	}
   247  
   248  	ioutil.WriteFile(filepath.Join(aDir, "manifest.json"), []byte(`//shdnn Corrupt!`), 0644)
   249  
   250  	re := render.New(render.Options{
   251  		TemplatesBox: packr.NewBox(tDir),
   252  		AssetsBox:    packr.NewBox(aDir),
   253  	}).Template
   254  
   255  	for original, expected := range cases {
   256  
   257  		tmpFile, err := os.Create(filepath.Join(tDir, "test.html"))
   258  		r.NoError(err)
   259  
   260  		_, err = tmpFile.Write([]byte("<%= assetPath(\"" + original + "\") %>"))
   261  		r.NoError(err)
   262  
   263  		result := re("text/html", filepath.Base(tmpFile.Name()))
   264  
   265  		bb := &bytes.Buffer{}
   266  		err = result.Render(bb, render.Data{})
   267  		r.Error(err)
   268  		r.Contains(err.Error(), expected)
   269  
   270  		os.Remove(tmpFile.Name())
   271  	}
   272  }