def test_create_json_output_mypy(repo, mocker): """ Given: - mypy error entries. When: - Running mypy_error_formatter. Then: - Ensure that the JSON error entries are entered as expected. """ mocked_lint_manager = mock_lint_manager(mocker) from demisto_sdk.commands.lint import lint_manager pack = repo.create_pack('Pack') integration = pack.create_integration(name="INT") integration.create_default_integration() mocker.patch.object(lint_manager, 'find_type', return_value=FileType.INTEGRATION) mocker.patch.object(lint_manager, 'get_file_displayed_name', return_value='Display') check = { 'linter': 'mypy', 'pack': 'myPack', 'type': 'error', 'messages': f'{integration.code.path}:280:12: error:' 'Item "None" of "Optional[datetime]" has no attribute "timestamp" [union-attr]\n' f' if incident_created_time.timestamp() > latest_created_time.tim...\n' f' ^\n' f'{integration.code.path}:11:2: note: ' f'See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports\n' f'{integration.code.path}:284:37: error:\n' f'Item "None" of "Optional[datetime]" has no attribute "timestamp" [union-attr]\n' f' if last_fetch.timestamp() < incident_created_time.timestamp():\n' f' ^\n' f'Found 6 errors in 1 file (checked 1 source file)' } json_contents = {} with ChangeCWD(repo.path): mocked_lint_manager.mypy_error_formatter(check, json_contents) expected_format = { f"{integration.code.path}": { 'file-type': 'py', 'entity-type': 'integration', 'display-name': 'Display', 'outputs': [ { 'linter': 'mypy', 'severity': 'error', 'message': 'Item "None" of "Optional[datetime]" has no attribute "timestamp" [union-attr]\n' ' if incident_created_time.timestamp() > latest_created_time.tim...\n' ' ^', 'line-number': "280", 'column-number': "12" }, { 'linter': 'mypy', 'severity': 'error', 'message': 'See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports', 'line-number': "11", 'column-number': "2" }, { 'linter': 'mypy', 'severity': 'error', 'message': 'Item "None" of "Optional[datetime]" has no attribute "timestamp" [union-attr]\n' ' if last_fetch.timestamp() < incident_created_time.timestamp():\n' ' ^', 'line-number': "284", 'column-number': "37" }, ] } } assert json_contents == expected_format
def test_json_output(self, repo): """ Given - Scenario 1: - A ui applicable error. - No pre existing json_outputs file. - Scenario 2: - A non ui applicable warning. - A pre existing json_outputs file. When - Running json_output method. Then - Scenario 1: - Ensure the json outputs file is created and it hold the json error in the `outputs` field. - Scenario 2: - Ensure the json outputs file is modified and holds the json warning in the `outputs` field. """ pack = repo.create_pack('PackName') integration = pack.create_integration('MyInt') integration.create_default_integration() json_path = os.path.join(repo.path, 'valid_json.json') base = BaseValidator(json_file_path=json_path) ui_applicable_error_message, ui_applicable_error_code = Errors.wrong_display_name( 'param1', 'param2') non_ui_applicable_error_message, non_ui_applicable_error_code = Errors.wrong_subtype( ) expected_json_1 = [{ 'filePath': integration.yml.path, 'fileType': 'yml', 'entityType': 'integration', 'errorType': 'Settings', 'name': 'Sample', 'severity': 'error', 'errorCode': ui_applicable_error_code, 'message': ui_applicable_error_message, 'ui': True, 'relatedField': '<parameter-name>.display' }] expected_json_2 = [{ 'filePath': integration.yml.path, 'fileType': 'yml', 'entityType': 'integration', 'errorType': 'Settings', 'name': 'Sample', 'severity': 'error', 'errorCode': ui_applicable_error_code, 'message': ui_applicable_error_message, 'ui': True, 'relatedField': '<parameter-name>.display', 'linter': 'validate' }, { 'filePath': integration.yml.path, 'fileType': 'yml', 'entityType': 'integration', 'errorType': 'Settings', 'name': 'Sample', 'severity': 'warning', 'errorCode': non_ui_applicable_error_code, 'message': non_ui_applicable_error_message, 'ui': False, 'relatedField': 'subtype', 'linter': 'validate' }] with ChangeCWD(repo.path): # create new file base.json_output(integration.yml.path, ui_applicable_error_code, ui_applicable_error_message, False) with open(base.json_file_path) as f: json_output = json.load(f) assert json_output.sort() == expected_json_1.sort() # update existing file base.json_output(integration.yml.path, non_ui_applicable_error_code, non_ui_applicable_error_message, True) with open(base.json_file_path) as f: json_output = json.load(f) assert json_output == expected_json_2
def test_load_user_metadata_basic(repo): """ When: - Dumping a specific pack, processing the pack's metadata. Given: - Pack object. Then: - Verify that pack's metadata information was loaded successfully. """ from demisto_sdk.commands.create_artifacts.content_artifacts_creator import \ ArtifactsManager pack_1 = repo.setup_one_pack('Pack1') pack_1.pack_metadata.write_json({ 'name': 'Pack Number 1', 'description': 'A description for the pack', 'created': '2020-06-08T15:37:54Z', 'price': 0, 'support': 'xsoar', 'url': 'some url', 'email': 'some email', 'currentVersion': '1.1.1', 'author': 'Cortex XSOAR', 'tags': ['tag1'], 'dependencies': [{ 'dependency': { 'dependency': '1' } }] }) with ChangeCWD(repo.path): with temp_dir() as temp: artifact_manager = ArtifactsManager(artifacts_path=temp, content_version='6.0.0', zip=False, suffix='', cpus=1, packs=True) pack_1_metadata = artifact_manager.content.packs['Pack1'].metadata pack_1_metadata.load_user_metadata('Pack1', 'Pack Number 1', pack_1.path, logging_setup(3)) assert pack_1_metadata.id == 'Pack1' assert pack_1_metadata.name == 'Pack Number 1' assert pack_1_metadata.description == 'A description for the pack' assert pack_1_metadata.created == datetime(2020, 6, 8, 15, 37, 54) assert pack_1_metadata.price == 0 assert pack_1_metadata.support == 'xsoar' assert pack_1_metadata.url == 'some url' assert pack_1_metadata.email == 'some email' assert pack_1_metadata.certification == 'certified' assert pack_1_metadata.current_version == parse('1.1.1') assert pack_1_metadata.author == 'Cortex XSOAR' assert pack_1_metadata.tags == ['tag1'] assert pack_1_metadata.dependencies == [{ 'dependency': { 'dependency': '1' } }]