def test_handle_error(): """ Given - An ignore errors list associated with a file. - An error, message, code and file paths. When - Running handle_error method. Then - Ensure the resulting error messages are correctly formatted. - Ensure ignored error codes return None. """ base_validator = BaseValidator(ignored_errors={"file_name": ["BA101"]}) formatted_error = base_validator.handle_error("Error-message", "SC102", "PATH") assert formatted_error == 'PATH: [SC102] - Error-message\n' formatted_error = base_validator.handle_error("another-error-message", "IN101", "path/to/file_name") assert formatted_error == 'path/to/file_name: [IN101] - another-error-message\n' formatted_error = base_validator.handle_error("ignore-file-specific", "BA101", "path/to/file_name") assert formatted_error is None
def test_handle_error_file_with_path(pack): """ Given - An ignore errors list associated with a file_path. - An error, message, code and file paths. When - Running handle_error method. Then _ Ensure ignoring right file when full path mentioned in .pack-ignore. - Ensure the resulting error messages are correctly formatted. - Ensure ignored error codes which can be ignored return None. - Ensure non ignored errors are in FOUND_FILES_AND_ERRORS list. - Ensure ignored error are not in FOUND_FILES_AND_ERRORS and in FOUND_FILES_AND_IGNORED_ERRORS """ integration = pack.create_integration("TestIntegration") rel_path_integration_readme = integration.readme.path[integration.readme. path.find("Packs"):] rel_path_pack_readme = pack.readme.path[pack.readme.path.find("Packs"):] pack_ignore_text = f"""[file:{rel_path_integration_readme}] ignore=ST109 [file:{rel_path_pack_readme}] ignore=BA101""" pack.pack_ignore.write_text(pack_ignore_text) base_validator = BaseValidator(ignored_errors={ rel_path_pack_readme: ["BA101"], rel_path_integration_readme: ["PA113"] }, print_as_warnings=True) formatted_error = base_validator.handle_error("Error-message", "BA101", integration.readme.path) assert formatted_error == f'[ERROR]: {integration.readme.path}: [BA101] - Error-message\n' assert f'{integration.readme.path} - [BA101]' in FOUND_FILES_AND_ERRORS formatted_error = base_validator.handle_error("Error-message", "PA113", integration.readme.path) assert formatted_error is None assert f'{integration.readme.path} - [PA113]' not in FOUND_FILES_AND_ERRORS assert f'{integration.readme.path} - [PA113]' in FOUND_FILES_AND_IGNORED_ERRORS formatted_error = base_validator.handle_error("Error-message", "PA113", pack.readme.path) assert formatted_error == f'[ERROR]: {pack.readme.path}: [PA113] - Error-message\n' assert f'{pack.readme.path} - [PA113]' in FOUND_FILES_AND_ERRORS formatted_error = base_validator.handle_error("Error-message", "BA101", pack.readme.path) assert formatted_error is None assert f'{pack.readme.path} - [BA101]' not in FOUND_FILES_AND_ERRORS assert f'{pack.readme.path} - [BA101]' in FOUND_FILES_AND_IGNORED_ERRORS
def test_handle_error_on_unignorable_error_codes(mocker, ignored_errors, error_code): """ Given - error code which is not allowed to be ignored. - error codes/prefix error codes as the ignored errors from the pack-ignore file. When - Running handle_error method Then - Ensure that the correct error message is returned - Ensure that the correct error message is printed out. - Ensure that the un-ignorable errors are in FOUND_FILES_AND_ERRORS list. - Ensure that the un-ignorable errors are not in FOUND_FILES_AND_IGNORED_ERRORS list. """ import click base_validator = BaseValidator(ignored_errors=ignored_errors) expected_error = f'[ERROR]: file_name: [{error_code}] can not be ignored in .pack-ignore\n' click_mock = mocker.patch.object(click, 'secho') result = base_validator.handle_error(error_message='', error_code=error_code, file_path='file_name', suggested_fix='fix') assert result == expected_error assert click_mock.called assert click_mock.call_args.args[0] == expected_error assert f'file_name - [{error_code}]' in FOUND_FILES_AND_ERRORS assert f'file_name - [{error_code}]' not in FOUND_FILES_AND_IGNORED_ERRORS
def test_handle_error(mocker): """ Given - An ignore errors list associated with a file. - An error, message, code and file paths. When - Running handle_error method. Then - Ensure the resulting error messages are correctly formatted. - Ensure ignored error codes return None. - Ensure non ignored errors are in FOUND_FILES_AND_ERRORS list. - Ensure ignored error are not in FOUND_FILES_AND_ERRORS and in FOUND_FILES_AND_IGNORED_ERRORS """ import click base_validator = BaseValidator(ignored_errors={"file_name": ["BA101"]}, print_as_warnings=True) # passing the flag checks - checked separately base_validator.checked_files.union({'PATH', "file_name"}) formatted_error = base_validator.handle_error("Error-message", "SC102", "PATH") assert formatted_error == '[ERROR]: PATH: [SC102] - Error-message\n' assert 'PATH - [SC102]' in FOUND_FILES_AND_ERRORS formatted_error = base_validator.handle_error("another-error-message", "IN101", "path/to/file_name") assert formatted_error == '[ERROR]: path/to/file_name: [IN101] - another-error-message\n' assert 'path/to/file_name - [IN101]' in FOUND_FILES_AND_ERRORS click_mock = mocker.patch.object(click, 'secho') formatted_error = base_validator.handle_error("ignore-file-specific", "BA101", "path/to/file_name") assert formatted_error is None assert 'path/to/file_name - [BA101]' not in FOUND_FILES_AND_ERRORS assert 'path/to/file_name - [BA101]' in FOUND_FILES_AND_IGNORED_ERRORS assert click_mock.call_args_list[0][0][ 0] == "[WARNING]: path/to/file_name: [BA101] - ignore-file-specific\n" formatted_error = base_validator.handle_error("Error-message", "ST109", "path/to/file_name") assert formatted_error == '[ERROR]: path/to/file_name: [ST109] - Error-message\n' assert 'path/to/file_name - [ST109]' in FOUND_FILES_AND_ERRORS
def get_modified_and_added_files(compare_type, prev_ver, ignored_errors=dict(), no_configuration_prints=False, staged=False, print_ignored_files=False, is_circle=False, branch_name=None): """Get the modified and added files from a specific branch Args: is_circle (bool): Whether the code runs on circle build. print_ignored_files (bool): Whether to print ignored files. staged (bool): Whether to return only staged files no_configuration_prints (bool): Whether to print additional config prints ignored_errors (dict): A dict of ignored errors per file. branch_name (str): the branch name compare_type (str): whether to run diff with two dots (..) or three (...) prev_ver (str): Against which branch to run the comparision - master/last release Returns: tuple. 3 sets representing modified files, added files and files of old format who have changed. """ if not branch_name: branch_name = get_current_working_branch() base_validator = BaseValidator(ignored_errors=ignored_errors) if not no_configuration_prints: if staged: click.echo("Collecting staged files only") else: click.echo("Collecting all committed files") prev_ver = add_origin(branch_name, prev_ver) # all committed changes of the current branch vs the prev_ver all_committed_files_string = run_command( f'git diff --name-status {prev_ver}{compare_type}refs/heads/{branch_name}' ) modified_files, added_files, _, old_format_files, changed_meta_files, ignored_files, new_packs = \ filter_changed_files(all_committed_files_string, prev_ver, print_ignored_files=print_ignored_files) if not is_circle: remote_configured = has_remote_configured() is_origin_demisto = is_origin_content_repo() if remote_configured and not is_origin_demisto: if not no_configuration_prints: click.echo( "Collecting all local changed files from fork against the content master" ) # only changes against prev_ver (without local changes) all_changed_files_string = run_command( 'git diff --name-status upstream/master...HEAD') modified_files_from_tag, added_files_from_tag, _, _, changed_meta_files_from_tag, \ ignored_files_from_tag, new_packs_from_tag = \ filter_changed_files(all_changed_files_string, print_ignored_files=print_ignored_files) # all local non-committed changes and changes against prev_ver outer_changes_files_string = run_command( 'git diff --name-status --no-merges upstream/master...HEAD') nc_modified_files, nc_added_files, nc_deleted_files, nc_old_format_files, nc_changed_meta_files, \ nc_ignored_files, nc_new_packs = \ filter_changed_files(outer_changes_files_string, print_ignored_files=print_ignored_files) else: if (not is_origin_demisto and not remote_configured) and not no_configuration_prints: error_message, error_code = Errors.changes_may_fail_validation( ) base_validator.handle_error(error_message, error_code, file_path="General-Error", warning=True, drop_line=True) if not no_configuration_prints and not staged: click.echo( "Collecting all local changed files against the content master" ) # only changes against prev_ver (without local changes) all_changed_files_string = run_command( 'git diff --name-status {}'.format(prev_ver)) modified_files_from_tag, added_files_from_tag, _, _, changed_meta_files_from_tag, \ ignored_files_from_tag, new_packs_from_tag = \ filter_changed_files(all_changed_files_string, print_ignored_files=print_ignored_files) # all local non-committed changes and changes against prev_ver outer_changes_files_string = run_command( 'git diff --name-status --no-merges HEAD') nc_modified_files, nc_added_files, nc_deleted_files, nc_old_format_files, nc_changed_meta_files, \ nc_ignored_files, nc_new_packs = \ filter_changed_files(outer_changes_files_string, print_ignored_files=print_ignored_files) old_format_files = old_format_files.union(nc_old_format_files) modified_files = modified_files.union( modified_files_from_tag.intersection(nc_modified_files)) added_files = added_files.union( added_files_from_tag.intersection(nc_added_files)) changed_meta_files = changed_meta_files.union( changed_meta_files_from_tag.intersection(nc_changed_meta_files)) ignored_files = ignored_files.union( ignored_files_from_tag.intersection(nc_ignored_files)) new_packs = new_packs.union( new_packs_from_tag.intersection(nc_new_packs)) modified_files = modified_files - set(nc_deleted_files) added_files = added_files - set(nc_deleted_files) changed_meta_files = changed_meta_files - set(nc_deleted_files) if staged: modified_files, added_files, old_format_files, changed_meta_files = \ filter_staged_only(modified_files, added_files, old_format_files, changed_meta_files) modified_packs = get_packs(modified_files).union( get_packs(old_format_files)).union(get_packs(added_files)) return modified_files, added_files, old_format_files, changed_meta_files, \ modified_packs, ignored_files, new_packs