diff options
author | Bjørn Erik Pedersen <[email protected]> | 2022-03-21 09:35:15 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2022-04-08 13:26:17 +0200 |
commit | d070bdf10f14d233288f7318a4e9f7555f070a65 (patch) | |
tree | fff8d59f98bdab3027bb45c4e10ca88594332872 /helpers | |
parent | b08193971a821fc27e549a73120c15e5e5186775 (diff) | |
download | hugo-d070bdf10f14d233288f7318a4e9f7555f070a65.tar.gz hugo-d070bdf10f14d233288f7318a4e9f7555f070a65.zip |
Rework the Destination filesystem to make --renderStaticToDisk work
See #9626
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/content_test.go | 4 | ||||
-rw-r--r-- | helpers/general_test.go | 5 | ||||
-rw-r--r-- | helpers/path.go | 12 | ||||
-rw-r--r-- | helpers/path_test.go | 94 | ||||
-rw-r--r-- | helpers/testhelpers_test.go | 15 |
5 files changed, 17 insertions, 113 deletions
diff --git a/helpers/content_test.go b/helpers/content_test.go index c1ff5c1d2..4b67b44f0 100644 --- a/helpers/content_test.go +++ b/helpers/content_test.go @@ -19,10 +19,10 @@ import ( "strings" "testing" - "github.com/gohugoio/hugo/config" "github.com/spf13/afero" "github.com/gohugoio/hugo/common/loggers" + "github.com/gohugoio/hugo/config" qt "github.com/frankban/quicktest" ) @@ -102,7 +102,7 @@ func TestBytesToHTML(t *testing.T) { } func TestNewContentSpec(t *testing.T) { - cfg := config.New() + cfg := config.NewWithTestDefaults() c := qt.New(t) cfg.Set("summaryLength", 32) diff --git a/helpers/general_test.go b/helpers/general_test.go index be9834d3f..75119f01d 100644 --- a/helpers/general_test.go +++ b/helpers/general_test.go @@ -20,9 +20,8 @@ import ( "testing" "time" - "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/common/loggers" + "github.com/gohugoio/hugo/config" qt "github.com/frankban/quicktest" "github.com/spf13/afero" @@ -30,7 +29,7 @@ import ( func TestResolveMarkup(t *testing.T) { c := qt.New(t) - cfg := config.New() + cfg := config.NewWithTestDefaults() spec, err := NewContentSpec(cfg, loggers.NewErrorLogger(), afero.NewMemMapFs(), nil) c.Assert(err, qt.IsNil) diff --git a/helpers/path.go b/helpers/path.go index b302b1569..73970d558 100644 --- a/helpers/path.go +++ b/helpers/path.go @@ -459,9 +459,17 @@ func IsDir(path string, fs afero.Fs) (bool, error) { return afero.IsDir(fs, path) } -// IsEmpty checks if a given path is empty. +// IsEmpty checks if a given path is empty, meaning it doesn't contain any regular files. func IsEmpty(path string, fs afero.Fs) (bool, error) { - return afero.IsEmpty(fs, path) + var hasFile bool + err := afero.Walk(fs, path, func(path string, info os.FileInfo, err error) error { + if info.IsDir() { + return nil + } + hasFile = true + return filepath.SkipDir + }) + return !hasFile, err } // Exists checks if a file or directory exists. diff --git a/helpers/path_test.go b/helpers/path_test.go index 6a119a741..3d0617f54 100644 --- a/helpers/path_test.go +++ b/helpers/path_test.go @@ -256,55 +256,6 @@ func TestIsDir(t *testing.T) { } } -func TestIsEmpty(t *testing.T) { - zeroSizedFile, _ := createZeroSizedFileInTempDir() - defer deleteFileInTempDir(zeroSizedFile) - nonZeroSizedFile, _ := createNonZeroSizedFileInTempDir() - defer deleteFileInTempDir(nonZeroSizedFile) - emptyDirectory, _ := createEmptyTempDir() - defer deleteTempDir(emptyDirectory) - nonEmptyZeroLengthFilesDirectory, _ := createTempDirWithZeroLengthFiles() - defer deleteTempDir(nonEmptyZeroLengthFilesDirectory) - nonEmptyNonZeroLengthFilesDirectory, _ := createTempDirWithNonZeroLengthFiles() - defer deleteTempDir(nonEmptyNonZeroLengthFilesDirectory) - nonExistentFile := os.TempDir() + "/this-file-does-not-exist.txt" - nonExistentDir := os.TempDir() + "/this/directory/does/not/exist/" - - fileDoesNotExist := fmt.Errorf("%q path does not exist", nonExistentFile) - dirDoesNotExist := fmt.Errorf("%q path does not exist", nonExistentDir) - - type test struct { - input string - expectedResult bool - expectedErr error - } - - data := []test{ - {zeroSizedFile.Name(), true, nil}, - {nonZeroSizedFile.Name(), false, nil}, - {emptyDirectory, true, nil}, - {nonEmptyZeroLengthFilesDirectory, false, nil}, - {nonEmptyNonZeroLengthFilesDirectory, false, nil}, - {nonExistentFile, false, fileDoesNotExist}, - {nonExistentDir, false, dirDoesNotExist}, - } - for i, d := range data { - exists, err := IsEmpty(d.input, new(afero.OsFs)) - if d.expectedResult != exists { - t.Errorf("Test %d failed. Expected result %t got %t", i, d.expectedResult, exists) - } - if d.expectedErr != nil { - if d.expectedErr.Error() != err.Error() { - t.Errorf("Test %d failed. Expected %q(%#v) got %q(%#v)", i, d.expectedErr, d.expectedErr, err, err) - } - } else { - if d.expectedErr != err { - t.Errorf("Test %d failed. Expected %q(%#v) got %q(%#v)", i, d.expectedErr, d.expectedErr, err, err) - } - } - } -} - func createZeroSizedFileInTempDir() (*os.File, error) { filePrefix := "_path_test_" f, e := ioutil.TempFile("", filePrefix) // dir is os.TempDir() @@ -346,51 +297,6 @@ func createEmptyTempDir() (string, error) { return d, nil } -func createTempDirWithZeroLengthFiles() (string, error) { - d, dirErr := createEmptyTempDir() - if dirErr != nil { - return "", dirErr - } - filePrefix := "_path_test_" - _, fileErr := ioutil.TempFile(d, filePrefix) // dir is os.TempDir() - if fileErr != nil { - // if there was an error no file was created. - // but we need to remove the directory to clean-up - deleteTempDir(d) - return "", fileErr - } - // the dir now has one, zero length file in it - return d, nil -} - -func createTempDirWithNonZeroLengthFiles() (string, error) { - d, dirErr := createEmptyTempDir() - if dirErr != nil { - return "", dirErr - } - filePrefix := "_path_test_" - f, fileErr := ioutil.TempFile(d, filePrefix) // dir is os.TempDir() - if fileErr != nil { - // if there was an error no file was created. - // but we need to remove the directory to clean-up - deleteTempDir(d) - return "", fileErr - } - byteString := []byte("byteString") - - fileErr = ioutil.WriteFile(f.Name(), byteString, 0644) - if fileErr != nil { - // delete the file - deleteFileInTempDir(f) - // also delete the directory - deleteTempDir(d) - return "", fileErr - } - - // the dir now has one, zero length file in it - return d, nil -} - func deleteTempDir(d string) { _ = os.RemoveAll(d) } diff --git a/helpers/testhelpers_test.go b/helpers/testhelpers_test.go index 1d1bf73ec..00be3db25 100644 --- a/helpers/testhelpers_test.go +++ b/helpers/testhelpers_test.go @@ -17,9 +17,8 @@ func newTestPathSpec(fs *hugofs.Fs, v config.Provider) *PathSpec { } func newTestDefaultPathSpec(configKeyValues ...any) *PathSpec { - v := config.New() - fs := hugofs.NewMem(v) cfg := newTestCfg() + fs := hugofs.NewMem(cfg) for i := 0; i < len(configKeyValues); i += 2 { cfg.Set(configKeyValues[i].(string), configKeyValues[i+1]) @@ -28,15 +27,7 @@ func newTestDefaultPathSpec(configKeyValues ...any) *PathSpec { } func newTestCfg() config.Provider { - v := config.New() - v.Set("contentDir", "content") - v.Set("dataDir", "data") - v.Set("i18nDir", "i18n") - v.Set("layoutDir", "layouts") - v.Set("assetDir", "assets") - v.Set("resourceDir", "resources") - v.Set("publishDir", "public") - v.Set("archetypeDir", "archetypes") + v := config.NewWithTestDefaults() langs.LoadLanguageSettings(v, nil) langs.LoadLanguageSettings(v, nil) mod, err := modules.CreateProjectModule(v) @@ -49,7 +40,7 @@ func newTestCfg() config.Provider { } func newTestContentSpec() *ContentSpec { - v := config.New() + v := config.NewWithTestDefaults() spec, err := NewContentSpec(v, loggers.NewErrorLogger(), afero.NewMemMapFs(), nil) if err != nil { panic(err) |