def copy_input_from(self, input_location): """ Copy the file at `input_location` to this project input/ directory. """ from scanpipe.pipes.input import copy_inputs copy_inputs([input_location], self.input_path)
def test_scanpipe_project_model_add_downloads(self): file_location = self.data_location / "notice.NOTICE" copy_inputs([file_location], self.project1.tmp_path) download = Download( uri="https://example.com/filename.zip", directory="", filename="notice.NOTICE", path=self.project1.tmp_path / "notice.NOTICE", size="", sha1="", md5="", ) self.project1.add_downloads([download]) inputs, missing_inputs = self.project1.inputs_with_source expected = [{ "is_file": True, "name": "notice.NOTICE", "size": 1178, "source": "https://example.com/filename.zip", }] self.assertEqual(expected, inputs) self.assertEqual({}, missing_inputs)
def test_scanpipe_pipes_make_codebase_resource(self): p1 = Project.objects.create(name="Analysis") resource_location = str(self.data_location / "notice.NOTICE") with self.assertRaises(AssertionError) as cm: make_codebase_resource(p1, resource_location) self.assertIn("is not under project/codebase/", str(cm.exception)) copy_inputs([resource_location], p1.codebase_path) resource_location = str(p1.codebase_path / "notice.NOTICE") make_codebase_resource(p1, resource_location) resource = p1.codebaseresources.get() self.assertEqual(1178, resource.size) self.assertEqual("4bd631df28995c332bf69d9d4f0f74d7ee089598", resource.sha1) self.assertEqual("90cd416fd24df31f608249b77bae80f1", resource.md5) self.assertEqual("text/plain", resource.mime_type) self.assertEqual("ASCII text", resource.file_type) self.assertEqual("", resource.status) self.assertEqual(CodebaseResource.Type.FILE, resource.type) # Duplicated path: skip the creation and no project error added make_codebase_resource(p1, resource_location) self.assertEqual(1, p1.codebaseresources.count()) self.assertEqual(0, p1.projecterrors.count())
def test_scanpipe_pipes_scancode_scan_package_and_save_results_timeout_error( self): project1 = Project.objects.create(name="Analysis") copy_inputs([self.data_location / "notice.NOTICE"], project1.codebase_path) codebase_resource = CodebaseResource.objects.create( project=project1, path="notice.NOTICE") with mock.patch("scancode.api.get_package_info") as get_package_info: get_package_info.side_effect = InterruptTimeoutError results, errors = scancode.scan_for_package_info( codebase_resource.location) scancode.save_scan_package_results(codebase_resource, results, errors) codebase_resource.refresh_from_db() self.assertEqual("scanned-with-error", codebase_resource.status) self.assertEqual(1, project1.projecterrors.count()) error = project1.projecterrors.latest("created_date") self.assertEqual("CodebaseResource", error.model) self.assertEqual("", error.traceback) expected_message = ( "ERROR: for scanner: packages:\n" "ERROR: Processing interrupted: timeout after 120 seconds.") self.assertEqual(expected_message, error.message)
def test_scanpipe_pipes_scancode_scan_file_and_save_results(self): project1 = Project.objects.create(name="Analysis") codebase_resource1 = CodebaseResource.objects.create( project=project1, path="not available") self.assertEqual(0, project1.projecterrors.count()) scan_results, scan_errors = scancode.scan_file( codebase_resource1.location) scancode.save_scan_file_results(codebase_resource1, scan_results, scan_errors) codebase_resource1.refresh_from_db() self.assertEqual("scanned-with-error", codebase_resource1.status) self.assertEqual(4, project1.projecterrors.count()) copy_inputs([self.data_location / "notice.NOTICE"], project1.codebase_path) codebase_resource2 = CodebaseResource.objects.create( project=project1, path="notice.NOTICE") scan_results, scan_errors = scancode.scan_file( codebase_resource2.location) scancode.save_scan_file_results(codebase_resource2, scan_results, scan_errors) codebase_resource2.refresh_from_db() self.assertEqual("scanned", codebase_resource2.status) expected = [ "apache-2.0", "apache-2.0 AND scancode-acknowledgment", "apache-2.0", "apache-2.0", ] self.assertEqual(expected, codebase_resource2.license_expressions)
def copy_inputs_to_codebase_directory(self): """ Copy input files to the project codebase/ directory. The code can also be copied there prior to running the Pipeline. """ copy_inputs(self.project.inputs(), self.project.codebase_path)