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.
This commit is contained in:
Pieter Noordhuis 2024-04-19 17:05:36 +02:00 committed by GitHub
parent b296f90767
commit cd675ded9a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 49 additions and 46 deletions

View File

@ -6,7 +6,6 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle"
@ -77,11 +76,11 @@ func testStatePull(t *testing.T, opts statePullOpts) {
ctx := context.Background() ctx := context.Background()
for _, file := range opts.localFiles { 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 { for _, file := range opts.localNotebooks {
testutil.TouchNotebook(t, filepath.Join(b.RootPath, "bar"), file) testutil.TouchNotebook(t, b.RootPath, "bar", file)
} }
if opts.withExistingSnapshot { if opts.withExistingSnapshot {

View File

@ -399,7 +399,7 @@ func TestGetEnvVarWithMatchingVersion(t *testing.T) {
versionVarName := "FOO_VERSION" versionVarName := "FOO_VERSION"
tmp := t.TempDir() tmp := t.TempDir()
testutil.Touch(t, tmp, "bar") file := testutil.Touch(t, tmp, "bar")
var tc = []struct { var tc = []struct {
envValue string envValue string
@ -408,19 +408,19 @@ func TestGetEnvVarWithMatchingVersion(t *testing.T) {
expected string expected string
}{ }{
{ {
envValue: filepath.Join(tmp, "bar"), envValue: file,
versionValue: "1.2.3", versionValue: "1.2.3",
currentVersion: "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", versionValue: "1.2.3",
currentVersion: "1.2.3", currentVersion: "1.2.3",
expected: "", expected: "",
}, },
{ {
envValue: filepath.Join(tmp, "bar"), envValue: file,
versionValue: "1.2.3", versionValue: "1.2.3",
currentVersion: "1.2.4", currentVersion: "1.2.4",
expected: "", expected: "",
@ -432,10 +432,10 @@ func TestGetEnvVarWithMatchingVersion(t *testing.T) {
expected: "", expected: "",
}, },
{ {
envValue: filepath.Join(tmp, "bar"), envValue: file,
versionValue: "", versionValue: "",
currentVersion: "1.2.3", currentVersion: "1.2.3",
expected: filepath.Join(tmp, "bar"), expected: file,
}, },
} }

View File

@ -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()
}

View File

@ -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
}

View File

@ -24,16 +24,20 @@ func TestSourceFileIsNotNotebook(t *testing.T) {
func TestUnknownFileDetectsNotebook(t *testing.T) { func TestUnknownFileDetectsNotebook(t *testing.T) {
tmpDir := t.TempDir() 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") 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() isNotebook, err := f.IsNotebook()
require.NoError(t, err) require.NoError(t, err)
require.False(t, isNotebook) require.False(t, isNotebook)
})
f = NewFile(nil, filepath.Join(tmpDir, "notebook.py"), "notebook.py") t.Run("notebook", func(t *testing.T) {
isNotebook, err = f.IsNotebook() path := testutil.TouchNotebook(t, tmpDir, "notebook.py")
f := NewFile(nil, path, filepath.Base(path))
isNotebook, err := f.IsNotebook()
require.NoError(t, err) require.NoError(t, err)
require.True(t, isNotebook) require.True(t, isNotebook)
})
} }