2020-04-03 22:02:46 -05:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2021-03-29 19:47:19 -05:00
|
|
|
jsonMod "encoding/json"
|
|
|
|
"fmt"
|
2021-09-29 12:17:48 -05:00
|
|
|
"os"
|
2021-03-29 19:47:19 -05:00
|
|
|
"path/filepath"
|
2020-05-14 16:53:28 -05:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-04-03 22:02:46 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2/caddytest"
|
2024-02-25 16:13:48 -05:00
|
|
|
|
|
|
|
_ "github.com/caddyserver/caddy/v2/internal/testmocks"
|
2020-04-03 22:02:46 -05:00
|
|
|
)
|
|
|
|
|
2020-05-14 16:53:28 -05:00
|
|
|
func TestCaddyfileAdaptToJSON(t *testing.T) {
|
|
|
|
// load the list of test files from the dir
|
2021-09-29 12:17:48 -05:00
|
|
|
files, err := os.ReadDir("./caddyfile_adapt")
|
2020-05-14 16:53:28 -05:00
|
|
|
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()
|
2021-09-29 12:17:48 -05:00
|
|
|
data, err := os.ReadFile("./caddyfile_adapt/" + filename)
|
2020-05-14 16:53:28 -05:00
|
|
|
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
|
2021-01-19 16:21:11 -05:00
|
|
|
// (append newline to Caddyfile to match formatter expectations)
|
2020-05-14 16:53:28 -05:00
|
|
|
parts := strings.Split(string(data), "----------")
|
2021-01-19 16:21:11 -05:00
|
|
|
caddyfile, json := strings.TrimSpace(parts[0])+"\n", 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
|
|
|
|
2021-03-29 19:47:19 -05:00
|
|
|
// replace os-specific default path for file_server's hide field
|
|
|
|
replacePath, _ := jsonMod.Marshal(fmt.Sprint(".", string(filepath.Separator), "Caddyfile"))
|
|
|
|
json = strings.ReplaceAll(json, `"./Caddyfile"`, string(replacePath))
|
|
|
|
|
2020-05-14 16:53:28 -05:00
|
|
|
// run the test
|
2021-01-19 16:21:11 -05:00
|
|
|
ok := caddytest.CompareAdapt(t, filename, caddyfile, "caddyfile", json)
|
2020-05-14 16:53:28 -05:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("failed to adapt %s", filename)
|
2020-05-11 17:50:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|