mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-26 05:30:54 +03:00
Rework the Destination filesystem to make --renderStaticToDisk work
See #9626
This commit is contained in:
parent
b08193971a
commit
d070bdf10f
75 changed files with 651 additions and 566 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue