github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/tpl/os/os_test.go (about)

     1  // Copyright 2017 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package os
    15  
    16  import (
    17  	"path/filepath"
    18  	"testing"
    19  
    20  	"github.com/gohugoio/hugo/config"
    21  
    22  	qt "github.com/frankban/quicktest"
    23  	"github.com/gohugoio/hugo/deps"
    24  	"github.com/gohugoio/hugo/hugofs"
    25  	"github.com/spf13/afero"
    26  )
    27  
    28  func TestReadFile(t *testing.T) {
    29  	t.Parallel()
    30  	c := qt.New(t)
    31  
    32  	workingDir := "/home/hugo"
    33  
    34  	v := config.New()
    35  	v.Set("workingDir", workingDir)
    36  
    37  	// f := newTestFuncsterWithViper(v)
    38  	ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})
    39  
    40  	afero.WriteFile(ns.deps.Fs.Source, filepath.Join(workingDir, "/f/f1.txt"), []byte("f1-content"), 0755)
    41  	afero.WriteFile(ns.deps.Fs.Source, filepath.Join("/home", "f2.txt"), []byte("f2-content"), 0755)
    42  
    43  	for _, test := range []struct {
    44  		filename string
    45  		expect   interface{}
    46  	}{
    47  		{filepath.FromSlash("/f/f1.txt"), "f1-content"},
    48  		{filepath.FromSlash("f/f1.txt"), "f1-content"},
    49  		{filepath.FromSlash("../f2.txt"), false},
    50  		{"", false},
    51  		{"b", false},
    52  	} {
    53  
    54  		result, err := ns.ReadFile(test.filename)
    55  
    56  		if b, ok := test.expect.(bool); ok && !b {
    57  			c.Assert(err, qt.Not(qt.IsNil))
    58  			continue
    59  		}
    60  
    61  		c.Assert(err, qt.IsNil)
    62  		c.Assert(result, qt.Equals, test.expect)
    63  	}
    64  }
    65  
    66  func TestFileExists(t *testing.T) {
    67  	t.Parallel()
    68  	c := qt.New(t)
    69  
    70  	workingDir := "/home/hugo"
    71  
    72  	v := config.New()
    73  	v.Set("workingDir", workingDir)
    74  
    75  	ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})
    76  
    77  	afero.WriteFile(ns.deps.Fs.Source, filepath.Join(workingDir, "/f/f1.txt"), []byte("f1-content"), 0755)
    78  	afero.WriteFile(ns.deps.Fs.Source, filepath.Join("/home", "f2.txt"), []byte("f2-content"), 0755)
    79  
    80  	for _, test := range []struct {
    81  		filename string
    82  		expect   interface{}
    83  	}{
    84  		{filepath.FromSlash("/f/f1.txt"), true},
    85  		{filepath.FromSlash("f/f1.txt"), true},
    86  		{filepath.FromSlash("../f2.txt"), false},
    87  		{"b", false},
    88  		{"", nil},
    89  	} {
    90  		result, err := ns.FileExists(test.filename)
    91  
    92  		if test.expect == nil {
    93  			c.Assert(err, qt.Not(qt.IsNil))
    94  			continue
    95  		}
    96  
    97  		c.Assert(err, qt.IsNil)
    98  		c.Assert(result, qt.Equals, test.expect)
    99  	}
   100  }
   101  
   102  func TestStat(t *testing.T) {
   103  	t.Parallel()
   104  	c := qt.New(t)
   105  	workingDir := "/home/hugo"
   106  
   107  	v := config.New()
   108  	v.Set("workingDir", workingDir)
   109  
   110  	ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})
   111  
   112  	afero.WriteFile(ns.deps.Fs.Source, filepath.Join(workingDir, "/f/f1.txt"), []byte("f1-content"), 0755)
   113  
   114  	for _, test := range []struct {
   115  		filename string
   116  		expect   interface{}
   117  	}{
   118  		{filepath.FromSlash("/f/f1.txt"), int64(10)},
   119  		{filepath.FromSlash("f/f1.txt"), int64(10)},
   120  		{"b", nil},
   121  		{"", nil},
   122  	} {
   123  		result, err := ns.Stat(test.filename)
   124  
   125  		if test.expect == nil {
   126  			c.Assert(err, qt.Not(qt.IsNil))
   127  			continue
   128  		}
   129  
   130  		c.Assert(err, qt.IsNil)
   131  		c.Assert(result.Size(), qt.Equals, test.expect)
   132  	}
   133  }