github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/tpl/tplimpl/integration_test.go (about)

     1  package tplimpl_test
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"testing"
     7  
     8  	qt "github.com/frankban/quicktest"
     9  	"github.com/gohugoio/hugo/hugolib"
    10  	"github.com/gohugoio/hugo/tpl"
    11  )
    12  
    13  func TestPrintUnusedTemplates(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	files := `
    17  -- config.toml --
    18  baseURL = 'http://example.com/'
    19  printUnusedTemplates=true
    20  -- content/p1.md --
    21  ---
    22  title: "P1"
    23  ---
    24  {{< usedshortcode >}}
    25  -- layouts/baseof.html --
    26  {{ block "main" . }}{{ end }}
    27  -- layouts/baseof.json --
    28  {{ block "main" . }}{{ end }}
    29  -- layouts/index.html --
    30  {{ define "main" }}FOO{{ end }}
    31  -- layouts/_default/single.json --
    32  -- layouts/_default/single.html --
    33  {{ define "main" }}MAIN{{ end }}
    34  -- layouts/post/single.html --
    35  {{ define "main" }}MAIN{{ end }}
    36  -- layouts/partials/usedpartial.html --
    37  -- layouts/partials/unusedpartial.html --
    38  -- layouts/shortcodes/usedshortcode.html --
    39  {{ partial "usedpartial.html" }}
    40  -- layouts/shortcodes/unusedshortcode.html --
    41  
    42  	`
    43  
    44  	b := hugolib.NewIntegrationTestBuilder(
    45  		hugolib.IntegrationTestConfig{
    46  			T:           t,
    47  			TxtarString: files,
    48  			NeedsOsFS:   true,
    49  		},
    50  	)
    51  	b.Build()
    52  
    53  	unused := b.H.Tmpl().(tpl.UnusedTemplatesProvider).UnusedTemplates()
    54  
    55  	var names []string
    56  	for _, tmpl := range unused {
    57  		names = append(names, tmpl.Name())
    58  	}
    59  
    60  	b.Assert(names, qt.DeepEquals, []string{"_default/single.json", "baseof.json", "partials/unusedpartial.html", "post/single.html", "shortcodes/unusedshortcode.html"})
    61  	b.Assert(unused[0].Filename(), qt.Equals, filepath.Join(b.Cfg.WorkingDir, "layouts/_default/single.json"))
    62  }
    63  
    64  // Verify that the new keywords in Go 1.18 is available.
    65  func TestGo18Constructs(t *testing.T) {
    66  	t.Parallel()
    67  
    68  	files := `
    69  -- config.toml --
    70  baseURL = 'http://example.com/'
    71  disableKinds = ["section", "home", "rss", "taxonomy",  "term", "rss"]
    72  -- content/p1.md --
    73  ---
    74  title: "P1"
    75  ---
    76  -- layouts/partials/counter.html --
    77  {{ if .Scratch.Get "counter" }}{{ .Scratch.Add "counter" 1 }}{{ else }}{{ .Scratch.Set "counter" 1 }}{{ end }}{{ return true }}
    78  -- layouts/_default/single.html --
    79  continue:{{ range seq 5 }}{{ if eq . 2 }}{{continue}}{{ end }}{{ . }}{{ end }}:END:
    80  break:{{ range seq 5 }}{{ if eq . 2 }}{{break}}{{ end }}{{ . }}{{ end }}:END:
    81  continue2:{{ range seq 5 }}{{ if eq . 2 }}{{ continue }}{{ end }}{{ . }}{{ end }}:END:
    82  break2:{{ range seq 5 }}{{ if eq . 2 }}{{ break }}{{ end }}{{ . }}{{ end }}:END:
    83  
    84  counter1: {{ partial "counter.html" . }}/{{ .Scratch.Get "counter" }}
    85  and1: {{ if (and false (partial "counter.html" .)) }}true{{ else }}false{{ end }}
    86  or1: {{ if (or true (partial "counter.html" .)) }}true{{ else }}false{{ end }}
    87  and2: {{ if (and true (partial "counter.html" .)) }}true{{ else }}false{{ end }}
    88  or2: {{ if (or false (partial "counter.html" .)) }}true{{ else }}false{{ end }}
    89  
    90  
    91  counter2: {{ .Scratch.Get "counter" }}
    92  
    93  
    94  	`
    95  
    96  	b := hugolib.NewIntegrationTestBuilder(
    97  		hugolib.IntegrationTestConfig{
    98  			T:           t,
    99  			TxtarString: files,
   100  			NeedsOsFS:   true,
   101  		},
   102  	)
   103  	b.Build()
   104  
   105  	b.AssertFileContent("public/p1/index.html", `
   106  continue:1345:END:
   107  break:1:END:
   108  continue2:1345:END:
   109  break2:1:END:
   110  counter1: true/1
   111  and1: false
   112  or1: true
   113  and2: true
   114  or2: true
   115  counter2: 3
   116  `)
   117  
   118  }
   119  
   120  // Issue 10495
   121  func TestCommentsBeforeBlockDefinition(t *testing.T) {
   122  	t.Parallel()
   123  
   124  	files := `
   125  -- config.toml --
   126  baseURL = 'http://example.com/'
   127  -- content/s1/p1.md --
   128  ---
   129  title: "S1P1"
   130  ---
   131  -- content/s2/p1.md --
   132  ---
   133  title: "S2P1"
   134  ---
   135  -- content/s3/p1.md --
   136  ---
   137  title: "S3P1"
   138  ---
   139  -- layouts/_default/baseof.html --
   140  {{ block "main" . }}{{ end }}
   141  -- layouts/s1/single.html --
   142  {{/* foo */}}
   143  {{ define "main" }}{{ .Title }}{{ end }}
   144  -- layouts/s2/single.html --
   145  {{- /* foo */}}
   146  {{ define "main" }}{{ .Title }}{{ end }}
   147  -- layouts/s3/single.html --
   148  {{- /* foo */ -}}
   149  {{ define "main" }}{{ .Title }}{{ end }}
   150  	`
   151  
   152  	b := hugolib.NewIntegrationTestBuilder(
   153  		hugolib.IntegrationTestConfig{
   154  			T:           t,
   155  			TxtarString: files,
   156  		},
   157  	)
   158  	b.Build()
   159  
   160  	b.AssertFileContent("public/s1/p1/index.html", `S1P1`)
   161  	b.AssertFileContent("public/s2/p1/index.html", `S2P1`)
   162  	b.AssertFileContent("public/s3/p1/index.html", `S3P1`)
   163  }
   164  
   165  func TestGoTemplateBugs(t *testing.T) {
   166  
   167  	t.Run("Issue 11112", func(t *testing.T) {
   168  		t.Parallel()
   169  
   170  		files := `
   171  -- config.toml --
   172  -- layouts/index.html --
   173  {{ $m := dict "key" "value" }}
   174  {{ $k := "" }}
   175  {{ $v := "" }}
   176  {{ range $k, $v = $m }}
   177  {{ $k }} = {{ $v }}
   178  {{ end }}
   179  	`
   180  
   181  		b := hugolib.NewIntegrationTestBuilder(
   182  			hugolib.IntegrationTestConfig{
   183  				T:           t,
   184  				TxtarString: files,
   185  			},
   186  		)
   187  		b.Build()
   188  
   189  		b.AssertFileContent("public/index.html", `key = value`)
   190  	})
   191  
   192  }
   193  
   194  func TestSecurityAllowActionJSTmpl(t *testing.T) {
   195  
   196  	filesTemplate := `
   197  -- config.toml --
   198  SECURITYCONFIG
   199  -- layouts/index.html --
   200  <script>
   201  var a = §§{{.Title }}§§;
   202  </script>
   203  	`
   204  
   205  	files := strings.ReplaceAll(filesTemplate, "SECURITYCONFIG", "")
   206  
   207  	b, err := hugolib.NewIntegrationTestBuilder(
   208  		hugolib.IntegrationTestConfig{
   209  			T:           t,
   210  			TxtarString: files,
   211  		},
   212  	).BuildE()
   213  
   214  	b.Assert(err, qt.Not(qt.IsNil))
   215  	b.Assert(err.Error(), qt.Contains, "{{.Title}} appears in a JS template literal")
   216  
   217  	files = strings.ReplaceAll(filesTemplate, "SECURITYCONFIG", `
   218  [security]
   219  [security.gotemplates]
   220  allowActionJSTmpl = true		
   221  `)
   222  
   223  	b = hugolib.NewIntegrationTestBuilder(
   224  		hugolib.IntegrationTestConfig{
   225  			T:           t,
   226  			TxtarString: files,
   227  		},
   228  	).Build()
   229  
   230  }