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