Skip to content

Commit

Permalink
get rid of "_, filename, _, _ := runtime.Caller(1)"
Browse files Browse the repository at this point in the history
Instead, store original WD on env var.
  • Loading branch information
denik committed Dec 20, 2024
1 parent 1e242cb commit 418564a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
48 changes: 34 additions & 14 deletions integration/bundle/init_default_python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,45 @@ func testDefaultPython(t *testing.T, pythonVersion string) {
err = os.WriteFile(filepath.Join(tmpDir, "config.json"), b, 0o644)
require.NoError(t, err)

testcli.AssertOutput(t, ctx, []string{"bundle", "init", "default-python", "--config-file", "config.json"}, "testdata/default_python/bundle_init.txt")
testcli.AssertOutput(
t,
ctx,
[]string{"bundle", "init", "default-python", "--config-file", "config.json"},
testutil.TestData("testdata/default_python/bundle_init.txt"),
)
testutil.Chdir(t, projectName)

t.Cleanup(func() {
// Delete the stack
testcli.RequireSuccessfulRun(t, ctx, "bundle", "destroy", "--auto-approve")
})

testcli.AssertOutput(t, ctx, []string{"bundle", "validate"}, "testdata/default_python/bundle_validate.txt")
testcli.AssertOutput(t, ctx, []string{"bundle", "deploy"}, "testdata/default_python/bundle_deploy.txt")

ignoredFields := []string{
"/bundle/terraform/exec_path",
"/resources/jobs/project_name_$UNIQUE_PRJ_job/email_notifications",
"/resources/jobs/project_name_$UNIQUE_PRJ_job/job_clusters/0/new_cluster/node_type_id",
"/resources/jobs/project_name_$UNIQUE_PRJ_job/url",
"/resources/pipelines/project_name_$UNIQUE_PRJ_pipeline/catalog",
"/resources/pipelines/project_name_$UNIQUE_PRJ_pipeline/url",
"/workspace/current_user",
}
testcli.AssertOutputJQ(t, ctx, []string{"bundle", "summary", "--output", "json"}, "testdata/default_python/bundle_summary.txt", ignoredFields)
testcli.AssertOutput(
t,
ctx,
[]string{"bundle", "validate"},
testutil.TestData("testdata/default_python/bundle_validate.txt"),
)
testcli.AssertOutput(
t,
ctx,
[]string{"bundle", "deploy"},
testutil.TestData("testdata/default_python/bundle_deploy.txt"),
)

testcli.AssertOutputJQ(
t,
ctx,
[]string{"bundle", "summary", "--output", "json"},
testutil.TestData("testdata/default_python/bundle_summary.txt"),
[]string{
"/bundle/terraform/exec_path",
"/resources/jobs/project_name_$UNIQUE_PRJ_job/email_notifications",
"/resources/jobs/project_name_$UNIQUE_PRJ_job/job_clusters/0/new_cluster/node_type_id",
"/resources/jobs/project_name_$UNIQUE_PRJ_job/url",
"/resources/pipelines/project_name_$UNIQUE_PRJ_pipeline/catalog",
"/resources/pipelines/project_name_$UNIQUE_PRJ_pipeline/url",
"/workspace/current_user",
},
)
}
16 changes: 4 additions & 12 deletions internal/testcli/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"
"testing"
Expand Down Expand Up @@ -46,35 +44,29 @@ func WriteFile(t testutil.TestingT, filename, data string) {
assert.NoError(t, err)
}

func AssertOutput(t testutil.TestingT, ctx context.Context, args []string, expectedFilename string) {
_, filename, _, _ := runtime.Caller(1)
dir := filepath.Dir(filename)
expectedPath := filepath.Join(dir, expectedFilename)
func AssertOutput(t testutil.TestingT, ctx context.Context, args []string, expectedPath string) {
expected := ReadFile(t, ctx, expectedPath)

out := captureOutput(t, ctx, args)

if out != expected {
actual := fmt.Sprintf("Output from %v", args)
testdiff.AssertEqualTexts(t, expectedFilename, actual, expected, out)
testdiff.AssertEqualTexts(t, expectedPath, actual, expected, out)

if OverwriteMode {
WriteFile(t, expectedPath, out)
}
}
}

func AssertOutputJQ(t testutil.TestingT, ctx context.Context, args []string, expectedFilename string, ignorePaths []string) {
_, filename, _, _ := runtime.Caller(1)
dir := filepath.Dir(filename)
expectedPath := filepath.Join(dir, expectedFilename)
func AssertOutputJQ(t testutil.TestingT, ctx context.Context, args []string, expectedPath string, ignorePaths []string) {
expected := ReadFile(t, ctx, expectedPath)

out := captureOutput(t, ctx, args)

if out != expected {
actual := fmt.Sprintf("Output from %v", args)
testdiff.AssertEqualJQ(t.(*testing.T), expectedFilename, actual, expected, out, ignorePaths)
testdiff.AssertEqualJQ(t.(*testing.T), expectedPath, actual, expected, out, ignorePaths)

if OverwriteMode {
WriteFile(t, expectedPath, out)
Expand Down
9 changes: 9 additions & 0 deletions internal/testutil/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func Chdir(t TestingT, dir string) string {

wd, err := os.Getwd()
require.NoError(t, err)
if os.Getenv("TESTS_ORIG_WD") == "" {
t.Setenv("TESTS_ORIG_WD", wd)
}

abs, err := filepath.Abs(dir)
require.NoError(t, err)
Expand All @@ -61,3 +64,9 @@ func Chdir(t TestingT, dir string) string {

return wd
}

// Helper to get absolute path to testdata file.
// It only able to helps if case Chdir() above was called or directory was not changed at all.
func TestData(filename string) string {
return filepath.Join(os.Getenv("TESTS_ORIG_WD"), filename)
}

0 comments on commit 418564a

Please sign in to comment.