Skip to content

Commit

Permalink
#29 adds path upload cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
caseneuve committed Apr 9, 2021
1 parent b8dd0ca commit 366d8bf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
23 changes: 21 additions & 2 deletions cli/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 1 addition & 5 deletions pythonanywhere/api/files_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cli_path.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import getpass
from tempfile import NamedTemporaryFile
from textwrap import dedent

import pytest
Expand Down Expand Up @@ -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

0 comments on commit 366d8bf

Please sign in to comment.