Skip to content

Commit ca42bfa

Browse files
committed
feat(cli): add delete commands and use them in the tests
1 parent 042740a commit ca42bfa

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

fossology/foss_cli.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def check_get_access_level(level: str):
136136

137137

138138
def needs_later_initialization_of_foss_instance(ctx):
139-
"""Check if lateron a Fossology Instance will be created.
139+
"""Check if later on a Fossology Instance will be created.
140140
141141
:param ctx: click context
142142
:type ctx: click.core.Context
@@ -580,6 +580,7 @@ def upload_file(
580580
file=upload_file,
581581
description=description if description else "upload via foss-cli",
582582
access_level=the_access_level,
583+
wait_time=10,
583584
)
584585

585586
ctx.obj["UPLOAD"] = the_upload
@@ -602,6 +603,56 @@ def upload_file(
602603
)
603604

604605

606+
@cli.command("delete_folder")
607+
@click.argument("folder_name")
608+
@click.pass_context
609+
def delete_folder(
610+
ctx: click.core.Context,
611+
folder_name: str,
612+
):
613+
"""The foss_cli delete_folder command."""
614+
615+
logger.debug(f"Try to delete folder {folder_name}")
616+
foss = ctx.obj["FOSS"]
617+
618+
folder = None
619+
for f in foss.list_folders():
620+
if f.name == folder_name:
621+
folder = f
622+
logger.debug(f"Found folder to delete: {folder}")
623+
break
624+
625+
if not folder:
626+
logger.fatal(f"Unable to find folder {folder_name}.")
627+
ctx.exit(1)
628+
629+
foss.delete_folder(folder)
630+
logger.debug(f"Delete command was send to {foss.host} for folder {folder}")
631+
632+
633+
@cli.command("delete_upload")
634+
@click.argument("upload_name")
635+
@click.pass_context
636+
def delete_upload(
637+
ctx: click.core.Context,
638+
upload_name: str,
639+
):
640+
"""The foss_cli folder_id command."""
641+
642+
logger.debug(f"Try to delete upload {upload_name}")
643+
foss = ctx.obj["FOSS"]
644+
645+
upload = None
646+
for u in foss.list_uploads(all_pages=True)[0]:
647+
if u.uploadname == upload_name:
648+
upload = u
649+
logger.debug(f"Found upload to delete: {upload}")
650+
break
651+
652+
foss.delete_upload(upload)
653+
logger.debug(f"Delete command was send to {foss.host} for upload {upload}")
654+
655+
605656
@cli.command("start_workflow")
606657
@click.argument(
607658
"file_name",
@@ -695,6 +746,7 @@ def start_workflow( # noqa: C901
695746
file=file_name,
696747
description=file_description,
697748
access_level=the_access_level,
749+
wait_time=10,
698750
)
699751
logger.debug(f"Finished upload for {file_name}")
700752

tests/test_foss_cli_create_cmds.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
__doc__ = """Test 'create_*' sub commands of foss_cli"""
22

3+
import time
4+
35
from fossology import foss_cli
46

57

@@ -20,6 +22,19 @@ def test_create_folder(runner, click_test_dict):
2022
)
2123
assert result.exit_code == 0
2224
assert d["VERBOSE"] == 2
25+
time.sleep(2)
26+
# cleanup
27+
result = runner.invoke(
28+
foss_cli.cli,
29+
[
30+
"-vv",
31+
"delete_folder",
32+
"FossFolder",
33+
],
34+
obj=d,
35+
catch_exceptions=False,
36+
)
37+
assert result.exit_code == 0
2338

2439

2540
def test_create_group(runner, click_test_dict):

tests/test_foss_cli_flow_cmds.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
__doc__ = """Test the "workflow" sub commands of foss_cli"""
22
import configparser
33
import os
4+
import time
45
from pathlib import PurePath
56

67
from fossology import foss_cli
@@ -28,6 +29,18 @@ def test_upload_file(runner, click_test_file_path, click_test_file, click_test_d
2829
assert d["DEBUG"]
2930
assert d["UPLOAD"].uploadname == click_test_file
3031
assert "Summary of upload" in result.output
32+
time.sleep(2)
33+
result = runner.invoke(
34+
foss_cli.cli,
35+
[
36+
"-vv",
37+
"delete_upload",
38+
click_test_file,
39+
],
40+
obj=d,
41+
catch_exceptions=False,
42+
)
43+
assert result.exit_code == 0
3144

3245

3346
def test_create_config_file_with_wrong_token_scope_uses_default_read(

tests/test_foss_cli_start_workflow.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_start_workflow_reuse_newest_job(
6161
runner, click_test_file_path, click_test_file, click_test_dict
6262
):
6363
d = click_test_dict
64-
# first upload is the initial one
64+
# first upload is the initial one
6565
# - it uploads
6666
# - it triggers a job on the upload
6767
q_path = PurePath(click_test_file_path, click_test_file)
@@ -109,3 +109,15 @@ def test_start_workflow_reuse_newest_job(
109109
assert "Report downloaded" in result.output
110110
assert "Report written to file: " in result.output
111111
assert result.exit_code == 0
112+
# cleanup
113+
result = runner.invoke(
114+
foss_cli.cli,
115+
[
116+
"-vv",
117+
"delete_upload",
118+
click_test_file,
119+
],
120+
obj=d,
121+
catch_exceptions=False,
122+
)
123+
assert result.exit_code == 0

0 commit comments

Comments
 (0)