Skip to content

Commit 2d2f6e6

Browse files
committed
split code
1 parent e41e74f commit 2d2f6e6

File tree

6 files changed

+115
-86
lines changed

6 files changed

+115
-86
lines changed

integration/bundle/init_default_python_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bundle_test
33
import (
44
"encoding/json"
55
"os"
6+
"os/exec"
67
"path/filepath"
78
"testing"
89

@@ -60,11 +61,19 @@ func testDefaultPython(t *testing.T, pythonVersion string) {
6061
testcli.PrepareReplacementsUser(t, replacements, *user)
6162
}
6263

63-
tmpDir1, pythonExe := pythontest.RequirePythonVENV(t, ctx, pythonVersion, true)
64+
tmpDir := t.TempDir()
65+
66+
opts := pythontest.VenvOpts{
67+
PythonVersion: pythonVersion,
68+
Dir: tmpDir,
69+
}
70+
71+
pythontest.RequireActivatedPythonEnv(t, ctx, &opts)
6472
extras, ok := extraInstalls[pythonVersion]
6573
if ok {
66-
args := append([]string{"pip", "install", "--python", pythonExe}, extras...)
67-
testutil.RunCommand(t, "uv", args...)
74+
args := append([]string{"pip", "install", "--python", opts.PythonExe}, extras...)
75+
cmd := exec.Command("uv", args...)
76+
require.NoError(t, cmd.Run())
6877
}
6978

7079
projectName := "project_name_" + uniqueProjectId
@@ -77,7 +86,7 @@ func testDefaultPython(t *testing.T, pythonVersion string) {
7786
}
7887
b, err := json.Marshal(initConfig)
7988
require.NoError(t, err)
80-
err = os.WriteFile(filepath.Join(tmpDir1, "config.json"), b, 0o644)
89+
err = os.WriteFile(filepath.Join(tmpDir, "config.json"), b, 0o644)
8190
require.NoError(t, err)
8291

8392
testcli.RequireOutput(t, ctx, []string{"bundle", "init", "default-python", "--config-file", "config.json"}, "testdata/default_python/bundle_init.txt")

internal/testcli/golden.go

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ import (
99
"runtime"
1010
"slices"
1111
"strings"
12+
"testing"
1213

1314
"github.com/databricks/cli/internal/testutil"
15+
"github.com/databricks/cli/libs/golden"
1416
"github.com/databricks/cli/libs/iamutil"
1517
"github.com/databricks/databricks-sdk-go"
1618
"github.com/databricks/databricks-sdk-go/service/iam"
1719
"github.com/elliotchance/orderedmap/v3"
18-
"github.com/stretchr/testify/assert"
1920
"github.com/stretchr/testify/require"
20-
"github.com/wI2L/jsondiff"
2121
)
2222

23+
var OverwriteMode = os.Getenv("TESTS_OUTPUT") == "OVERWRITE"
24+
2325
func ReadFile(t testutil.TestingT, ctx context.Context, filename string) string {
2426
data, err := os.ReadFile(filename)
2527
if os.IsNotExist(err) {
@@ -30,12 +32,6 @@ func ReadFile(t testutil.TestingT, ctx context.Context, filename string) string
3032
return NormalizeNewlines(string(data))
3133
}
3234

33-
func WriteFile(t testutil.TestingT, ctx context.Context, filename, data string) {
34-
t.Logf("Overwriting %s", filename)
35-
err := os.WriteFile(filename, []byte(data), 0o644)
36-
require.NoError(t, err)
37-
}
38-
3935
func captureOutput(t testutil.TestingT, ctx context.Context, args []string) string {
4036
t.Logf("run args: [%s]", strings.Join(args, ", "))
4137
r := NewRunner(t, ctx, args...)
@@ -45,20 +41,10 @@ func captureOutput(t testutil.TestingT, ctx context.Context, args []string) stri
4541
return ReplaceOutput(t, ctx, out)
4642
}
4743

48-
func assertEqualTexts(t testutil.TestingT, filename1, filename2, expected, out string) {
49-
if len(out) < 1000 && len(expected) < 1000 {
50-
// This shows full strings + diff which could be useful when debugging newlines
51-
assert.Equal(t, expected, out)
52-
} else {
53-
// only show diff for large texts
54-
diff := testutil.Diff(filename1, filename2, expected, out)
55-
t.Errorf("Diff:\n" + diff)
56-
}
57-
}
58-
59-
func logDiff(t testutil.TestingT, filename1, filename2, expected, out string) {
60-
diff := testutil.Diff(filename1, filename2, expected, out)
61-
t.Logf("Diff:\n" + diff)
44+
func WriteFile(t testutil.TestingT, filename, data string) {
45+
t.Logf("Overwriting %s", filename)
46+
err := os.WriteFile(filename, []byte(data), 0o644)
47+
require.NoError(t, err)
6248
}
6349

6450
func RequireOutput(t testutil.TestingT, ctx context.Context, args []string, expectedFilename string) {
@@ -71,10 +57,10 @@ func RequireOutput(t testutil.TestingT, ctx context.Context, args []string, expe
7157

7258
if out != expected {
7359
actual := fmt.Sprintf("Output from %v", args)
74-
assertEqualTexts(t, expectedFilename, actual, expected, out)
60+
golden.AssertEqualTexts(t, expectedFilename, actual, expected, out)
7561

76-
if os.Getenv("TESTS_OUTPUT") == "OVERWRITE" {
77-
WriteFile(t, ctx, expectedPath, out)
62+
if OverwriteMode {
63+
WriteFile(t, expectedPath, out)
7864
}
7965
}
8066
}
@@ -88,46 +74,13 @@ func RequireOutputJQ(t testutil.TestingT, ctx context.Context, args []string, ex
8874
out := captureOutput(t, ctx, args)
8975

9076
if out != expected {
91-
patch, err := jsondiff.CompareJSON([]byte(expected), []byte(out))
9277
actual := fmt.Sprintf("Output from %v", args)
93-
if err != nil {
94-
t.Logf("CompareJSON error for %s vs %s: %s (fallback to textual comparison)", args, expectedFilename, err)
95-
assertEqualTexts(t, expectedFilename, actual, expected, out)
96-
} else {
97-
logDiff(t, expectedFilename, actual, expected, out)
98-
ignoredDiffs := []string{}
99-
erroredDiffs := []string{}
100-
for _, op := range patch {
101-
if matchesPrefixes(ignorePaths, op.Path) {
102-
ignoredDiffs = append(ignoredDiffs, fmt.Sprintf("%7s %s %v", op.Type, op.Path, op.Value))
103-
} else {
104-
erroredDiffs = append(erroredDiffs, fmt.Sprintf("%7s %s %v", op.Type, op.Path, op.Value))
105-
}
106-
}
107-
if len(ignoredDiffs) > 0 {
108-
t.Logf("Ignored differences between %s and %s:\n ==> %s", expectedFilename, args, strings.Join(ignoredDiffs, "\n ==> "))
109-
}
110-
if len(erroredDiffs) > 0 {
111-
t.Errorf("Unexpected differences between %s and %s:\n ==> %s", expectedFilename, args, strings.Join(erroredDiffs, "\n ==> "))
112-
}
113-
}
78+
golden.AssertEqualJSONs(t.(*testing.T), expectedFilename, actual, expected, out, ignorePaths)
11479

115-
if os.Getenv("TESTS_OUTPUT") == "OVERWRITE" {
116-
WriteFile(t, ctx, filepath.Join(dir, expectedFilename), out)
117-
}
118-
}
119-
}
120-
121-
func matchesPrefixes(prefixes []string, path string) bool {
122-
for _, p := range prefixes {
123-
if p == path {
124-
return true
125-
}
126-
if strings.HasPrefix(path, p+"/") {
127-
return true
80+
if OverwriteMode {
81+
WriteFile(t, expectedPath, out)
12882
}
12983
}
130-
return false
13184
}
13285

13386
var (

internal/testcli/golden_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,3 @@ func TestSort(t *testing.T) {
1111
stableSortReverseLength(input)
1212
assert.Equal(t, []string{"bc", "cd", "a"}, input)
1313
}
14-
15-
func TestMatchesPrefixes(t *testing.T) {
16-
assert.False(t, matchesPrefixes([]string{}, ""))
17-
assert.False(t, matchesPrefixes([]string{"/hello", "/hello/world"}, ""))
18-
assert.True(t, matchesPrefixes([]string{"/hello", "/a/b"}, "/hello"))
19-
assert.True(t, matchesPrefixes([]string{"/hello", "/a/b"}, "/a/b/c"))
20-
}

internal/testutil/diff.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

libs/golden/golden.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package golden
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/databricks/cli/internal/testutil"
9+
"github.com/hexops/gotextdiff"
10+
"github.com/hexops/gotextdiff/myers"
11+
"github.com/hexops/gotextdiff/span"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/wI2L/jsondiff"
14+
)
15+
16+
func UnifiedDiff(filename1, filename2, s1, s2 string) string {
17+
edits := myers.ComputeEdits(span.URIFromPath(filename1), s1, s2)
18+
return fmt.Sprint(gotextdiff.ToUnified(filename1, filename2, s1, edits))
19+
}
20+
21+
func AssertEqualTexts(t testutil.TestingT, filename1, filename2, expected, out string) {
22+
if len(out) < 1000 && len(expected) < 1000 {
23+
// This shows full strings + diff which could be useful when debugging newlines
24+
assert.Equal(t, expected, out)
25+
} else {
26+
// only show diff for large texts
27+
diff := UnifiedDiff(filename1, filename2, expected, out)
28+
t.Errorf("Diff:\n" + diff)
29+
}
30+
}
31+
32+
func AssertEqualJSONs(t *testing.T, expectedName, outName, expected, out string, ignorePaths []string) {
33+
patch, err := jsondiff.CompareJSON([]byte(expected), []byte(out))
34+
if err != nil {
35+
t.Logf("CompareJSON error for %s vs %s: %s (fallback to textual comparison)", outName, expectedName, err)
36+
AssertEqualTexts(t, expectedName, outName, expected, out)
37+
} else {
38+
diff := UnifiedDiff(expectedName, outName, expected, out)
39+
t.Logf("Diff:\n%s", diff)
40+
ignoredDiffs := []string{}
41+
erroredDiffs := []string{}
42+
for _, op := range patch {
43+
if matchesPrefixes(ignorePaths, op.Path) {
44+
ignoredDiffs = append(ignoredDiffs, fmt.Sprintf("%7s %s %v", op.Type, op.Path, op.Value))
45+
} else {
46+
erroredDiffs = append(erroredDiffs, fmt.Sprintf("%7s %s %v", op.Type, op.Path, op.Value))
47+
}
48+
}
49+
if len(ignoredDiffs) > 0 {
50+
t.Logf("Ignored differences between %s and %s:\n ==> %s", expectedName, outName, strings.Join(ignoredDiffs, "\n ==> "))
51+
}
52+
if len(erroredDiffs) > 0 {
53+
t.Errorf("Unexpected differences between %s and %s:\n ==> %s", expectedName, outName, strings.Join(erroredDiffs, "\n ==> "))
54+
}
55+
}
56+
}
57+
58+
func matchesPrefixes(prefixes []string, path string) bool {
59+
for _, p := range prefixes {
60+
if p == path {
61+
return true
62+
}
63+
if strings.HasPrefix(path, p+"/") {
64+
return true
65+
}
66+
}
67+
return false
68+
}

libs/golden/golden_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package golden
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestDiff(t *testing.T) {
10+
assert.Equal(t, "", UnifiedDiff("a", "b", "", ""))
11+
assert.Equal(t, "", UnifiedDiff("a", "b", "abc", "abc"))
12+
assert.Equal(t, "+123", UnifiedDiff("a", "b", "abc", "abc\123"))
13+
}
14+
15+
func TestMatchesPrefixes(t *testing.T) {
16+
assert.False(t, matchesPrefixes([]string{}, ""))
17+
assert.False(t, matchesPrefixes([]string{"/hello", "/hello/world"}, ""))
18+
assert.True(t, matchesPrefixes([]string{"/hello", "/a/b"}, "/hello"))
19+
assert.True(t, matchesPrefixes([]string{"/hello", "/a/b"}, "/a/b/c"))
20+
}

0 commit comments

Comments
 (0)