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