path_test.go (4898B)
1 // Copyright 2018 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 path
15
16 import (
17 "path/filepath"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/config"
22 "github.com/gohugoio/hugo/deps"
23 )
24
25 var ns = New(&deps.Deps{Cfg: config.New()})
26
27 type tstNoStringer struct{}
28
29 func TestBase(t *testing.T) {
30 t.Parallel()
31 c := qt.New(t)
32
33 for _, test := range []struct {
34 path any
35 expect any
36 }{
37 {filepath.FromSlash(`foo/bar.txt`), `bar.txt`},
38 {filepath.FromSlash(`foo/bar/txt `), `txt `},
39 {filepath.FromSlash(`foo/bar.t`), `bar.t`},
40 {`foo.bar.txt`, `foo.bar.txt`},
41 {`.x`, `.x`},
42 {``, `.`},
43 // errors
44 {tstNoStringer{}, false},
45 } {
46
47 result, err := ns.Base(test.path)
48
49 if b, ok := test.expect.(bool); ok && !b {
50 c.Assert(err, qt.Not(qt.IsNil))
51 continue
52 }
53
54 c.Assert(err, qt.IsNil)
55 c.Assert(result, qt.Equals, test.expect)
56 }
57 }
58
59 func TestBaseName(t *testing.T) {
60 t.Parallel()
61 c := qt.New(t)
62
63 for _, test := range []struct {
64 path any
65 expect any
66 }{
67 {filepath.FromSlash(`foo/bar.txt`), `bar`},
68 {filepath.FromSlash(`foo/bar/txt `), `txt `},
69 {filepath.FromSlash(`foo/bar.t`), `bar`},
70 {`foo.bar.txt`, `foo.bar`},
71 {`.x`, ``},
72 {``, `.`},
73 // errors
74 {tstNoStringer{}, false},
75 } {
76
77 result, err := ns.BaseName(test.path)
78
79 if b, ok := test.expect.(bool); ok && !b {
80 c.Assert(err, qt.Not(qt.IsNil))
81 continue
82 }
83
84 c.Assert(err, qt.IsNil)
85 c.Assert(result, qt.Equals, test.expect)
86 }
87 }
88
89 func TestDir(t *testing.T) {
90 t.Parallel()
91 c := qt.New(t)
92
93 for _, test := range []struct {
94 path any
95 expect any
96 }{
97 {filepath.FromSlash(`foo/bar.txt`), `foo`},
98 {filepath.FromSlash(`foo/bar/txt `), `foo/bar`},
99 {filepath.FromSlash(`foo/bar.t`), `foo`},
100 {`foo.bar.txt`, `.`},
101 {`.x`, `.`},
102 {``, `.`},
103 // errors
104 {tstNoStringer{}, false},
105 } {
106
107 result, err := ns.Dir(test.path)
108
109 if b, ok := test.expect.(bool); ok && !b {
110 c.Assert(err, qt.Not(qt.IsNil))
111 continue
112 }
113
114 c.Assert(err, qt.IsNil)
115 c.Assert(result, qt.Equals, test.expect)
116 }
117 }
118
119 func TestExt(t *testing.T) {
120 t.Parallel()
121 c := qt.New(t)
122
123 for _, test := range []struct {
124 path any
125 expect any
126 }{
127 {filepath.FromSlash(`foo/bar.json`), `.json`},
128 {`foo.bar.txt `, `.txt `},
129 {``, ``},
130 {`.x`, `.x`},
131 // errors
132 {tstNoStringer{}, false},
133 } {
134
135 result, err := ns.Ext(test.path)
136
137 if b, ok := test.expect.(bool); ok && !b {
138 c.Assert(err, qt.Not(qt.IsNil))
139 continue
140 }
141
142 c.Assert(err, qt.IsNil)
143 c.Assert(result, qt.Equals, test.expect)
144 }
145 }
146
147 func TestJoin(t *testing.T) {
148 t.Parallel()
149 c := qt.New(t)
150
151 for _, test := range []struct {
152 elements any
153 expect any
154 }{
155 {
156 []string{"", "baz", filepath.FromSlash(`foo/bar.txt`)},
157 `baz/foo/bar.txt`,
158 },
159 {
160 []any{"", "baz", DirFile{"big", "john"}, filepath.FromSlash(`foo/bar.txt`)},
161 `baz/big|john/foo/bar.txt`,
162 },
163 {nil, ""},
164 // errors
165 {tstNoStringer{}, false},
166 {[]any{"", tstNoStringer{}}, false},
167 } {
168
169 result, err := ns.Join(test.elements)
170
171 if b, ok := test.expect.(bool); ok && !b {
172 c.Assert(err, qt.Not(qt.IsNil))
173 continue
174 }
175
176 c.Assert(err, qt.IsNil)
177 c.Assert(result, qt.Equals, test.expect)
178 }
179 }
180
181 func TestSplit(t *testing.T) {
182 t.Parallel()
183 c := qt.New(t)
184
185 for _, test := range []struct {
186 path any
187 expect any
188 }{
189 {filepath.FromSlash(`foo/bar.txt`), DirFile{`foo/`, `bar.txt`}},
190 {filepath.FromSlash(`foo/bar/txt `), DirFile{`foo/bar/`, `txt `}},
191 {`foo.bar.txt`, DirFile{``, `foo.bar.txt`}},
192 {``, DirFile{``, ``}},
193 // errors
194 {tstNoStringer{}, false},
195 } {
196
197 result, err := ns.Split(test.path)
198
199 if b, ok := test.expect.(bool); ok && !b {
200 c.Assert(err, qt.Not(qt.IsNil))
201 continue
202 }
203
204 c.Assert(err, qt.IsNil)
205 c.Assert(result, qt.Equals, test.expect)
206 }
207 }
208
209 func TestClean(t *testing.T) {
210 t.Parallel()
211 c := qt.New(t)
212
213 for _, test := range []struct {
214 path any
215 expect any
216 }{
217 {filepath.FromSlash(`foo/bar.txt`), `foo/bar.txt`},
218 {filepath.FromSlash(`foo/bar/txt`), `foo/bar/txt`},
219 {filepath.FromSlash(`foo/bar`), `foo/bar`},
220 {filepath.FromSlash(`foo/bar.t`), `foo/bar.t`},
221 {``, `.`},
222 // errors
223 {tstNoStringer{}, false},
224 } {
225
226 result, err := ns.Clean(test.path)
227
228 if b, ok := test.expect.(bool); ok && !b {
229 c.Assert(err, qt.Not(qt.IsNil))
230 continue
231 }
232
233 c.Assert(err, qt.IsNil)
234 c.Assert(result, qt.Equals, test.expect)
235 }
236 }