Skip to content

Commit f4f0aec

Browse files
tdruezJonoYang
andauthored
Do not sys.exit in execute_project function (#1265)
* Use return instead of sys.exit(0) #1264 Signed-off-by: Jono Yang <jyang@nexb.com> * Add unit test for the execute_project function async option #1264 Signed-off-by: tdruez <tdruez@nexb.com> --------- Signed-off-by: Jono Yang <jyang@nexb.com> Signed-off-by: tdruez <tdruez@nexb.com> Co-authored-by: Jono Yang <jyang@nexb.com>
1 parent 7b97d3b commit f4f0aec

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

scanpipe/management/commands/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
# Visit https://github.com/nexB/scancode.io for support and download.
2222

2323
import shutil
24-
import sys
2524
import traceback
2625
from pathlib import Path
2726

@@ -396,7 +395,7 @@ def execute_project(project, run_async=False, command=None): # noqa: C901
396395
if verbosity > 0:
397396
msg = f"{run.pipeline_name} added to the tasks queue for execution."
398397
command.stdout.write(msg, command.style.SUCCESS)
399-
sys.exit(0)
398+
return
400399

401400
if verbosity > 0:
402401
command.stdout.write(f"Start the {run.pipeline_name} pipeline execution...")

scanpipe/tests/test_commands.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,33 @@ def test_scanpipe_management_command_execute(self):
375375
self.assertTrue(run3.task_stopped)
376376
self.assertEqual("", run3.task_output)
377377

378+
def test_scanpipe_management_command_execute_project_function(self):
379+
project = Project.objects.create(name="my_project")
380+
381+
expected = "No pipelines to run on project my_project"
382+
with self.assertRaisesMessage(CommandError, expected):
383+
commands.execute_project(project)
384+
385+
run1 = project.add_pipeline(self.pipeline_name)
386+
with mock.patch("scanpipe.tasks.execute_pipeline_task", task_success):
387+
returned_value = commands.execute_project(project, run_async=False)
388+
self.assertIsNone(returned_value)
389+
run1.refresh_from_db()
390+
self.assertTrue(run1.task_succeeded)
391+
run1.delete()
392+
393+
project.add_pipeline(self.pipeline_name)
394+
expected = "SCANCODEIO_ASYNC=False is not compatible with --async option."
395+
with override_settings(SCANCODEIO_ASYNC=False):
396+
with self.assertRaisesMessage(CommandError, expected):
397+
commands.execute_project(project, run_async=True)
398+
399+
with override_settings(SCANCODEIO_ASYNC=True):
400+
with mock.patch("scanpipe.models.Run.start") as mock_start:
401+
returned_value = commands.execute_project(project, run_async=True)
402+
mock_start.assert_called_once()
403+
self.assertIsNone(returned_value)
404+
378405
def test_scanpipe_management_command_status(self):
379406
project = Project.objects.create(name="my_project")
380407
run = project.add_pipeline(self.pipeline_name)

0 commit comments

Comments
 (0)