github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/hugolib/mount_filters_test.go (about)

     1  // Copyright 2021 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 hugolib
    15  
    16  import (
    17  	"fmt"
    18  	"os"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/gohugoio/hugo/common/loggers"
    23  
    24  	"github.com/gohugoio/hugo/hugofs/files"
    25  
    26  	"github.com/gohugoio/hugo/htesting"
    27  	"github.com/gohugoio/hugo/hugofs"
    28  
    29  	qt "github.com/frankban/quicktest"
    30  )
    31  
    32  func TestMountFilters(t *testing.T) {
    33  	t.Parallel()
    34  	b := newTestSitesBuilder(t)
    35  	workingDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-test-mountfilters")
    36  	b.Assert(err, qt.IsNil)
    37  	defer clean()
    38  
    39  	for _, component := range files.ComponentFolders {
    40  		b.Assert(os.MkdirAll(filepath.Join(workingDir, component), 0777), qt.IsNil)
    41  	}
    42  	b.WithWorkingDir(workingDir).WithLogger(loggers.NewInfoLogger())
    43  	b.WithConfigFile("toml", fmt.Sprintf(`
    44  workingDir = %q
    45  
    46  [module]
    47  [[module.mounts]]
    48  source = 'content'
    49  target = 'content'
    50  excludeFiles = "/a/c/**"
    51  [[module.mounts]]
    52  source = 'static'
    53  target = 'static'
    54  [[module.mounts]]
    55  source = 'layouts'
    56  target = 'layouts'
    57  excludeFiles = "/**/foo.html"
    58  [[module.mounts]]
    59  source = 'data'
    60  target = 'data'
    61  includeFiles = "/mydata/**"
    62  [[module.mounts]]
    63  source = 'assets'
    64  target = 'assets'
    65  excludeFiles = ["/**exclude.*", "/moooo.*"]
    66  [[module.mounts]]
    67  source = 'i18n'
    68  target = 'i18n'
    69  [[module.mounts]]
    70  source = 'archetypes'
    71  target = 'archetypes'
    72  
    73  	
    74  `, workingDir))
    75  
    76  	b.WithContent("/a/b/p1.md", "---\ntitle: Include\n---")
    77  	b.WithContent("/a/c/p2.md", "---\ntitle: Exclude\n---")
    78  
    79  	b.WithSourceFile(
    80  		"data/mydata/b.toml", `b1='bval'`,
    81  		"data/nodata/c.toml", `c1='bval'`,
    82  		"layouts/partials/foo.html", `foo`,
    83  		"assets/exclude.txt", `foo`,
    84  		"assets/js/exclude.js", `foo`,
    85  		"assets/js/include.js", `foo`,
    86  		"assets/js/exclude.js", `foo`,
    87  	)
    88  
    89  	b.WithTemplatesAdded("index.html", `
    90  
    91  Data: {{ site.Data }}:END
    92  
    93  Template: {{ templates.Exists "partials/foo.html" }}:END
    94  Resource1: {{ resources.Get "js/include.js" }}:END
    95  Resource2: {{ resources.Get "js/exclude.js" }}:END
    96  Resource3: {{ resources.Get "exclude.txt" }}:END
    97  Resources: {{ resources.Match "**.js" }}
    98  `)
    99  
   100  	b.Build(BuildCfg{})
   101  
   102  	assertExists := func(name string, shouldExist bool) {
   103  		b.Helper()
   104  		b.Assert(b.CheckExists(name), qt.Equals, shouldExist)
   105  	}
   106  
   107  	assertExists("public/a/b/p1/index.html", true)
   108  	assertExists("public/a/c/p2/index.html", false)
   109  
   110  	b.AssertFileContent(filepath.Join("public", "index.html"), `
   111  Data: map[mydata:map[b:map[b1:bval]]]:END	
   112  Template: false
   113  Resource1: js/include.js:END
   114  Resource2: :END
   115  Resource3: :END
   116  Resources: [js/include.js]
   117  `)
   118  
   119  }