diff --git a/acceptance/cmd/workspace/import-export/fixtures/file-a b/acceptance/cmd/workspace/import-export/fixtures/file-a new file mode 100644 index 00000000000..8baef1b4abc --- /dev/null +++ b/acceptance/cmd/workspace/import-export/fixtures/file-a @@ -0,0 +1 @@ +abc diff --git a/acceptance/cmd/workspace/import-export/fixtures/local-file-a b/acceptance/cmd/workspace/import-export/fixtures/local-file-a new file mode 100644 index 00000000000..164661011f6 --- /dev/null +++ b/acceptance/cmd/workspace/import-export/fixtures/local-file-a @@ -0,0 +1 @@ +local content diff --git a/acceptance/cmd/workspace/import-export/fixtures/old-file-a b/acceptance/cmd/workspace/import-export/fixtures/old-file-a new file mode 100644 index 00000000000..0d30f1c3752 --- /dev/null +++ b/acceptance/cmd/workspace/import-export/fixtures/old-file-a @@ -0,0 +1 @@ +old file diff --git a/acceptance/cmd/workspace/import-export/fixtures/old-pyNotebook.py b/acceptance/cmd/workspace/import-export/fixtures/old-pyNotebook.py new file mode 100644 index 00000000000..c6ef2f616a0 --- /dev/null +++ b/acceptance/cmd/workspace/import-export/fixtures/old-pyNotebook.py @@ -0,0 +1,2 @@ +# Databricks notebook source +print("old notebook") diff --git a/acceptance/cmd/workspace/import-export/fixtures/pyNotebook.py b/acceptance/cmd/workspace/import-export/fixtures/pyNotebook.py new file mode 100644 index 00000000000..1645e04b1de --- /dev/null +++ b/acceptance/cmd/workspace/import-export/fixtures/pyNotebook.py @@ -0,0 +1 @@ +# Databricks notebook source diff --git a/acceptance/cmd/workspace/import-export/fixtures/rNotebook.r b/acceptance/cmd/workspace/import-export/fixtures/rNotebook.r new file mode 100644 index 00000000000..1645e04b1de --- /dev/null +++ b/acceptance/cmd/workspace/import-export/fixtures/rNotebook.r @@ -0,0 +1 @@ +# Databricks notebook source diff --git a/acceptance/cmd/workspace/import-export/fixtures/scalaNotebook.scala b/acceptance/cmd/workspace/import-export/fixtures/scalaNotebook.scala new file mode 100644 index 00000000000..fefc120ef2d --- /dev/null +++ b/acceptance/cmd/workspace/import-export/fixtures/scalaNotebook.scala @@ -0,0 +1 @@ +// Databricks notebook source diff --git a/acceptance/cmd/workspace/import-export/fixtures/script.sh b/acceptance/cmd/workspace/import-export/fixtures/script.sh new file mode 100644 index 00000000000..065b3c7439b --- /dev/null +++ b/acceptance/cmd/workspace/import-export/fixtures/script.sh @@ -0,0 +1,2 @@ +#!/usr/bin/bash +echo hello, world diff --git a/acceptance/cmd/workspace/import-export/fixtures/sqlNotebook.sql b/acceptance/cmd/workspace/import-export/fixtures/sqlNotebook.sql new file mode 100644 index 00000000000..ae23affadae --- /dev/null +++ b/acceptance/cmd/workspace/import-export/fixtures/sqlNotebook.sql @@ -0,0 +1 @@ +-- Databricks notebook source diff --git a/integration/cmd/workspace/testdata/import_dir/a/b/c/file-b b/acceptance/cmd/workspace/import-export/import_dir/a/b/c/file-b similarity index 100% rename from integration/cmd/workspace/testdata/import_dir/a/b/c/file-b rename to acceptance/cmd/workspace/import-export/import_dir/a/b/c/file-b diff --git a/integration/cmd/workspace/testdata/import_dir/file-a b/acceptance/cmd/workspace/import-export/import_dir/file-a similarity index 100% rename from integration/cmd/workspace/testdata/import_dir/file-a rename to acceptance/cmd/workspace/import-export/import_dir/file-a diff --git a/integration/cmd/workspace/testdata/import_dir/jupyterNotebook.ipynb b/acceptance/cmd/workspace/import-export/import_dir/jupyterNotebook.ipynb similarity index 100% rename from integration/cmd/workspace/testdata/import_dir/jupyterNotebook.ipynb rename to acceptance/cmd/workspace/import-export/import_dir/jupyterNotebook.ipynb diff --git a/integration/cmd/workspace/testdata/import_dir/pyNotebook.py b/acceptance/cmd/workspace/import-export/import_dir/pyNotebook.py similarity index 100% rename from integration/cmd/workspace/testdata/import_dir/pyNotebook.py rename to acceptance/cmd/workspace/import-export/import_dir/pyNotebook.py diff --git a/integration/cmd/workspace/testdata/import_dir/rNotebook.r b/acceptance/cmd/workspace/import-export/import_dir/rNotebook.r similarity index 100% rename from integration/cmd/workspace/testdata/import_dir/rNotebook.r rename to acceptance/cmd/workspace/import-export/import_dir/rNotebook.r diff --git a/integration/cmd/workspace/testdata/import_dir/scalaNotebook.scala b/acceptance/cmd/workspace/import-export/import_dir/scalaNotebook.scala similarity index 100% rename from integration/cmd/workspace/testdata/import_dir/scalaNotebook.scala rename to acceptance/cmd/workspace/import-export/import_dir/scalaNotebook.scala diff --git a/integration/cmd/workspace/testdata/import_dir/sqlNotebook.sql b/acceptance/cmd/workspace/import-export/import_dir/sqlNotebook.sql similarity index 100% rename from integration/cmd/workspace/testdata/import_dir/sqlNotebook.sql rename to acceptance/cmd/workspace/import-export/import_dir/sqlNotebook.sql diff --git a/acceptance/cmd/workspace/import-export/out.test.toml b/acceptance/cmd/workspace/import-export/out.test.toml new file mode 100644 index 00000000000..c7c6f58ed6e --- /dev/null +++ b/acceptance/cmd/workspace/import-export/out.test.toml @@ -0,0 +1 @@ +Cloud = true diff --git a/acceptance/cmd/workspace/import-export/output.txt b/acceptance/cmd/workspace/import-export/output.txt new file mode 100644 index 00000000000..1cf39410115 --- /dev/null +++ b/acceptance/cmd/workspace/import-export/output.txt @@ -0,0 +1,331 @@ + +=== list prints the table header and nothing on stderr +ID Type Language Path + +=== import --content with --language creates a notebook + +>>> [CLI] workspace import /Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-source/pyScript --content cHJpbnQoMSk= --language PYTHON + +>>> [CLI] workspace import /Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-source/pyNb --content YCMgRGF0YWJyaWNrcyBub3RlYm9vayBzb3VyY2UKcHJpbnQoMSk= --language PYTHON +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-source/pyScript +{"object_type":"NOTEBOOK","language":"PYTHON"} +# Databricks notebook source +print(1) +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-source/pyNb +{"object_type":"NOTEBOOK","language":"PYTHON"} +`# Databricks notebook source +print(1) + +=== import --content --format AUTO: a notebook only for a source extension and notebook content + +>>> [CLI] workspace import /Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-auto/py-nb-as-file --content YCMgRGF0YWJyaWNrcyBub3RlYm9vayBzb3VyY2UKcHJpbnQoMSk= --format AUTO + +>>> [CLI] workspace import /Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-auto/py-nb-as-notebook.py --content YCMgRGF0YWJyaWNrcyBub3RlYm9vayBzb3VyY2UKcHJpbnQoMSk= --format AUTO + +>>> [CLI] workspace import /Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-auto/not-a-notebook.py --content cHJpbnQoMSk= --format AUTO + +>>> [CLI] workspace list /Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-auto -o json +{"path":"/Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-auto/not-a-notebook.py","object_type":"FILE","language":null} +{"path":"/Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-auto/py-nb-as-file","object_type":"FILE","language":null} +{"path":"/Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-auto/py-nb-as-notebook","object_type":"NOTEBOOK","language":"PYTHON"} +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-auto/py-nb-as-file +{"object_type":"FILE","language":null} +`# Databricks notebook source +print(1) +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-auto/py-nb-as-notebook +{"object_type":"NOTEBOOK","language":"PYTHON"} +`# Databricks notebook source +print(1) +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/content-auto/not-a-notebook.py +{"object_type":"FILE","language":null} +print(1) + +=== import --file with --language creates a notebook + +>>> [CLI] workspace import /Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-source/pyNotebook --file import_dir/pyNotebook.py --language PYTHON + +>>> [CLI] workspace import /Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-source/scalaNotebook --file import_dir/scalaNotebook.scala --language SCALA +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-source/pyNotebook +{"object_type":"NOTEBOOK","language":"PYTHON"} +# Databricks notebook source +print("python") + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-source/scalaNotebook +{"object_type":"NOTEBOOK","language":"SCALA"} +// Databricks notebook source +println("scala") + + +=== import --file without --language or --format expects a zip archive + +>>> musterr [CLI] workspace import /Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-source/scalaNotebook-error --file import_dir/scalaNotebook.scala +Error: The zip archive contains no items. + +=== import --file --format AUTO: a notebook only for a source extension and notebook content + +>>> [CLI] workspace import /Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-auto/py-nb-as-file --file import_dir/pyNotebook.py --format AUTO + +>>> [CLI] workspace import /Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-auto/py-nb-as-notebook.py --file import_dir/pyNotebook.py --format AUTO + +>>> [CLI] workspace import /Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-auto/not-a-notebook.py --file import_dir/file-a --format AUTO + +>>> [CLI] workspace list /Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-auto -o json +{"path":"/Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-auto/not-a-notebook.py","object_type":"FILE","language":null} +{"path":"/Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-auto/py-nb-as-file","object_type":"FILE","language":null} +{"path":"/Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-auto/py-nb-as-notebook","object_type":"NOTEBOOK","language":"PYTHON"} +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-auto/py-nb-as-file +{"object_type":"FILE","language":null} +# Databricks notebook source +print("python") + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-auto/py-nb-as-notebook +{"object_type":"NOTEBOOK","language":"PYTHON"} +# Databricks notebook source +print("python") + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/file-auto/not-a-notebook.py +{"object_type":"FILE","language":null} +hello, world + + +=== export prints a file or notebook on stdout, or writes it with --file +export script.sh: stdout is the uploaded file, stderr is empty +export file-a: stdout is the uploaded file +export pyNotebook: stdout is the uploaded notebook +export pyNotebook --format JUPYTER: stdout is a Jupyter notebook +{"has_cells":true,"has_metadata":true} +export --file: stdout is empty, the file has the content +{"has_cells":true,"has_metadata":true} + +=== import-dir imports files and notebooks of every language +import-dir --log-level=debug: stdout below, stderr is empty +Importing files from ./import_dir +a/b/c/file-b -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/a/b/c/file-b +file-a -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/file-a +jupyterNotebook.ipynb -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/jupyterNotebook +pyNotebook.py -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/pyNotebook +rNotebook.r -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/rNotebook +scalaNotebook.scala -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/scalaNotebook +sqlNotebook.sql -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/sqlNotebook +Import complete +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/file-a +{"object_type":"FILE","language":null} +hello, world + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/a/b/c/file-b +{"object_type":"FILE","language":null} +file-in-dir + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/pyNotebook +{"object_type":"NOTEBOOK","language":"PYTHON"} +# Databricks notebook source +print("python") + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/sqlNotebook +{"object_type":"NOTEBOOK","language":"SQL"} +-- Databricks notebook source +SELECT "sql" + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/rNotebook +{"object_type":"NOTEBOOK","language":"R"} +# Databricks notebook source +print("r") + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/scalaNotebook +{"object_type":"NOTEBOOK","language":"SCALA"} +// Databricks notebook source +println("scala") + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/jupyterNotebook +{"object_type":"NOTEBOOK","language":"PYTHON"} +# Databricks notebook source +print("jupyter") + +=== import-dir keeps existing objects without --overwrite +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/file-a +{"object_type":"FILE","language":null} +old file + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/pyNotebook +{"object_type":"NOTEBOOK","language":"PYTHON"} +# Databricks notebook source +print("old notebook") + + +>>> [CLI] workspace import-dir ./import_dir /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep +Importing files from ./import_dir +a/b/c/file-b -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/a/b/c/file-b +file-a -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/file-a (skipped; already exists) +jupyterNotebook.ipynb -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/jupyterNotebook +pyNotebook.py -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/pyNotebook (skipped; already exists) +rNotebook.r -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/rNotebook +scalaNotebook.scala -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/scalaNotebook +sqlNotebook.sql -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/sqlNotebook +Import complete +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/file-a +{"object_type":"FILE","language":null} +old file + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/pyNotebook +{"object_type":"NOTEBOOK","language":"PYTHON"} +# Databricks notebook source +print("old notebook") + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/a/b/c/file-b +{"object_type":"FILE","language":null} +file-in-dir + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/sqlNotebook +{"object_type":"NOTEBOOK","language":"SQL"} +-- Databricks notebook source +SELECT "sql" + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/rNotebook +{"object_type":"NOTEBOOK","language":"R"} +# Databricks notebook source +print("r") + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/scalaNotebook +{"object_type":"NOTEBOOK","language":"SCALA"} +// Databricks notebook source +println("scala") + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-keep/jupyterNotebook +{"object_type":"NOTEBOOK","language":"PYTHON"} +# Databricks notebook source +print("jupyter") + +=== import-dir --overwrite replaces existing objects +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/file-a +{"object_type":"FILE","language":null} +old file + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/pyNotebook +{"object_type":"NOTEBOOK","language":"PYTHON"} +# Databricks notebook source +print("old notebook") + + +>>> [CLI] workspace import-dir ./import_dir /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite --overwrite +Importing files from ./import_dir +a/b/c/file-b -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/a/b/c/file-b +file-a -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/file-a +jupyterNotebook.ipynb -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/jupyterNotebook +pyNotebook.py -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/pyNotebook +rNotebook.r -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/rNotebook +scalaNotebook.scala -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/scalaNotebook +sqlNotebook.sql -> /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/sqlNotebook +Import complete +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/file-a +{"object_type":"FILE","language":null} +hello, world + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/pyNotebook +{"object_type":"NOTEBOOK","language":"PYTHON"} +# Databricks notebook source +print("python") + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/a/b/c/file-b +{"object_type":"FILE","language":null} +file-in-dir + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/sqlNotebook +{"object_type":"NOTEBOOK","language":"SQL"} +-- Databricks notebook source +SELECT "sql" + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/rNotebook +{"object_type":"NOTEBOOK","language":"R"} +# Databricks notebook source +print("r") + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/scalaNotebook +{"object_type":"NOTEBOOK","language":"SCALA"} +// Databricks notebook source +println("scala") + +--- /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir-overwrite/jupyterNotebook +{"object_type":"NOTEBOOK","language":"PYTHON"} +# Databricks notebook source +print("jupyter") + +=== export-dir exports files and notebooks with their source extension +export-dir: stdout below, stderr is empty +Exporting files from /Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/a/b/c/file-b -> [TEST_TMP_DIR]/export/all/a/b/c/file-b +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/file-a -> [TEST_TMP_DIR]/export/all/file-a +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/jupyterNotebook -> [TEST_TMP_DIR]/export/all/jupyterNotebook.py +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/pyNotebook -> [TEST_TMP_DIR]/export/all/pyNotebook.py +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/rNotebook -> [TEST_TMP_DIR]/export/all/rNotebook.r +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/scalaNotebook -> [TEST_TMP_DIR]/export/all/scalaNotebook.scala +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/import-dir/sqlNotebook -> [TEST_TMP_DIR]/export/all/sqlNotebook.sql +Export complete +--- file-a +hello, world + +--- a/b/c/file-b +file-in-dir + +--- pyNotebook.py +# Databricks notebook source +print("python") + +--- sqlNotebook.sql +-- Databricks notebook source +SELECT "sql" + +--- rNotebook.r +# Databricks notebook source +print("r") + +--- scalaNotebook.scala +// Databricks notebook source +println("scala") + +--- jupyterNotebook.py +# Databricks notebook source +print("jupyter") + +=== export-dir keeps existing local files without --overwrite + +>>> [CLI] workspace export-dir /Users/[USERNAME]/import-export-[UNIQUE_NAME]/export [TEST_TMP_DIR]/export/keep +Exporting files from /Users/[USERNAME]/import-export-[UNIQUE_NAME]/export +file-a -> [TEST_TMP_DIR]/export/keep/file-a (skipped; already exists) +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/export/pyNotebook -> [TEST_TMP_DIR]/export/keep/pyNotebook.py +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/export/rNotebook -> [TEST_TMP_DIR]/export/keep/rNotebook.r +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/export/scalaNotebook -> [TEST_TMP_DIR]/export/keep/scalaNotebook.scala +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/export/script.sh -> [TEST_TMP_DIR]/export/keep/script.sh +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/export/sqlNotebook -> [TEST_TMP_DIR]/export/keep/sqlNotebook.sql +Export complete + +>>> cat [TEST_TMP_DIR]/export/keep/file-a +local content + + +=== export-dir --overwrite replaces existing local files + +>>> [CLI] workspace export-dir /Users/[USERNAME]/import-export-[UNIQUE_NAME]/export [TEST_TMP_DIR]/export/overwrite --overwrite +Exporting files from /Users/[USERNAME]/import-export-[UNIQUE_NAME]/export +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/export/file-a -> [TEST_TMP_DIR]/export/overwrite/file-a +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/export/pyNotebook -> [TEST_TMP_DIR]/export/overwrite/pyNotebook.py +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/export/rNotebook -> [TEST_TMP_DIR]/export/overwrite/rNotebook.r +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/export/scalaNotebook -> [TEST_TMP_DIR]/export/overwrite/scalaNotebook.scala +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/export/script.sh -> [TEST_TMP_DIR]/export/overwrite/script.sh +/Users/[USERNAME]/import-export-[UNIQUE_NAME]/export/sqlNotebook -> [TEST_TMP_DIR]/export/overwrite/sqlNotebook.sql +Export complete +header-only notebooks are exported with their language's header: +--- pyNotebook.py +# Databricks notebook source +--- sqlNotebook.sql +-- Databricks notebook source +--- rNotebook.r +# Databricks notebook source +--- scalaNotebook.scala +// Databricks notebook source + +=== delete removes the test directory + +>>> [CLI] workspace delete --recursive /Users/[USERNAME]/import-export-[UNIQUE_NAME] + +>>> musterr [CLI] workspace get-status /Users/[USERNAME]/import-export-[UNIQUE_NAME] +Error: Path (/Users/[USERNAME]/import-export-[UNIQUE_NAME]) doesn't exist. diff --git a/acceptance/cmd/workspace/import-export/script b/acceptance/cmd/workspace/import-export/script new file mode 100644 index 00000000000..7e8dc43f2fc --- /dev/null +++ b/acceptance/cmd/workspace/import-export/script @@ -0,0 +1,147 @@ +DIR="/Users/$CURRENT_USER_NAME/import-export-$UNIQUE_NAME" + +cleanup() { + $CLI workspace delete --recursive "$DIR" >> LOG.cleanup 2>&1 || true +} +trap cleanup EXIT + +# The leading backtick is kept from the original integration test: the marker +# does not have to start the content for the workspace to detect a notebook. +NB_CONTENT='`# Databricks notebook source +print(1)' + +title "list prints the table header and nothing on stderr" +$CLI workspace list / > tmp-list.txt 2> tmp-stderr.txt +head -n1 tmp-list.txt | tr -s " " +assert_empty tmp-stderr.txt + +for sub in content-source content-auto file-source file-auto export import-dir import-dir-keep import-dir-overwrite; do + $CLI workspace mkdirs "$DIR/$sub" +done + +title "import --content with --language creates a notebook" +trace $CLI workspace import "$DIR/content-source/pyScript" --content "$(b64 'print(1)')" --language PYTHON +trace $CLI workspace import "$DIR/content-source/pyNb" --content "$(b64 "$NB_CONTENT")" --language PYTHON +show "$DIR/content-source/pyScript" +show "$DIR/content-source/pyNb" + +title "import --content --format AUTO: a notebook only for a source extension and notebook content" +trace $CLI workspace import "$DIR/content-auto/py-nb-as-file" --content "$(b64 "$NB_CONTENT")" --format AUTO +trace $CLI workspace import "$DIR/content-auto/py-nb-as-notebook.py" --content "$(b64 "$NB_CONTENT")" --format AUTO +trace $CLI workspace import "$DIR/content-auto/not-a-notebook.py" --content "$(b64 'print(1)')" --format AUTO +trace $CLI workspace list "$DIR/content-auto" -o json | workspace_list_fields +show "$DIR/content-auto/py-nb-as-file" +show "$DIR/content-auto/py-nb-as-notebook" +show "$DIR/content-auto/not-a-notebook.py" + +title "import --file with --language creates a notebook" +trace $CLI workspace import "$DIR/file-source/pyNotebook" --file import_dir/pyNotebook.py --language PYTHON +trace $CLI workspace import "$DIR/file-source/scalaNotebook" --file import_dir/scalaNotebook.scala --language SCALA +show "$DIR/file-source/pyNotebook" +show "$DIR/file-source/scalaNotebook" + +title "import --file without --language or --format expects a zip archive" +trace musterr $CLI workspace import "$DIR/file-source/scalaNotebook-error" --file import_dir/scalaNotebook.scala + +title "import --file --format AUTO: a notebook only for a source extension and notebook content" +trace $CLI workspace import "$DIR/file-auto/py-nb-as-file" --file import_dir/pyNotebook.py --format AUTO +trace $CLI workspace import "$DIR/file-auto/py-nb-as-notebook.py" --file import_dir/pyNotebook.py --format AUTO +trace $CLI workspace import "$DIR/file-auto/not-a-notebook.py" --file import_dir/file-a --format AUTO +trace $CLI workspace list "$DIR/file-auto" -o json | workspace_list_fields +show "$DIR/file-auto/py-nb-as-file" +show "$DIR/file-auto/py-nb-as-notebook" +show "$DIR/file-auto/not-a-notebook.py" + +title "export prints a file or notebook on stdout, or writes it with --file" +for name in script.sh file-a pyNotebook.py sqlNotebook.sql rNotebook.r scalaNotebook.scala; do + $CLI workspace import "$DIR/export/$name" --file "fixtures/$name" --format AUTO +done +# No trace on commands whose stdout and stderr are captured: trace writes to stderr. +echo "export script.sh: stdout is the uploaded file, stderr is empty" +$CLI workspace export "$DIR/export/script.sh" > tmp-stdout.txt 2> tmp-stderr.txt +cmp fixtures/script.sh tmp-stdout.txt +assert_empty tmp-stderr.txt +echo "export file-a: stdout is the uploaded file" +$CLI workspace export "$DIR/export/file-a" > tmp-stdout.txt +cmp fixtures/file-a tmp-stdout.txt +echo "export pyNotebook: stdout is the uploaded notebook" +$CLI workspace export "$DIR/export/pyNotebook" > tmp-stdout.txt +cmp fixtures/pyNotebook.py tmp-stdout.txt +echo "export pyNotebook --format JUPYTER: stdout is a Jupyter notebook" +$CLI workspace export "$DIR/export/pyNotebook" --format JUPYTER > tmp-stdout.txt +jupyter_fields tmp-stdout.txt + +mkdir -p "$TEST_TMP_DIR/export" +echo "export --file: stdout is empty, the file has the content" +$CLI workspace export "$DIR/export/file-a" --file "$TEST_TMP_DIR/export/file.txt" > tmp-stdout.txt +assert_empty tmp-stdout.txt +cmp fixtures/file-a "$TEST_TMP_DIR/export/file.txt" +$CLI workspace export "$DIR/export/pyNotebook" --file "$TEST_TMP_DIR/export/pyNb.py" > tmp-stdout.txt +assert_empty tmp-stdout.txt +cmp fixtures/pyNotebook.py "$TEST_TMP_DIR/export/pyNb.py" +$CLI workspace export "$DIR/export/pyNotebook" --format JUPYTER --file "$TEST_TMP_DIR/export/jupyterNb.ipynb" > tmp-stdout.txt +assert_empty tmp-stdout.txt +jupyter_fields "$TEST_TMP_DIR/export/jupyterNb.ipynb" + +title "import-dir imports files and notebooks of every language" +# The debug log goes to a LOG file (it differs between runs), so stderr holds only the command's own output. +echo "import-dir --log-level=debug: stdout below, stderr is empty" +$CLI workspace import-dir ./import_dir "$DIR/import-dir" --log-level=debug --log-file LOG.import-dir 2> tmp-stderr.txt +assert_empty tmp-stderr.txt +for name in file-a a/b/c/file-b pyNotebook sqlNotebook rNotebook scalaNotebook jupyterNotebook; do + show "$DIR/import-dir/$name" +done + +for mode in keep overwrite; do + $CLI workspace import "$DIR/import-dir-$mode/file-a" --file fixtures/old-file-a --format AUTO + $CLI workspace import "$DIR/import-dir-$mode/pyNotebook.py" --file fixtures/old-pyNotebook.py --format AUTO +done + +title "import-dir keeps existing objects without --overwrite" +show "$DIR/import-dir-keep/file-a" +show "$DIR/import-dir-keep/pyNotebook" +trace $CLI workspace import-dir ./import_dir "$DIR/import-dir-keep" +for name in file-a pyNotebook a/b/c/file-b sqlNotebook rNotebook scalaNotebook jupyterNotebook; do + show "$DIR/import-dir-keep/$name" +done + +title "import-dir --overwrite replaces existing objects" +show "$DIR/import-dir-overwrite/file-a" +show "$DIR/import-dir-overwrite/pyNotebook" +trace $CLI workspace import-dir ./import_dir "$DIR/import-dir-overwrite" --overwrite +for name in file-a pyNotebook a/b/c/file-b sqlNotebook rNotebook scalaNotebook jupyterNotebook; do + show "$DIR/import-dir-overwrite/$name" +done + +title "export-dir exports files and notebooks with their source extension" +echo "export-dir: stdout below, stderr is empty" +$CLI workspace export-dir "$DIR/import-dir" "$TEST_TMP_DIR/export/all" 2> tmp-stderr.txt +assert_empty tmp-stderr.txt +for name in file-a a/b/c/file-b pyNotebook.py sqlNotebook.sql rNotebook.r scalaNotebook.scala jupyterNotebook.py; do + echo "--- $name" + cat "$TEST_TMP_DIR/export/all/$name" + echo +done + +title "export-dir keeps existing local files without --overwrite" +mkdir -p "$TEST_TMP_DIR/export/keep" "$TEST_TMP_DIR/export/overwrite" +cp fixtures/local-file-a "$TEST_TMP_DIR/export/keep/file-a" +cp fixtures/local-file-a "$TEST_TMP_DIR/export/overwrite/file-a" +trace $CLI workspace export-dir "$DIR/export" "$TEST_TMP_DIR/export/keep" +trace cat "$TEST_TMP_DIR/export/keep/file-a" +echo + +title "export-dir --overwrite replaces existing local files" +trace $CLI workspace export-dir "$DIR/export" "$TEST_TMP_DIR/export/overwrite" --overwrite +cmp fixtures/file-a "$TEST_TMP_DIR/export/overwrite/file-a" +echo "header-only notebooks are exported with their language's header:" +for name in pyNotebook.py sqlNotebook.sql rNotebook.r scalaNotebook.scala; do + echo "--- $name" + cat "$TEST_TMP_DIR/export/overwrite/$name" + cmp "fixtures/$name" "$TEST_TMP_DIR/export/overwrite/$name" +done + +title "delete removes the test directory" +trap - EXIT +trace $CLI workspace delete --recursive "$DIR" +trace musterr $CLI workspace get-status "$DIR" diff --git a/acceptance/cmd/workspace/import-export/script.prepare b/acceptance/cmd/workspace/import-export/script.prepare new file mode 100644 index 00000000000..181d7826aec --- /dev/null +++ b/acceptance/cmd/workspace/import-export/script.prepare @@ -0,0 +1,31 @@ +# workspace_object_fields is the allow-list of get-status fields this test asserts on. +workspace_object_fields() { + jq -c '{object_type, language}' +} + +# workspace_list_fields prints one line per listed object, sorted by path. +workspace_list_fields() { + jq -c 'sort_by(.path) | .[] | {path, object_type, language}' +} + +# jupyter_fields reports whether an exported notebook has the top-level ipynb fields. +jupyter_fields() { + jq -c '{has_cells: has("cells"), has_metadata: has("metadata")}' "$@" +} + +# Prints the object type and language of a workspace object, then its SOURCE export. +show() { + echo "--- $1" + $CLI workspace get-status "$1" | workspace_object_fields + $CLI workspace export "$1" + echo +} + +# Prints a captured stream only when it is not empty, so the golden shows it. +assert_empty() { + test ! -s "$1" || { echo "unexpected output in $1:"; cat "$1"; } +} + +b64() { + printf '%s' "$1" | base64 | tr -d '\n' +} diff --git a/acceptance/cmd/workspace/import-export/test.toml b/acceptance/cmd/workspace/import-export/test.toml new file mode 100644 index 00000000000..c66d8af81c0 --- /dev/null +++ b/acceptance/cmd/workspace/import-export/test.toml @@ -0,0 +1,19 @@ +Cloud = true + +Env.MSYS_NO_PATHCONV = "1" + +Ignore = ["export", "tmp-*"] + +# export-dir and import-dir print local paths with the OS separator. Only the +# local side is normalized, so a backslash in a workspace path still shows. +[[Repls]] +Old = '(\[TEST_TMP_DIR\])\\export\\(all|keep|overwrite)\\' +New = '${1}/export/${2}/' + +[[Repls]] +Old = '(\[TEST_TMP_DIR\]/export/all/)a\\b\\c\\' +New = '${1}a/b/c/' + +[[Repls]] +Old = '(?m)^a\\b\\c\\file-b' +New = 'a/b/c/file-b' diff --git a/integration/cmd/workspace/workspace_test.go b/integration/cmd/workspace/workspace_test.go deleted file mode 100644 index 53456ff8d7a..00000000000 --- a/integration/cmd/workspace/workspace_test.go +++ /dev/null @@ -1,391 +0,0 @@ -package workspace_test - -import ( - "context" - "encoding/base64" - "fmt" - "io" - "os" - "path" - "path/filepath" - "strings" - "testing" - - "github.com/databricks/cli/integration/internal/acc" - "github.com/databricks/cli/internal/testcli" - "github.com/databricks/cli/libs/filer" - "github.com/databricks/databricks-sdk-go/service/workspace" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestWorkspaceList(t *testing.T) { - ctx := t.Context() - stdout, stderr := testcli.RequireSuccessfulRun(t, ctx, "workspace", "list", "/") - outStr := stdout.String() - assert.Contains(t, outStr, "ID") - assert.Contains(t, outStr, "Type") - assert.Contains(t, outStr, "Language") - assert.Contains(t, outStr, "Path") - assert.Empty(t, stderr.String()) -} - -func TestWorkpaceExportPrintsContents(t *testing.T) { - ctx, wt := acc.WorkspaceTest(t) - w := wt.W - - tmpdir := acc.TemporaryWorkspaceDir(wt, "workspace-export-") - f, err := filer.NewWorkspaceFilesClient(w, tmpdir) - require.NoError(t, err) - - // Write file to workspace - contents := "#!/usr/bin/bash\necho hello, world\n" - err = f.Write(ctx, "file-a", strings.NewReader(contents)) - require.NoError(t, err) - - // Run export - stdout, stderr := testcli.RequireSuccessfulRun(t, ctx, "workspace", "export", path.Join(tmpdir, "file-a")) - assert.Equal(t, contents, stdout.String()) - assert.Empty(t, stderr.String()) -} - -func setupWorkspaceImportExportTest(t *testing.T) (context.Context, filer.Filer, string) { - ctx, wt := acc.WorkspaceTest(t) - w := wt.W - - tmpdir := acc.TemporaryWorkspaceDir(wt, "workspace-import-") - f, err := filer.NewWorkspaceFilesClient(w, tmpdir) - require.NoError(t, err) - - return ctx, f, tmpdir -} - -func assertLocalFileContents(t *testing.T, path, content string) { - require.FileExists(t, path) - b, err := os.ReadFile(path) - require.NoError(t, err) - assert.Contains(t, string(b), content) -} - -func assertFilerFileContents(t *testing.T, ctx context.Context, f filer.Filer, path, content string) { - r, err := f.Read(ctx, path) - require.NoError(t, err) - b, err := io.ReadAll(r) - require.NoError(t, err) - assert.Contains(t, string(b), content) -} - -func assertWorkspaceFileType(t *testing.T, ctx context.Context, f filer.Filer, path string, fileType workspace.ObjectType) { - info, err := f.Stat(ctx, path) - require.NoError(t, err) - assert.Equal(t, fileType, info.Sys().(workspace.ObjectInfo).ObjectType) -} - -func TestExportDir(t *testing.T) { - ctx, f, sourceDir := setupWorkspaceImportExportTest(t) - targetDir := t.TempDir() - - var err error - - // Write test data to the workspace - err = f.Write(ctx, "file-a", strings.NewReader("abc")) - require.NoError(t, err) - err = f.Write(ctx, "pyNotebook.py", strings.NewReader("# Databricks notebook source")) - require.NoError(t, err) - err = f.Write(ctx, "sqlNotebook.sql", strings.NewReader("-- Databricks notebook source")) - require.NoError(t, err) - err = f.Write(ctx, "scalaNotebook.scala", strings.NewReader("// Databricks notebook source")) - require.NoError(t, err) - err = f.Write(ctx, "rNotebook.r", strings.NewReader("# Databricks notebook source")) - require.NoError(t, err) - err = f.Write(ctx, "a/b/c/file-b", strings.NewReader("def"), filer.CreateParentDirectories) - require.NoError(t, err) - - expectedLogs := strings.Join([]string{ - "Exporting files from " + sourceDir, - fmt.Sprintf("%s -> %s", path.Join(sourceDir, "a/b/c/file-b"), filepath.Join(targetDir, "a/b/c/file-b")), - fmt.Sprintf("%s -> %s", path.Join(sourceDir, "file-a"), filepath.Join(targetDir, "file-a")), - fmt.Sprintf("%s -> %s", path.Join(sourceDir, "pyNotebook"), filepath.Join(targetDir, "pyNotebook.py")), - fmt.Sprintf("%s -> %s", path.Join(sourceDir, "rNotebook"), filepath.Join(targetDir, "rNotebook.r")), - fmt.Sprintf("%s -> %s", path.Join(sourceDir, "scalaNotebook"), filepath.Join(targetDir, "scalaNotebook.scala")), - fmt.Sprintf("%s -> %s", path.Join(sourceDir, "sqlNotebook"), filepath.Join(targetDir, "sqlNotebook.sql")), - "Export complete\n", - }, "\n") - - // Run Export - stdout, stderr := testcli.RequireSuccessfulRun(t, ctx, "workspace", "export-dir", sourceDir, targetDir) - assert.Equal(t, expectedLogs, stdout.String()) - assert.Empty(t, stderr.String()) - - // Assert files were exported - assertLocalFileContents(t, filepath.Join(targetDir, "file-a"), "abc") - assertLocalFileContents(t, filepath.Join(targetDir, "pyNotebook.py"), "# Databricks notebook source") - assertLocalFileContents(t, filepath.Join(targetDir, "sqlNotebook.sql"), "-- Databricks notebook source") - assertLocalFileContents(t, filepath.Join(targetDir, "rNotebook.r"), "# Databricks notebook source") - assertLocalFileContents(t, filepath.Join(targetDir, "scalaNotebook.scala"), "// Databricks notebook source") - assertLocalFileContents(t, filepath.Join(targetDir, "a/b/c/file-b"), "def") -} - -func TestExportDirDoesNotOverwrite(t *testing.T) { - ctx, f, sourceDir := setupWorkspaceImportExportTest(t) - targetDir := t.TempDir() - - var err error - - // Write remote file - err = f.Write(ctx, "file-a", strings.NewReader("content from workspace")) - require.NoError(t, err) - - // Write local file - err = os.WriteFile(filepath.Join(targetDir, "file-a"), []byte("local content"), os.ModePerm) - require.NoError(t, err) - - // Run Export - testcli.RequireSuccessfulRun(t, ctx, "workspace", "export-dir", sourceDir, targetDir) - - // Assert file is not overwritten - assertLocalFileContents(t, filepath.Join(targetDir, "file-a"), "local content") -} - -func TestExportDirWithOverwriteFlag(t *testing.T) { - ctx, f, sourceDir := setupWorkspaceImportExportTest(t) - targetDir := t.TempDir() - - var err error - - // Write remote file - err = f.Write(ctx, "file-a", strings.NewReader("content from workspace")) - require.NoError(t, err) - - // Write local file - err = os.WriteFile(filepath.Join(targetDir, "file-a"), []byte("local content"), os.ModePerm) - require.NoError(t, err) - - // Run Export - testcli.RequireSuccessfulRun(t, ctx, "workspace", "export-dir", sourceDir, targetDir, "--overwrite") - - // Assert file has been overwritten - assertLocalFileContents(t, filepath.Join(targetDir, "file-a"), "content from workspace") -} - -func TestImportDir(t *testing.T) { - ctx, workspaceFiler, targetDir := setupWorkspaceImportExportTest(t) - stdout, stderr := testcli.RequireSuccessfulRun(t, ctx, "workspace", "import-dir", "./testdata/import_dir", targetDir, "--log-level=debug") - - expectedLogs := strings.Join([]string{ - "Importing files from " + "./testdata/import_dir", - fmt.Sprintf("%s -> %s", filepath.FromSlash("a/b/c/file-b"), path.Join(targetDir, "a/b/c/file-b")), - fmt.Sprintf("%s -> %s", filepath.FromSlash("file-a"), path.Join(targetDir, "file-a")), - fmt.Sprintf("%s -> %s", filepath.FromSlash("jupyterNotebook.ipynb"), path.Join(targetDir, "jupyterNotebook")), - fmt.Sprintf("%s -> %s", filepath.FromSlash("pyNotebook.py"), path.Join(targetDir, "pyNotebook")), - fmt.Sprintf("%s -> %s", filepath.FromSlash("rNotebook.r"), path.Join(targetDir, "rNotebook")), - fmt.Sprintf("%s -> %s", filepath.FromSlash("scalaNotebook.scala"), path.Join(targetDir, "scalaNotebook")), - fmt.Sprintf("%s -> %s", filepath.FromSlash("sqlNotebook.sql"), path.Join(targetDir, "sqlNotebook")), - "Import complete\n", - }, "\n") - - assert.Equal(t, expectedLogs, stdout.String()) - assert.Empty(t, stderr.String()) - - // Assert files are imported - assertFilerFileContents(t, ctx, workspaceFiler, "file-a", "hello, world") - assertFilerFileContents(t, ctx, workspaceFiler, "a/b/c/file-b", "file-in-dir") - assertFilerFileContents(t, ctx, workspaceFiler, "pyNotebook", "# Databricks notebook source\nprint(\"python\")") - assertFilerFileContents(t, ctx, workspaceFiler, "sqlNotebook", "-- Databricks notebook source\nSELECT \"sql\"") - assertFilerFileContents(t, ctx, workspaceFiler, "rNotebook", "# Databricks notebook source\nprint(\"r\")") - assertFilerFileContents(t, ctx, workspaceFiler, "scalaNotebook", "// Databricks notebook source\nprintln(\"scala\")") - assertFilerFileContents(t, ctx, workspaceFiler, "jupyterNotebook", "# Databricks notebook source\nprint(\"jupyter\")") -} - -func TestImportDirDoesNotOverwrite(t *testing.T) { - ctx, workspaceFiler, targetDir := setupWorkspaceImportExportTest(t) - var err error - - // create preexisting files in the workspace - err = workspaceFiler.Write(ctx, "file-a", strings.NewReader("old file")) - require.NoError(t, err) - err = workspaceFiler.Write(ctx, "pyNotebook.py", strings.NewReader("# Databricks notebook source\nprint(\"old notebook\")")) - require.NoError(t, err) - - // Assert contents of pre existing files - assertFilerFileContents(t, ctx, workspaceFiler, "file-a", "old file") - assertFilerFileContents(t, ctx, workspaceFiler, "pyNotebook", "# Databricks notebook source\nprint(\"old notebook\")") - - testcli.RequireSuccessfulRun(t, ctx, "workspace", "import-dir", "./testdata/import_dir", targetDir) - - // Assert files are imported - assertFilerFileContents(t, ctx, workspaceFiler, "a/b/c/file-b", "file-in-dir") - assertFilerFileContents(t, ctx, workspaceFiler, "sqlNotebook", "-- Databricks notebook source\nSELECT \"sql\"") - assertFilerFileContents(t, ctx, workspaceFiler, "rNotebook", "# Databricks notebook source\nprint(\"r\")") - assertFilerFileContents(t, ctx, workspaceFiler, "scalaNotebook", "// Databricks notebook source\nprintln(\"scala\")") - assertFilerFileContents(t, ctx, workspaceFiler, "jupyterNotebook", "# Databricks notebook source\nprint(\"jupyter\")") - - // Assert pre existing files are not changed - assertFilerFileContents(t, ctx, workspaceFiler, "file-a", "old file") - assertFilerFileContents(t, ctx, workspaceFiler, "pyNotebook", "# Databricks notebook source\nprint(\"old notebook\")") -} - -func TestImportDirWithOverwriteFlag(t *testing.T) { - ctx, workspaceFiler, targetDir := setupWorkspaceImportExportTest(t) - var err error - - // create preexisting files in the workspace - err = workspaceFiler.Write(ctx, "file-a", strings.NewReader("old file")) - require.NoError(t, err) - err = workspaceFiler.Write(ctx, "pyNotebook.py", strings.NewReader("# Databricks notebook source\nprint(\"old notebook\")")) - require.NoError(t, err) - - // Assert contents of pre existing files - assertFilerFileContents(t, ctx, workspaceFiler, "file-a", "old file") - assertFilerFileContents(t, ctx, workspaceFiler, "pyNotebook", "# Databricks notebook source\nprint(\"old notebook\")") - - testcli.RequireSuccessfulRun(t, ctx, "workspace", "import-dir", "./testdata/import_dir", targetDir, "--overwrite") - - // Assert files are imported - assertFilerFileContents(t, ctx, workspaceFiler, "a/b/c/file-b", "file-in-dir") - assertFilerFileContents(t, ctx, workspaceFiler, "sqlNotebook", "-- Databricks notebook source\nSELECT \"sql\"") - assertFilerFileContents(t, ctx, workspaceFiler, "rNotebook", "# Databricks notebook source\nprint(\"r\")") - assertFilerFileContents(t, ctx, workspaceFiler, "scalaNotebook", "// Databricks notebook source\nprintln(\"scala\")") - assertFilerFileContents(t, ctx, workspaceFiler, "jupyterNotebook", "# Databricks notebook source\nprint(\"jupyter\")") - - // Assert pre existing files are overwritten - assertFilerFileContents(t, ctx, workspaceFiler, "file-a", "hello, world") - assertFilerFileContents(t, ctx, workspaceFiler, "pyNotebook", "# Databricks notebook source\nprint(\"python\")") -} - -func TestExport(t *testing.T) { - ctx, f, sourceDir := setupWorkspaceImportExportTest(t) - - var err error - - // Export vanilla file - err = f.Write(ctx, "file-a", strings.NewReader("abc")) - require.NoError(t, err) - stdout, _ := testcli.RequireSuccessfulRun(t, ctx, "workspace", "export", path.Join(sourceDir, "file-a")) - b, err := io.ReadAll(&stdout) - require.NoError(t, err) - assert.Equal(t, "abc", string(b)) - - // Export python notebook - err = f.Write(ctx, "pyNotebook.py", strings.NewReader("# Databricks notebook source\n")) - require.NoError(t, err) - stdout, _ = testcli.RequireSuccessfulRun(t, ctx, "workspace", "export", path.Join(sourceDir, "pyNotebook")) - b, err = io.ReadAll(&stdout) - require.NoError(t, err) - assert.Equal(t, "# Databricks notebook source\n", string(b)) - - // Export python notebook as jupyter - stdout, _ = testcli.RequireSuccessfulRun(t, ctx, "workspace", "export", path.Join(sourceDir, "pyNotebook"), "--format", "JUPYTER") - b, err = io.ReadAll(&stdout) - require.NoError(t, err) - assert.Contains(t, string(b), `"cells":`, "jupyter notebooks contain the cells field") - assert.Contains(t, string(b), `"metadata":`, "jupyter notebooks contain the metadata field") -} - -func TestExportWithFileFlag(t *testing.T) { - ctx, f, sourceDir := setupWorkspaceImportExportTest(t) - localTmpDir := t.TempDir() - - var err error - - // Export vanilla file - err = f.Write(ctx, "file-a", strings.NewReader("abc")) - require.NoError(t, err) - stdout, _ := testcli.RequireSuccessfulRun(t, ctx, "workspace", "export", path.Join(sourceDir, "file-a"), "--file", filepath.Join(localTmpDir, "file.txt")) - b, err := io.ReadAll(&stdout) - require.NoError(t, err) - // Expect nothing to be printed to stdout - assert.Empty(t, string(b)) - assertLocalFileContents(t, filepath.Join(localTmpDir, "file.txt"), "abc") - - // Export python notebook - err = f.Write(ctx, "pyNotebook.py", strings.NewReader("# Databricks notebook source\n")) - require.NoError(t, err) - stdout, _ = testcli.RequireSuccessfulRun(t, ctx, "workspace", "export", path.Join(sourceDir, "pyNotebook"), "--file", filepath.Join(localTmpDir, "pyNb.py")) - b, err = io.ReadAll(&stdout) - require.NoError(t, err) - assert.Empty(t, string(b)) - assertLocalFileContents(t, filepath.Join(localTmpDir, "pyNb.py"), "# Databricks notebook source\n") - - // Export python notebook as jupyter - stdout, _ = testcli.RequireSuccessfulRun(t, ctx, "workspace", "export", path.Join(sourceDir, "pyNotebook"), "--format", "JUPYTER", "--file", filepath.Join(localTmpDir, "jupyterNb.ipynb")) - b, err = io.ReadAll(&stdout) - require.NoError(t, err) - assert.Empty(t, string(b)) - assertLocalFileContents(t, filepath.Join(localTmpDir, "jupyterNb.ipynb"), `"cells":`) - assertLocalFileContents(t, filepath.Join(localTmpDir, "jupyterNb.ipynb"), `"metadata":`) -} - -func TestImportFileUsingContentFormatSource(t *testing.T) { - ctx, workspaceFiler, targetDir := setupWorkspaceImportExportTest(t) - - // Content = `print(1)`. Uploaded as a notebook by default - testcli.RequireSuccessfulRun(t, ctx, "workspace", "import", path.Join(targetDir, "pyScript"), - "--content", base64.StdEncoding.EncodeToString([]byte("print(1)")), "--language=PYTHON") - assertFilerFileContents(t, ctx, workspaceFiler, "pyScript", "print(1)") - assertWorkspaceFileType(t, ctx, workspaceFiler, "pyScript", workspace.ObjectTypeNotebook) - - // Import with content = `# Databricks notebook source\nprint(1)`. Uploaded as a notebook with the content just being print(1) - testcli.RequireSuccessfulRun(t, ctx, "workspace", "import", path.Join(targetDir, "pyNb"), - "--content", base64.StdEncoding.EncodeToString([]byte("`# Databricks notebook source\nprint(1)")), - "--language=PYTHON") - assertFilerFileContents(t, ctx, workspaceFiler, "pyNb", "print(1)") - assertWorkspaceFileType(t, ctx, workspaceFiler, "pyNb", workspace.ObjectTypeNotebook) -} - -func TestImportFileUsingContentFormatAuto(t *testing.T) { - ctx, workspaceFiler, targetDir := setupWorkspaceImportExportTest(t) - - // Content = `# Databricks notebook source\nprint(1)`. Upload as file if path has no extension. - testcli.RequireSuccessfulRun(t, ctx, "workspace", "import", path.Join(targetDir, "py-nb-as-file"), - "--content", base64.StdEncoding.EncodeToString([]byte("`# Databricks notebook source\nprint(1)")), "--format=AUTO") - assertFilerFileContents(t, ctx, workspaceFiler, "py-nb-as-file", "# Databricks notebook source\nprint(1)") - assertWorkspaceFileType(t, ctx, workspaceFiler, "py-nb-as-file", workspace.ObjectTypeFile) - - // Content = `# Databricks notebook source\nprint(1)`. Upload as notebook if path has py extension - testcli.RequireSuccessfulRun(t, ctx, "workspace", "import", path.Join(targetDir, "py-nb-as-notebook.py"), - "--content", base64.StdEncoding.EncodeToString([]byte("`# Databricks notebook source\nprint(1)")), "--format=AUTO") - assertFilerFileContents(t, ctx, workspaceFiler, "py-nb-as-notebook", "# Databricks notebook source\nprint(1)") - assertWorkspaceFileType(t, ctx, workspaceFiler, "py-nb-as-notebook", workspace.ObjectTypeNotebook) - - // Content = `print(1)`. Upload as file if content is not notebook (even if path has .py extension) - testcli.RequireSuccessfulRun(t, ctx, "workspace", "import", path.Join(targetDir, "not-a-notebook.py"), "--content", - base64.StdEncoding.EncodeToString([]byte("print(1)")), "--format=AUTO") - assertFilerFileContents(t, ctx, workspaceFiler, "not-a-notebook.py", "print(1)") - assertWorkspaceFileType(t, ctx, workspaceFiler, "not-a-notebook.py", workspace.ObjectTypeFile) -} - -func TestImportFileFormatSource(t *testing.T) { - ctx, workspaceFiler, targetDir := setupWorkspaceImportExportTest(t) - testcli.RequireSuccessfulRun(t, ctx, "workspace", "import", path.Join(targetDir, "pyNotebook"), "--file", "./testdata/import_dir/pyNotebook.py", "--language=PYTHON") - assertFilerFileContents(t, ctx, workspaceFiler, "pyNotebook", "# Databricks notebook source\nprint(\"python\")") - assertWorkspaceFileType(t, ctx, workspaceFiler, "pyNotebook", workspace.ObjectTypeNotebook) - - testcli.RequireSuccessfulRun(t, ctx, "workspace", "import", path.Join(targetDir, "scalaNotebook"), "--file", "./testdata/import_dir/scalaNotebook.scala", "--language=SCALA") - assertFilerFileContents(t, ctx, workspaceFiler, "scalaNotebook", "// Databricks notebook source\nprintln(\"scala\")") - assertWorkspaceFileType(t, ctx, workspaceFiler, "scalaNotebook", workspace.ObjectTypeNotebook) - - // Upload a non-zip archive without a specifying a format: expect API to throw an error that the uploaded item is not a zip archive - _, _, err := testcli.RequireErrorRun(t, ctx, "workspace", "import", path.Join(targetDir, "scalaNotebook-error"), "--file", "./testdata/import_dir/scalaNotebook.scala") - assert.ErrorContains(t, err, "The zip archive contains no items.") -} - -func TestImportFileFormatAuto(t *testing.T) { - ctx, workspaceFiler, targetDir := setupWorkspaceImportExportTest(t) - - // Upload as file if path has no extension - testcli.RequireSuccessfulRun(t, ctx, "workspace", "import", path.Join(targetDir, "py-nb-as-file"), "--file", "./testdata/import_dir/pyNotebook.py", "--format=AUTO") - assertFilerFileContents(t, ctx, workspaceFiler, "py-nb-as-file", "# Databricks notebook source") - assertFilerFileContents(t, ctx, workspaceFiler, "py-nb-as-file", "print(\"python\")") - assertWorkspaceFileType(t, ctx, workspaceFiler, "py-nb-as-file", workspace.ObjectTypeFile) - - // Upload as notebook if path has extension - testcli.RequireSuccessfulRun(t, ctx, "workspace", "import", path.Join(targetDir, "py-nb-as-notebook.py"), "--file", "./testdata/import_dir/pyNotebook.py", "--format=AUTO") - assertFilerFileContents(t, ctx, workspaceFiler, "py-nb-as-notebook", "# Databricks notebook source\nprint(\"python\")") - assertWorkspaceFileType(t, ctx, workspaceFiler, "py-nb-as-notebook", workspace.ObjectTypeNotebook) - - // Upload as file if content is not notebook (even if path has .py extension) - testcli.RequireSuccessfulRun(t, ctx, "workspace", "import", path.Join(targetDir, "not-a-notebook.py"), "--file", "./testdata/import_dir/file-a", "--format=AUTO") - assertFilerFileContents(t, ctx, workspaceFiler, "not-a-notebook.py", "hello, world") - assertWorkspaceFileType(t, ctx, workspaceFiler, "not-a-notebook.py", workspace.ObjectTypeFile) -}