| 1 |
// Copyright 2024 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 source_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"path/filepath" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/gohugoio/hugo/config" |
| 22 |
"github.com/gohugoio/hugo/config/testconfig" |
| 23 |
"github.com/gohugoio/hugo/helpers" |
| 24 |
"github.com/gohugoio/hugo/source" |
| 25 |
"github.com/spf13/afero" |
| 26 |
|
| 27 |
qt "github.com/frankban/quicktest" |
| 28 |
"github.com/gohugoio/hugo/hugofs" |
| 29 |
) |
| 30 |
|
| 31 |
func TestIgnoreDotFilesAndDirectories(t *testing.T) { |
| 32 |
c := qt.New(t) |
| 33 |
|
| 34 |
tests := []struct { |
| 35 |
path string |
| 36 |
ignore bool |
| 37 |
ignoreFilesRegexpes any |
| 38 |
}{ |
| 39 |
{".foobar/", true, nil}, |
| 40 |
{"foobar/.barfoo/", true, nil}, |
| 41 |
{"barfoo.md", false, nil}, |
| 42 |
{"foobar/barfoo.md", false, nil}, |
| 43 |
{"foobar/.barfoo.md", true, nil}, |
| 44 |
{".barfoo.md", true, nil}, |
| 45 |
{".md", true, nil}, |
| 46 |
{"foobar/barfoo.md~", true, nil}, |
| 47 |
{".foobar/barfoo.md~", true, nil}, |
| 48 |
{"foobar~/barfoo.md", false, nil}, |
| 49 |
{"foobar/bar~foo.md", false, nil}, |
| 50 |
{"foobar/foo.md", true, []string{"\\.md$", "\\.boo$"}}, |
| 51 |
{"foobar/foo.html", false, []string{"\\.md$", "\\.boo$"}}, |
| 52 |
{"foobar/foo.md", true, []string{"foo.md$"}}, |
| 53 |
{"foobar/foo.md", true, []string{".*", "\\.md$", "\\.boo$"}}, |
| 54 |
{"foobar/.#content.md", true, []string{"/\\.#"}}, |
| 55 |
{".#foobar.md", true, []string{"^\\.#"}}, |
| 56 |
} |
| 57 |
|
| 58 |
for i, test := range tests { |
| 59 |
c.Run(fmt.Sprintf("[%d] %s", i, test.path), func(c *qt.C) { |
| 60 |
c.Parallel() |
| 61 |
v := config.New() |
| 62 |
v.Set("ignoreFiles", test.ignoreFilesRegexpes) |
| 63 |
v.Set("publishDir", "public") |
| 64 |
afs := afero.NewMemMapFs() |
| 65 |
conf := testconfig.GetTestConfig(afs, v) |
| 66 |
fs := hugofs.NewFromOld(afs, v) |
| 67 |
ps, err := helpers.NewPathSpec(fs, conf, nil, nil) |
| 68 |
c.Assert(err, qt.IsNil) |
| 69 |
|
| 70 |
s := source.NewSourceSpec(ps, nil, fs.Source) |
| 71 |
|
| 72 |
if ignored := s.IgnoreFile(filepath.FromSlash(test.path)); test.ignore != ignored { |
| 73 |
t.Errorf("[%d] File not ignored", i) |
| 74 |
} |
| 75 |
}) |
| 76 |
|
| 77 |
} |
| 78 |
} |