configdir_test.go (3991B)
1 // Copyright 2018 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 hugolib
15
16 import (
17 "path/filepath"
18 "testing"
19
20 "github.com/gohugoio/hugo/common/herrors"
21
22 qt "github.com/frankban/quicktest"
23 "github.com/gohugoio/hugo/htesting"
24 "github.com/spf13/afero"
25 )
26
27 func TestLoadConfigDir(t *testing.T) {
28 t.Parallel()
29
30 c := qt.New(t)
31
32 configContent := `
33 baseURL = "https://example.org"
34 paginagePath = "pag_root"
35
36 [languages.en]
37 weight = 0
38 languageName = "English"
39
40 [languages.no]
41 weight = 10
42 languageName = "FOO"
43
44 [params]
45 p1 = "p1_base"
46
47 `
48
49 mm := afero.NewMemMapFs()
50
51 writeToFs(t, mm, "hugo.toml", configContent)
52
53 fb := htesting.NewTestdataBuilder(mm, "config/_default", t)
54
55 fb.Add("config.toml", `paginatePath = "pag_default"`)
56
57 fb.Add("params.yaml", `
58 p2: "p2params_default"
59 p3: "p3params_default"
60 p4: "p4params_default"
61 `)
62 fb.Add("menus.toml", `
63 [[docs]]
64 name = "About Hugo"
65 weight = 1
66 [[docs]]
67 name = "Home"
68 weight = 2
69 `)
70
71 fb.Add("menus.no.toml", `
72 [[docs]]
73 name = "Om Hugo"
74 weight = 1
75 `)
76
77 fb.Add("params.no.toml",
78 `
79 p3 = "p3params_no_default"
80 p4 = "p4params_no_default"`,
81 )
82 fb.Add("languages.no.toml", `languageName = "Norsk_no_default"`)
83
84 fb.Build()
85
86 fb = fb.WithWorkingDir("config/production")
87
88 fb.Add("config.toml", `paginatePath = "pag_production"`)
89
90 fb.Add("params.no.toml", `
91 p2 = "p2params_no_production"
92 p3 = "p3params_no_production"
93 `)
94
95 fb.Build()
96
97 fb = fb.WithWorkingDir("config/development")
98
99 // This is set in all the config.toml variants above, but this will win.
100 fb.Add("config.TOML", `paginatePath = "pag_development"`)
101 // Issue #5646
102 fb.Add("config.toml.swp", `p3 = "paginatePath = "nono"`)
103
104 fb.Add("params.no.toml", `p3 = "p3params_no_development"`)
105 fb.Add("params.toml", `p3 = "p3params_development"`)
106
107 fb.Build()
108
109 cfg, _, err := LoadConfig(ConfigSourceDescriptor{Fs: mm, Environment: "development", Filename: "hugo.toml", AbsConfigDir: "config"})
110 c.Assert(err, qt.IsNil)
111
112 c.Assert(cfg.GetString("paginatePath"), qt.Equals, "pag_development") // /config/development/config.toml
113
114 c.Assert(cfg.GetInt("languages.no.weight"), qt.Equals, 10) // /config.toml
115 c.Assert(cfg.GetString("languages.no.languageName"), qt.Equals, "Norsk_no_default") // /config/_default/languages.no.toml
116
117 c.Assert(cfg.GetString("params.p1"), qt.Equals, "p1_base")
118 c.Assert(cfg.GetString("params.p2"), qt.Equals, "p2params_default") // Is in both _default and production
119 c.Assert(cfg.GetString("params.p3"), qt.Equals, "p3params_development")
120 c.Assert(cfg.GetString("languages.no.params.p3"), qt.Equals, "p3params_no_development")
121
122 c.Assert(len(cfg.Get("menus.docs").([]any)), qt.Equals, 2)
123 noMenus := cfg.Get("languages.no.menus.docs")
124 c.Assert(noMenus, qt.Not(qt.IsNil))
125 c.Assert(len(noMenus.([]any)), qt.Equals, 1)
126 }
127
128 func TestLoadConfigDirError(t *testing.T) {
129 t.Parallel()
130
131 c := qt.New(t)
132
133 configContent := `
134 baseURL = "https://example.org"
135
136 `
137
138 mm := afero.NewMemMapFs()
139
140 writeToFs(t, mm, "hugo.toml", configContent)
141
142 fb := htesting.NewTestdataBuilder(mm, "config/development", t)
143
144 fb.Add("config.toml", `invalid & syntax`).Build()
145
146 _, _, err := LoadConfig(ConfigSourceDescriptor{Fs: mm, Environment: "development", Filename: "hugo.toml", AbsConfigDir: "config"})
147 c.Assert(err, qt.Not(qt.IsNil))
148
149 fe := herrors.UnwrapFileError(err)
150 c.Assert(fe, qt.Not(qt.IsNil))
151 c.Assert(fe.Position().Filename, qt.Equals, filepath.FromSlash("config/development/config.toml"))
152 }