From 879b1dda4d404d59f28d6fedce0f51aa0f5fc15d Mon Sep 17 00:00:00 2001 From: Thomas Druez Date: Thu, 17 Sep 2020 17:28:36 +0400 Subject: [PATCH] Add more unit test for the project management command #13 Signed-off-by: Thomas Druez --- manage.py | 1 + scancodeio/__init__.py | 4 +--- scanpipe/tests/test_commands.py | 35 +++++++++++++++++++++++++++++---- setup.py | 23 ++++++++++++++++++---- 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/manage.py b/manage.py index 4b3f4d9ab..2cf05ef6c 100755 --- a/manage.py +++ b/manage.py @@ -2,4 +2,5 @@ if __name__ == "__main__": from scancodeio import command_line + command_line() diff --git a/scancodeio/__init__.py b/scancodeio/__init__.py index 36b814f8c..83c9d439c 100644 --- a/scancodeio/__init__.py +++ b/scancodeio/__init__.py @@ -21,8 +21,8 @@ # Visit https://github.com/nexB/scancode.io for support and download. import os -from pathlib import Path import sys +from pathlib import Path from django.conf import settings @@ -61,5 +61,3 @@ def command_line(): os.environ.setdefault("DJANGO_SETTINGS_MODULE", "scancodeio.settings") execute_from_command_line(sys.argv) - - diff --git a/scanpipe/tests/test_commands.py b/scanpipe/tests/test_commands.py index 42a918cac..bc05521bf 100644 --- a/scanpipe/tests/test_commands.py +++ b/scanpipe/tests/test_commands.py @@ -43,14 +43,41 @@ def test_scanpipe_management_command_graph(self): self.assertIn("DockerPipeline.png", out_value) self.assertTrue(Path(f"/{temp_dir}/DockerPipeline.png").exists()) - def test_scanpipe_management_command_createproject(self): + def test_scanpipe_management_command_createproject_base(self): out = StringIO() - with self.assertRaises(CommandError) as error: - call_command("createproject", stderr=out) expected = "Error: the following arguments are required: name" - self.assertEqual(expected, str(error.exception)) + with self.assertRaisesMessage(CommandError, expected): + call_command("createproject", stdout=out) call_command("createproject", "my_project", stdout=out) self.assertIn("Project my_project created", out.getvalue()) self.assertTrue(Project.objects.get(name="my_project")) + + with self.assertRaises(SystemExit): + call_command("createproject", "my_project", stderr=out) + expected = "Project with this Name already exists." + self.assertIn(expected, out.getvalue()) + + def test_scanpipe_management_command_createproject_with_pipelines(self): + out = StringIO() + + options = ["--pipeline", "non-existing.py"] + with self.assertRaises(SystemExit): + call_command("createproject", "my_project", *options, stderr=out) + self.assertIn("non-existing.py is not a valid pipeline", out.getvalue()) + + options = [ + "--pipeline", + "scanpipe/pipelines/docker.py", + "--pipeline", + "scanpipe/pipelines/root_filesystems.py", + ] + call_command("createproject", "my_project", *options, stdout=out) + self.assertIn("Project my_project created", out.getvalue()) + project = Project.objects.get(name="my_project") + expected = [ + "scanpipe/pipelines/docker.py", + "scanpipe/pipelines/root_filesystems.py", + ] + self.assertEqual(expected, [run.pipeline for run in project.runs.all()]) diff --git a/setup.py b/setup.py index b6d6ad1a0..221893792 100755 --- a/setup.py +++ b/setup.py @@ -74,9 +74,24 @@ "Topic :: Utilities", ], keywords=[ - "open source", "scan", "license", "package", "dependency", - "copyright", "filetype", "author", "extract", "licensing", - "scancode", "scanpipe", "docker", "rootfs", "vm", "virtual machine", - "pipeline", "code analysis", "container", + "open source", + "scan", + "license", + "package", + "dependency", + "copyright", + "filetype", + "author", + "extract", + "licensing", + "scancode", + "scanpipe", + "docker", + "rootfs", + "vm", + "virtual machine", + "pipeline", + "code analysis", + "container", ], )