Use native configuration diff and source evidence
This commit is contained in:
@@ -272,7 +272,7 @@ func TestAssembledSplitBundleCommandsUseOneEffectiveFamilyConfiguration(t *testi
|
||||
}, &diff); err != nil {
|
||||
t.Fatalf("compare profiles: %v", err)
|
||||
}
|
||||
if !strings.Contains(diff.String(), "audita.proposal_profile") || !strings.Contains(diff.String(), "character_items_arannis.profile_id") {
|
||||
if !strings.Contains(diff.String(), "/audita/proposal_profile") || !strings.Contains(diff.String(), "/character_items_arannis/profile_id") {
|
||||
t.Fatalf("profile diff = %q, want proposal profile and expanded family changes", diff.String())
|
||||
}
|
||||
|
||||
|
||||
@@ -140,6 +140,11 @@ func configSourcesWithDependencies(_ context.Context, args []string, out io.Writ
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, selection := range config.EffectiveEmptyImports(resolved.Pipeline, resolved.Campaign) {
|
||||
if _, err := fmt.Fprintf(out, "empty_import\t%s\t%s\t%s:%d:%d\n", selection.Path, selection.Selector, selection.Source, selection.Line, selection.Column); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -186,24 +186,24 @@ scriptorium:
|
||||
}
|
||||
output := first.String()
|
||||
for _, want := range []string{
|
||||
"changed\taudita.modules\t[\"glossary\",\"grammar\"]\t[]\n",
|
||||
"changed\tworkspace.cleanup_after_publish\ttrue\tfalse\n",
|
||||
"changed\twhisperx.language\t\"en\"\t\"fr\"\n",
|
||||
"removed\tscriptorium.artifacts.production_only.enabled\tfalse\n",
|
||||
"added\tscriptorium.artifacts.testing_only.enabled\tfalse\n",
|
||||
"changed\t/audita/modules\t[\"glossary\",\"grammar\"]\t[]\n",
|
||||
"changed\t/workspace/cleanup_after_publish\ttrue\tfalse\n",
|
||||
"changed\t/whisperx/language\t\"en\"\t\"fr\"\n",
|
||||
"removed\t/scriptorium/artifacts/production_only\t{",
|
||||
"added\t/scriptorium/artifacts/testing_only\t{",
|
||||
} {
|
||||
if !strings.Contains(output, want) {
|
||||
t.Fatalf("diff output missing %q:\n%s", want, output)
|
||||
}
|
||||
}
|
||||
if strings.Index(output, "audita.modules") > strings.Index(output, "workspace.cleanup_after_publish") {
|
||||
if strings.Index(output, "/audita/modules") > strings.Index(output, "/workspace/cleanup_after_publish") {
|
||||
t.Fatalf("diff records are not sorted by path:\n%s", output)
|
||||
}
|
||||
var reversed bytes.Buffer
|
||||
if err := ConfigDiff(context.Background(), []string{"testing", "production", "--config", pipelinePath}, &reversed); err != nil {
|
||||
t.Fatalf("reversed ConfigDiff() error = %v", err)
|
||||
}
|
||||
if !strings.Contains(reversed.String(), "changed\twhisperx.language\t\"fr\"\t\"en\"\n") {
|
||||
if !strings.Contains(reversed.String(), "changed\t/whisperx/language\t\"fr\"\t\"en\"\n") {
|
||||
t.Fatalf("reversed diff did not independently resolve overlays:\n%s", reversed.String())
|
||||
}
|
||||
}
|
||||
@@ -249,8 +249,8 @@ scriptorium:
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{
|
||||
"changed\taudita.modules\tnull\t[]\n",
|
||||
"changed\tscriptorium.artifacts\tnull\t{}\n",
|
||||
"changed\t/audita/modules\tnull\t[]\n",
|
||||
"changed\t/scriptorium/artifacts\tnull\t{}\n",
|
||||
} {
|
||||
if !strings.Contains(diff.String(), want) {
|
||||
t.Fatalf("diff output missing %q:\n%s", want, diff.String())
|
||||
@@ -289,9 +289,9 @@ func TestConfigDiffComparesExpandedFamiliesAndPublishRules(t *testing.T) {
|
||||
}
|
||||
output := out.String()
|
||||
for _, want := range []string{
|
||||
"changed\tscriptorium.artifacts.character_note_arannis.enabled\tfalse\ttrue\n",
|
||||
"changed\tscriptorium.artifacts.character_note_arannis.vars.character_name\t\"Arannis\"\t\"wizard\"\n",
|
||||
"changed\tpublish.outputs\t",
|
||||
"changed\t/scriptorium/artifacts/character_note_arannis/enabled\tfalse\ttrue\n",
|
||||
"changed\t/scriptorium/artifacts/character_note_arannis/vars/character_name\t\"Arannis\"\t\"wizard\"\n",
|
||||
"changed\t/publish/outputs\t",
|
||||
} {
|
||||
if !strings.Contains(output, want) {
|
||||
t.Fatalf("family diff output missing %q:\n%s", want, output)
|
||||
@@ -486,7 +486,7 @@ func TestConfigCommandsInspectMaintainedSplitBundleWithoutRuntimeState(t *testin
|
||||
if err := ConfigDiff(context.Background(), []string{"default", "testing", "--config", pipelinePath, "--campaign-file", campaignPath}, &diff); err != nil {
|
||||
t.Fatalf("ConfigDiff() error = %v", err)
|
||||
}
|
||||
if !strings.Contains(diff.String(), "audita.proposal_profile") {
|
||||
if !strings.Contains(diff.String(), "/audita/proposal_profile") {
|
||||
t.Fatalf("split bundle overlay diff = %q, want proposal profile change", diff.String())
|
||||
}
|
||||
if _, err := os.Stat(workspacePath); !os.IsNotExist(err) {
|
||||
|
||||
@@ -2,14 +2,16 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type configDiffFlags struct {
|
||||
@@ -30,13 +32,6 @@ type configDiffResolved struct {
|
||||
right *inspectionConfig
|
||||
}
|
||||
|
||||
type configDiffRecord struct {
|
||||
Kind string
|
||||
Path string
|
||||
Left string
|
||||
Right string
|
||||
}
|
||||
|
||||
// ConfigDiff compares two explicitly selected overlays through the same
|
||||
// read-only configuration resolution boundary used by config validate, show,
|
||||
// and sources. Differences describe normalized effective values rather than
|
||||
@@ -57,43 +52,42 @@ func configDiffWithDependencies(_ context.Context, args []string, out io.Writer,
|
||||
if err != nil {
|
||||
return fmt.Errorf("config diff: %w", err)
|
||||
}
|
||||
leftValues, err := config.EffectivePipelineValues(resolved.left.Pipeline)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config diff: project left effective configuration: %w", err)
|
||||
}
|
||||
rightValues, err := config.EffectivePipelineValues(resolved.right.Pipeline)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config diff: project right effective configuration: %w", err)
|
||||
}
|
||||
records, err := diffEffectivePipelineValues(leftValues, rightValues)
|
||||
differences, err := config.DiffEffectivePipelines(resolved.left.Pipeline, resolved.right.Pipeline)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config diff: %w", err)
|
||||
}
|
||||
leftDigest := config.EffectivePipelineDigest(resolved.left.Pipeline)
|
||||
rightDigest := config.EffectivePipelineDigest(resolved.right.Pipeline)
|
||||
if len(records) == 0 {
|
||||
if leftDigest != rightDigest {
|
||||
return fmt.Errorf("internal error: effective pipeline digests differ without a semantic difference")
|
||||
}
|
||||
if len(differences) == 0 {
|
||||
_, err := fmt.Fprintln(out, "no differences")
|
||||
return err
|
||||
}
|
||||
for _, record := range records {
|
||||
switch record.Kind {
|
||||
case "added":
|
||||
if _, err := fmt.Fprintf(out, "added\t%s\t%s\n", record.Path, record.Right); err != nil {
|
||||
for _, difference := range differences {
|
||||
kind := "changed"
|
||||
if !difference.Left.Present {
|
||||
kind = "added"
|
||||
}
|
||||
if !difference.Right.Present {
|
||||
kind = "removed"
|
||||
}
|
||||
fields := []string{kind, difference.Path.String()}
|
||||
if difference.Left.Present {
|
||||
value, err := compactSnapshot(difference.Left)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case "removed":
|
||||
if _, err := fmt.Fprintf(out, "removed\t%s\t%s\n", record.Path, record.Left); err != nil {
|
||||
fields = append(fields, value)
|
||||
}
|
||||
if difference.Right.Present {
|
||||
value, err := compactSnapshot(difference.Right)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case "changed":
|
||||
if _, err := fmt.Fprintf(out, "changed\t%s\t%s\t%s\n", record.Path, record.Left, record.Right); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("internal error: unsupported difference kind %q", record.Kind)
|
||||
fields = append(fields, value)
|
||||
}
|
||||
if difference.HiddenChanged {
|
||||
fields = append(fields, "hidden_changed=true")
|
||||
}
|
||||
if _, err := fmt.Fprintln(out, strings.Join(fields, "\t")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -264,50 +258,14 @@ func resolveSharedDiffCampaignPath(left, right *config.PipelineConfig, flags con
|
||||
return leftPath, nil
|
||||
}
|
||||
|
||||
func diffEffectivePipelineValues(left, right []config.EffectivePipelineValueRecord) ([]configDiffRecord, error) {
|
||||
leftByPath, err := effectivePipelineValueMap("left", left)
|
||||
func compactSnapshot(snapshot configkit.ValueSnapshot) (string, error) {
|
||||
var value any
|
||||
if err := yaml.Unmarshal([]byte(snapshot.YAML), &value); err != nil {
|
||||
return "", fmt.Errorf("decode safe diff value: %w", err)
|
||||
}
|
||||
encoded, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", fmt.Errorf("encode safe diff value: %w", err)
|
||||
}
|
||||
rightByPath, err := effectivePipelineValueMap("right", right)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paths := make([]string, 0, len(leftByPath)+len(rightByPath))
|
||||
seen := make(map[string]struct{}, len(leftByPath)+len(rightByPath))
|
||||
for path := range leftByPath {
|
||||
seen[path] = struct{}{}
|
||||
paths = append(paths, path)
|
||||
}
|
||||
for path := range rightByPath {
|
||||
if _, exists := seen[path]; !exists {
|
||||
paths = append(paths, path)
|
||||
}
|
||||
}
|
||||
sort.Strings(paths)
|
||||
records := make([]configDiffRecord, 0)
|
||||
for _, path := range paths {
|
||||
leftValue, leftExists := leftByPath[path]
|
||||
rightValue, rightExists := rightByPath[path]
|
||||
switch {
|
||||
case !leftExists:
|
||||
records = append(records, configDiffRecord{Kind: "added", Path: path, Right: rightValue})
|
||||
case !rightExists:
|
||||
records = append(records, configDiffRecord{Kind: "removed", Path: path, Left: leftValue})
|
||||
case leftValue != rightValue:
|
||||
records = append(records, configDiffRecord{Kind: "changed", Path: path, Left: leftValue, Right: rightValue})
|
||||
}
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func effectivePipelineValueMap(side string, records []config.EffectivePipelineValueRecord) (map[string]string, error) {
|
||||
values := make(map[string]string, len(records))
|
||||
for _, record := range records {
|
||||
if _, exists := values[record.Path]; exists {
|
||||
return nil, fmt.Errorf("internal error: %s effective configuration has duplicate path %q", side, record.Path)
|
||||
}
|
||||
values[record.Path] = record.Value
|
||||
}
|
||||
return values, nil
|
||||
return string(encoded), nil
|
||||
}
|
||||
|
||||
40
internal/app/config_native_inspection_test.go
Normal file
40
internal/app/config_native_inspection_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfigDiffUsesEscapedPointersAndAtomicLists(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
root := filepath.Join(dir, "pipeline.yml")
|
||||
mustWriteTestFile(t, root, "whisperx: {transcribe_url: 'https://example.test/transcribe'}\nscriptorium:\n artifacts:\n recap:\n enabled: false\n output_path: artifacts/recap.md\n vars: {'a/b~c': before}\naudita: {modules: [glossary, grammar]}\n")
|
||||
mustWriteTestFile(t, filepath.Join(dir, "overlays", "changed.yml"), "scriptorium:\n artifacts:\n recap:\n vars: {'a/b~c': after}\naudita: {modules: [glossary, homophones]}\n")
|
||||
var output bytes.Buffer
|
||||
if err := ConfigDiff(context.Background(), []string{"default", "changed", "--config", root}, &output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{"changed\t/audita/modules\t[\"glossary\",\"grammar\"]\t[\"glossary\",\"homophones\"]", "changed\t/scriptorium/artifacts/recap/vars/a~1b~0c\t\"before\"\t\"after\""} {
|
||||
if !strings.Contains(output.String(), want) {
|
||||
t.Fatalf("diff lacks %q: %s", want, output.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigSourcesReportsEmptyOptionalImportAndNormalization(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
root := filepath.Join(dir, "pipeline.yml")
|
||||
mustWriteTestFile(t, root, "config:\n imports:\n - path: absent.yml\nworkspace: {root: ./work}\nwhisperx: {transcribe_url: 'https://example.test/transcribe'}\n")
|
||||
var output bytes.Buffer
|
||||
if err := ConfigSources(context.Background(), []string{"--config", root}, &output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{"empty_import\t/config/imports/0\tpath\t" + root, "workspace.root\tresolution\tresolution", "workspace.root\troot\t" + root} {
|
||||
if !strings.Contains(output.String(), want) {
|
||||
t.Fatalf("sources lack %q: %s", want, output.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user