github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/tpl/templates/integration_test.go (about)

     1  // Copyright 2022 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package templates_test
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/gohugoio/hugo/hugolib"
    20  )
    21  
    22  func TestExists(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	files := `
    26  -- config.toml --
    27  baseURL = 'http://example.com/'
    28  -- layouts/index.html --
    29  index.html: {{ templates.Exists "index.html" }}
    30  post/single.html: {{ templates.Exists "post/single.html" }}
    31  partials/foo.html: {{ templates.Exists "partials/foo.html" }}
    32  partials/doesnotexist.html: {{ templates.Exists "partials/doesnotexist.html" }}
    33  -- layouts/post/single.html --
    34  -- layouts/partials/foo.html --
    35    `
    36  
    37  	b := hugolib.NewIntegrationTestBuilder(
    38  		hugolib.IntegrationTestConfig{
    39  			T:           t,
    40  			TxtarString: files,
    41  		},
    42  	).Build()
    43  
    44  	b.AssertFileContent("public/index.html", `
    45  index.html: true
    46  post/single.html: true
    47  partials/foo.html: true
    48  partials/doesnotexist.html: false  
    49  `)
    50  }
    51  
    52  func TestExistsWithBaseOf(t *testing.T) {
    53  	t.Parallel()
    54  
    55  	files := `
    56  -- config.toml --
    57  baseURL = 'http://example.com/'
    58  -- layouts/baseof.html --
    59  {{ block "main" . }}{{ end }}
    60  -- layouts/index.html --
    61  {{ define "main" }}
    62  index.html: {{ templates.Exists "index.html" }}
    63  post/single.html: {{ templates.Exists "post/single.html" }}
    64  post/doesnotexist.html: {{ templates.Exists "post/doesnotexist.html" }}
    65  {{ end }}
    66  -- layouts/post/single.html --
    67  {{ define "main" }}MAIN{{ end }}
    68  
    69  
    70  `
    71  
    72  	b := hugolib.NewIntegrationTestBuilder(
    73  		hugolib.IntegrationTestConfig{
    74  			T:           t,
    75  			TxtarString: files,
    76  		},
    77  	).Build()
    78  
    79  	b.AssertFileContent("public/index.html", `
    80  index.html: true
    81  post/single.html: true
    82  post/doesnotexist.html: false
    83  
    84  `)
    85  }
    86  
    87  
    88  // See #10774
    89  func TestPageFunctionExists(t *testing.T) {
    90  	t.Parallel()
    91  
    92  	files := `
    93  -- config.toml --
    94  baseURL = 'http://example.com/'
    95  -- layouts/index.html --
    96  Home: {{ page.IsHome }}
    97  
    98  `
    99  
   100  	b := hugolib.NewIntegrationTestBuilder(
   101  		hugolib.IntegrationTestConfig{
   102  			T:           t,
   103  			TxtarString: files,
   104  		},
   105  	).Build()
   106  
   107  	b.AssertFileContent("public/index.html", `
   108  Home: true
   109  
   110  `)
   111  }