Debian Codegraph @

tpl/os/os_test.go

raw
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)
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"), ""},
42 {"", false},
43 {"b", ""},
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), qt.Commentf("filename: %q", test.filename))
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)
63 ns := os.New(b.H.Deps)
64
65 for _, test := range []struct {
66 filename string
67 expect bool
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 {"", false},
74 } {
75 result, err := ns.FileExists(test.filename)
76
77 c.Assert(err, qt.IsNil)
78 c.Assert(result, qt.Equals, test.expect)
79 }
80 }
81
82 func TestStat(t *testing.T) {
83 t.Parallel()
84 b := newFileTestBuilder(t)
85 ns := os.New(b.H.Deps)
86
87 for _, test := range []struct {
88 filename string
89 expect any
90 }{
91 {filepath.FromSlash("/f/f1.txt"), int64(10)},
92 {filepath.FromSlash("f/f1.txt"), int64(10)},
93 {"b", nil},
94 {"", nil},
95 } {
96 result, err := ns.Stat(test.filename)
97
98 if test.expect == nil {
99 b.Assert(err, qt.IsNil)
100 b.Assert(result, qt.IsNil)
101 continue
102 }
103
104 b.Assert(err, qt.IsNil)
105 b.Assert(result.Size(), qt.Equals, test.expect)
106 }
107 }
108
109 func newFileTestBuilder(t *testing.T) *hugolib.IntegrationTestBuilder {
110 files := `
111 -- f/f1.txt --
112 f1-content
113 -- home/f2.txt --
114 f2-content
115 `
116
117 return hugolib.Test(t, files, hugolib.TestOptWithConfig(func(c *hugolib.IntegrationTestConfig) {
118 c.WorkingDir = "/mywork"
119 }))
120 }