Skip to content

Commit a3879cd

Browse files
committed
add RequireOutputJQ - WIP, just prints patches for now
1 parent 3b30d61 commit a3879cd

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/spf13/cobra v1.8.1 // Apache 2.0
2626
github.com/spf13/pflag v1.0.5 // BSD-3-Clause
2727
github.com/stretchr/testify v1.10.0 // MIT
28+
github.com/wI2L/jsondiff v0.6.1 // MIT
2829
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
2930
golang.org/x/mod v0.22.0
3031
golang.org/x/oauth2 v0.24.0
@@ -58,6 +59,10 @@ require (
5859
github.com/mattn/go-colorable v0.1.13 // indirect
5960
github.com/pmezard/go-difflib v1.0.0 // indirect
6061
github.com/stretchr/objx v0.5.2 // indirect
62+
github.com/tidwall/gjson v1.18.0 // indirect
63+
github.com/tidwall/match v1.1.1 // indirect
64+
github.com/tidwall/pretty v1.2.1 // indirect
65+
github.com/tidwall/sjson v1.2.5 // indirect
6166
github.com/zclconf/go-cty v1.15.0 // indirect
6267
go.opencensus.io v0.24.0 // indirect
6368
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect

go.sum

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/bundle/init_default_python_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,5 @@ func testDefaultPython(t *testing.T, pythonVersion string) {
110110
testcli.RequireSuccessfulRun(t, ctx, "bundle", "destroy", "--auto-approve")
111111
})
112112

113-
testcli.RequireOutput(t, ctx, []string{"bundle", "summary", "--output", "json"}, "testdata/default_python/bundle_summary.txt")
113+
testcli.RequireOutputJQ(t, ctx, []string{"bundle", "summary", "--output", "json"}, "testdata/default_python/bundle_summary.txt", []string{"/hello"})
114114
}

internal/testcli/golden.go

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/elliotchance/orderedmap/v3"
1818
"github.com/stretchr/testify/assert"
1919
"github.com/stretchr/testify/require"
20+
"github.com/wI2L/jsondiff"
2021
)
2122

2223
func ReadFile(t testutil.TestingT, ctx context.Context, filename string) string {
@@ -34,28 +35,62 @@ func WriteFile(t testutil.TestingT, ctx context.Context, filename, data string)
3435
require.NoError(t, err)
3536
}
3637

37-
func RequireOutput(t testutil.TestingT, ctx context.Context, args []string, expectedFilename string) {
38-
_, filename, _, _ := runtime.Caller(1)
39-
dir := filepath.Dir(filename)
40-
expected := ReadFile(t, ctx, filepath.Join(dir, expectedFilename))
41-
38+
func captureOutput(t testutil.TestingT, ctx context.Context, args []string) string {
4239
t.Logf("run args: [%s]", strings.Join(args, ", "))
4340
r := NewRunner(t, ctx, args...)
4441
stdout, stderr, err := r.Run()
4542
require.NoError(t, err)
4643
out := stderr.String() + stdout.String()
44+
return ReplaceOutput(t, ctx, out)
45+
}
4746

48-
out = ReplaceOutput(t, ctx, out)
47+
func assertEqualTexts(t testutil.TestingT, filename1, filename2, expected, out string) {
48+
if len(out) < 1000 && len(expected) < 1000 {
49+
// This shows full strings + diff which could be useful when debugging newlines
50+
assert.Equal(t, expected, out)
51+
} else {
52+
// only show diff for large texts
53+
diff := testutil.Diff(filename1, filename2, expected, out)
54+
t.Errorf("Diff:\n" + diff)
55+
}
56+
}
57+
58+
func RequireOutput(t testutil.TestingT, ctx context.Context, args []string, expectedFilename string) {
59+
_, filename, _, _ := runtime.Caller(1)
60+
dir := filepath.Dir(filename)
61+
expectedPath := filepath.Join(dir, expectedFilename)
62+
expected := ReadFile(t, ctx, expectedPath)
63+
64+
out := captureOutput(t, ctx, args)
4965

5066
if out != expected {
51-
if len(out) < 1000 && len(expected) < 1000 {
52-
// This shows full strings + diff which could be useful when debugging newlines
53-
assert.Equal(t, expected, out)
54-
} else {
55-
// only show diff for large texts
67+
actual := fmt.Sprintf("Output from %v", args)
68+
assertEqualTexts(t, expectedFilename, actual, expected, out)
69+
70+
if os.Getenv("TESTS_OUTPUT") == "OVERWRITE" {
71+
WriteFile(t, ctx, expectedPath, out)
72+
}
73+
}
74+
}
75+
76+
func RequireOutputJQ(t testutil.TestingT, ctx context.Context, args []string, expectedFilename string, ignorePaths []string) {
77+
_, filename, _, _ := runtime.Caller(1)
78+
dir := filepath.Dir(filename)
79+
expectedPath := filepath.Join(dir, expectedFilename)
80+
expected := ReadFile(t, ctx, expectedPath)
81+
82+
out := captureOutput(t, ctx, args)
83+
84+
if out != expected {
85+
patch, err := jsondiff.CompareJSON([]byte(expected), []byte(out))
86+
if err != nil {
87+
t.Logf("CompareJSON error for %s vs %s: %s (fallback to textual comparison)", args, expectedFilename, err)
5688
actual := fmt.Sprintf("Output from %v", args)
57-
diff := testutil.Diff(expectedFilename, actual, expected, out)
58-
t.Errorf("Diff:\n" + diff)
89+
assertEqualTexts(t, expectedFilename, actual, expected, out)
90+
} else {
91+
for _, op := range patch {
92+
t.Errorf("PATCH %s %s: %s", args, expectedFilename, op)
93+
}
5994
}
6095

6196
if os.Getenv("TESTS_OUTPUT") == "OVERWRITE" {

0 commit comments

Comments
 (0)