Skip to content

Commit 7e8133c

Browse files
caseneuvefiliplajszczak
authored andcommitted
#29 adds path share and unshare commands
1 parent 90563a1 commit 7e8133c

File tree

2 files changed

+95
-4
lines changed

2 files changed

+95
-4
lines changed

cli/path.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,25 @@ def delete(
126126

127127

128128
@app.command()
129-
def share(path: str = typer.Argument(..., help="Path to PythonAnywhere file or directory")):
130-
pass
129+
def share(
130+
path: str = typer.Argument(..., help="Path to PythonAnywhere file to be shared"),
131+
check: bool = typer.Option(False, "-c", "--check", help="Check sharing status")
132+
):
133+
path = standarize_path(path)
134+
pa_path = PAPath(path)
135+
136+
link = pa_path.get_sharing_url() if check else pa_path.share()
137+
138+
if not link:
139+
sys.exit(1)
140+
typer.echo(link)
131141

132142

133143
@app.command()
134-
def unshare(path: str = typer.Argument(..., help="Path to PythonAnywhere file or directory")):
135-
pass
144+
def unshare(path: str = typer.Argument(..., help="Path to PythonAnywhere file to be unshared")):
145+
path = standarize_path(path)
146+
pa_path = PAPath(path)
147+
148+
success = pa_path.unshare()
149+
150+
sys.exit(0 if success else 1)

tests/test_cli_path.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ def test_prints_tree_for_empty_directory(self, mock_path, home_dir):
188188
class TestUpload:
189189
file = NamedTemporaryFile()
190190

191+
def test_creates_pa_path_with_provided_path(self, mock_path, home_dir):
192+
runner.invoke(app, ["upload", "~/hello.txt", "-c", self.file.name])
193+
mock_path.assert_called_once_with(f"{home_dir}/hello.txt")
194+
191195
def test_exits_with_success_when_successful_upload(self, mock_path):
192196
mock_path.return_value.upload.return_value = True
193197

@@ -206,6 +210,10 @@ def test_exits_with_error_when_unsuccessful_upload(self, mock_path):
206210

207211

208212
class TestDelete:
213+
def test_creates_pa_path_with_provided_path(self, mock_path, home_dir):
214+
runner.invoke(app, ["delete", "~/hello.txt"])
215+
mock_path.assert_called_once_with(f"{home_dir}/hello.txt")
216+
209217
def test_exits_with_success_when_successful_delete(self, mock_path):
210218
mock_path.return_value.delete.return_value = True
211219

@@ -221,3 +229,71 @@ def test_exits_with_error_when_unsuccessful_delete(self, mock_path):
221229

222230
assert mock_path.return_value.delete.called
223231
assert result.exit_code == 1
232+
233+
234+
class TestShare:
235+
def test_creates_pa_path_with_provided_path(self, mock_path, home_dir):
236+
runner.invoke(app, ["share", "~/hello.txt"])
237+
mock_path.assert_called_once_with(f"{home_dir}/hello.txt")
238+
239+
def test_exits_with_success_and_prints_sharing_url_when_successful_share(self, mock_path):
240+
mock_path.return_value.share.return_value = "link"
241+
242+
result = runner.invoke(app, ["share", "~/hello.txt"])
243+
244+
assert "link" in result.stdout
245+
assert mock_path.return_value.share.called
246+
assert not mock_path.return_value.get_sharing_url.called
247+
assert result.exit_code == 0
248+
249+
def test_exits_with_error_when_unsuccessful_share(self, mock_path):
250+
mock_path.return_value.share.return_value = ""
251+
252+
result = runner.invoke(app, ["share", "~/hello.txt"])
253+
254+
assert mock_path.return_value.share.called
255+
assert not mock_path.return_value.get_sharing_url.called
256+
assert result.stdout == ""
257+
assert result.exit_code == 1
258+
259+
def test_exits_with_success_and_prints_sharing_url_when_path_already_shared(self, mock_path):
260+
mock_path.return_value.get_sharing_url.return_value = "link"
261+
262+
result = runner.invoke(app, ["share", "--check", "~/hello.txt"])
263+
264+
assert mock_path.return_value.get_sharing_url.called
265+
assert not mock_path.return_value.share.called
266+
assert "link" in result.stdout
267+
assert result.exit_code == 0
268+
269+
def test_exits_with_error_when_path_was_not_shared(self, mock_path):
270+
mock_path.return_value.get_sharing_url.return_value = ""
271+
272+
result = runner.invoke(app, ["share", "--check", "~/hello.txt"])
273+
274+
assert mock_path.return_value.get_sharing_url.called
275+
assert not mock_path.return_value.share.called
276+
assert result.stdout == ""
277+
assert result.exit_code == 1
278+
279+
280+
class TestUnshare:
281+
def test_creates_pa_path_with_provided_path(self, mock_path, home_dir):
282+
runner.invoke(app, ["unshare", "~/hello.txt"])
283+
mock_path.assert_called_once_with(f"{home_dir}/hello.txt")
284+
285+
def test_exits_with_success_when_successful_unshare_or_file_not_shared(self, mock_path):
286+
mock_path.return_value.unshare.return_value = True
287+
288+
result = runner.invoke(app, ["unshare", "~/hello.txt"])
289+
290+
assert mock_path.return_value.unshare.called
291+
assert result.exit_code == 0
292+
293+
def test_exits_with_error_when_unsuccessful_unshare(self, mock_path):
294+
mock_path.return_value.unshare.return_value = False
295+
296+
result = runner.invoke(app, ["unshare", "~/hello.txt"])
297+
298+
assert mock_path.return_value.unshare.called
299+
assert result.exit_code == 1

0 commit comments

Comments
 (0)