github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/modules/config_test.go (about) 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 106 c.Run("Replacements", func(c *qt.C) { 107 for _, tomlConfig := range []string{` 108 [module] 109 replacements="a->b,github.com/bep/mycomponent->c" 110 [[module.imports]] 111 path="github.com/bep/mycomponent" 112 `, ` 113 [module] 114 replacements=["a->b","github.com/bep/mycomponent->c"] 115 [[module.imports]] 116 path="github.com/bep/mycomponent" 117 `} { 118 119 cfg, err := config.FromConfigString(tomlConfig, "toml") 120 c.Assert(err, qt.IsNil) 121 122 mcfg, err := DecodeConfig(cfg) 123 c.Assert(err, qt.IsNil) 124 c.Assert(mcfg.Replacements, qt.DeepEquals, []string{"a->b", "github.com/bep/mycomponent->c"}) 125 c.Assert(mcfg.replacementsMap, qt.DeepEquals, map[string]string{ 126 "a": "b", 127 "github.com/bep/mycomponent": "c", 128 }) 129 130 c.Assert(mcfg.Imports[0].Path, qt.Equals, "c") 131 132 } 133 }) 134 } 135 136 func TestDecodeConfigBothOldAndNewProvided(t *testing.T) { 137 c := qt.New(t) 138 tomlConfig := ` 139 140 theme = ["b", "c"] 141 142 [module] 143 [[module.imports]] 144 path="a" 145 146 ` 147 cfg, err := config.FromConfigString(tomlConfig, "toml") 148 c.Assert(err, qt.IsNil) 149 150 modCfg, err := DecodeConfig(cfg) 151 c.Assert(err, qt.IsNil) 152 c.Assert(len(modCfg.Imports), qt.Equals, 3) 153 c.Assert(modCfg.Imports[0].Path, qt.Equals, "a") 154 } 155 156 // Test old style theme import. 157 func TestDecodeConfigTheme(t *testing.T) { 158 c := qt.New(t) 159 tomlConfig := ` 160 161 theme = ["a", "b"] 162 ` 163 cfg, err := config.FromConfigString(tomlConfig, "toml") 164 c.Assert(err, qt.IsNil) 165 166 mcfg, err := DecodeConfig(cfg) 167 c.Assert(err, qt.IsNil) 168 169 c.Assert(len(mcfg.Imports), qt.Equals, 2) 170 c.Assert(mcfg.Imports[0].Path, qt.Equals, "a") 171 c.Assert(mcfg.Imports[1].Path, qt.Equals, "b") 172 }