120 lines
5.4 KiB
Go
120 lines
5.4 KiB
Go
package ckconfig
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"go.maximumdirect.net/commandkit"
|
|
"go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
func TestNativeConfigurationInputOutcomes(t *testing.T) {
|
|
_, loadErr := configkit.Load[map[string]any](configkit.Bytes("malformed-input", []byte("broken: [")))
|
|
var native *configkit.LoadError
|
|
if !errors.As(loadErr, &native) || len(native.Diagnostics()) == 0 {
|
|
t.Fatalf("fixture: %v", loadErr)
|
|
}
|
|
wrapped := fmt.Errorf("application loader: %w", loadErr)
|
|
app := newConfigTestApp(t, AppCommand(func(context.Context, Selection) (*configkit.Result[map[string]any], error) { return nil, wrapped }))
|
|
cases := []struct {
|
|
name string
|
|
args []string
|
|
report string
|
|
}{
|
|
{"show", []string{"config", "show"}, ""},
|
|
{"sources", []string{"config", "sources", "--format=json"}, `"sources"`},
|
|
{"validation", []string{"config", "validate", "--details", "--format=json"}, `"code": "yaml.invalid"`},
|
|
{"filtered validation", []string{"config", "validate", "--details", "--code=not-selected", "--format=json"}, `"state": "filtered"`},
|
|
{"discovery", []string{"config", "explain", "--list"}, ""},
|
|
{"diff", []string{"config", "diff"}, ""},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var out, diag bytes.Buffer
|
|
result := app.Execute(context.Background(), tc.args, commandkit.Streams{Out: &out, Err: &diag})
|
|
var got *configkit.LoadError
|
|
if result.Kind != commandkit.OutcomeUsage || result.Status != 2 || !errors.Is(result.Cause, loadErr) || !errors.As(result.Cause, &got) || got != native {
|
|
t.Fatalf("outcome: %#v", result)
|
|
}
|
|
if !strings.Contains(diag.String(), native.Diagnostics()[0].Message) || !strings.Contains(diag.String(), "malformed-input") {
|
|
t.Fatalf("diagnostics: %q", diag.String())
|
|
}
|
|
if tc.report != "" && !strings.Contains(out.String(), tc.report) {
|
|
t.Fatalf("report: %q", out.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNativeConfigurationComparisonFailureSides(t *testing.T) {
|
|
_, loadErr := configkit.Load[map[string]any](configkit.Bytes("bad-side", []byte("broken: [")))
|
|
for _, bad := range []string{"left", "right"} {
|
|
t.Run(bad, func(t *testing.T) {
|
|
app := newConfigTestApp(t, AppCommand(func(_ context.Context, selection Selection) (*configkit.Result[map[string]any], error) {
|
|
if selection.Config.Value == bad {
|
|
return nil, fmt.Errorf("wrapped: %w", loadErr)
|
|
}
|
|
return configkit.Load[map[string]any](configkit.Bytes("good-side", []byte("ok: true")))
|
|
}))
|
|
var diag bytes.Buffer
|
|
result := app.Execute(context.Background(), []string{"config", "diff", "--config=left", "--against-config=right"}, commandkit.Streams{Out: io.Discard, Err: &diag})
|
|
if result.Kind != commandkit.OutcomeUsage || result.Status != 2 || !errors.Is(result.Cause, loadErr) || !strings.Contains(diag.String(), "bad-side") || !strings.Contains(diag.String(), bad) {
|
|
t.Fatalf("outcome=%#v diagnostics=%q", result, diag.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConfigurationFailureCancellationAndDelivery(t *testing.T) {
|
|
_, loadErr := configkit.Load[map[string]any](configkit.Bytes("bad-input", []byte("broken: [")))
|
|
for _, args := range [][]string{{"config", "show"}, {"config", "sources"}, {"config", "validate"}, {"config", "explain", "--list"}, {"config", "diff"}} {
|
|
t.Run(strings.Join(args, "_"), func(t *testing.T) {
|
|
calls := 0
|
|
app := newConfigTestApp(t, AppCommand(func(context.Context, Selection) (*configkit.Result[map[string]any], error) {
|
|
calls++
|
|
return nil, loadErr
|
|
}))
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
var out bytes.Buffer
|
|
result := app.Execute(ctx, args, commandkit.Streams{Out: &out, Err: io.Discard})
|
|
if result.Status != 1 || !errors.Is(result.Cause, context.Canceled) || calls != 0 || out.Len() != 0 {
|
|
t.Fatalf("outcome=%#v calls=%d output=%q", result, calls, out.String())
|
|
}
|
|
ctx, cancel = context.WithCancel(context.Background())
|
|
defer cancel()
|
|
app = newConfigTestApp(t, AppCommand(func(context.Context, Selection) (*configkit.Result[map[string]any], error) {
|
|
cancel()
|
|
return nil, loadErr
|
|
}))
|
|
result = app.Execute(ctx, args, commandkit.Streams{Out: io.Discard, Err: io.Discard})
|
|
if result.Status != 1 || !errors.Is(result.Cause, context.Canceled) || !errors.Is(result.Cause, loadErr) {
|
|
t.Fatalf("joined outcome=%#v", result)
|
|
}
|
|
})
|
|
}
|
|
app := newConfigTestApp(t, AppCommand(func(context.Context, Selection) (*configkit.Result[map[string]any], error) { return nil, loadErr }))
|
|
for _, args := range [][]string{{"config", "sources"}, {"config", "validate"}} {
|
|
result := app.Execute(context.Background(), args, commandkit.Streams{Out: shortWriter{}, Err: io.Discard})
|
|
if result.Kind != commandkit.OutcomeOperational || result.Status != 1 || !errors.Is(result.Cause, loadErr) || result.Presentation.Output.State != commandkit.DeliveryIncomplete {
|
|
t.Fatalf("delivery %v: %#v", args, result)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestConfigurationOperationalFailures(t *testing.T) {
|
|
_, loadErr := configkit.Load[map[string]any](configkit.File("/nonexistent-commandkit-outcome-test"))
|
|
for _, failure := range []error{errors.New("unavailable"), loadErr} {
|
|
app := newConfigTestApp(t, AppCommand(func(context.Context, Selection) (*configkit.Result[map[string]any], error) { return nil, failure }))
|
|
result := app.Execute(context.Background(), []string{"config", "show"}, commandkit.Streams{Out: io.Discard, Err: io.Discard})
|
|
if result.Status != 1 || !errors.Is(result.Cause, failure) {
|
|
t.Fatalf("outcome=%#v", result)
|
|
}
|
|
}
|
|
}
|