From 418564aac1d128341a00d68d55556a04841586ad Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Fri, 20 Dec 2024 14:43:29 +0100 Subject: [PATCH] get rid of "_, filename, _, _ := runtime.Caller(1)" Instead, store original WD on env var. --- .../bundle/init_default_python_test.go | 48 +++++++++++++------ internal/testcli/golden.go | 16 ++----- internal/testutil/env.go | 9 ++++ 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/integration/bundle/init_default_python_test.go b/integration/bundle/init_default_python_test.go index 4196852f02..9b65636e96 100644 --- a/integration/bundle/init_default_python_test.go +++ b/integration/bundle/init_default_python_test.go @@ -88,7 +88,12 @@ 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() { @@ -96,17 +101,32 @@ func testDefaultPython(t *testing.T, pythonVersion string) { 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", + }, + ) } diff --git a/internal/testcli/golden.go b/internal/testcli/golden.go index f4f082cb39..34f38f18a4 100644 --- a/internal/testcli/golden.go +++ b/internal/testcli/golden.go @@ -4,9 +4,7 @@ import ( "context" "fmt" "os" - "path/filepath" "regexp" - "runtime" "slices" "strings" "testing" @@ -46,17 +44,14 @@ 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) @@ -64,17 +59,14 @@ func AssertOutput(t testutil.TestingT, ctx context.Context, args []string, expec } } -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) diff --git a/internal/testutil/env.go b/internal/testutil/env.go index 10557c4e63..2597519200 100644 --- a/internal/testutil/env.go +++ b/internal/testutil/env.go @@ -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) @@ -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) +}