Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: os.Stat function
  • Loading branch information
rymut committed Dec 23, 2024
commit 1a5367843ed92fdaa3fc9579e0e763ee39b1fbd5
18 changes: 5 additions & 13 deletions tpl/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/hugofs/files"
"github.com/spf13/afero"
"github.com/spf13/cast"
)
Expand Down Expand Up @@ -188,7 +189,7 @@ func (ns *Namespace) FileExists(i any, mode ...any) (bool, error) {
}

// Stat returns the os.FileInfo structure describing file.
func (ns *Namespace) Stat(i any, mode ...any) (_os.FileInfo, error) {
func (ns *Namespace) Stat(i any) (_os.FileInfo, error) {
path, err := cast.ToStringE(i)
if err != nil {
return nil, err
Expand All @@ -197,18 +198,9 @@ func (ns *Namespace) Stat(i any, mode ...any) (_os.FileInfo, error) {
if path == "" {
return nil, errors.New("fileStat needs a path to a file")
}

old := true
if (len(mode) != 0) {
old, err = cast.ToBoolE(mode[0])
if err != nil {
return nil, err
}
}
var r _os.FileInfo
if (old) {
r, err = ns.readFileFs.Stat(path)
} else {
r, err := ns.mountsFs.Stat(path)
if err != nil {
path = filepath.Join(files.ComponentFolderContent, path)
r, err = ns.mountsFs.Stat(path)
}
if err != nil {
Expand Down
103 changes: 103 additions & 0 deletions tpl/os/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,54 @@ func TestStat(t *testing.T) {
}
}


func TestStatWithMounts(t *testing.T) {
t.Parallel()
b := newFileTestBuilderWithMounts(t).Build()
ns := os.New(b.H.Deps)

for _, test := range []struct {
filename string
expectError bool
expectSize any
expectIsDir any
expectHint string
}{
{filepath.FromSlash("f/f1.txt"), false, int64(10), false, "must be present in workdir"},
{filepath.FromSlash("about.pl.md"), false, int64(2), false, "must be present in contentDir"},
{filepath.FromSlash("en/about.md"), false, int64(2), false, "must be present in contentDir"},
{filepath.FromSlash("assets/testing.json"), false, int64(13), false, "mapping must be present"},
{filepath.FromSlash("assets/virtual/file1.json"), false, int64(13), false, "mapping to virtual directory must be present"},
{filepath.FromSlash("assets/file2.json"), false, int64(13), false, "mapping to directory must be present"},
{filepath.FromSlash("assets/files/raw1.txt"), false, int64(12), false, "mapping to subdirectory must be present"},
{filepath.FromSlash("assets/files"), false, nil, true, "directory mapping to subdirectory must be present"},
{filepath.FromSlash("assets/virtual"), false, int64(0), true, "directory mapping to virtual directory must be present"},
{"b", true, nil, nil, "cannot exist"},
{"", true, nil, nil, "cannot exist"},
{".", false, nil, true, "root directory exist"},
{filepath.FromSlash("../content/about.pl.md"), false, int64(2), false, "must be present in contentDir (relative to content dir)"},
} {
for _, prefix := range []string{"", filepath.FromSlash("/")} {
if prefix != "" && test.filename == "" {
continue
}
filename := prefix + test.filename
result, err := ns.Stat(filename)
if test.expectError == true {
b.Assert(err, qt.Not(qt.IsNil), qt.Commentf("file '%s' %s", filename, test.expectHint))
continue
}

b.Assert(err, qt.IsNil, qt.Commentf("file '%s' %s", filename, test.expectHint))
if test.expectSize != nil {
// size for is dir is platform dependent
b.Assert(result.Size(), qt.Equals, test.expectSize, qt.Commentf("file '%s' invalid size", filename))
}
b.Assert(result.IsDir(), qt.Equals, test.expectIsDir, qt.Commentf("file '%s' must be directory", filename))
}
}
}

func newFileTestBuilder(t *testing.T) *hugolib.IntegrationTestBuilder {
files := `
-- f/f1.txt --
Expand All @@ -126,3 +174,58 @@ f2-content
},
)
}

func newFileTestBuilderWithMounts(t *testing.T) *hugolib.IntegrationTestBuilder {
files := `
-- f/f1.txt --
f1-content
-- home/f2.txt --
f2-content
-- hugo.toml --
baseURL = "https://example.com/"
defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = true
[module]
[[module.imports]]
path = "module1"
[[module.imports.mounts]]
source = "assets"
target = "assets/virtual"
[[module.imports.mounts]]
source = "assets/file1.json"
target = "assets/testing.json"
[[module.imports]]
path = "module2"
[languages]
[languages.en]
contentDir = "content/en"
languageName = "English"
[languages.fr]
contentDir = "content/fr"
languageName = "Français"
[languages.pl]
languageName = "Polski"

-- content/en/about.md --
EN
-- content/fr/about.md --
FR
-- content/about.pl.md --
PL
-- themes/module1/assets/file1.json --
file1-content
-- themes/module2/assets/files/raw1.txt --
raw1-content
-- themes/module2/assets/file2.json --
file2-content
`

return hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
WorkingDir: "/mywork",
//NeedsOsFS: true,
},
)
}