mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-26 02:09:47 +08:00
160d199999
Previous commit improved the Caddyfile adapter so it doesn't unnecessarily add names to "skip" in "auto_https" when the server is already HTTP-only. This commit updates the tests to reflect that change, while also fixing the Caddyfile formatting in many of the tests. We also print the line number of the divergence between input and formatted version in Caddyfile adapt warnings - very useful for finding initial formatting problems.
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package integration
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/caddyserver/caddy/v2/caddytest"
|
|
)
|
|
|
|
func TestCaddyfileAdaptToJSON(t *testing.T) {
|
|
// load the list of test files from the dir
|
|
files, err := ioutil.ReadDir("./caddyfile_adapt")
|
|
if err != nil {
|
|
t.Errorf("failed to read caddyfile_adapt dir: %s", err)
|
|
}
|
|
|
|
// prep a regexp to fix strings on windows
|
|
winNewlines := regexp.MustCompile(`\r?\n`)
|
|
|
|
for _, f := range files {
|
|
if f.IsDir() {
|
|
continue
|
|
}
|
|
|
|
// read the test file
|
|
filename := f.Name()
|
|
data, err := ioutil.ReadFile("./caddyfile_adapt/" + filename)
|
|
if err != nil {
|
|
t.Errorf("failed to read %s dir: %s", filename, err)
|
|
}
|
|
|
|
// split the Caddyfile (first) and JSON (second) parts
|
|
// (append newline to Caddyfile to match formatter expectations)
|
|
parts := strings.Split(string(data), "----------")
|
|
caddyfile, json := strings.TrimSpace(parts[0])+"\n", strings.TrimSpace(parts[1])
|
|
|
|
// replace windows newlines in the json with unix newlines
|
|
json = winNewlines.ReplaceAllString(json, "\n")
|
|
|
|
// run the test
|
|
ok := caddytest.CompareAdapt(t, filename, caddyfile, "caddyfile", json)
|
|
if !ok {
|
|
t.Errorf("failed to adapt %s", filename)
|
|
}
|
|
}
|
|
}
|