mirror of https://github.com/databricks/cli.git
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:
parent
b296f90767
commit
cd675ded9a
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue