caddy-autoreload/autoreload/caddyfile_test.go
2025-12-23 12:15:28 +11:00

59 lines
1.4 KiB
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright 2025 Suyono
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package autoreload
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"testing"
caddycmd "github.com/caddyserver/caddy/v2/cmd"
_ "github.com/caddyserver/caddy/v2/modules/standard"
)
func prettyJSON(t *testing.T, b []byte) string {
t.Helper()
buf := new(bytes.Buffer)
if err := json.Indent(buf, b, "", " "); err != nil {
return string(b)
}
return buf.String()
}
func TestParseModule(t *testing.T) {
cfgdir, ok := os.LookupEnv("CADDY_CONFIG_DIR")
if !ok {
t.Fatal("CADDY_CONFIG_DIR env variable is not set")
}
var (
b []byte
str string
err error
)
if b, str, err = caddycmd.LoadConfig(filepath.Join(cfgdir, "Caddyfile"), "caddyfile"); err != nil {
t.Fatal("failed to load config:", err)
}
t.Log("loaded config file:", str)
t.Log("json data:", prettyJSON(t, b))
}