2023-02-15 16:14:59 +00:00
|
|
|
package notebook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/workspace"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-02-21 12:49:01 +00:00
|
|
|
func TestDetectSource(t *testing.T) {
|
2023-02-15 16:14:59 +00:00
|
|
|
var nb bool
|
|
|
|
var lang workspace.Language
|
|
|
|
var err error
|
|
|
|
|
2023-02-21 12:49:01 +00:00
|
|
|
nb, lang, err = Detect("./testdata/py_source.py")
|
2023-02-15 16:14:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, nb)
|
|
|
|
assert.Equal(t, workspace.LanguagePython, lang)
|
|
|
|
|
2023-02-21 12:49:01 +00:00
|
|
|
nb, lang, err = Detect("./testdata/r_source.r")
|
2023-02-15 16:14:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, nb)
|
|
|
|
assert.Equal(t, workspace.LanguageR, lang)
|
|
|
|
|
2023-02-21 12:49:01 +00:00
|
|
|
nb, lang, err = Detect("./testdata/scala_source.scala")
|
2023-02-15 16:14:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, nb)
|
|
|
|
assert.Equal(t, workspace.LanguageScala, lang)
|
|
|
|
|
2023-02-21 12:49:01 +00:00
|
|
|
nb, lang, err = Detect("./testdata/sql_source.sql")
|
2023-02-15 16:14:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, nb)
|
|
|
|
assert.Equal(t, workspace.LanguageSql, lang)
|
|
|
|
|
|
|
|
nb, lang, err = Detect("./testdata/txt.txt")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, nb)
|
|
|
|
assert.Equal(t, workspace.Language(""), lang)
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:49:01 +00:00
|
|
|
func TestDetectCallsDetectJupyter(t *testing.T) {
|
|
|
|
nb, lang, err := Detect("./testdata/py_ipynb.ipynb")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, nb)
|
|
|
|
assert.Equal(t, workspace.LanguagePython, lang)
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:14:59 +00:00
|
|
|
func TestDetectUnknownExtension(t *testing.T) {
|
2023-03-21 17:13:16 +00:00
|
|
|
_, _, err := Detect("./testdata/doesntexist.foobar")
|
|
|
|
assert.True(t, os.IsNotExist(err))
|
|
|
|
|
|
|
|
nb, _, err := Detect("./testdata/unknown_extension.foobar")
|
2023-02-15 16:14:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, nb)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetectNoExtension(t *testing.T) {
|
2023-03-21 17:13:16 +00:00
|
|
|
_, _, err := Detect("./testdata/doesntexist")
|
|
|
|
assert.True(t, os.IsNotExist(err))
|
|
|
|
|
|
|
|
nb, _, err := Detect("./testdata/no_extension")
|
2023-02-15 16:14:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, nb)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetectFileDoesNotExists(t *testing.T) {
|
|
|
|
_, _, err := Detect("./testdata/doesntexist.py")
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetectEmptyFile(t *testing.T) {
|
|
|
|
// Create empty file.
|
|
|
|
dir := t.TempDir()
|
|
|
|
path := filepath.Join(dir, "file.py")
|
|
|
|
err := os.WriteFile(path, nil, 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// No contents means not a notebook.
|
|
|
|
nb, _, err := Detect(path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, nb)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetectFileWithLongHeader(t *testing.T) {
|
|
|
|
// Create 128kb garbage file.
|
|
|
|
dir := t.TempDir()
|
|
|
|
path := filepath.Join(dir, "file.py")
|
|
|
|
buf := make([]byte, 128*1024)
|
|
|
|
err := os.WriteFile(path, buf, 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Garbage contents means not a notebook.
|
|
|
|
nb, _, err := Detect(path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, nb)
|
|
|
|
}
|