summaryrefslogtreecommitdiffstats
path: root/tpl
diff options
context:
space:
mode:
authorsatotake <doublequotation@gmail.com>2018-08-05 12:37:20 +0900
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-08-06 09:54:26 +0200
commitd71120852a8e14d0ea4d24de269fce041ef7b666 (patch)
tree9df7354faf35580fffb9f55357019b7e4388a2a2 /tpl
parenta6b1eb1e9150aa5c1c86fe7424cc4167d6f59a5a (diff)
Add fileStat to tpl/os/os
Diffstat (limited to 'tpl')
-rw-r--r--tpl/os/init.go7
-rw-r--r--tpl/os/os.go19
-rw-r--r--tpl/os/os_test.go34
3 files changed, 60 insertions, 0 deletions
diff --git a/tpl/os/init.go b/tpl/os/init.go
index 3ef8702d6..08a36fe7b 100644
--- a/tpl/os/init.go
+++ b/tpl/os/init.go
@@ -55,6 +55,13 @@ func init() {
},
)
+ ns.AddMethodMapping(ctx.FileStat,
+ []string{"fileStat"},
+ [][2]string{
+ {`{{ (fileExists "files/README.txt").Size }}`, `11`},
+ },
+ )
+
return ns
}
diff --git a/tpl/os/os.go b/tpl/os/os.go
index 79d035d7e..04f151ec9 100644
--- a/tpl/os/os.go
+++ b/tpl/os/os.go
@@ -130,3 +130,22 @@ func (ns *Namespace) FileExists(i interface{}) (bool, error) {
return status, nil
}
+
+// FileStat Stat returns the os.FileInfo structure describing file.
+func (ns *Namespace) FileStat(i interface{}) (_os.FileInfo, error) {
+ path, err := cast.ToStringE(i)
+ if err != nil {
+ return nil, err
+ }
+
+ if path == "" {
+ return nil, errors.New("fileStat needs a path to a file")
+ }
+
+ r, err := ns.readFileFs.Stat(path)
+ if err != nil {
+ return nil, err
+ }
+
+ return r, nil
+}
diff --git a/tpl/os/os_test.go b/tpl/os/os_test.go
index 0919f885a..60e6b1f63 100644
--- a/tpl/os/os_test.go
+++ b/tpl/os/os_test.go
@@ -99,3 +99,37 @@ func TestFileExists(t *testing.T) {
assert.Equal(t, test.expect, result, errMsg)
}
}
+
+func TestFileStat(t *testing.T) {
+ t.Parallel()
+
+ workingDir := "/home/hugo"
+
+ v := viper.New()
+ v.Set("workingDir", workingDir)
+
+ ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})
+
+ afero.WriteFile(ns.deps.Fs.Source, filepath.Join(workingDir, "/f/f1.txt"), []byte("f1-content"), 0755)
+
+ for i, test := range []struct {
+ filename string
+ expect interface{}
+ }{
+ {filepath.FromSlash("/f/f1.txt"), int64(10)},
+ {filepath.FromSlash("f/f1.txt"), int64(10)},
+ {"b", nil},
+ {"", nil},
+ } {
+ errMsg := fmt.Sprintf("[%d] %v", i, test)
+ result, err := ns.FileStat(test.filename)
+
+ if test.expect == nil {
+ require.Error(t, err, errMsg)
+ continue
+ }
+
+ require.NoError(t, err, errMsg)
+ assert.Equal(t, test.expect, result.Size(), errMsg)
+ }
+}