53 lines
1.7 KiB
Go
53 lines
1.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
|
)
|
|
|
|
func TestLoadConfigKitUsesDiscoveryProfileAndOverrides(t *testing.T) {
|
|
t.Parallel()
|
|
root := t.TempDir()
|
|
path := filepath.Join(root, "notarius.yml")
|
|
writeConfigKitFile(t, path, "version: 5\npipelines:\n base:\n input: {module: input}\n")
|
|
profiles := filepath.Join(root, "profiles")
|
|
writeConfigKitFile(t, filepath.Join(profiles, "development.yml"), "pipelines:\n profiled:\n input: {module: input}\n")
|
|
loaded, gotPath, err := loadConfigKit(path, "development", []configkit.Override{{Path: configkit.Keys("output", "directory"), Value: "generated", Flag: "--output"}}, commandContractOptions(t))
|
|
if err != nil {
|
|
t.Fatalf("loadConfigKit() error = %v", err)
|
|
}
|
|
if gotPath != path || loaded.Config.Pipelines["profiled"].Input.Module != "input" || loaded.Config.Output.Directory == "generated" {
|
|
t.Fatalf("loaded = %#v, path=%q", loaded.Config, gotPath)
|
|
}
|
|
}
|
|
|
|
func TestWriteConfigLoadErrorUsesSafeDiagnostics(t *testing.T) {
|
|
t.Parallel()
|
|
path := filepath.Join(t.TempDir(), "invalid.yml")
|
|
writeConfigKitFile(t, path, "version: 4\n")
|
|
_, _, err := loadConfigKit(path, "", nil, commandContractOptions(t))
|
|
if err == nil {
|
|
t.Fatal("loadConfigKit() error = nil")
|
|
}
|
|
var output bytes.Buffer
|
|
writeConfigLoadError(&output, err)
|
|
if !strings.Contains(output.String(), "version") || strings.Contains(output.String(), "notarius: configkit") {
|
|
t.Fatalf("diagnostics = %q", output.String())
|
|
}
|
|
}
|
|
|
|
func writeConfigKitFile(t *testing.T, path, contents string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|