| 1 |
// Copyright 2024 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 |
//go:build withdeploy |
| 15 |
|
| 16 |
package deployconfig |
| 17 |
|
| 18 |
import ( |
| 19 |
"fmt" |
| 20 |
"testing" |
| 21 |
|
| 22 |
qt "github.com/frankban/quicktest" |
| 23 |
"github.com/gohugoio/hugo/config" |
| 24 |
) |
| 25 |
|
| 26 |
func TestDecodeConfigFromTOML(t *testing.T) { |
| 27 |
c := qt.New(t) |
| 28 |
|
| 29 |
tomlConfig := ` |
| 30 |
|
| 31 |
someOtherValue = "foo" |
| 32 |
|
| 33 |
[deployment] |
| 34 |
|
| 35 |
order = ["o1", "o2"] |
| 36 |
|
| 37 |
# All lowercase. |
| 38 |
[[deployment.targets]] |
| 39 |
name = "name0" |
| 40 |
url = "url0" |
| 41 |
cloudfrontdistributionid = "cdn0" |
| 42 |
include = "*.html" |
| 43 |
|
| 44 |
# All uppercase. |
| 45 |
[[deployment.targets]] |
| 46 |
NAME = "name1" |
| 47 |
URL = "url1" |
| 48 |
CLOUDFRONTDISTRIBUTIONID = "cdn1" |
| 49 |
INCLUDE = "*.jpg" |
| 50 |
|
| 51 |
# Camelcase. |
| 52 |
[[deployment.targets]] |
| 53 |
name = "name2" |
| 54 |
url = "url2" |
| 55 |
cloudFrontDistributionID = "cdn2" |
| 56 |
exclude = "*.png" |
| 57 |
|
| 58 |
# All lowercase. |
| 59 |
[[deployment.matchers]] |
| 60 |
pattern = "^pattern0$" |
| 61 |
cachecontrol = "cachecontrol0" |
| 62 |
contentencoding = "contentencoding0" |
| 63 |
contenttype = "contenttype0" |
| 64 |
|
| 65 |
# All uppercase. |
| 66 |
[[deployment.matchers]] |
| 67 |
PATTERN = "^pattern1$" |
| 68 |
CACHECONTROL = "cachecontrol1" |
| 69 |
CONTENTENCODING = "contentencoding1" |
| 70 |
CONTENTTYPE = "contenttype1" |
| 71 |
GZIP = true |
| 72 |
FORCE = true |
| 73 |
|
| 74 |
# Camelcase. |
| 75 |
[[deployment.matchers]] |
| 76 |
pattern = "^pattern2$" |
| 77 |
cacheControl = "cachecontrol2" |
| 78 |
contentEncoding = "contentencoding2" |
| 79 |
contentType = "contenttype2" |
| 80 |
gzip = true |
| 81 |
force = true |
| 82 |
` |
| 83 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 84 |
c.Assert(err, qt.IsNil) |
| 85 |
|
| 86 |
dcfg, err := DecodeConfig(cfg) |
| 87 |
c.Assert(err, qt.IsNil) |
| 88 |
|
| 89 |
// Order. |
| 90 |
c.Assert(len(dcfg.Order), qt.Equals, 2) |
| 91 |
c.Assert(dcfg.Order[0], qt.Equals, "o1") |
| 92 |
c.Assert(dcfg.Order[1], qt.Equals, "o2") |
| 93 |
c.Assert(len(dcfg.Ordering), qt.Equals, 2) |
| 94 |
|
| 95 |
// Targets. |
| 96 |
c.Assert(len(dcfg.Targets), qt.Equals, 3) |
| 97 |
wantInclude := []string{"*.html", "*.jpg", ""} |
| 98 |
wantExclude := []string{"", "", "*.png"} |
| 99 |
for i := 0; i < 3; i++ { |
| 100 |
tgt := dcfg.Targets[i] |
| 101 |
c.Assert(tgt.Name, qt.Equals, fmt.Sprintf("name%d", i)) |
| 102 |
c.Assert(tgt.URL, qt.Equals, fmt.Sprintf("url%d", i)) |
| 103 |
c.Assert(tgt.CloudFrontDistributionID, qt.Equals, fmt.Sprintf("cdn%d", i)) |
| 104 |
c.Assert(tgt.Include, qt.Equals, wantInclude[i]) |
| 105 |
if wantInclude[i] != "" { |
| 106 |
c.Assert(tgt.IncludeGlob, qt.Not(qt.IsNil)) |
| 107 |
} |
| 108 |
c.Assert(tgt.Exclude, qt.Equals, wantExclude[i]) |
| 109 |
if wantExclude[i] != "" { |
| 110 |
c.Assert(tgt.ExcludeGlob, qt.Not(qt.IsNil)) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// Matchers. |
| 115 |
c.Assert(len(dcfg.Matchers), qt.Equals, 3) |
| 116 |
for i := 0; i < 3; i++ { |
| 117 |
m := dcfg.Matchers[i] |
| 118 |
c.Assert(m.Pattern, qt.Equals, fmt.Sprintf("^pattern%d$", i)) |
| 119 |
c.Assert(m.Re, qt.Not(qt.IsNil)) |
| 120 |
c.Assert(m.CacheControl, qt.Equals, fmt.Sprintf("cachecontrol%d", i)) |
| 121 |
c.Assert(m.ContentEncoding, qt.Equals, fmt.Sprintf("contentencoding%d", i)) |
| 122 |
c.Assert(m.ContentType, qt.Equals, fmt.Sprintf("contenttype%d", i)) |
| 123 |
c.Assert(m.Gzip, qt.Equals, i != 0) |
| 124 |
c.Assert(m.Force, qt.Equals, i != 0) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
func TestInvalidOrderingPattern(t *testing.T) { |
| 129 |
c := qt.New(t) |
| 130 |
|
| 131 |
tomlConfig := ` |
| 132 |
|
| 133 |
someOtherValue = "foo" |
| 134 |
|
| 135 |
[deployment] |
| 136 |
order = ["["] # invalid regular expression |
| 137 |
` |
| 138 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 139 |
c.Assert(err, qt.IsNil) |
| 140 |
|
| 141 |
_, err = DecodeConfig(cfg) |
| 142 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 143 |
} |
| 144 |
|
| 145 |
func TestInvalidMatcherPattern(t *testing.T) { |
| 146 |
c := qt.New(t) |
| 147 |
|
| 148 |
tomlConfig := ` |
| 149 |
|
| 150 |
someOtherValue = "foo" |
| 151 |
|
| 152 |
[deployment] |
| 153 |
[[deployment.matchers]] |
| 154 |
Pattern = "[" # invalid regular expression |
| 155 |
` |
| 156 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 157 |
c.Assert(err, qt.IsNil) |
| 158 |
|
| 159 |
_, err = DecodeConfig(cfg) |
| 160 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 161 |
} |
| 162 |
|
| 163 |
func TestDecodeConfigDefault(t *testing.T) { |
| 164 |
c := qt.New(t) |
| 165 |
|
| 166 |
dcfg, err := DecodeConfig(config.New()) |
| 167 |
c.Assert(err, qt.IsNil) |
| 168 |
c.Assert(len(dcfg.Targets), qt.Equals, 0) |
| 169 |
c.Assert(len(dcfg.Matchers), qt.Equals, 0) |
| 170 |
} |
| 171 |
|
| 172 |
func TestEmptyTarget(t *testing.T) { |
| 173 |
c := qt.New(t) |
| 174 |
|
| 175 |
tomlConfig := ` |
| 176 |
[deployment] |
| 177 |
[[deployment.targets]] |
| 178 |
` |
| 179 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 180 |
c.Assert(err, qt.IsNil) |
| 181 |
|
| 182 |
_, err = DecodeConfig(cfg) |
| 183 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 184 |
} |
| 185 |
|
| 186 |
func TestEmptyMatcher(t *testing.T) { |
| 187 |
c := qt.New(t) |
| 188 |
|
| 189 |
tomlConfig := ` |
| 190 |
[deployment] |
| 191 |
[[deployment.matchers]] |
| 192 |
` |
| 193 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 194 |
c.Assert(err, qt.IsNil) |
| 195 |
|
| 196 |
_, err = DecodeConfig(cfg) |
| 197 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 198 |
} |