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