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