2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2020-12-21 17:36:38 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// update allows golden files to be updated based on the current output.
|
|
|
|
var update = flag.Bool("update", false, "update golden files")
|
|
|
|
|
|
|
|
// golden reads the expected value from the file at filename and returns the value.
|
|
|
|
// filename is relative to the ./testdata directory.
|
|
|
|
//
|
|
|
|
// If the `-update` flag is used with `go test`, the golden file will be updated
|
|
|
|
// to the value of actual.
|
|
|
|
func golden(t *testing.T, actual, filename string) string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
path := filepath.Join("testdata", filename)
|
|
|
|
if *update && actual != "" {
|
|
|
|
if dir := filepath.Dir(path); dir != "." {
|
|
|
|
require.NoError(t, os.MkdirAll(dir, 0755))
|
|
|
|
}
|
2022-11-10 16:26:01 +00:00
|
|
|
err := os.WriteFile(path, []byte(actual), 0644)
|
2020-12-21 17:36:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
expected, err := os.ReadFile(path)
|
2020-12-21 17:36:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return string(expected)
|
|
|
|
}
|