2023-01-04 12:24:36 -05:00
|
|
|
// Copyright 2023 The Hugo Authors. All rights reserved.
|
2015-12-10 17:19:38 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
package helpers_test
|
2014-09-10 13:48:14 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-12-07 13:48:00 -05:00
|
|
|
"path/filepath"
|
2015-04-02 00:40:29 -04:00
|
|
|
"reflect"
|
2015-01-27 17:01:32 -05:00
|
|
|
"runtime"
|
2014-09-18 17:58:44 -04:00
|
|
|
"strconv"
|
2014-09-10 13:48:14 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-09-18 17:58:44 -04:00
|
|
|
"time"
|
2014-11-04 20:28:07 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
2014-09-10 13:48:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMakePath(t *testing.T) {
|
|
|
|
tests := []struct {
|
2015-10-18 04:36:27 -04:00
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
removeAccents bool
|
2014-09-10 13:48:14 -04:00
|
|
|
}{
|
2022-01-17 17:33:47 -05:00
|
|
|
{"dot.slash/backslash\\underscore_pound#plus+hyphen-", "dot.slash/backslash\\underscore_pound#plus+hyphen-", true},
|
|
|
|
{"abcXYZ0123456789", "abcXYZ0123456789", true},
|
|
|
|
{"%20 %2", "%20-2", true},
|
|
|
|
{"foo- bar", "foo-bar", true},
|
2015-10-18 04:36:27 -04:00
|
|
|
{" Foo bar ", "Foo-bar", true},
|
|
|
|
{"Foo.Bar/foo_Bar-Foo", "Foo.Bar/foo_Bar-Foo", true},
|
2015-11-25 17:03:28 -05:00
|
|
|
{"fOO,bar:foobAR", "fOObarfoobAR", true},
|
2015-10-18 04:36:27 -04:00
|
|
|
{"FOo/BaR.html", "FOo/BaR.html", true},
|
|
|
|
{"трям/трям", "трям/трям", true},
|
|
|
|
{"은행", "은행", true},
|
|
|
|
{"Банковский кассир", "Банковскии-кассир", true},
|
|
|
|
// Issue #1488
|
|
|
|
{"संस्कृत", "संस्कृत", false},
|
2016-03-22 14:43:03 -04:00
|
|
|
{"a%C3%B1ame", "a%C3%B1ame", false}, // Issue #1292
|
2016-03-12 18:48:21 -05:00
|
|
|
{"this+is+a+test", "this+is+a+test", false}, // Issue #1290
|
2017-01-07 13:29:20 -05:00
|
|
|
{"~foo", "~foo", false}, // Issue #2177
|
2022-01-17 17:33:47 -05:00
|
|
|
{"foo--bar", "foo--bar", true}, // Issue #7288
|
2022-12-19 03:58:56 -05:00
|
|
|
{"foo@bar", "foo@bar", true}, // Issue #10548
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2023-01-04 12:24:36 -05:00
|
|
|
p := newTestPathSpec("removePathAccents", test.removeAccents)
|
2016-10-24 07:45:30 -04:00
|
|
|
output := p.MakePath(test.input)
|
2014-09-10 13:48:14 -04:00
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 08:53:25 -04:00
|
|
|
func TestMakePathSanitized(t *testing.T) {
|
2023-01-04 12:24:36 -05:00
|
|
|
p := newTestPathSpec()
|
2015-09-01 08:53:25 -04:00
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{" FOO bar ", "foo-bar"},
|
|
|
|
{"Foo.Bar/fOO_bAr-Foo", "foo.bar/foo_bar-foo"},
|
2015-11-25 17:03:28 -05:00
|
|
|
{"FOO,bar:FooBar", "foobarfoobar"},
|
2014-09-10 13:48:14 -04:00
|
|
|
{"foo/BAR.HTML", "foo/bar.html"},
|
|
|
|
{"трям/трям", "трям/трям"},
|
|
|
|
{"은행", "은행"},
|
|
|
|
}
|
2015-09-01 08:53:25 -04:00
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-10-24 07:45:30 -04:00
|
|
|
output := p.MakePathSanitized(test.input)
|
2015-09-01 08:53:25 -04:00
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakePathSanitizedDisablePathToLower(t *testing.T) {
|
2023-01-04 12:24:36 -05:00
|
|
|
p := newTestPathSpec("disablePathToLower", true)
|
2015-09-01 08:53:25 -04:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{" FOO bar ", "FOO-bar"},
|
|
|
|
{"Foo.Bar/fOO_bAr-Foo", "Foo.Bar/fOO_bAr-Foo"},
|
2015-11-25 17:03:28 -05:00
|
|
|
{"FOO,bar:FooBar", "FOObarFooBar"},
|
2015-09-01 08:53:25 -04:00
|
|
|
{"foo/BAR.HTML", "foo/BAR.HTML"},
|
|
|
|
{"трям/трям", "трям/трям"},
|
|
|
|
{"은행", "은행"},
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
for _, test := range tests {
|
2016-10-24 07:45:30 -04:00
|
|
|
output := p.MakePathSanitized(test.input)
|
2014-09-10 13:48:14 -04:00
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-09 16:40:15 -05:00
|
|
|
func TestMakePathRelative(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
inPath, path1, path2, output string
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []test{
|
|
|
|
{"/abc/bcd/ab.css", "/abc/bcd", "/bbc/bcd", "/ab.css"},
|
|
|
|
{"/abc/bcd/ab.css", "/abcd/bcd", "/abc/bcd", "/ab.css"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2023-01-04 12:24:36 -05:00
|
|
|
output, _ := helpers.MakePathRelative(d.inPath, d.path1, d.path2)
|
2014-11-09 16:40:15 -05:00
|
|
|
if d.output != output {
|
|
|
|
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
_, error := helpers.MakePathRelative("a/b/c.ss", "/a/c", "/d/c", "/e/f")
|
2014-11-09 16:40:15 -05:00
|
|
|
|
|
|
|
if error == nil {
|
2015-03-06 08:56:44 -05:00
|
|
|
t.Errorf("Test failed, expected error")
|
2014-11-09 16:40:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 18:11:39 -04:00
|
|
|
func TestGetDottedRelativePath(t *testing.T) {
|
|
|
|
// on Windows this will receive both kinds, both country and western ...
|
|
|
|
for _, f := range []func(string) string{filepath.FromSlash, func(s string) string { return s }} {
|
|
|
|
doTestGetDottedRelativePath(f, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestGetDottedRelativePath(urlFixer func(string) string, t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
input, expected string
|
|
|
|
}
|
|
|
|
data := []test{
|
|
|
|
{"", "./"},
|
|
|
|
{urlFixer("/"), "./"},
|
|
|
|
{urlFixer("post"), "../"},
|
|
|
|
{urlFixer("/post"), "../"},
|
|
|
|
{urlFixer("post/"), "../"},
|
|
|
|
{urlFixer("tags/foo.html"), "../"},
|
|
|
|
{urlFixer("/tags/foo.html"), "../"},
|
|
|
|
{urlFixer("/post/"), "../"},
|
|
|
|
{urlFixer("////post/////"), "../"},
|
|
|
|
{urlFixer("/foo/bar/index.html"), "../../"},
|
|
|
|
{urlFixer("/foo/bar/foo/"), "../../../"},
|
|
|
|
{urlFixer("/foo/bar/foo"), "../../../"},
|
|
|
|
{urlFixer("foo/bar/foo/"), "../../../"},
|
|
|
|
{urlFixer("foo/bar/foo/bar"), "../../../../"},
|
|
|
|
{"404.html", "./"},
|
|
|
|
{"404.xml", "./"},
|
|
|
|
{"/404.html", "./"},
|
|
|
|
}
|
|
|
|
for i, d := range data {
|
2023-01-04 12:24:36 -05:00
|
|
|
output := helpers.GetDottedRelativePath(d.input)
|
2015-05-15 18:11:39 -04:00
|
|
|
if d.expected != output {
|
|
|
|
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
func TestMakeTitle(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
input, expected string
|
|
|
|
}
|
|
|
|
data := []test{
|
|
|
|
{"Make-Title", "Make Title"},
|
|
|
|
{"MakeTitle", "MakeTitle"},
|
|
|
|
{"make_title", "make_title"},
|
|
|
|
}
|
|
|
|
for i, d := range data {
|
2023-01-04 12:24:36 -05:00
|
|
|
output := helpers.MakeTitle(d.input)
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expected != output {
|
|
|
|
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirExists(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
input string
|
|
|
|
expected bool
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []test{
|
|
|
|
{".", true},
|
|
|
|
{"./", true},
|
|
|
|
{"..", true},
|
|
|
|
{"../", true},
|
|
|
|
{"./..", true},
|
|
|
|
{"./../", true},
|
2014-12-07 13:48:00 -05:00
|
|
|
{os.TempDir(), true},
|
2023-01-04 12:24:36 -05:00
|
|
|
{os.TempDir() + helpers.FilePathSeparator, true},
|
2014-09-10 13:48:14 -04:00
|
|
|
{"/", true},
|
|
|
|
{"/some-really-random-directory-name", false},
|
|
|
|
{"/some/really/random/directory/name", false},
|
|
|
|
{"./some-really-random-local-directory-name", false},
|
|
|
|
{"./some/really/random/local/directory/name", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2023-01-04 12:24:36 -05:00
|
|
|
exists, _ := helpers.DirExists(filepath.FromSlash(d.input), new(afero.OsFs))
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expected != exists {
|
|
|
|
t.Errorf("Test %d failed. Expected %t got %t", i, d.expected, exists)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsDir(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
input string
|
|
|
|
expected bool
|
|
|
|
}
|
|
|
|
data := []test{
|
|
|
|
{"./", true},
|
|
|
|
{"/", true},
|
|
|
|
{"./this-directory-does-not-existi", false},
|
|
|
|
{"/this-absolute-directory/does-not-exist", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2014-11-04 20:28:07 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
exists, _ := helpers.IsDir(d.input, new(afero.OsFs))
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expected != exists {
|
|
|
|
t.Errorf("Test %d failed. Expected %t got %t", i, d.expected, exists)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 04:52:06 -04:00
|
|
|
func createZeroSizedFileInTempDir(t *testing.T) *os.File {
|
|
|
|
t.Helper()
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
filePrefix := "_path_test_"
|
2023-05-15 04:52:06 -04:00
|
|
|
f, err := os.CreateTemp(t.TempDir(), filePrefix)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
2023-05-15 04:52:06 -04:00
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
return f
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
2023-05-15 04:52:06 -04:00
|
|
|
func createNonZeroSizedFileInTempDir(t *testing.T) *os.File {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
f := createZeroSizedFileInTempDir(t)
|
2014-09-10 13:48:14 -04:00
|
|
|
byteString := []byte("byteString")
|
2023-05-15 04:52:06 -04:00
|
|
|
err := os.WriteFile(f.Name(), byteString, 0644)
|
2014-09-10 13:48:14 -04:00
|
|
|
if err != nil {
|
2023-05-15 04:52:06 -04:00
|
|
|
t.Error(err)
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
2023-05-15 04:52:06 -04:00
|
|
|
return f
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExists(t *testing.T) {
|
2023-05-15 04:52:06 -04:00
|
|
|
zeroSizedFile := createZeroSizedFileInTempDir(t)
|
|
|
|
nonZeroSizedFile := createNonZeroSizedFileInTempDir(t)
|
2023-02-18 17:43:26 -05:00
|
|
|
emptyDirectory := t.TempDir()
|
2014-09-10 13:48:14 -04:00
|
|
|
nonExistentFile := os.TempDir() + "/this-file-does-not-exist.txt"
|
2017-02-02 16:25:42 -05:00
|
|
|
nonExistentDir := os.TempDir() + "/this/directory/does/not/exist/"
|
2014-09-10 13:48:14 -04:00
|
|
|
|
|
|
|
type test struct {
|
|
|
|
input string
|
|
|
|
expectedResult bool
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []test{
|
|
|
|
{zeroSizedFile.Name(), true, nil},
|
|
|
|
{nonZeroSizedFile.Name(), true, nil},
|
|
|
|
{emptyDirectory, true, nil},
|
|
|
|
{nonExistentFile, false, nil},
|
|
|
|
{nonExistentDir, false, nil},
|
|
|
|
}
|
|
|
|
for i, d := range data {
|
2023-01-04 12:24:36 -05:00
|
|
|
exists, err := helpers.Exists(d.input, new(afero.OsFs))
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expectedResult != exists {
|
|
|
|
t.Errorf("Test %d failed. Expected result %t got %t", i, d.expectedResult, exists)
|
|
|
|
}
|
|
|
|
if d.expectedErr != err {
|
|
|
|
t.Errorf("Test %d failed. Expected %q got %q", i, d.expectedErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAbsPathify(t *testing.T) {
|
|
|
|
type test struct {
|
2015-01-27 04:15:57 -05:00
|
|
|
inPath, workingDir, expected string
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
data := []test{
|
2015-01-27 04:15:57 -05:00
|
|
|
{os.TempDir(), filepath.FromSlash("/work"), filepath.Clean(os.TempDir())}, // TempDir has trailing slash
|
|
|
|
{"dir", filepath.FromSlash("/work"), filepath.FromSlash("/work/dir")},
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
2015-01-27 17:01:32 -05:00
|
|
|
windowsData := []test{
|
|
|
|
{"c:\\banana\\..\\dir", "c:\\foo", "c:\\dir"},
|
|
|
|
{"\\dir", "c:\\foo", "c:\\foo\\dir"},
|
|
|
|
{"c:\\", "c:\\foo", "c:\\"},
|
|
|
|
}
|
|
|
|
|
|
|
|
unixData := []test{
|
|
|
|
{"/banana/../dir/", "/work", "/dir"},
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
for i, d := range data {
|
2015-01-27 04:15:57 -05:00
|
|
|
// todo see comment in AbsPathify
|
2023-01-04 12:24:36 -05:00
|
|
|
ps := newTestPathSpec("workingDir", d.workingDir)
|
2015-01-27 04:15:57 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
expected := ps.AbsPathify(d.inPath)
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expected != expected {
|
2015-01-27 06:12:35 -05:00
|
|
|
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expected, expected)
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
}
|
2015-01-27 17:01:32 -05:00
|
|
|
t.Logf("Running platform specific path tests for %s", runtime.GOOS)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
for i, d := range windowsData {
|
2023-01-04 12:24:36 -05:00
|
|
|
ps := newTestPathSpec("workingDir", d.workingDir)
|
2015-01-27 17:01:32 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
expected := ps.AbsPathify(d.inPath)
|
2015-01-27 17:01:32 -05:00
|
|
|
if d.expected != expected {
|
|
|
|
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expected, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i, d := range unixData {
|
2023-01-04 12:24:36 -05:00
|
|
|
ps := newTestPathSpec("workingDir", d.workingDir)
|
2015-01-27 17:01:32 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
expected := ps.AbsPathify(d.inPath)
|
2015-01-27 17:01:32 -05:00
|
|
|
if d.expected != expected {
|
|
|
|
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expected, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
func TestExtractAndGroupRootPaths(t *testing.T) {
|
|
|
|
in := []string{
|
|
|
|
filepath.FromSlash("/a/b/c/d"),
|
|
|
|
filepath.FromSlash("/a/b/c/e"),
|
|
|
|
filepath.FromSlash("/a/b/e/f"),
|
|
|
|
filepath.FromSlash("/a/b"),
|
|
|
|
filepath.FromSlash("/a/b/c/b/g"),
|
|
|
|
filepath.FromSlash("/c/d/e"),
|
|
|
|
}
|
|
|
|
|
|
|
|
inCopy := make([]string, len(in))
|
|
|
|
copy(inCopy, in)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
result := helpers.ExtractAndGroupRootPaths(in)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
|
|
|
c.Assert(fmt.Sprint(result), qt.Equals, filepath.FromSlash("[/a/b/{c,e} /c/d/e]"))
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
|
|
|
|
// Make sure the original is preserved
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(in, qt.DeepEquals, inCopy)
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2015-11-23 10:32:06 -05:00
|
|
|
func TestExtractRootPaths(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input []string
|
|
|
|
expected []string
|
2020-12-02 07:23:25 -05:00
|
|
|
}{{
|
|
|
|
[]string{
|
|
|
|
filepath.FromSlash("a/b"), filepath.FromSlash("a/b/c/"), "b",
|
|
|
|
filepath.FromSlash("/c/d"), filepath.FromSlash("d/"), filepath.FromSlash("//e//"),
|
|
|
|
},
|
|
|
|
[]string{"a", "a", "b", "c", "d", "e"},
|
|
|
|
}}
|
2015-11-23 10:32:06 -05:00
|
|
|
|
|
|
|
for _, test := range tests {
|
2023-01-04 12:24:36 -05:00
|
|
|
output := helpers.ExtractRootPaths(test.input)
|
2015-11-23 10:32:06 -05:00
|
|
|
if !reflect.DeepEqual(output, test.expected) {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
2015-04-02 00:40:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
func TestFindCWD(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
expectedDir string
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:23:25 -05:00
|
|
|
// cwd, _ := os.Getwd()
|
2014-09-10 13:48:14 -04:00
|
|
|
data := []test{
|
2018-02-21 03:23:43 -05:00
|
|
|
//{cwd, nil},
|
|
|
|
// Commenting this out. It doesn't work properly.
|
|
|
|
// There's a good reason why we don't use os.Getwd(), it doesn't actually work the way we want it to.
|
|
|
|
// I really don't know a better way to test this function. - SPF 2014.11.04
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
for i, d := range data {
|
2023-01-04 12:24:36 -05:00
|
|
|
dir, err := helpers.FindCWD()
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expectedDir != dir {
|
|
|
|
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expectedDir, dir)
|
|
|
|
}
|
|
|
|
if d.expectedErr != err {
|
2015-03-06 08:56:44 -05:00
|
|
|
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expectedErr, err)
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSafeWriteToDisk(t *testing.T) {
|
2023-05-15 04:52:06 -04:00
|
|
|
emptyFile := createZeroSizedFileInTempDir(t)
|
2023-02-18 17:43:26 -05:00
|
|
|
tmpDir := t.TempDir()
|
2014-09-10 13:48:14 -04:00
|
|
|
|
2014-09-10 14:47:31 -04:00
|
|
|
randomString := "This is a random string!"
|
|
|
|
reader := strings.NewReader(randomString)
|
2014-09-10 13:48:14 -04:00
|
|
|
|
2014-09-10 14:47:31 -04:00
|
|
|
fileExists := fmt.Errorf("%v already exists", emptyFile.Name())
|
2014-09-18 17:58:44 -04:00
|
|
|
|
2014-09-10 14:47:31 -04:00
|
|
|
type test struct {
|
|
|
|
filename string
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
2014-09-18 17:58:44 -04:00
|
|
|
now := time.Now().Unix()
|
|
|
|
nowStr := strconv.FormatInt(now, 10)
|
2014-09-10 14:47:31 -04:00
|
|
|
data := []test{
|
|
|
|
{emptyFile.Name(), fileExists},
|
2014-09-18 17:58:44 -04:00
|
|
|
{tmpDir + "/" + nowStr, nil},
|
2014-09-10 14:47:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2023-01-04 12:24:36 -05:00
|
|
|
e := helpers.SafeWriteToDisk(d.filename, reader, new(afero.OsFs))
|
2014-09-10 14:47:31 -04:00
|
|
|
if d.expectedErr != nil {
|
|
|
|
if d.expectedErr.Error() != e.Error() {
|
|
|
|
t.Errorf("Test %d failed. Expected error %q but got %q", i, d.expectedErr.Error(), e.Error())
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
2014-09-18 17:38:07 -04:00
|
|
|
} else {
|
|
|
|
if d.expectedErr != e {
|
|
|
|
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expectedErr, e)
|
|
|
|
}
|
2023-02-18 17:43:26 -05:00
|
|
|
contents, _ := os.ReadFile(d.filename)
|
2014-09-18 17:38:07 -04:00
|
|
|
if randomString != string(contents) {
|
|
|
|
t.Errorf("Test %d failed. Expected contents %q but got %q", i, randomString, string(contents))
|
|
|
|
}
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
2014-09-18 17:38:07 -04:00
|
|
|
reader.Seek(0, 0)
|
2014-09-10 14:47:31 -04:00
|
|
|
}
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteToDisk(t *testing.T) {
|
2023-05-15 04:52:06 -04:00
|
|
|
emptyFile := createZeroSizedFileInTempDir(t)
|
2023-02-18 17:43:26 -05:00
|
|
|
tmpDir := t.TempDir()
|
2014-09-10 13:48:14 -04:00
|
|
|
|
|
|
|
randomString := "This is a random string!"
|
|
|
|
reader := strings.NewReader(randomString)
|
|
|
|
|
|
|
|
type test struct {
|
|
|
|
filename string
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
2014-09-18 17:58:44 -04:00
|
|
|
now := time.Now().Unix()
|
|
|
|
nowStr := strconv.FormatInt(now, 10)
|
2014-09-10 13:48:14 -04:00
|
|
|
data := []test{
|
|
|
|
{emptyFile.Name(), nil},
|
2014-09-18 17:58:44 -04:00
|
|
|
{tmpDir + "/" + nowStr, nil},
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2023-01-04 12:24:36 -05:00
|
|
|
e := helpers.WriteToDisk(d.filename, reader, new(afero.OsFs))
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expectedErr != e {
|
2014-09-18 17:38:07 -04:00
|
|
|
t.Errorf("Test %d failed. WriteToDisk Error Expected %q but got %q", i, d.expectedErr, e)
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
2023-02-18 17:43:26 -05:00
|
|
|
contents, e := os.ReadFile(d.filename)
|
2014-09-18 17:38:07 -04:00
|
|
|
if e != nil {
|
2015-03-06 08:56:44 -05:00
|
|
|
t.Errorf("Test %d failed. Could not read file %s. Reason: %s\n", i, d.filename, e)
|
2014-09-18 17:38:07 -04:00
|
|
|
}
|
2014-09-10 13:48:14 -04:00
|
|
|
if randomString != string(contents) {
|
|
|
|
t.Errorf("Test %d failed. Expected contents %q but got %q", i, randomString, string(contents))
|
|
|
|
}
|
2014-09-18 17:38:07 -04:00
|
|
|
reader.Seek(0, 0)
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
}
|
2014-12-26 22:40:10 -05:00
|
|
|
|
|
|
|
func TestGetTempDir(t *testing.T) {
|
|
|
|
dir := os.TempDir()
|
2023-01-04 12:24:36 -05:00
|
|
|
if helpers.FilePathSeparator != dir[len(dir)-1:] {
|
|
|
|
dir = dir + helpers.FilePathSeparator
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
testDir := "hugoTestFolder" + helpers.FilePathSeparator
|
2014-12-26 22:40:10 -05:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"", dir},
|
2023-01-04 12:24:36 -05:00
|
|
|
{testDir + " Foo bar ", dir + testDir + " Foo bar " + helpers.FilePathSeparator},
|
|
|
|
{testDir + "Foo.Bar/foo_Bar-Foo", dir + testDir + "Foo.Bar/foo_Bar-Foo" + helpers.FilePathSeparator},
|
|
|
|
{testDir + "fOO,bar:foo%bAR", dir + testDir + "fOObarfoo%bAR" + helpers.FilePathSeparator},
|
|
|
|
{testDir + "fOO,bar:foobAR", dir + testDir + "fOObarfoobAR" + helpers.FilePathSeparator},
|
|
|
|
{testDir + "FOo/BaR.html", dir + testDir + "FOo/BaR.html" + helpers.FilePathSeparator},
|
|
|
|
{testDir + "трям/трям", dir + testDir + "трям/трям" + helpers.FilePathSeparator},
|
|
|
|
{testDir + "은행", dir + testDir + "은행" + helpers.FilePathSeparator},
|
|
|
|
{testDir + "Банковский кассир", dir + testDir + "Банковский кассир" + helpers.FilePathSeparator},
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2023-01-04 12:24:36 -05:00
|
|
|
output := helpers.GetTempDir(test.input, new(afero.MemMapFs))
|
2014-12-26 22:40:10 -05:00
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|