| 1 |
// Copyright 2019 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 modules |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"os" |
| 19 |
"path/filepath" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/common/hugo" |
| 23 |
"github.com/gohugoio/hugo/common/loggers" |
| 24 |
"github.com/gohugoio/hugo/common/version" |
| 25 |
|
| 26 |
"github.com/gohugoio/hugo/config" |
| 27 |
|
| 28 |
qt "github.com/frankban/quicktest" |
| 29 |
) |
| 30 |
|
| 31 |
func TestConfigHugoVersionIsValid(t *testing.T) { |
| 32 |
c := qt.New(t) |
| 33 |
|
| 34 |
for _, test := range []struct { |
| 35 |
in HugoVersion |
| 36 |
expect bool |
| 37 |
}{ |
| 38 |
{HugoVersion{Min: "0.33.0"}, true}, |
| 39 |
{HugoVersion{Min: "0.56.0-DEV"}, true}, |
| 40 |
{HugoVersion{Min: "0.33.0", Max: "0.55.0"}, false}, |
| 41 |
{HugoVersion{Min: "0.33.0", Max: "0.199.0"}, true}, |
| 42 |
} { |
| 43 |
c.Assert(test.in.IsValid(), qt.Equals, test.expect, qt.Commentf("%#v", test.in)) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func TestDecodeConfig(t *testing.T) { |
| 48 |
c := qt.New(t) |
| 49 |
|
| 50 |
c.Run("Basic", func(c *qt.C) { |
| 51 |
tempDir := c.TempDir() |
| 52 |
tomlConfig := ` |
| 53 |
workingDir = %q |
| 54 |
[module] |
| 55 |
workspace = "hugo.work" |
| 56 |
[module.hugoVersion] |
| 57 |
min = "0.54.2" |
| 58 |
max = "0.199.0" |
| 59 |
extended = true |
| 60 |
[[module.mounts]] |
| 61 |
source="src/project/blog" |
| 62 |
target="content/blog" |
| 63 |
lang="en" |
| 64 |
[[module.imports]] |
| 65 |
path="github.com/bep/mycomponent" |
| 66 |
[[module.imports.mounts]] |
| 67 |
source="scss" |
| 68 |
target="assets/bootstrap/scss" |
| 69 |
[[module.imports.mounts]] |
| 70 |
source="src/markdown/blog" |
| 71 |
target="content/blog" |
| 72 |
lang="en" |
| 73 |
` |
| 74 |
|
| 75 |
hugoWorkFilename := filepath.Join(tempDir, "hugo.work") |
| 76 |
f, _ := os.Create(hugoWorkFilename) |
| 77 |
f.Close() |
| 78 |
cfg, err := config.FromConfigString(fmt.Sprintf(tomlConfig, tempDir), "toml") |
| 79 |
c.Assert(err, qt.IsNil) |
| 80 |
|
| 81 |
mcfg, err := DecodeConfig(loggers.NewDefault().Logger(), cfg) |
| 82 |
c.Assert(err, qt.IsNil) |
| 83 |
|
| 84 |
v056 := version.VersionString("0.56.0") |
| 85 |
|
| 86 |
hv := mcfg.HugoVersion |
| 87 |
|
| 88 |
c.Assert(v056.Compare(hv.Min), qt.Equals, -1) |
| 89 |
c.Assert(v056.Compare(hv.Max), qt.Equals, 1) |
| 90 |
c.Assert(hv.Extended, qt.Equals, true) |
| 91 |
|
| 92 |
if hugo.IsExtended { |
| 93 |
c.Assert(hv.IsValid(), qt.Equals, true) |
| 94 |
} |
| 95 |
|
| 96 |
c.Assert(mcfg.Workspace, qt.Equals, hugoWorkFilename) |
| 97 |
|
| 98 |
c.Assert(len(mcfg.Mounts), qt.Equals, 1) |
| 99 |
c.Assert(len(mcfg.Imports), qt.Equals, 1) |
| 100 |
imp := mcfg.Imports[0] |
| 101 |
imp.Path = "github.com/bep/mycomponent" |
| 102 |
c.Assert(imp.Mounts[1].Source, qt.Equals, "src/markdown/blog") |
| 103 |
c.Assert(imp.Mounts[1].Target, qt.Equals, "content/blog") |
| 104 |
c.Assert(imp.Mounts[1].Lang, qt.Equals, "en") |
| 105 |
}) |
| 106 |
|
| 107 |
c.Run("Replacements", func(c *qt.C) { |
| 108 |
for _, tomlConfig := range []string{` |
| 109 |
[module] |
| 110 |
replacements="a->b,github.com/bep/mycomponent->c" |
| 111 |
[[module.imports]] |
| 112 |
path="github.com/bep/mycomponent" |
| 113 |
`, ` |
| 114 |
[module] |
| 115 |
replacements=["a->b","github.com/bep/mycomponent->c"] |
| 116 |
[[module.imports]] |
| 117 |
path="github.com/bep/mycomponent" |
| 118 |
`} { |
| 119 |
|
| 120 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 121 |
c.Assert(err, qt.IsNil) |
| 122 |
|
| 123 |
mcfg, err := DecodeConfig(loggers.NewDefault().Logger(), cfg) |
| 124 |
c.Assert(err, qt.IsNil) |
| 125 |
c.Assert(mcfg.Replacements, qt.DeepEquals, []string{"a->b", "github.com/bep/mycomponent->c"}) |
| 126 |
c.Assert(mcfg.replacementsMap, qt.DeepEquals, map[string]string{ |
| 127 |
"a": "b", |
| 128 |
"github.com/bep/mycomponent": "c", |
| 129 |
}) |
| 130 |
|
| 131 |
c.Assert(mcfg.Imports[0].Path, qt.Equals, "c") |
| 132 |
|
| 133 |
} |
| 134 |
}) |
| 135 |
} |
| 136 |
|
| 137 |
func TestDecodeConfigBothOldAndNewProvided(t *testing.T) { |
| 138 |
c := qt.New(t) |
| 139 |
tomlConfig := ` |
| 140 |
|
| 141 |
theme = ["b", "c"] |
| 142 |
|
| 143 |
[module] |
| 144 |
[[module.imports]] |
| 145 |
path="a" |
| 146 |
|
| 147 |
` |
| 148 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 149 |
c.Assert(err, qt.IsNil) |
| 150 |
|
| 151 |
modCfg, err := DecodeConfig(loggers.NewDefault().Logger(), cfg) |
| 152 |
c.Assert(err, qt.IsNil) |
| 153 |
c.Assert(len(modCfg.Imports), qt.Equals, 3) |
| 154 |
c.Assert(modCfg.Imports[0].Path, qt.Equals, "a") |
| 155 |
} |
| 156 |
|
| 157 |
// Test old style theme import. |
| 158 |
func TestDecodeConfigTheme(t *testing.T) { |
| 159 |
c := qt.New(t) |
| 160 |
tomlConfig := ` |
| 161 |
|
| 162 |
theme = ["a", "b"] |
| 163 |
` |
| 164 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 165 |
c.Assert(err, qt.IsNil) |
| 166 |
|
| 167 |
mcfg, err := DecodeConfig(loggers.NewDefault().Logger(), cfg) |
| 168 |
c.Assert(err, qt.IsNil) |
| 169 |
|
| 170 |
c.Assert(len(mcfg.Imports), qt.Equals, 2) |
| 171 |
c.Assert(mcfg.Imports[0].Path, qt.Equals, "a") |
| 172 |
c.Assert(mcfg.Imports[1].Path, qt.Equals, "b") |
| 173 |
} |