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
14package hugolib
15
16import (
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
32func 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(`
44workingDir = %q
45
46[module]
47[[module.mounts]]
48source = 'content'
49target = 'content'
50excludeFiles = "/a/c/**"
51[[module.mounts]]
52source = 'static'
53target = 'static'
54[[module.mounts]]
55source = 'layouts'
56target = 'layouts'
57excludeFiles = "/**/foo.html"
58[[module.mounts]]
59source = 'data'
60target = 'data'
61includeFiles = "/mydata/**"
62[[module.mounts]]
63source = 'assets'
64target = 'assets'
65excludeFiles = ["/**exclude.*", "/moooo.*"]
66[[module.mounts]]
67source = 'i18n'
68target = 'i18n'
69[[module.mounts]]
70source = 'archetypes'
71target = '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
91Data: {{ site.Data }}:END
92
93Template: {{ templates.Exists "partials/foo.html" }}:END
94Resource1: {{ resources.Get "js/include.js" }}:END
95Resource2: {{ resources.Get "js/exclude.js" }}:END
96Resource3: {{ resources.Get "exclude.txt" }}:END
97Resources: {{ 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(filepath.Join(workingDir, 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(workingDir, "public", "index.html"), `
111Data: map[mydata:map[b:map[b1:bval]]]:END
112Template: false
113Resource1: js/include.js:END
114Resource2: :END
115Resource3: :END
116Resources: [js/include.js]
117`)
118
119}
120