From cd675ded9ac504e9652a7e9a90ce83493adaad98 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Fri, 19 Apr 2024 17:05:36 +0200 Subject: [PATCH] Update `testutil` helpers to return path (#1383) ## Changes I spotted a few call sites where the path of a test file was synthesized multiple times. It is easier to capture the path as a variable and reuse it. --- bundle/deploy/state_pull_test.go | 5 ++--- bundle/deploy/terraform/init_test.go | 14 +++++++------- internal/testutil/helpers.go | 26 -------------------------- internal/testutil/touch.go | 26 ++++++++++++++++++++++++++ libs/fileset/file_test.go | 24 ++++++++++++++---------- 5 files changed, 49 insertions(+), 46 deletions(-) delete mode 100644 internal/testutil/helpers.go create mode 100644 internal/testutil/touch.go diff --git a/bundle/deploy/state_pull_test.go b/bundle/deploy/state_pull_test.go index ca483473..bcb88374 100644 --- a/bundle/deploy/state_pull_test.go +++ b/bundle/deploy/state_pull_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "io" "os" - "path/filepath" "testing" "github.com/databricks/cli/bundle" @@ -77,11 +76,11 @@ func testStatePull(t *testing.T, opts statePullOpts) { ctx := context.Background() for _, file := range opts.localFiles { - testutil.Touch(t, filepath.Join(b.RootPath, "bar"), file) + testutil.Touch(t, b.RootPath, "bar", file) } for _, file := range opts.localNotebooks { - testutil.TouchNotebook(t, filepath.Join(b.RootPath, "bar"), file) + testutil.TouchNotebook(t, b.RootPath, "bar", file) } if opts.withExistingSnapshot { diff --git a/bundle/deploy/terraform/init_test.go b/bundle/deploy/terraform/init_test.go index ffc21585..421e9be3 100644 --- a/bundle/deploy/terraform/init_test.go +++ b/bundle/deploy/terraform/init_test.go @@ -399,7 +399,7 @@ func TestGetEnvVarWithMatchingVersion(t *testing.T) { versionVarName := "FOO_VERSION" tmp := t.TempDir() - testutil.Touch(t, tmp, "bar") + file := testutil.Touch(t, tmp, "bar") var tc = []struct { envValue string @@ -408,19 +408,19 @@ func TestGetEnvVarWithMatchingVersion(t *testing.T) { expected string }{ { - envValue: filepath.Join(tmp, "bar"), + envValue: file, versionValue: "1.2.3", currentVersion: "1.2.3", - expected: filepath.Join(tmp, "bar"), + expected: file, }, { - envValue: filepath.Join(tmp, "does-not-exist"), + envValue: "does-not-exist", versionValue: "1.2.3", currentVersion: "1.2.3", expected: "", }, { - envValue: filepath.Join(tmp, "bar"), + envValue: file, versionValue: "1.2.3", currentVersion: "1.2.4", expected: "", @@ -432,10 +432,10 @@ func TestGetEnvVarWithMatchingVersion(t *testing.T) { expected: "", }, { - envValue: filepath.Join(tmp, "bar"), + envValue: file, versionValue: "", currentVersion: "1.2.3", - expected: filepath.Join(tmp, "bar"), + expected: file, }, } diff --git a/internal/testutil/helpers.go b/internal/testutil/helpers.go deleted file mode 100644 index 853cc16c..00000000 --- a/internal/testutil/helpers.go +++ /dev/null @@ -1,26 +0,0 @@ -package testutil - -import ( - "os" - "path/filepath" - "testing" - - "github.com/stretchr/testify/require" -) - -func TouchNotebook(t *testing.T, path, file string) { - os.MkdirAll(path, 0755) - f, err := os.Create(filepath.Join(path, file)) - require.NoError(t, err) - - err = os.WriteFile(filepath.Join(path, file), []byte("# Databricks notebook source"), 0644) - require.NoError(t, err) - f.Close() -} - -func Touch(t *testing.T, path, file string) { - os.MkdirAll(path, 0755) - f, err := os.Create(filepath.Join(path, file)) - require.NoError(t, err) - f.Close() -} diff --git a/internal/testutil/touch.go b/internal/testutil/touch.go new file mode 100644 index 00000000..55683f3e --- /dev/null +++ b/internal/testutil/touch.go @@ -0,0 +1,26 @@ +package testutil + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func TouchNotebook(t *testing.T, elems ...string) string { + path := filepath.Join(elems...) + os.MkdirAll(filepath.Dir(path), 0755) + err := os.WriteFile(path, []byte("# Databricks notebook source"), 0644) + require.NoError(t, err) + return path +} + +func Touch(t *testing.T, elems ...string) string { + path := filepath.Join(elems...) + os.MkdirAll(filepath.Dir(path), 0755) + f, err := os.Create(path) + require.NoError(t, err) + f.Close() + return path +} diff --git a/libs/fileset/file_test.go b/libs/fileset/file_test.go index 4adcb1c5..cdfc9ba1 100644 --- a/libs/fileset/file_test.go +++ b/libs/fileset/file_test.go @@ -24,16 +24,20 @@ func TestSourceFileIsNotNotebook(t *testing.T) { func TestUnknownFileDetectsNotebook(t *testing.T) { tmpDir := t.TempDir() - testutil.Touch(t, tmpDir, "test.py") - testutil.TouchNotebook(t, tmpDir, "notebook.py") - f := NewFile(nil, filepath.Join(tmpDir, "test.py"), "test.py") - isNotebook, err := f.IsNotebook() - require.NoError(t, err) - require.False(t, isNotebook) + t.Run("file", func(t *testing.T) { + path := testutil.Touch(t, tmpDir, "test.py") + f := NewFile(nil, path, filepath.Base(path)) + isNotebook, err := f.IsNotebook() + require.NoError(t, err) + require.False(t, isNotebook) + }) - f = NewFile(nil, filepath.Join(tmpDir, "notebook.py"), "notebook.py") - isNotebook, err = f.IsNotebook() - require.NoError(t, err) - require.True(t, isNotebook) + t.Run("notebook", func(t *testing.T) { + path := testutil.TouchNotebook(t, tmpDir, "notebook.py") + f := NewFile(nil, path, filepath.Base(path)) + isNotebook, err := f.IsNotebook() + require.NoError(t, err) + require.True(t, isNotebook) + }) }