github.com/fighterlyt/hugo@v0.47.1/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  	"fmt"
    18  	"path/filepath"
    19  	"testing"
    20  
    21  	"github.com/gohugoio/hugo/deps"
    22  	"github.com/gohugoio/hugo/hugofs"
    23  	"github.com/spf13/afero"
    24  	"github.com/spf13/viper"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestReadFile(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	workingDir := "/home/hugo"
    33  
    34  	v := viper.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 i, 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  		errMsg := fmt.Sprintf("[%d] %v", i, test)
    54  
    55  		result, err := ns.ReadFile(test.filename)
    56  
    57  		if b, ok := test.expect.(bool); ok && !b {
    58  			require.Error(t, err, errMsg)
    59  			continue
    60  		}
    61  
    62  		require.NoError(t, err, errMsg)
    63  		assert.Equal(t, test.expect, result, errMsg)
    64  	}
    65  }
    66  
    67  func TestFileExists(t *testing.T) {
    68  	t.Parallel()
    69  
    70  	workingDir := "/home/hugo"
    71  
    72  	v := viper.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 i, 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  		errMsg := fmt.Sprintf("[%d] %v", i, test)
    91  		result, err := ns.FileExists(test.filename)
    92  
    93  		if test.expect == nil {
    94  			require.Error(t, err, errMsg)
    95  			continue
    96  		}
    97  
    98  		require.NoError(t, err, errMsg)
    99  		assert.Equal(t, test.expect, result, errMsg)
   100  	}
   101  }
   102  
   103  func TestStat(t *testing.T) {
   104  	t.Parallel()
   105  
   106  	workingDir := "/home/hugo"
   107  
   108  	v := viper.New()
   109  	v.Set("workingDir", workingDir)
   110  
   111  	ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})
   112  
   113  	afero.WriteFile(ns.deps.Fs.Source, filepath.Join(workingDir, "/f/f1.txt"), []byte("f1-content"), 0755)
   114  
   115  	for i, test := range []struct {
   116  		filename string
   117  		expect   interface{}
   118  	}{
   119  		{filepath.FromSlash("/f/f1.txt"), int64(10)},
   120  		{filepath.FromSlash("f/f1.txt"), int64(10)},
   121  		{"b", nil},
   122  		{"", nil},
   123  	} {
   124  		errMsg := fmt.Sprintf("[%d] %v", i, test)
   125  		result, err := ns.Stat(test.filename)
   126  
   127  		if test.expect == nil {
   128  			require.Error(t, err, errMsg)
   129  			continue
   130  		}
   131  
   132  		require.NoError(t, err, errMsg)
   133  		assert.Equal(t, test.expect, result.Size(), errMsg)
   134  	}
   135  }