| 1 |
// Copyright 2019 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 hugofs |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"path/filepath" |
| 19 |
"sort" |
| 20 |
"testing" |
| 21 |
|
| 22 |
iofs "io/fs" |
| 23 |
|
| 24 |
"github.com/gohugoio/hugo/common/hmaps" |
| 25 |
"github.com/gohugoio/hugo/config" |
| 26 |
"github.com/gohugoio/hugo/hugofs/hglob" |
| 27 |
"github.com/gohugoio/hugo/hugolib/sitesmatrix" |
| 28 |
|
| 29 |
qt "github.com/frankban/quicktest" |
| 30 |
"github.com/gohugoio/hugo/htesting" |
| 31 |
"github.com/spf13/afero" |
| 32 |
) |
| 33 |
|
| 34 |
const ( |
| 35 |
eni = iota |
| 36 |
svi |
| 37 |
noi |
| 38 |
fri |
| 39 |
) |
| 40 |
|
| 41 |
var ( |
| 42 |
en = sitesMatrixForLangs(eni) |
| 43 |
sv = sitesMatrixForLangs(svi) |
| 44 |
no = sitesMatrixForLangs(noi) |
| 45 |
fr = sitesMatrixForLangs(fri) |
| 46 |
) |
| 47 |
|
| 48 |
func TestLanguageRootMapping(t *testing.T) { |
| 49 |
c := qt.New(t) |
| 50 |
v := config.New() |
| 51 |
v.Set("contentDir", "content") |
| 52 |
|
| 53 |
fs := NewBaseFileDecorator(afero.NewMemMapFs()) |
| 54 |
|
| 55 |
c.Assert(afero.WriteFile(fs, filepath.Join("content/sv/svdir", "main.txt"), []byte("main sv"), 0o755), qt.IsNil) |
| 56 |
|
| 57 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/a/mysvblogcontent", "sv-f.txt"), []byte("some sv blog content"), 0o755), qt.IsNil) |
| 58 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/a/myenblogcontent", "en-f.txt"), []byte("some en blog content in a"), 0o755), qt.IsNil) |
| 59 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/a/mysvblogcontent/d1", "sv-d1-f.txt"), []byte("some sv blog content"), 0o755), qt.IsNil) |
| 60 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/a/myenblogcontent/d1", "en-d1-f.txt"), []byte("some en blog content in a"), 0o755), qt.IsNil) |
| 61 |
|
| 62 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/a/myotherenblogcontent", "en-f2.txt"), []byte("some en content"), 0o755), qt.IsNil) |
| 63 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/a/mysvdocs", "sv-docs.txt"), []byte("some sv docs content"), 0o755), qt.IsNil) |
| 64 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/b/myenblogcontent", "en-b-f.txt"), []byte("some en content"), 0o755), qt.IsNil) |
| 65 |
|
| 66 |
rfs, err := NewRootMappingFs(fs, |
| 67 |
&RootMapping{ |
| 68 |
From: "content/blog", // Virtual path, first element is one of content, static, layouts etc. |
| 69 |
To: "themes/a/mysvblogcontent", // Real path |
| 70 |
Meta: &FileMeta{SitesMatrix: sv}, |
| 71 |
}, |
| 72 |
&RootMapping{ |
| 73 |
From: "content/blog", |
| 74 |
To: "themes/a/myenblogcontent", |
| 75 |
Meta: &FileMeta{SitesMatrix: en}, |
| 76 |
}, |
| 77 |
&RootMapping{ |
| 78 |
From: "content/blog", |
| 79 |
To: "content/sv", |
| 80 |
Meta: &FileMeta{SitesMatrix: sv}, |
| 81 |
}, |
| 82 |
&RootMapping{ |
| 83 |
From: "content/blog", |
| 84 |
To: "themes/a/myotherenblogcontent", |
| 85 |
Meta: &FileMeta{SitesMatrix: en}, |
| 86 |
}, |
| 87 |
&RootMapping{ |
| 88 |
From: "content/docs", |
| 89 |
To: "themes/a/mysvdocs", |
| 90 |
Meta: &FileMeta{SitesMatrix: sv}, |
| 91 |
}, |
| 92 |
) |
| 93 |
|
| 94 |
c.Assert(err, qt.IsNil) |
| 95 |
|
| 96 |
collected, err := collectPaths(rfs, "content") |
| 97 |
c.Assert(err, qt.IsNil) |
| 98 |
c.Assert(collected, qt.DeepEquals, |
| 99 |
[]string{"/blog/d1/en-d1-f.txt", "/blog/d1/sv-d1-f.txt", "/blog/en-f.txt", "/blog/en-f2.txt", "/blog/sv-f.txt", "/blog/svdir/main.txt", "/docs/sv-docs.txt"}, qt.Commentf("%#v", collected)) |
| 100 |
|
| 101 |
dirs, err := rfs.Mounts(filepath.FromSlash("content/blog")) |
| 102 |
c.Assert(err, qt.IsNil) |
| 103 |
c.Assert(len(dirs), qt.Equals, 4) |
| 104 |
for _, dir := range dirs { |
| 105 |
f, err := dir.Meta().Open() |
| 106 |
c.Assert(err, qt.IsNil) |
| 107 |
f.Close() |
| 108 |
} |
| 109 |
|
| 110 |
blog, err := rfs.Open(filepath.FromSlash("content/blog")) |
| 111 |
c.Assert(err, qt.IsNil) |
| 112 |
fis, err := blog.(iofs.ReadDirFile).ReadDir(-1) |
| 113 |
c.Assert(err, qt.IsNil) |
| 114 |
for _, fi := range fis { |
| 115 |
f, err := fi.(FileMetaInfo).Meta().Open() |
| 116 |
c.Assert(err, qt.IsNil) |
| 117 |
f.Close() |
| 118 |
} |
| 119 |
blog.Close() |
| 120 |
|
| 121 |
getDirnames := func(name string, rfs *RootMappingFs) []string { |
| 122 |
c.Helper() |
| 123 |
filename := filepath.FromSlash(name) |
| 124 |
f, err := rfs.Open(filename) |
| 125 |
c.Assert(err, qt.IsNil) |
| 126 |
names, err := f.Readdirnames(-1) |
| 127 |
|
| 128 |
f.Close() |
| 129 |
c.Assert(err, qt.IsNil) |
| 130 |
|
| 131 |
info, err := rfs.Stat(filename) |
| 132 |
c.Assert(err, qt.IsNil) |
| 133 |
f2, err := info.(FileMetaInfo).Meta().Open() |
| 134 |
c.Assert(err, qt.IsNil) |
| 135 |
names2, err := f2.Readdirnames(-1) |
| 136 |
c.Assert(err, qt.IsNil) |
| 137 |
c.Assert(names2, qt.DeepEquals, names) |
| 138 |
f2.Close() |
| 139 |
|
| 140 |
return names |
| 141 |
} |
| 142 |
rfsEn := rfs.Filter(func(rm *RootMapping) bool { |
| 143 |
return rm.Meta.SitesMatrix.HasLanguage(eni) |
| 144 |
}) |
| 145 |
|
| 146 |
c.Assert(getDirnames("content/blog", rfsEn), qt.DeepEquals, []string{"d1", "en-f.txt", "en-f2.txt"}) |
| 147 |
|
| 148 |
rfsSv := rfs.Filter(func(rm *RootMapping) bool { |
| 149 |
return rm.Meta.SitesMatrix.HasLanguage(svi) |
| 150 |
}) |
| 151 |
|
| 152 |
c.Assert(getDirnames("content/blog", rfsSv), qt.DeepEquals, []string{"d1", "sv-f.txt", "svdir"}) |
| 153 |
|
| 154 |
// Make sure we have not messed with the original |
| 155 |
c.Assert(getDirnames("content/blog", rfs), qt.DeepEquals, []string{"d1", "sv-f.txt", "en-f.txt", "svdir", "en-f2.txt"}) |
| 156 |
|
| 157 |
c.Assert(getDirnames("content", rfsSv), qt.DeepEquals, []string{"blog", "docs"}) |
| 158 |
c.Assert(getDirnames("content", rfs), qt.DeepEquals, []string{"blog", "docs"}) |
| 159 |
} |
| 160 |
|
| 161 |
func TestRootMappingFsDirnames(t *testing.T) { |
| 162 |
c := qt.New(t) |
| 163 |
fs := NewBaseFileDecorator(afero.NewMemMapFs()) |
| 164 |
|
| 165 |
testfile := "myfile.txt" |
| 166 |
c.Assert(fs.Mkdir("f1t", 0o755), qt.IsNil) |
| 167 |
c.Assert(fs.Mkdir("f2t", 0o755), qt.IsNil) |
| 168 |
c.Assert(fs.Mkdir("f3t", 0o755), qt.IsNil) |
| 169 |
c.Assert(afero.WriteFile(fs, filepath.Join("f2t", testfile), []byte("some content"), 0o755), qt.IsNil) |
| 170 |
|
| 171 |
rfs, err := newRootMappingFsFromFromTo("", fs, "static/bf1", "f1t", "static/cf2", "f2t", "static/af3", "f3t") |
| 172 |
c.Assert(err, qt.IsNil) |
| 173 |
|
| 174 |
fif, err := rfs.Stat(filepath.Join("static/cf2", testfile)) |
| 175 |
c.Assert(err, qt.IsNil) |
| 176 |
c.Assert(fif.Name(), qt.Equals, "myfile.txt") |
| 177 |
fifm := fif.(FileMetaInfo).Meta() |
| 178 |
c.Assert(fifm.Filename, qt.Equals, filepath.FromSlash("f2t/myfile.txt")) |
| 179 |
|
| 180 |
root, err := rfs.Open("static") |
| 181 |
c.Assert(err, qt.IsNil) |
| 182 |
|
| 183 |
dirnames, err := root.Readdirnames(-1) |
| 184 |
c.Assert(err, qt.IsNil) |
| 185 |
c.Assert(dirnames, qt.DeepEquals, []string{"af3", "bf1", "cf2"}) |
| 186 |
} |
| 187 |
|
| 188 |
func TestRootMappingFsFilename(t *testing.T) { |
| 189 |
c := qt.New(t) |
| 190 |
workDir, clean, err := htesting.CreateTempDir(Os, "hugo-root-filename") |
| 191 |
c.Assert(err, qt.IsNil) |
| 192 |
defer clean() |
| 193 |
fs := NewBaseFileDecorator(Os) |
| 194 |
|
| 195 |
testfilename := filepath.Join(workDir, "f1t/foo/file.txt") |
| 196 |
|
| 197 |
c.Assert(fs.MkdirAll(filepath.Join(workDir, "f1t/foo"), 0o777), qt.IsNil) |
| 198 |
c.Assert(afero.WriteFile(fs, testfilename, []byte("content"), 0o666), qt.IsNil) |
| 199 |
|
| 200 |
rfs, err := newRootMappingFsFromFromTo(workDir, fs, "static/f1", filepath.Join(workDir, "f1t"), "static/f2", filepath.Join(workDir, "f2t")) |
| 201 |
c.Assert(err, qt.IsNil) |
| 202 |
|
| 203 |
fi, err := rfs.Stat(filepath.FromSlash("static/f1/foo/file.txt")) |
| 204 |
c.Assert(err, qt.IsNil) |
| 205 |
fim := fi.(FileMetaInfo) |
| 206 |
c.Assert(fim.Meta().Filename, qt.Equals, testfilename) |
| 207 |
_, err = rfs.Stat(filepath.FromSlash("static/f1")) |
| 208 |
c.Assert(err, qt.IsNil) |
| 209 |
} |
| 210 |
|
| 211 |
func TestRootMappingFsMount(t *testing.T) { |
| 212 |
c := qt.New(t) |
| 213 |
fs := NewBaseFileDecorator(afero.NewMemMapFs()) |
| 214 |
|
| 215 |
testfile := "test.txt" |
| 216 |
|
| 217 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/a/mynoblogcontent", testfile), []byte("some no content"), 0o755), qt.IsNil) |
| 218 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/a/myenblogcontent", testfile), []byte("some en content"), 0o755), qt.IsNil) |
| 219 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/a/mysvblogcontent", testfile), []byte("some sv content"), 0o755), qt.IsNil) |
| 220 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/a/mysvblogcontent", "other.txt"), []byte("some sv content"), 0o755), qt.IsNil) |
| 221 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/a/singlefiles", "no.txt"), []byte("no text"), 0o755), qt.IsNil) |
| 222 |
c.Assert(afero.WriteFile(fs, filepath.Join("themes/a/singlefiles", "sv.txt"), []byte("sv text"), 0o755), qt.IsNil) |
| 223 |
|
| 224 |
bfs := NewBasePathFs(fs, "themes/a") |
| 225 |
rm := []*RootMapping{ |
| 226 |
// Directories |
| 227 |
{ |
| 228 |
From: "content/blog", |
| 229 |
To: "mynoblogcontent", |
| 230 |
Meta: &FileMeta{SitesMatrix: no}, |
| 231 |
}, |
| 232 |
{ |
| 233 |
From: "content/blog", |
| 234 |
To: "myenblogcontent", |
| 235 |
Meta: &FileMeta{SitesMatrix: en}, |
| 236 |
}, |
| 237 |
{ |
| 238 |
From: "content/blog", |
| 239 |
To: "mysvblogcontent", |
| 240 |
Meta: &FileMeta{SitesMatrix: sv}, |
| 241 |
}, |
| 242 |
// Files |
| 243 |
{ |
| 244 |
From: "content/singles/p1.md", |
| 245 |
To: "singlefiles/no.txt", |
| 246 |
ToBase: "singlefiles", |
| 247 |
Meta: &FileMeta{SitesMatrix: no}, |
| 248 |
}, |
| 249 |
{ |
| 250 |
From: "content/singles/p1.md", |
| 251 |
To: "singlefiles/sv.txt", |
| 252 |
ToBase: "singlefiles", |
| 253 |
Meta: &FileMeta{SitesMatrix: sv}, |
| 254 |
}, |
| 255 |
} |
| 256 |
|
| 257 |
rfs, err := NewRootMappingFs(bfs, rm...) |
| 258 |
c.Assert(err, qt.IsNil) |
| 259 |
|
| 260 |
blog, err := rfs.Stat(filepath.FromSlash("content/blog")) |
| 261 |
c.Assert(err, qt.IsNil) |
| 262 |
c.Assert(blog.IsDir(), qt.Equals, true) |
| 263 |
blogm := blog.(FileMetaInfo).Meta() |
| 264 |
c.Assert(blogm.SitesMatrix.HasLanguage(noi), qt.IsTrue) // First match |
| 265 |
|
| 266 |
f, err := blogm.Open() |
| 267 |
c.Assert(err, qt.IsNil) |
| 268 |
defer f.Close() |
| 269 |
dirs1, err := f.Readdirnames(-1) |
| 270 |
c.Assert(err, qt.IsNil) |
| 271 |
// Union with duplicate dir names filtered. |
| 272 |
c.Assert(dirs1, qt.DeepEquals, []string{"test.txt", "test.txt", "other.txt", "test.txt"}) |
| 273 |
|
| 274 |
d, err := rfs.Open(filepath.FromSlash("content/blog")) |
| 275 |
c.Assert(err, qt.IsNil) |
| 276 |
files, err := d.(iofs.ReadDirFile).ReadDir(-1) |
| 277 |
c.Assert(err, qt.IsNil) |
| 278 |
c.Assert(len(files), qt.Equals, 4) |
| 279 |
|
| 280 |
singlesDir, err := rfs.Open(filepath.FromSlash("content/singles")) |
| 281 |
c.Assert(err, qt.IsNil) |
| 282 |
defer singlesDir.Close() |
| 283 |
singles, err := singlesDir.(iofs.ReadDirFile).ReadDir(-1) |
| 284 |
c.Assert(err, qt.IsNil) |
| 285 |
c.Assert(singles, qt.HasLen, 2) |
| 286 |
for i, langi := range []int{noi, svi} { |
| 287 |
fi := singles[i].(FileMetaInfo) |
| 288 |
c.Assert(fi.Meta().SitesMatrix.HasLanguage(langi), qt.IsTrue) |
| 289 |
c.Assert(fi.Name(), qt.Equals, "p1.md") |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
func TestRootMappingFsMountOverlap(t *testing.T) { |
| 294 |
c := qt.New(t) |
| 295 |
fs := NewBaseFileDecorator(afero.NewMemMapFs()) |
| 296 |
|
| 297 |
c.Assert(afero.WriteFile(fs, filepath.FromSlash("da/a.txt"), []byte("some no content"), 0o755), qt.IsNil) |
| 298 |
c.Assert(afero.WriteFile(fs, filepath.FromSlash("db/b.txt"), []byte("some no content"), 0o755), qt.IsNil) |
| 299 |
c.Assert(afero.WriteFile(fs, filepath.FromSlash("dc/c.txt"), []byte("some no content"), 0o755), qt.IsNil) |
| 300 |
c.Assert(afero.WriteFile(fs, filepath.FromSlash("de/e.txt"), []byte("some no content"), 0o755), qt.IsNil) |
| 301 |
|
| 302 |
rm := []*RootMapping{ |
| 303 |
{ |
| 304 |
From: "static", |
| 305 |
To: "da", |
| 306 |
}, |
| 307 |
{ |
| 308 |
From: "static/b", |
| 309 |
To: "db", |
| 310 |
}, |
| 311 |
{ |
| 312 |
From: "static/b/c", |
| 313 |
To: "dc", |
| 314 |
}, |
| 315 |
{ |
| 316 |
From: "/static/e/", |
| 317 |
To: "de", |
| 318 |
}, |
| 319 |
} |
| 320 |
|
| 321 |
rfs, err := NewRootMappingFs(fs, rm...) |
| 322 |
c.Assert(err, qt.IsNil) |
| 323 |
|
| 324 |
checkDirnames := func(name string, expect []string) { |
| 325 |
c.Helper() |
| 326 |
name = filepath.FromSlash(name) |
| 327 |
f, err := rfs.Open(name) |
| 328 |
c.Assert(err, qt.IsNil) |
| 329 |
defer f.Close() |
| 330 |
names, err := f.Readdirnames(-1) |
| 331 |
c.Assert(err, qt.IsNil) |
| 332 |
c.Assert(names, qt.DeepEquals, expect, qt.Commentf(fmt.Sprintf("%#v", names))) |
| 333 |
} |
| 334 |
|
| 335 |
checkDirnames("static", []string{"a.txt", "b", "e"}) |
| 336 |
checkDirnames("static/b", []string{"b.txt", "c"}) |
| 337 |
checkDirnames("static/b/c", []string{"c.txt"}) |
| 338 |
|
| 339 |
fi, err := rfs.Stat(filepath.FromSlash("static/b/b.txt")) |
| 340 |
c.Assert(err, qt.IsNil) |
| 341 |
c.Assert(fi.Name(), qt.Equals, "b.txt") |
| 342 |
} |
| 343 |
|
| 344 |
func TestRootMappingFsOs(t *testing.T) { |
| 345 |
c := qt.New(t) |
| 346 |
fs := NewBaseFileDecorator(afero.NewOsFs()) |
| 347 |
|
| 348 |
d, clean, err := htesting.CreateTempDir(fs, "hugo-root-mapping-os") |
| 349 |
c.Assert(err, qt.IsNil) |
| 350 |
defer clean() |
| 351 |
|
| 352 |
testfile := "myfile.txt" |
| 353 |
c.Assert(fs.Mkdir(filepath.Join(d, "f1t"), 0o755), qt.IsNil) |
| 354 |
c.Assert(fs.Mkdir(filepath.Join(d, "f2t"), 0o755), qt.IsNil) |
| 355 |
c.Assert(fs.Mkdir(filepath.Join(d, "f3t"), 0o755), qt.IsNil) |
| 356 |
|
| 357 |
// Deep structure |
| 358 |
deepDir := filepath.Join(d, "d1", "d2", "d3", "d4", "d5") |
| 359 |
c.Assert(fs.MkdirAll(deepDir, 0o755), qt.IsNil) |
| 360 |
for i := 1; i <= 3; i++ { |
| 361 |
c.Assert(fs.MkdirAll(filepath.Join(d, "d1", "d2", "d3", "d4", fmt.Sprintf("d4-%d", i)), 0o755), qt.IsNil) |
| 362 |
c.Assert(afero.WriteFile(fs, filepath.Join(d, "d1", "d2", "d3", fmt.Sprintf("f-%d.txt", i)), []byte("some content"), 0o755), qt.IsNil) |
| 363 |
} |
| 364 |
|
| 365 |
c.Assert(afero.WriteFile(fs, filepath.Join(d, "f2t", testfile), []byte("some content"), 0o755), qt.IsNil) |
| 366 |
|
| 367 |
// https://github.com/gohugoio/hugo/issues/6854 |
| 368 |
mystaticDir := filepath.Join(d, "mystatic", "a", "b", "c") |
| 369 |
c.Assert(fs.MkdirAll(mystaticDir, 0o755), qt.IsNil) |
| 370 |
c.Assert(afero.WriteFile(fs, filepath.Join(mystaticDir, "ms-1.txt"), []byte("some content"), 0o755), qt.IsNil) |
| 371 |
|
| 372 |
rfs, err := newRootMappingFsFromFromTo( |
| 373 |
d, |
| 374 |
fs, |
| 375 |
"static/bf1", filepath.Join(d, "f1t"), |
| 376 |
"static/cf2", filepath.Join(d, "f2t"), |
| 377 |
"static/af3", filepath.Join(d, "f3t"), |
| 378 |
"static", filepath.Join(d, "mystatic"), |
| 379 |
"static/a/b/c", filepath.Join(d, "d1", "d2", "d3"), |
| 380 |
"layouts", filepath.Join(d, "d1"), |
| 381 |
) |
| 382 |
|
| 383 |
c.Assert(err, qt.IsNil) |
| 384 |
|
| 385 |
fif, err := rfs.Stat(filepath.Join("static/cf2", testfile)) |
| 386 |
c.Assert(err, qt.IsNil) |
| 387 |
c.Assert(fif.Name(), qt.Equals, "myfile.txt") |
| 388 |
|
| 389 |
root, err := rfs.Open("static") |
| 390 |
c.Assert(err, qt.IsNil) |
| 391 |
|
| 392 |
dirnames, err := root.Readdirnames(-1) |
| 393 |
c.Assert(err, qt.IsNil) |
| 394 |
c.Assert(dirnames, qt.DeepEquals, []string{"a", "af3", "bf1", "cf2"}, qt.Commentf(fmt.Sprintf("%#v", dirnames))) |
| 395 |
|
| 396 |
getDirnames := func(dirname string) []string { |
| 397 |
dirname = filepath.FromSlash(dirname) |
| 398 |
f, err := rfs.Open(dirname) |
| 399 |
c.Assert(err, qt.IsNil) |
| 400 |
defer f.Close() |
| 401 |
dirnames, err := f.Readdirnames(-1) |
| 402 |
c.Assert(err, qt.IsNil) |
| 403 |
sort.Strings(dirnames) |
| 404 |
return dirnames |
| 405 |
} |
| 406 |
|
| 407 |
c.Assert(getDirnames("static/a/b"), qt.DeepEquals, []string{"c"}) |
| 408 |
c.Assert(getDirnames("static/a/b/c"), qt.DeepEquals, []string{"d4", "f-1.txt", "f-2.txt", "f-3.txt", "ms-1.txt"}) |
| 409 |
c.Assert(getDirnames("static/a/b/c/d4"), qt.DeepEquals, []string{"d4-1", "d4-2", "d4-3", "d5"}) |
| 410 |
|
| 411 |
all, err := collectPaths(rfs, "static") |
| 412 |
c.Assert(err, qt.IsNil) |
| 413 |
|
| 414 |
c.Assert(all, qt.DeepEquals, []string{"/a/b/c/f-1.txt", "/a/b/c/f-2.txt", "/a/b/c/f-3.txt", "/a/b/c/ms-1.txt", "/cf2/myfile.txt"}) |
| 415 |
|
| 416 |
fis, err := collectFileinfos(rfs, "static") |
| 417 |
c.Assert(err, qt.IsNil) |
| 418 |
|
| 419 |
dirc := fis[3].Meta() |
| 420 |
|
| 421 |
f, err := dirc.Open() |
| 422 |
c.Assert(err, qt.IsNil) |
| 423 |
defer f.Close() |
| 424 |
dirEntries, err := f.(iofs.ReadDirFile).ReadDir(-1) |
| 425 |
c.Assert(err, qt.IsNil) |
| 426 |
sortDirEntries(dirEntries) |
| 427 |
i := 0 |
| 428 |
for _, fi := range dirEntries { |
| 429 |
if fi.IsDir() || fi.Name() == "ms-1.txt" { |
| 430 |
continue |
| 431 |
} |
| 432 |
i++ |
| 433 |
meta := fi.(FileMetaInfo).Meta() |
| 434 |
c.Assert(meta.Filename, qt.Equals, filepath.Join(d, fmt.Sprintf("/d1/d2/d3/f-%d.txt", i))) |
| 435 |
} |
| 436 |
|
| 437 |
_, err = rfs.Stat(filepath.FromSlash("layouts/d2/d3/f-1.txt")) |
| 438 |
c.Assert(err, qt.IsNil) |
| 439 |
_, err = rfs.Stat(filepath.FromSlash("layouts/d2/d3")) |
| 440 |
c.Assert(err, qt.IsNil) |
| 441 |
} |
| 442 |
|
| 443 |
func TestRootMappingFsOsBase(t *testing.T) { |
| 444 |
c := qt.New(t) |
| 445 |
fs := NewBaseFileDecorator(afero.NewOsFs()) |
| 446 |
|
| 447 |
d, clean, err := htesting.CreateTempDir(fs, "hugo-root-mapping-os-base") |
| 448 |
c.Assert(err, qt.IsNil) |
| 449 |
defer clean() |
| 450 |
|
| 451 |
// Deep structure |
| 452 |
deepDir := filepath.Join(d, "d1", "d2", "d3", "d4", "d5") |
| 453 |
c.Assert(fs.MkdirAll(deepDir, 0o755), qt.IsNil) |
| 454 |
for i := 1; i <= 3; i++ { |
| 455 |
c.Assert(fs.MkdirAll(filepath.Join(d, "d1", "d2", "d3", "d4", fmt.Sprintf("d4-%d", i)), 0o755), qt.IsNil) |
| 456 |
c.Assert(afero.WriteFile(fs, filepath.Join(d, "d1", "d2", "d3", fmt.Sprintf("f-%d.txt", i)), []byte("some content"), 0o755), qt.IsNil) |
| 457 |
} |
| 458 |
|
| 459 |
mystaticDir := filepath.Join(d, "mystatic", "a", "b", "c") |
| 460 |
c.Assert(fs.MkdirAll(mystaticDir, 0o755), qt.IsNil) |
| 461 |
c.Assert(afero.WriteFile(fs, filepath.Join(mystaticDir, "ms-1.txt"), []byte("some content"), 0o755), qt.IsNil) |
| 462 |
|
| 463 |
bfs := NewBasePathFs(fs, d) |
| 464 |
|
| 465 |
rfs, err := newRootMappingFsFromFromTo( |
| 466 |
"", |
| 467 |
bfs, |
| 468 |
"static", "mystatic", |
| 469 |
"static/a/b/c", filepath.Join("d1", "d2", "d3"), |
| 470 |
) |
| 471 |
c.Assert(err, qt.IsNil) |
| 472 |
|
| 473 |
getDirnames := func(dirname string) []string { |
| 474 |
dirname = filepath.FromSlash(dirname) |
| 475 |
f, err := rfs.Open(dirname) |
| 476 |
c.Assert(err, qt.IsNil) |
| 477 |
defer f.Close() |
| 478 |
dirnames, err := f.Readdirnames(-1) |
| 479 |
c.Assert(err, qt.IsNil) |
| 480 |
sort.Strings(dirnames) |
| 481 |
return dirnames |
| 482 |
} |
| 483 |
|
| 484 |
c.Assert(getDirnames("static/a/b/c"), qt.DeepEquals, []string{"d4", "f-1.txt", "f-2.txt", "f-3.txt", "ms-1.txt"}) |
| 485 |
} |
| 486 |
|
| 487 |
func TestRootMappingFileFilter(t *testing.T) { |
| 488 |
c := qt.New(t) |
| 489 |
fs := NewBaseFileDecorator(afero.NewMemMapFs()) |
| 490 |
|
| 491 |
for _, lang := range []string{"no", "en", "fr"} { |
| 492 |
for i := 1; i <= 3; i++ { |
| 493 |
c.Assert(afero.WriteFile(fs, filepath.Join(lang, fmt.Sprintf("my%s%d.txt", lang, i)), []byte("some text file for"+lang), 0o755), qt.IsNil) |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
for _, lang := range []string{"no", "en", "fr"} { |
| 498 |
for i := 1; i <= 3; i++ { |
| 499 |
c.Assert(afero.WriteFile(fs, filepath.Join(lang, "sub", fmt.Sprintf("mysub%s%d.txt", lang, i)), []byte("some text file for"+lang), 0o755), qt.IsNil) |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
rm := []*RootMapping{ |
| 504 |
{ |
| 505 |
From: "content", |
| 506 |
To: "no", |
| 507 |
Meta: &FileMeta{SitesMatrix: no, InclusionFilter: hglob.MustNewFilenameFilter(nil, []string{"**.txt"})}, |
| 508 |
}, |
| 509 |
{ |
| 510 |
From: "content", |
| 511 |
To: "en", |
| 512 |
Meta: &FileMeta{SitesMatrix: en}, |
| 513 |
}, |
| 514 |
{ |
| 515 |
From: "content", |
| 516 |
To: "fr", |
| 517 |
Meta: &FileMeta{SitesMatrix: fr, InclusionFilter: hglob.MustNewFilenameFilter(nil, []string{"**.txt"})}, |
| 518 |
}, |
| 519 |
} |
| 520 |
|
| 521 |
rfs, err := NewRootMappingFs(fs, rm...) |
| 522 |
c.Assert(err, qt.IsNil) |
| 523 |
|
| 524 |
assertExists := func(filename string, shouldExist bool) { |
| 525 |
c.Helper() |
| 526 |
filename = filepath.Clean(filename) |
| 527 |
_, err1 := rfs.Stat(filename) |
| 528 |
f, err2 := rfs.Open(filename) |
| 529 |
if shouldExist { |
| 530 |
c.Assert(err1, qt.IsNil) |
| 531 |
c.Assert(err2, qt.IsNil) |
| 532 |
c.Assert(f.Close(), qt.IsNil) |
| 533 |
} else { |
| 534 |
c.Assert(err1, qt.Not(qt.IsNil)) |
| 535 |
c.Assert(err2, qt.Not(qt.IsNil)) |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
assertExists("content/myno1.txt", false) |
| 540 |
assertExists("content/myen1.txt", true) |
| 541 |
assertExists("content/myfr1.txt", false) |
| 542 |
|
| 543 |
dirEntriesSub, err := afero.ReadDir(rfs, filepath.Join("content", "sub")) |
| 544 |
c.Assert(err, qt.IsNil) |
| 545 |
c.Assert(len(dirEntriesSub), qt.Equals, 3) |
| 546 |
|
| 547 |
f, err := rfs.Open("content") |
| 548 |
c.Assert(err, qt.IsNil) |
| 549 |
defer f.Close() |
| 550 |
dirEntries, err := f.(iofs.ReadDirFile).ReadDir(-1) |
| 551 |
|
| 552 |
c.Assert(err, qt.IsNil) |
| 553 |
c.Assert(len(dirEntries), qt.Equals, 4) |
| 554 |
} |
| 555 |
|
| 556 |
var testDims = sitesmatrix.NewTestingDimensions([]string{"en", "no"}, []string{"v1", "v2", "v3"}, []string{"admin", "editor", "viewer", "guest"}) |
| 557 |
|
| 558 |
func sitesMatrixForLangs(langs ...int) *sitesmatrix.IntSets { |
| 559 |
return sitesmatrix.NewIntSetsBuilder(testDims).WithSets(hmaps.NewOrderedIntSet(langs...), nil, nil).Build() |
| 560 |
} |