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 FileExists
  • Loading branch information
rymut committed Dec 23, 2024
commit 128b9783e4d69359fafc9b21e022e01665e4da8b
17 changes: 4 additions & 13 deletions tpl/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (ns *Namespace) ReadDir(i any, mode ...any) ([]_os.FileInfo, error) {
}

// FileExists checks whether a file exists under the given path.
func (ns *Namespace) FileExists(i any, mode ...any) (bool, error) {
func (ns *Namespace) FileExists(i any) (bool, error) {
path, err := cast.ToStringE(i)
if err != nil {
return false, err
Expand All @@ -167,18 +167,9 @@ func (ns *Namespace) FileExists(i any, mode ...any) (bool, error) {
if path == "" {
return false, errors.New("fileExists needs a path to a file")
}

old := true
if (len(mode) != 0) {
old, err = cast.ToBoolE(mode[0])
if err != nil {
return false, err
}
}
var status bool
if (old) {
status, err = afero.Exists(ns.readFileFs, path)
} else {
status, err := afero.Exists(ns.mountsFs, path)
if (err != nil) || (status == false) {
path = filepath.Join(files.ComponentFolderContent, path)
status, err = afero.Exists(ns.mountsFs, path)
}
if err != nil {
Expand Down
95 changes: 32 additions & 63 deletions tpl/os/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,43 @@ func TestReadFile(t *testing.T) {

func TestFileExists(t *testing.T) {
t.Parallel()
c := qt.New(t)

b := newFileTestBuilder(t).Build()
ns := os.New(b.H.Deps)

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

if test.expect == nil {
c.Assert(err, qt.Not(qt.IsNil))
continue
b.Assert(err, qt.IsNil, qt.Commentf("file '%s' %s", filename, test.comment))
b.Assert(result, qt.Equals, test.expect, qt.Commentf("file '%s' %s", filename, test.comment))
}

c.Assert(err, qt.IsNil)
c.Assert(result, qt.Equals, test.expect)
}
}

Expand All @@ -89,41 +102,15 @@ func TestStat(t *testing.T) {
b := newFileTestBuilder(t).Build()
ns := os.New(b.H.Deps)

for _, test := range []struct {
filename string
expect any
}{
{filepath.FromSlash("/f/f1.txt"), int64(10)},
{filepath.FromSlash("f/f1.txt"), int64(10)},
{"b", nil},
{"", nil},
} {
result, err := ns.Stat(test.filename)

if test.expect == nil {
b.Assert(err, qt.Not(qt.IsNil))
continue
}

b.Assert(err, qt.IsNil)
b.Assert(result.Size(), qt.Equals, test.expect)
}
}


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
comment string
}{
{filepath.FromSlash("f/f1.txt"), false, int64(10), false, "must be present in workdir"},
{filepath.FromSlash("../f2.txt"), true, nil, nil, "cannot exist" },
{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"},
Expand All @@ -144,11 +131,11 @@ func TestStatWithMounts(t *testing.T) {
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))
b.Assert(err, qt.Not(qt.IsNil), qt.Commentf("file '%s' %s", filename, test.comment))
continue
}

b.Assert(err, qt.IsNil, qt.Commentf("file '%s' %s", filename, test.expectHint))
b.Assert(err, qt.IsNil, qt.Commentf("file '%s' %s", filename, test.comment))
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))
Expand All @@ -163,23 +150,6 @@ func newFileTestBuilder(t *testing.T) *hugolib.IntegrationTestBuilder {
-- f/f1.txt --
f1-content
-- home/f2.txt --
f2-content
`

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

func newFileTestBuilderWithMounts(t *testing.T) *hugolib.IntegrationTestBuilder {
files := `
-- f/f1.txt --
f1-content
-- home/f2.txt --
f2-content
-- hugo.toml --
baseURL = "https://example.com/"
Expand Down Expand Up @@ -225,7 +195,6 @@ file2-content
T: t,
TxtarString: files,
WorkingDir: "/mywork",
//NeedsOsFS: true,
},
)
}