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