hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

os_test.go (2782B)

    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_test
   15 
   16 import (
   17 	"path/filepath"
   18 	"testing"
   19 
   20 	"github.com/gohugoio/hugo/hugolib"
   21 	"github.com/gohugoio/hugo/tpl/os"
   22 
   23 	qt "github.com/frankban/quicktest"
   24 )
   25 
   26 func TestReadFile(t *testing.T) {
   27 	t.Parallel()
   28 
   29 	b := newFileTestBuilder(t).Build()
   30 
   31 	// helpers.PrintFs(b.H.PathSpec.BaseFs.Work, "", _os.Stdout)
   32 
   33 	ns := os.New(b.H.Deps)
   34 
   35 	for _, test := range []struct {
   36 		filename string
   37 		expect   any
   38 	}{
   39 		{filepath.FromSlash("/f/f1.txt"), "f1-content"},
   40 		{filepath.FromSlash("f/f1.txt"), "f1-content"},
   41 		{filepath.FromSlash("../f2.txt"), false},
   42 		{"", false},
   43 		{"b", false},
   44 	} {
   45 
   46 		result, err := ns.ReadFile(test.filename)
   47 
   48 		if bb, ok := test.expect.(bool); ok && !bb {
   49 			b.Assert(err, qt.Not(qt.IsNil))
   50 			continue
   51 		}
   52 
   53 		b.Assert(err, qt.IsNil)
   54 		b.Assert(result, qt.Equals, test.expect)
   55 	}
   56 }
   57 
   58 func TestFileExists(t *testing.T) {
   59 	t.Parallel()
   60 	c := qt.New(t)
   61 
   62 	b := newFileTestBuilder(t).Build()
   63 	ns := os.New(b.H.Deps)
   64 
   65 	for _, test := range []struct {
   66 		filename string
   67 		expect   any
   68 	}{
   69 		{filepath.FromSlash("/f/f1.txt"), true},
   70 		{filepath.FromSlash("f/f1.txt"), true},
   71 		{filepath.FromSlash("../f2.txt"), false},
   72 		{"b", false},
   73 		{"", nil},
   74 	} {
   75 		result, err := ns.FileExists(test.filename)
   76 
   77 		if test.expect == nil {
   78 			c.Assert(err, qt.Not(qt.IsNil))
   79 			continue
   80 		}
   81 
   82 		c.Assert(err, qt.IsNil)
   83 		c.Assert(result, qt.Equals, test.expect)
   84 	}
   85 }
   86 
   87 func TestStat(t *testing.T) {
   88 	t.Parallel()
   89 	b := newFileTestBuilder(t).Build()
   90 	ns := os.New(b.H.Deps)
   91 
   92 	for _, test := range []struct {
   93 		filename string
   94 		expect   any
   95 	}{
   96 		{filepath.FromSlash("/f/f1.txt"), int64(10)},
   97 		{filepath.FromSlash("f/f1.txt"), int64(10)},
   98 		{"b", nil},
   99 		{"", nil},
  100 	} {
  101 		result, err := ns.Stat(test.filename)
  102 
  103 		if test.expect == nil {
  104 			b.Assert(err, qt.Not(qt.IsNil))
  105 			continue
  106 		}
  107 
  108 		b.Assert(err, qt.IsNil)
  109 		b.Assert(result.Size(), qt.Equals, test.expect)
  110 	}
  111 }
  112 
  113 func newFileTestBuilder(t *testing.T) *hugolib.IntegrationTestBuilder {
  114 	files := `
  115 -- f/f1.txt --
  116 f1-content
  117 -- home/f2.txt --
  118 f2-content
  119 	`
  120 
  121 	return hugolib.NewIntegrationTestBuilder(
  122 		hugolib.IntegrationTestConfig{
  123 			T:           t,
  124 			TxtarString: files,
  125 			WorkingDir:  "/mywork",
  126 		},
  127 	)
  128 }