2020-04-03 22:02:46 -05:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2020-05-14 16:53:28 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-04-03 22:02:46 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2/caddytest"
|
|
|
|
)
|
|
|
|
|
2020-05-14 16:53:28 -05:00
|
|
|
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)
|
2020-04-03 22:02:46 -05:00
|
|
|
}
|
|
|
|
|
2020-05-14 16:53:28 -05:00
|
|
|
// prep a regexp to fix strings on windows
|
|
|
|
winNewlines := regexp.MustCompile(`\r?\n`)
|
2020-04-03 22:02:46 -05:00
|
|
|
|
2020-05-14 16:53:28 -05:00
|
|
|
for _, f := range files {
|
|
|
|
if f.IsDir() {
|
|
|
|
continue
|
2020-04-03 22:02:46 -05:00
|
|
|
}
|
|
|
|
|
2020-05-14 16:53:28 -05:00
|
|
|
// 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)
|
2020-04-03 22:02:46 -05:00
|
|
|
}
|
|
|
|
|
2020-05-14 16:53:28 -05:00
|
|
|
// split the Caddyfile (first) and JSON (second) parts
|
|
|
|
parts := strings.Split(string(data), "----------")
|
|
|
|
caddyfile, json := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
|
2020-05-05 13:29:21 -05:00
|
|
|
|
2020-05-14 16:53:28 -05:00
|
|
|
// replace windows newlines in the json with unix newlines
|
|
|
|
json = winNewlines.ReplaceAllString(json, "\n")
|
2020-05-11 17:50:49 -05:00
|
|
|
|
2020-05-14 16:53:28 -05:00
|
|
|
// run the test
|
|
|
|
ok := caddytest.CompareAdapt(t, caddyfile, "caddyfile", json)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("failed to adapt %s", filename)
|
2020-05-11 17:50:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|