Skip to content

Commit 6a78815

Browse files
authored
Make exporting to directory munge target_path (#20)
1 parent 9bc3739 commit 6a78815

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

databricks_cli/configure/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939

4040
def require_config(function):
4141
def decorator(*args, **kwargs):
42+
if kwargs.get('test_mode'):
43+
del kwargs['test_mode']
44+
return function(*args, **kwargs)
4245
config = DatabricksConfig.fetch_from_fs()
4346
if not config.is_valid:
4447
error_and_quit(('You haven\'t configured the CLI yet! '

databricks_cli/workspace/api.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,12 @@ def import_workspace(source_path, target_path, language, fmt, is_overwrite):
110110

111111

112112
def export_workspace(source_path, target_path, fmt, is_overwrite):
113+
"""
114+
Faithfully exports the source_path to the target_path. Does not
115+
attempt to do any munging of the target_path if it is a directory.
116+
"""
113117
if os.path.exists(target_path) and not is_overwrite:
114-
raise LocalFileExistsException()
118+
raise LocalFileExistsException('Target {} already exists.'.format(target_path))
115119
workspace_client = get_workspace_client()
116120
output = workspace_client.export_workspace(source_path, fmt)
117121
content = output['content']

databricks_cli/workspace/cli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ def export_workspace_cli(source_path, target_path, format, overwrite): # NOQA
110110
format is documented at
111111
https://docs.databricks.com/api/latest/workspace.html#notebookexportformat.
112112
"""
113+
if os.path.isdir(target_path):
114+
file_info = get_status(source_path)
115+
if not file_info.is_notebook:
116+
raise RuntimeError('Export can only be called on a notebook.')
117+
extension = WorkspaceLanguage.to_extension(file_info.language)
118+
target_path = os.path.join(target_path, file_info.basename + extension)
113119
export_workspace(source_path, target_path, format, overwrite) # NOQA
114120

115121

tests/configure/test_config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ def test_function(x):
4040
assert test_function(1) == 1
4141

4242

43+
def test_require_config_valid_test_mode():
44+
@config.require_config
45+
def test_function(x):
46+
return x
47+
48+
assert test_function(1, test_mode=True) == 1 # noqa
49+
50+
4351
def test_require_config_invalid():
4452
with mock.patch('databricks_cli.configure.config.DatabricksConfig') as DatabricksConfigMock:
4553
with mock.patch('databricks_cli.configure.config.error_and_quit') as error_and_quit_mock:

tests/workspace/test_cli.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@
2626

2727
import databricks_cli.workspace.cli as cli
2828
import databricks_cli.workspace.api as api
29-
from databricks_cli.workspace.api import WorkspaceFileInfo
30-
from databricks_cli.workspace.types import WorkspaceLanguage
29+
from databricks_cli.workspace.api import WorkspaceFileInfo, NOTEBOOK
30+
from databricks_cli.workspace.types import WorkspaceLanguage, WorkspaceFormat
31+
32+
def test_export_workspace_cli(tmpdir):
33+
path = tmpdir.strpath
34+
with mock.patch('databricks_cli.workspace.cli.get_status') as get_status_mock:
35+
with mock.patch('databricks_cli.workspace.cli.export_workspace') as export_workspace_mock:
36+
get_status_mock.return_value = WorkspaceFileInfo('/notebook-name', NOTEBOOK, WorkspaceLanguage.SCALA)
37+
cli.export_workspace_cli.callback('/notebook-name', path, WorkspaceFormat.SOURCE, False, test_mode=True)
38+
assert export_workspace_mock.call_args[0][1] == os.path.join(path, 'notebook-name.scala')
3139

3240
def test_export_dir_helper(tmpdir):
3341
"""

0 commit comments

Comments
 (0)