Use native configuration validation and diagnostics
This commit is contained in:
@@ -129,7 +129,6 @@ func TestOperationOutcomePreservesCausesAndOperationalPrecedence(t *testing.T) {
|
||||
{catalogInput, 2},
|
||||
{errors.Join(catalogInput, operational), 1},
|
||||
{errors.Join(catalogInput, context.Canceled), 1},
|
||||
{&config.SessionIdentityError{Source: "session.yml", Field: "session_id", Expected: "one", Actual: "two"}, 2},
|
||||
{fmt.Errorf("request: %w", errors.Join(input, invalid)), 2},
|
||||
{errors.Join(input, operational), 1},
|
||||
{fmt.Errorf("request: %w", errors.Join(invalid, context.Canceled)), 1},
|
||||
@@ -175,3 +174,62 @@ func TestNativeTreeRejectsRemovedRoutesAndMissingOperandsBeforeAcquisition(t *te
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDomainCommandsUseNativeConfigurationDiagnostics(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
invalid := filepath.Join(root, "invalid.yml")
|
||||
if err := os.WriteFile(invalid, []byte("unknown_field: true\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
path, code string
|
||||
status int
|
||||
}{
|
||||
{invalid, "decode.unknown_field", 2},
|
||||
{filepath.Join(root, "missing.yml"), "source.missing", 1},
|
||||
} {
|
||||
for _, command := range [][]string{{"config", "show"}, {"artifacts", "list"}} {
|
||||
var out, diag bytes.Buffer
|
||||
args := append(append([]string{}, command...), "--config", tc.path, "--quiet")
|
||||
result := Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diag})
|
||||
var load *configkit.LoadError
|
||||
if result.Status != tc.status || !errors.As(result.Cause, &load) || out.Len() != 0 {
|
||||
t.Fatalf("%v: %+v", args, result)
|
||||
}
|
||||
if !strings.Contains(diag.String(), tc.code) || !strings.Contains(diag.String(), tc.path) || strings.Contains(diag.String(), "strict decode failed or validation failed") {
|
||||
t.Fatalf("%v: %s", args, diag.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeDiagnosticsRetainWarningsAndJoinedOperationalCause(t *testing.T) {
|
||||
_, loadErr := configkit.Load(configkit.NoSource(), configkit.Options[struct{}]{Definition: configkit.Definition[struct{}]{Validate: func(_ *struct{}, v *configkit.Validation) {
|
||||
v.Warning(configkit.Keys(), "review warning")
|
||||
v.Error(configkit.Keys(), "review invalid input")
|
||||
}}})
|
||||
if loadErr == nil {
|
||||
t.Fatal("expected validation failure")
|
||||
}
|
||||
cleanup := errors.New("resource release failed")
|
||||
for _, verbose := range []bool{false, true} {
|
||||
application, err := commandkit.New(commandkit.Identity{Name: "test"}, commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "fail", Run: func(inv commandkit.Invocation, _ struct{}) commandkit.Outcome {
|
||||
return reportOperationOutcome(inv, errors.Join(loadErr, cleanup))
|
||||
}}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
args := []string{"fail"}
|
||||
if verbose {
|
||||
args = append(args, "--verbose")
|
||||
}
|
||||
var diag bytes.Buffer
|
||||
outcome := application.Execute(context.Background(), args, commandkit.Streams{Out: io.Discard, Err: &diag})
|
||||
if outcome.Status != 1 || !errors.Is(outcome.Cause, cleanup) || !errors.Is(outcome.Cause, loadErr) {
|
||||
t.Fatalf("%+v", outcome)
|
||||
}
|
||||
if !strings.Contains(diag.String(), "review invalid input") || !strings.Contains(diag.String(), cleanup.Error()) || strings.Contains(diag.String(), "review warning") != verbose {
|
||||
t.Fatalf("verbose=%t: %s", verbose, diag.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user