| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 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 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package caddyfile |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | func TestFormatter(t *testing.T) { |
| 23 | for i, tc := range []struct { |
| 24 | description string |
| 25 | input string |
| 26 | expect string |
| 27 | }{ |
| 28 | { |
| 29 | description: "very simple", |
| 30 | input: `abc def |
| 31 | g hi jkl |
| 32 | mn`, |
| 33 | expect: `abc def |
| 34 | g hi jkl |
| 35 | mn`, |
| 36 | }, |
| 37 | { |
| 38 | description: "basic indentation, line breaks, and nesting", |
| 39 | input: ` a |
| 40 | b |
| 41 | |
| 42 | c { |
| 43 | d |
| 44 | } |
| 45 | |
| 46 | e { f |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | g { |
| 52 | h { |
| 53 | i |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | j { k { |
| 58 | l |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | m { |
| 63 | n { o |
| 64 | } |
| 65 | p { q r |
| 66 | s } |
| 67 | } |
| 68 | |
| 69 | { |
| 70 | { t |
| 71 | u |
| 72 | |
| 73 | v |
| 74 | |
| 75 | w |
| 76 | } |
| 77 | }`, |
| 78 | expect: `a |
| 79 | b |
| 80 | |
| 81 | c { |
| 82 | d |
| 83 | } |
| 84 | |
| 85 | e { |
| 86 | f |
| 87 | } |
| 88 | |
| 89 | g { |
| 90 | h { |
| 91 | i |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | j { |
| 96 | k { |
| 97 | l |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | m { |
| 102 | n { |
| 103 | o |
| 104 | } |
| 105 | p { |
| 106 | q r |
| 107 | s |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | { |
| 112 | { |
| 113 | t |
| 114 | u |
| 115 | |
| 116 | v |
| 117 | |
| 118 | w |
| 119 | } |
| 120 | }`, |
| 121 | }, |
| 122 | { |
| 123 | description: "block spacing", |
| 124 | input: `a{ |
| 125 | b |
| 126 | } |
| 127 | |
| 128 | c{ d |
| 129 | }`, |
| 130 | expect: `a { |
| 131 | b |
| 132 | } |
| 133 | |
| 134 | c { |
| 135 | d |
| 136 | }`, |
| 137 | }, |
| 138 | { |
| 139 | description: "advanced spacing", |
| 140 | input: `abc { |
| 141 | def |
| 142 | }ghi{ |
| 143 | jkl mno |
| 144 | pqr}`, |
| 145 | expect: `abc { |
| 146 | def |
| 147 | } |
| 148 | |
| 149 | ghi { |
| 150 | jkl mno |
| 151 | pqr |
| 152 | }`, |
| 153 | }, |
| 154 | { |
| 155 | description: "env var placeholders", |
| 156 | input: `{$A} |
| 157 | |
| 158 | b { |
| 159 | {$C} |
| 160 | } |
| 161 | |
| 162 | d { {$E} |
| 163 | } |
| 164 | |
| 165 | { {$F} |
| 166 | } |
| 167 | `, |
| 168 | expect: `{$A} |
| 169 | |
| 170 | b { |
| 171 | {$C} |
| 172 | } |
| 173 | |
| 174 | d { |
| 175 | {$E} |
| 176 | } |
| 177 | |
| 178 | { |
| 179 | {$F} |
| 180 | }`, |
| 181 | }, |
| 182 | { |
| 183 | description: "env var placeholders with port", |
| 184 | input: `:{$PORT}`, |
| 185 | expect: `:{$PORT}`, |
| 186 | }, |
| 187 | { |
| 188 | description: "comments", |
| 189 | input: `#a "\n" |
| 190 | |
| 191 | #b { |
| 192 | c |
| 193 | } |
| 194 | |
| 195 | d { |
| 196 | e#f |
| 197 | # g |
| 198 | } |
| 199 | |
| 200 | h { # i |
| 201 | }`, |
| 202 | expect: `#a "\n" |
| 203 | |
| 204 | #b { |
| 205 | c |
| 206 | } |
| 207 | |
| 208 | d { |
| 209 | e#f |
| 210 | # g |
| 211 | } |
| 212 | |
| 213 | h { |
| 214 | # i |
| 215 | }`, |
| 216 | }, |
| 217 | { |
| 218 | description: "quotes and escaping", |
| 219 | input: `"a \"b\" "#c |
| 220 | d |
| 221 | |
| 222 | e { |
| 223 | "f" |
| 224 | } |
| 225 | |
| 226 | g { "h" |
| 227 | } |
| 228 | |
| 229 | i { |
| 230 | "foo |
| 231 | bar" |
| 232 | } |
| 233 | |
| 234 | j { |
| 235 | "\"k\" l m" |
| 236 | }`, |
| 237 | expect: `"a \"b\" "#c |
| 238 | d |
| 239 | |
| 240 | e { |
| 241 | "f" |
| 242 | } |
| 243 | |
| 244 | g { |
| 245 | "h" |
| 246 | } |
| 247 | |
| 248 | i { |
| 249 | "foo |
| 250 | bar" |
| 251 | } |
| 252 | |
| 253 | j { |
| 254 | "\"k\" l m" |
| 255 | }`, |
| 256 | }, |
| 257 | { |
| 258 | description: "bad nesting (too many open)", |
| 259 | input: `a |
| 260 | { |
| 261 | { |
| 262 | }`, |
| 263 | expect: `a { |
| 264 | { |
| 265 | } |
| 266 | `, |
| 267 | }, |
| 268 | { |
| 269 | description: "bad nesting (too many close)", |
| 270 | input: `a |
| 271 | { |
| 272 | { |
| 273 | }}}`, |
| 274 | expect: `a { |
| 275 | { |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | `, |
| 280 | }, |
| 281 | { |
| 282 | description: "json", |
| 283 | input: `foo |
| 284 | bar "{\"key\":34}" |
| 285 | `, |
| 286 | expect: `foo |
| 287 | bar "{\"key\":34}"`, |
| 288 | }, |
| 289 | { |
| 290 | description: "escaping after spaces", |
| 291 | input: `foo \"literal\"`, |
| 292 | expect: `foo \"literal\"`, |
| 293 | }, |
| 294 | { |
| 295 | description: "simple placeholders as standalone tokens", |
| 296 | input: `foo {bar}`, |
| 297 | expect: `foo {bar}`, |
| 298 | }, |
| 299 | { |
| 300 | description: "simple placeholders within tokens", |
| 301 | input: `foo{bar} foo{bar}baz`, |
| 302 | expect: `foo{bar} foo{bar}baz`, |
| 303 | }, |
| 304 | { |
| 305 | description: "placeholders and malformed braces", |
| 306 | input: `foo{bar} foo{ bar}baz`, |
| 307 | expect: `foo{bar} foo { |
| 308 | bar |
| 309 | } |
| 310 | |
| 311 | baz`, |
| 312 | }, |
| 313 | { |
| 314 | description: "hash within string is not a comment", |
| 315 | input: `redir / /some/#/path`, |
| 316 | expect: `redir / /some/#/path`, |
| 317 | }, |
| 318 | { |
| 319 | description: "brace does not fold into comment above", |
| 320 | input: `# comment |
| 321 | { |
| 322 | foo |
| 323 | }`, |
| 324 | expect: `# comment |
| 325 | { |
| 326 | foo |
| 327 | }`, |
| 328 | }, |
| 329 | { |
| 330 | description: "matthewpi/vscode-caddyfile-support#13", |
| 331 | input: `{ |
| 332 | email {$ACMEEMAIL} |
| 333 | #debug |
| 334 | } |
| 335 | |
| 336 | block { |
| 337 | } |
| 338 | `, |
| 339 | expect: `{ |
| 340 | email {$ACMEEMAIL} |
| 341 | #debug |
| 342 | } |
| 343 | |
| 344 | block { |
| 345 | } |
| 346 | `, |
| 347 | }, |
| 348 | { |
| 349 | description: "matthewpi/vscode-caddyfile-support#13 - bad formatting", |
| 350 | input: `{ |
| 351 | email {$ACMEEMAIL} |
| 352 | #debug |
| 353 | } |
| 354 | |
| 355 | block { |
| 356 | } |
| 357 | `, |
| 358 | expect: `{ |
| 359 | email {$ACMEEMAIL} |
| 360 | #debug |
| 361 | } |
| 362 | |
| 363 | block { |
| 364 | } |
| 365 | `, |
| 366 | }, |
| 367 | { |
| 368 | description: "keep heredoc as-is", |
| 369 | input: `block { |
| 370 | heredoc <<HEREDOC |
| 371 | Here's more than one space Here's more than one space |
| 372 | HEREDOC |
| 373 | } |
| 374 | `, |
| 375 | expect: `block { |
| 376 | heredoc <<HEREDOC |
| 377 | Here's more than one space Here's more than one space |
| 378 | HEREDOC |
| 379 | } |
| 380 | `, |
| 381 | }, |
| 382 | { |
| 383 | description: "Mixing heredoc with regular part", |
| 384 | input: `block { |
| 385 | heredoc <<HEREDOC |
| 386 | Here's more than one space Here's more than one space |
| 387 | HEREDOC |
| 388 | respond "More than one space will be eaten" 200 |
| 389 | } |
| 390 | |
| 391 | block2 { |
| 392 | heredoc <<HEREDOC |
| 393 | Here's more than one space Here's more than one space |
| 394 | HEREDOC |
| 395 | respond "More than one space will be eaten" 200 |
| 396 | } |
| 397 | `, |
| 398 | expect: `block { |
| 399 | heredoc <<HEREDOC |
| 400 | Here's more than one space Here's more than one space |
| 401 | HEREDOC |
| 402 | respond "More than one space will be eaten" 200 |
| 403 | } |
| 404 | |
| 405 | block2 { |
| 406 | heredoc <<HEREDOC |
| 407 | Here's more than one space Here's more than one space |
| 408 | HEREDOC |
| 409 | respond "More than one space will be eaten" 200 |
| 410 | } |
| 411 | `, |
| 412 | }, |
| 413 | { |
| 414 | description: "Heredoc as regular token", |
| 415 | input: `block { |
| 416 | heredoc <<HEREDOC "More than one space will be eaten" |
| 417 | } |
| 418 | `, |
| 419 | expect: `block { |
| 420 | heredoc <<HEREDOC "More than one space will be eaten" |
| 421 | } |
| 422 | `, |
| 423 | }, |
| 424 | { |
| 425 | description: "Escape heredoc", |
| 426 | input: `block { |
| 427 | heredoc \<<HEREDOC |
| 428 | respond "More than one space will be eaten" 200 |
| 429 | } |
| 430 | `, |
| 431 | expect: `block { |
| 432 | heredoc \<<HEREDOC |
| 433 | respond "More than one space will be eaten" 200 |
| 434 | } |
| 435 | `, |
| 436 | }, |
| 437 | { |
| 438 | description: "Preserve braces wrapped by backquotes", |
| 439 | input: "block {respond `All braces should remain: {{now | date \"2006\"}}`}", |
| 440 | expect: "block {respond `All braces should remain: {{now | date \"2006\"}}`}", |
| 441 | }, |
| 442 | { |
| 443 | description: "Preserve braces wrapped by quotes", |
| 444 | input: "block {respond \"All braces should remain: {{now | date `2006`}}\"}", |
| 445 | expect: "block {respond \"All braces should remain: {{now | date `2006`}}\"}", |
| 446 | }, |
| 447 | { |
| 448 | description: "Preserve quoted backticks and backticked quotes", |
| 449 | input: "block { respond \"`\" } block { respond `\"`}", |
| 450 | expect: "block {\n\trespond \"`\"\n}\n\nblock {\n\trespond `\"`\n}", |
| 451 | }, |
| 452 | { |
| 453 | description: "No trailing space on line before env variable", |
| 454 | input: `{ |
| 455 | a |
| 456 | |
| 457 | {$ENV_VAR} |
| 458 | } |
| 459 | `, |
| 460 | expect: `{ |
| 461 | a |
| 462 | |
| 463 | {$ENV_VAR} |
| 464 | } |
| 465 | `, |
| 466 | }, |
| 467 | { |
| 468 | description: "issue #7425: multiline backticked string indentation", |
| 469 | input: `https://localhost:8953 { |
| 470 | respond ` + "`" + `Here are some random numbers: |
| 471 | |
| 472 | {{randNumeric 16}} |
| 473 | |
| 474 | Hope this helps.` + "`" + ` |
| 475 | }`, |
| 476 | expect: "https://localhost:8953 {\n\trespond `Here are some random numbers:\n\n{{randNumeric 16}}\n\nHope this helps.`\n}", |
| 477 | }, |
| 478 | { |
| 479 | description: "imports before global options block keep standalone brace", |
| 480 | input: `import ./conf.d/matcher_my_subnet.caddy |
| 481 | import ./conf.d/matcher_not_my_subnet.caddy |
| 482 | { |
| 483 | order crowdsec first |
| 484 | order appsec after crowdsec |
| 485 | }`, |
| 486 | expect: `import ./conf.d/matcher_my_subnet.caddy |
| 487 | import ./conf.d/matcher_not_my_subnet.caddy |
| 488 | { |
| 489 | order crowdsec first |
| 490 | order appsec after crowdsec |
| 491 | }`, |
| 492 | }, |
| 493 | } { |
| 494 | // the formatter should output a trailing newline, |
| 495 | // even if the tests aren't written to expect that |
| 496 | if !strings.HasSuffix(tc.expect, "\n") { |
| 497 | tc.expect += "\n" |
| 498 | } |
| 499 | |
| 500 | actual := Format([]byte(tc.input)) |
| 501 | |
| 502 | if string(actual) != tc.expect { |
| 503 | t.Errorf("\n[TEST %d: %s]\n====== EXPECTED ======\n%s\n====== ACTUAL ======\n%s^^^^^^^^^^^^^^^^^^^^^", |
| 504 | i, tc.description, string(tc.expect), string(actual)) |
| 505 | } |
| 506 | } |
| 507 | } |