2017-03-13 18:55:02 -04:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
2017-03-13 18:55:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadFile(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
workingDir := "/home/hugo"
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
v := config.New()
|
2017-03-13 18:55:02 -04:00
|
|
|
v.Set("workingDir", workingDir)
|
|
|
|
|
|
|
|
// f := newTestFuncsterWithViper(v)
|
|
|
|
ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})
|
|
|
|
|
|
|
|
afero.WriteFile(ns.deps.Fs.Source, filepath.Join(workingDir, "/f/f1.txt"), []byte("f1-content"), 0755)
|
|
|
|
afero.WriteFile(ns.deps.Fs.Source, filepath.Join("/home", "f2.txt"), []byte("f2-content"), 0755)
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
for _, test := range []struct {
|
2017-03-13 18:55:02 -04:00
|
|
|
filename string
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{filepath.FromSlash("/f/f1.txt"), "f1-content"},
|
|
|
|
{filepath.FromSlash("f/f1.txt"), "f1-content"},
|
|
|
|
{filepath.FromSlash("../f2.txt"), false},
|
|
|
|
{"", false},
|
|
|
|
{"b", false},
|
|
|
|
} {
|
|
|
|
|
|
|
|
result, err := ns.ReadFile(test.filename)
|
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
2017-03-13 18:55:02 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(result, qt.Equals, test.expect)
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
}
|
2017-09-28 13:52:34 -04:00
|
|
|
|
|
|
|
func TestFileExists(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-09-28 13:52:34 -04:00
|
|
|
|
|
|
|
workingDir := "/home/hugo"
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
v := config.New()
|
2017-09-28 13:52:34 -04:00
|
|
|
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)
|
|
|
|
afero.WriteFile(ns.deps.Fs.Source, filepath.Join("/home", "f2.txt"), []byte("f2-content"), 0755)
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
for _, test := range []struct {
|
2017-09-28 13:52:34 -04:00
|
|
|
filename string
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{filepath.FromSlash("/f/f1.txt"), true},
|
|
|
|
{filepath.FromSlash("f/f1.txt"), true},
|
|
|
|
{filepath.FromSlash("../f2.txt"), false},
|
|
|
|
{"b", false},
|
|
|
|
{"", nil},
|
|
|
|
} {
|
|
|
|
result, err := ns.FileExists(test.filename)
|
|
|
|
|
|
|
|
if test.expect == nil {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
2017-09-28 13:52:34 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(result, qt.Equals, test.expect)
|
2017-09-28 13:52:34 -04:00
|
|
|
}
|
|
|
|
}
|
2018-08-04 23:37:20 -04:00
|
|
|
|
2018-08-05 08:59:29 -04:00
|
|
|
func TestStat(t *testing.T) {
|
2018-08-04 23:37:20 -04:00
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-08-04 23:37:20 -04:00
|
|
|
workingDir := "/home/hugo"
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
v := config.New()
|
2018-08-04 23:37:20 -04:00
|
|
|
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)
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
for _, test := range []struct {
|
2018-08-04 23:37:20 -04:00
|
|
|
filename string
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{filepath.FromSlash("/f/f1.txt"), int64(10)},
|
|
|
|
{filepath.FromSlash("f/f1.txt"), int64(10)},
|
|
|
|
{"b", nil},
|
|
|
|
{"", nil},
|
|
|
|
} {
|
2018-08-05 08:59:29 -04:00
|
|
|
result, err := ns.Stat(test.filename)
|
2018-08-04 23:37:20 -04:00
|
|
|
|
|
|
|
if test.expect == nil {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
2018-08-04 23:37:20 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(result.Size(), qt.Equals, test.expect)
|
2018-08-04 23:37:20 -04:00
|
|
|
}
|
|
|
|
}
|