diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-01-04 18:24:36 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-05-16 18:01:29 +0200 |
commit | 241b21b0fd34d91fccb2ce69874110dceae6f926 (patch) | |
tree | d4e0118eac7e9c42f065815447a70805f8d6ad3e /hugolib/filesystems/basefs_test.go | |
parent | 6aededf6b42011c3039f5f66487a89a8dd65e0e7 (diff) |
Create a struct with all of Hugo's config options
Primary motivation is documentation, but it will also hopefully simplify the code.
Also,
* Lower case the default output format names; this is in line with the custom ones (map keys) and how
it's treated all the places. This avoids doing `stringds.EqualFold` everywhere.
Closes #10896
Closes #10620
Diffstat (limited to 'hugolib/filesystems/basefs_test.go')
-rw-r--r-- | hugolib/filesystems/basefs_test.go | 231 |
1 files changed, 91 insertions, 140 deletions
diff --git a/hugolib/filesystems/basefs_test.go b/hugolib/filesystems/basefs_test.go index a729e63b1..1724f3838 100644 --- a/hugolib/filesystems/basefs_test.go +++ b/hugolib/filesystems/basefs_test.go @@ -1,4 +1,4 @@ -// Copyright 2019 The Hugo Authors. All rights reserved. +// Copyright 2023 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package filesystems +package filesystems_test import ( "errors" @@ -21,63 +21,20 @@ import ( "strings" "testing" - "github.com/gobwas/glob" - "github.com/gohugoio/hugo/config" - - "github.com/gohugoio/hugo/langs" + "github.com/gohugoio/hugo/config/testconfig" "github.com/spf13/afero" qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/hugofs" + "github.com/gohugoio/hugo/hugolib/filesystems" "github.com/gohugoio/hugo/hugolib/paths" - "github.com/gohugoio/hugo/modules" ) -func initConfig(fs afero.Fs, cfg config.Provider) error { - if _, err := langs.LoadLanguageSettings(cfg, nil); err != nil { - return err - } - - modConfig, err := modules.DecodeConfig(cfg) - if err != nil { - return err - } - - workingDir := cfg.GetString("workingDir") - themesDir := cfg.GetString("themesDir") - if !filepath.IsAbs(themesDir) { - themesDir = filepath.Join(workingDir, themesDir) - } - globAll := glob.MustCompile("**", '/') - modulesClient := modules.NewClient(modules.ClientConfig{ - Fs: fs, - WorkingDir: workingDir, - ThemesDir: themesDir, - ModuleConfig: modConfig, - IgnoreVendor: globAll, - }) - - moduleConfig, err := modulesClient.Collect() - if err != nil { - return err - } - - if err := modules.ApplyProjectConfigDefaults(cfg, moduleConfig.ActiveModules[0]); err != nil { - return err - } - - cfg.Set("allModules", moduleConfig.ActiveModules) - - return nil -} - func TestNewBaseFs(t *testing.T) { c := qt.New(t) - v := config.NewWithTestDefaults() - - fs := hugofs.NewMem(v) + v := config.New() themes := []string{"btheme", "atheme"} @@ -87,6 +44,9 @@ func TestNewBaseFs(t *testing.T) { v.Set("themesDir", "themes") v.Set("defaultContentLanguage", "en") v.Set("theme", themes[:1]) + v.Set("publishDir", "public") + + afs := afero.NewMemMapFs() // Write some data to the themes for _, theme := range themes { @@ -94,39 +54,39 @@ func TestNewBaseFs(t *testing.T) { base := filepath.Join(workingDir, "themes", theme, dir) filenameTheme := filepath.Join(base, fmt.Sprintf("theme-file-%s.txt", theme)) filenameOverlap := filepath.Join(base, "f3.txt") - fs.Source.Mkdir(base, 0755) + afs.Mkdir(base, 0755) content := []byte(fmt.Sprintf("content:%s:%s", theme, dir)) - afero.WriteFile(fs.Source, filenameTheme, content, 0755) - afero.WriteFile(fs.Source, filenameOverlap, content, 0755) + afero.WriteFile(afs, filenameTheme, content, 0755) + afero.WriteFile(afs, filenameOverlap, content, 0755) } // Write some files to the root of the theme base := filepath.Join(workingDir, "themes", theme) - afero.WriteFile(fs.Source, filepath.Join(base, fmt.Sprintf("theme-root-%s.txt", theme)), []byte(fmt.Sprintf("content:%s", theme)), 0755) - afero.WriteFile(fs.Source, filepath.Join(base, "file-theme-root.txt"), []byte(fmt.Sprintf("content:%s", theme)), 0755) + afero.WriteFile(afs, filepath.Join(base, fmt.Sprintf("theme-root-%s.txt", theme)), []byte(fmt.Sprintf("content:%s", theme)), 0755) + afero.WriteFile(afs, filepath.Join(base, "file-theme-root.txt"), []byte(fmt.Sprintf("content:%s", theme)), 0755) } - afero.WriteFile(fs.Source, filepath.Join(workingDir, "file-root.txt"), []byte("content-project"), 0755) + afero.WriteFile(afs, filepath.Join(workingDir, "file-root.txt"), []byte("content-project"), 0755) - afero.WriteFile(fs.Source, filepath.Join(workingDir, "themes", "btheme", "config.toml"), []byte(` + afero.WriteFile(afs, filepath.Join(workingDir, "themes", "btheme", "config.toml"), []byte(` theme = ["atheme"] `), 0755) - setConfigAndWriteSomeFilesTo(fs.Source, v, "contentDir", "mycontent", 3) - setConfigAndWriteSomeFilesTo(fs.Source, v, "i18nDir", "myi18n", 4) - setConfigAndWriteSomeFilesTo(fs.Source, v, "layoutDir", "mylayouts", 5) - setConfigAndWriteSomeFilesTo(fs.Source, v, "staticDir", "mystatic", 6) - setConfigAndWriteSomeFilesTo(fs.Source, v, "dataDir", "mydata", 7) - setConfigAndWriteSomeFilesTo(fs.Source, v, "archetypeDir", "myarchetypes", 8) - setConfigAndWriteSomeFilesTo(fs.Source, v, "assetDir", "myassets", 9) - setConfigAndWriteSomeFilesTo(fs.Source, v, "resourceDir", "myrsesource", 10) + setConfigAndWriteSomeFilesTo(afs, v, "contentDir", "mycontent", 3) + setConfigAndWriteSomeFilesTo(afs, v, "i18nDir", "myi18n", 4) + setConfigAndWriteSomeFilesTo(afs, v, "layoutDir", "mylayouts", 5) + setConfigAndWriteSomeFilesTo(afs, v, "staticDir", "mystatic", 6) + setConfigAndWriteSomeFilesTo(afs, v, "dataDir", "mydata", 7) + setConfigAndWriteSomeFilesTo(afs, v, "archetypeDir", "myarchetypes", 8) + setConfigAndWriteSomeFilesTo(afs, v, "assetDir", "myassets", 9) + setConfigAndWriteSomeFilesTo(afs, v, "resourceDir", "myrsesource", 10) - v.Set("publishDir", "public") - c.Assert(initConfig(fs.Source, v), qt.IsNil) + conf := testconfig.GetTestConfig(afs, v) + fs := hugofs.NewFrom(afs, conf.BaseConfig()) - p, err := paths.New(fs, v) + p, err := paths.New(fs, conf) c.Assert(err, qt.IsNil) - bfs, err := NewBase(p, nil) + bfs, err := filesystems.NewBase(p, nil) c.Assert(err, qt.IsNil) c.Assert(bfs, qt.Not(qt.IsNil)) @@ -180,31 +140,14 @@ theme = ["atheme"] } } -func createConfig() config.Provider { - v := config.NewWithTestDefaults() - v.Set("contentDir", "mycontent") - v.Set("i18nDir", "myi18n") - v.Set("staticDir", "mystatic") - v.Set("dataDir", "mydata") - v.Set("layoutDir", "mylayouts") - v.Set("archetypeDir", "myarchetypes") - v.Set("assetDir", "myassets") - v.Set("resourceDir", "resources") - v.Set("publishDir", "public") - v.Set("defaultContentLanguage", "en") - - return v -} - func TestNewBaseFsEmpty(t *testing.T) { c := qt.New(t) - v := createConfig() - fs := hugofs.NewMem(v) - c.Assert(initConfig(fs.Source, v), qt.IsNil) - - p, err := paths.New(fs, v) + afs := afero.NewMemMapFs() + conf := testconfig.GetTestConfig(afs, nil) + fs := hugofs.NewFrom(afs, conf.BaseConfig()) + p, err := paths.New(fs, conf) c.Assert(err, qt.IsNil) - bfs, err := NewBase(p, nil) + bfs, err := filesystems.NewBase(p, nil) c.Assert(err, qt.IsNil) c.Assert(bfs, qt.Not(qt.IsNil)) c.Assert(bfs.Archetypes.Fs, qt.Not(qt.IsNil)) @@ -218,47 +161,47 @@ func TestNewBaseFsEmpty(t *testing.T) { func TestRealDirs(t *testing.T) { c := qt.New(t) - v := createConfig() + v := config.New() root, themesDir := t.TempDir(), t.TempDir() v.Set("workingDir", root) v.Set("themesDir", themesDir) + v.Set("assetDir", "myassets") v.Set("theme", "mytheme") - fs := hugofs.NewDefault(v) - sfs := fs.Source + afs := hugofs.Os defer func() { os.RemoveAll(root) os.RemoveAll(themesDir) }() - c.Assert(sfs.MkdirAll(filepath.Join(root, "myassets", "scss", "sf1"), 0755), qt.IsNil) - c.Assert(sfs.MkdirAll(filepath.Join(root, "myassets", "scss", "sf2"), 0755), qt.IsNil) - c.Assert(sfs.MkdirAll(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf2"), 0755), qt.IsNil) - c.Assert(sfs.MkdirAll(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf3"), 0755), qt.IsNil) - c.Assert(sfs.MkdirAll(filepath.Join(root, "resources"), 0755), qt.IsNil) - c.Assert(sfs.MkdirAll(filepath.Join(themesDir, "mytheme", "resources"), 0755), qt.IsNil) - - c.Assert(sfs.MkdirAll(filepath.Join(root, "myassets", "js", "f2"), 0755), qt.IsNil) + c.Assert(afs.MkdirAll(filepath.Join(root, "myassets", "scss", "sf1"), 0755), qt.IsNil) + c.Assert(afs.MkdirAll(filepath.Join(root, "myassets", "scss", "sf2"), 0755), qt.IsNil) + c.Assert(afs.MkdirAll(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf2"), 0755), qt.IsNil) + c.Assert(afs.MkdirAll(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf3"), 0755), qt.IsNil) + c.Assert(afs.MkdirAll(filepath.Join(root, "resources"), 0755), qt.IsNil) + c.Assert(afs.MkdirAll(filepath.Join(themesDir, "mytheme", "resources"), 0755), qt.IsNil) - afero.WriteFile(sfs, filepath.Join(filepath.Join(root, "myassets", "scss", "sf1", "a1.scss")), []byte("content"), 0755) - afero.WriteFile(sfs, filepath.Join(filepath.Join(root, "myassets", "scss", "sf2", "a3.scss")), []byte("content"), 0755) - afero.WriteFile(sfs, filepath.Join(filepath.Join(root, "myassets", "scss", "a2.scss")), []byte("content"), 0755) - afero.WriteFile(sfs, filepath.Join(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf2", "a3.scss")), []byte("content"), 0755) - afero.WriteFile(sfs, filepath.Join(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf3", "a4.scss")), []byte("content"), 0755) + c.Assert(afs.MkdirAll(filepath.Join(root, "myassets", "js", "f2"), 0755), qt.IsNil) - afero.WriteFile(sfs, filepath.Join(filepath.Join(themesDir, "mytheme", "resources", "t1.txt")), []byte("content"), 0755) - afero.WriteFile(sfs, filepath.Join(filepath.Join(root, "resources", "p1.txt")), []byte("content"), 0755) - afero.WriteFile(sfs, filepath.Join(filepath.Join(root, "resources", "p2.txt")), []byte("content"), 0755) + afero.WriteFile(afs, filepath.Join(filepath.Join(root, "myassets", "scss", "sf1", "a1.scss")), []byte("content"), 0755) + afero.WriteFile(afs, filepath.Join(filepath.Join(root, "myassets", "scss", "sf2", "a3.scss")), []byte("content"), 0755) + afero.WriteFile(afs, filepath.Join(filepath.Join(root, "myassets", "scss", "a2.scss")), []byte("content"), 0755) + afero.WriteFile(afs, filepath.Join(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf2", "a3.scss")), []byte("content"), 0755) + afero.WriteFile(afs, filepath.Join(filepath.Join(themesDir, "mytheme", "assets", "scss", "sf3", "a4.scss")), []byte("content"), 0755) - afero.WriteFile(sfs, filepath.Join(filepath.Join(root, "myassets", "js", "f2", "a1.js")), []byte("content"), 0755) - afero.WriteFile(sfs, filepath.Join(filepath.Join(root, "myassets", "js", "a2.js")), []byte("content"), 0755) + afero.WriteFile(afs, filepath.Join(filepath.Join(themesDir, "mytheme", "resources", "t1.txt")), []byte("content"), 0755) + afero.WriteFile(afs, filepath.Join(filepath.Join(root, "resources", "p1.txt")), []byte("content"), 0755) + afero.WriteFile(afs, filepath.Join(filepath.Join(root, "resources", "p2.txt")), []byte("content"), 0755) - c.Assert(initConfig(fs.Source, v), qt.IsNil) + afero.WriteFile(afs, filepath.Join(filepath.Join(root, "myassets", "js", "f2", "a1.js")), []byte("content"), 0755) + afero.WriteFile(afs, filepath.Join(filepath.Join(root, "myassets", "js", "a2.js")), []byte("content"), 0755) - p, err := paths.New(fs, v) + conf := testconfig.GetTestConfig(afs, v) + fs := hugofs.NewFrom(afs, conf.BaseConfig()) + p, err := paths.New(fs, conf) c.Assert(err, qt.IsNil) - bfs, err := NewBase(p, nil) + bfs, err := filesystems.NewBase(p, nil) c.Assert(err, qt.IsNil) c.Assert(bfs, qt.Not(qt.IsNil)) @@ -269,45 +212,48 @@ func TestRealDirs(t *testing.T) { c.Assert(realDirs[0], qt.Equals, filepath.Join(root, "myassets/scss")) c.Assert(realDirs[len(realDirs)-1], qt.Equals, filepath.Join(themesDir, "mytheme/assets/scss")) - c.Assert(bfs.theBigFs, qt.Not(qt.IsNil)) } func TestStaticFs(t *testing.T) { c := qt.New(t) - v := createConfig() + v := config.New() workDir := "mywork" v.Set("workingDir", workDir) v.Set("themesDir", "themes") + v.Set("staticDir", "mystatic") v.Set("theme", []string{"t1", "t2"}) - fs := hugofs.NewMem(v) + afs := afero.NewMemMapFs() themeStaticDir := filepath.Join(workDir, "themes", "t1", "static") themeStaticDir2 := filepath.Join(workDir, "themes", "t2", "static") - afero.WriteFile(fs.Source, filepath.Join(workDir, "mystatic", "f1.txt"), []byte("Hugo Rocks!"), 0755) - afero.WriteFile(fs.Source, filepath.Join(themeStaticDir, "f1.txt"), []byte("Hugo Themes Rocks!"), 0755) - afero.WriteFile(fs.Source, filepath.Join(themeStaticDir, "f2.txt"), []byte("Hugo Themes Still Rocks!"), 0755) - afero.WriteFile(fs.Source, filepath.Join(themeStaticDir2, "f2.txt"), []byte("Hugo Themes Rocks in t2!"), 0755) + afero.WriteFile(afs, filepath.Join(workDir, "mystatic", "f1.txt"), []byte("Hugo Rocks!"), 0755) + afero.WriteFile(afs, filepath.Join(themeStaticDir, "f1.txt"), []byte("Hugo Themes Rocks!"), 0755) + afero.WriteFile(afs, filepath.Join(themeStaticDir, "f2.txt"), []byte("Hugo Themes Still Rocks!"), 0755) + afero.WriteFile(afs, filepath.Join(themeStaticDir2, "f2.txt"), []byte("Hugo Themes Rocks in t2!"), 0755) - c.Assert(initConfig(fs.Source, v), qt.IsNil) + conf := testconfig.GetTestConfig(afs, v) + fs := hugofs.NewFrom(afs, conf.BaseConfig()) + p, err := paths.New(fs, conf) - p, err := paths.New(fs, v) c.Assert(err, qt.IsNil) - bfs, err := NewBase(p, nil) + bfs, err := filesystems.NewBase(p, nil) c.Assert(err, qt.IsNil) sfs := bfs.StaticFs("en") + checkFileContent(sfs, "f1.txt", c, "Hugo Rocks!") checkFileContent(sfs, "f2.txt", c, "Hugo Themes Still Rocks!") } func TestStaticFsMultiHost(t *testing.T) { c := qt.New(t) - v := createConfig() + v := config.New() workDir := "mywork" v.Set("workingDir", workDir) v.Set("themesDir", "themes") + v.Set("staticDir", "mystatic") v.Set("theme", "t1") v.Set("defaultContentLanguage", "en") @@ -323,21 +269,24 @@ func TestStaticFsMultiHost(t *testing.T) { v.Set("languages", langConfig) - fs := hugofs.NewMem(v) + afs := afero.NewMemMapFs() themeStaticDir := filepath.Join(workDir, "themes", "t1", "static") - afero.WriteFile(fs.Source, filepath.Join(workDir, "mystatic", "f1.txt"), []byte("Hugo Rocks!"), 0755) - afero.WriteFile(fs.Source, filepath.Join(workDir, "static_no", "f1.txt"), []byte("Hugo Rocks in Norway!"), 0755) + afero.WriteFile(afs, filepath.Join(workDir, "mystatic", "f1.txt"), []byte("Hugo Rocks!"), 0755) + afero.WriteFile(afs, filepath.Join(workDir, "static_no", "f1.txt"), []byte("Hugo Rocks in Norway!"), 0755) + + afero.WriteFile(afs, filepath.Join(themeStaticDir, "f1.txt"), []byte("Hugo Themes Rocks!"), 0755) + afero.WriteFile(afs, filepath.Join(themeStaticDir, "f2.txt"), []byte("Hugo Themes Still Rocks!"), 0755) - afero.WriteFile(fs.Source, filepath.Join(themeStaticDir, "f1.txt"), []byte("Hugo Themes Rocks!"), 0755) - afero.WriteFile(fs.Source, filepath.Join(themeStaticDir, "f2.txt"), []byte("Hugo Themes Still Rocks!"), 0755) + conf := testconfig.GetTestConfig(afs, v) + fs := hugofs.NewFrom(afs, conf.BaseConfig()) - c.Assert(initConfig(fs.Source, v), qt.IsNil) + fmt.Println("IS", conf.IsMultihost()) - p, err := paths.New(fs, v) + p, err := paths.New(fs, conf) c.Assert(err, qt.IsNil) - bfs, err := NewBase(p, nil) + bfs, err := filesystems.NewBase(p, nil) c.Assert(err, qt.IsNil) enFs := bfs.StaticFs("en") checkFileContent(enFs, "f1.txt", c, "Hugo Rocks!") @@ -350,14 +299,14 @@ func TestStaticFsMultiHost(t *testing.T) { func TestMakePathRelative(t *testing.T) { c := qt.New(t) - v := createConfig() - fs := hugofs.NewMem(v) + v := config.New() + afs := afero.NewMemMapFs() workDir := "mywork" v.Set("workingDir", workDir) - c.Assert(fs.Source.MkdirAll(filepath.Join(workDir, "dist", "d1"), 0777), qt.IsNil) - c.Assert(fs.Source.MkdirAll(filepath.Join(workDir, "static", "d2"), 0777), qt.IsNil) - c.Assert(fs.Source.MkdirAll(filepath.Join(workDir, "dust", "d2"), 0777), qt.IsNil) + c.Assert(afs.MkdirAll(filepath.Join(workDir, "dist", "d1"), 0777), qt.IsNil) + c.Assert(afs.MkdirAll(filepath.Join(workDir, "static", "d2"), 0777), qt.IsNil) + c.Assert(afs.MkdirAll(filepath.Join(workDir, "dust", "d2"), 0777), qt.IsNil) moduleCfg := map[string]any{ "mounts": []any{ @@ -378,11 +327,12 @@ func TestMakePathRelative(t *testing.T) { v.Set("module", moduleCfg) - c.Assert(initConfig(fs.Source, v), qt.IsNil) + conf := testconfig.GetTestConfig(afs, v) + fs := hugofs.NewFrom(afs, conf.BaseConfig()) - p, err := paths.New(fs, v) + p, err := paths.New(fs, conf) c.Assert(err, qt.IsNil) - bfs, err := NewBase(p, nil) + bfs, err := filesystems.NewBase(p, nil) c.Assert(err, qt.IsNil) sfs := bfs.Static[""] @@ -399,6 +349,7 @@ func TestMakePathRelative(t *testing.T) { } func checkFileCount(fs afero.Fs, dirname string, c *qt.C, expected int) { + c.Helper() count, _, err := countFilesAndGetFilenames(fs, dirname) c.Assert(err, qt.IsNil) c.Assert(count, qt.Equals, expected) |