diff --git a/cli/path.py b/cli/path.py index 6fc2564..0be6053 100644 --- a/cli/path.py +++ b/cli/path.py @@ -90,8 +90,27 @@ def tree(path: str = typer.Argument(..., help="Path to PythonAnywhere file or di @app.command() -def upload(path: str = typer.Argument(..., help="Path to PythonAnywhere file or directory")): - pass +def upload( + path: str = typer.Argument( + ..., + help=( + "Full path of FILE where CONTENTS should be uploaded to -- " + "Warning: if FILE already exists, it's contents will be overwritten" + ) + ), + file: typer.FileBinaryRead = typer.Option( + ..., + "-c", + "--contents", + help="Path to exisitng file or stdin stream that should be uploaded to PATH" + ), +): + path = standarize_path(path) + pa_path = PAPath(path) + + success = pa_path.upload(file) + + sys.exit(0 if success else 1) @app.command() diff --git a/pythonanywhere/api/files_api.py b/pythonanywhere/api/files_api.py index 1a05ae5..3ce903c 100644 --- a/pythonanywhere/api/files_api.py +++ b/pythonanywhere/api/files_api.py @@ -77,15 +77,11 @@ def path_get(self, path): ) def path_post(self, dest_path, content): - """Uploads contents of `source` to `dest_path` which should be + """Uploads contents of `content` to `dest_path` which should be a valid absolute path of a file available to a PythonAnywhere user. If `dest_path` contains directories which don't exist yet, they will be created. - With `as_string` optional keyword set to `True`, method - interprets `source` as string containing file contents, - otherwise `source` is expected to be a valid path to e file. - Returns 200 if existing file on PythonAnywhere has been updated with `source` contents, or 201 if file from `dest_path` has been created with those contents.""" diff --git a/tests/test_cli_path.py b/tests/test_cli_path.py index f0dded6..a4b72f0 100644 --- a/tests/test_cli_path.py +++ b/tests/test_cli_path.py @@ -1,4 +1,5 @@ import getpass +from tempfile import NamedTemporaryFile from textwrap import dedent import pytest @@ -182,3 +183,23 @@ def test_prints_tree_for_empty_directory(self, mock_path, home_dir): """) assert result.stdout == expected + + +class TestUpload: + file = NamedTemporaryFile() + + def test_exits_with_success_when_successful_upload(self, mock_path): + mock_path.return_value.upload.return_value = True + + result = runner.invoke(app, ["upload", "~/hello.txt", "-c", self.file.name]) + + assert mock_path.return_value.upload.called + assert result.exit_code == 0 + + def test_exits_with_error_when_unsuccessful_upload(self, mock_path): + mock_path.return_value.upload.return_value = False + + result = runner.invoke(app, ["upload", "~/hello.txt", "-c", self.file.name]) + + assert mock_path.return_value.upload.called + assert result.exit_code == 1