Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions docs/source/add_validators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,20 @@ Consider the example:
validate(
'/Users/project',
error_validator_groups=validator_groups,
warning_validators_groups=defaults.WARNING_VALIDATOR_GROUPS
post_error_validator_groups=defaults.POST_ERROR_VALIDATOR_GROUPS
)
)

As you can see, we simply copy the default validators structure, modify it to suit our needs and pass to the ``validate`` method.

The minor issue is that since we pass our own error validators, the default warning validators have to be restored by hand.
The minor issue is that since we pass our own error validators, the default post error validators have to be restored by hand.
We did so by passing them as an argument too.

The intricacies
~~~~~~~~~~~~~~~

The are two kinds of validators: error validators and warning validators.
The difference between them is that warning validators don't halt the validation process, while the error validators do.
The are two kinds of validators: error validators and post error validators.
The difference between them is that post error validators don't halt the validation process, while the error validators do.
The error validators are expected to be grouped according to their purpose, like so::

ERROR_VALIDATOR_GROUPS = OrderedDict(
Expand Down Expand Up @@ -250,10 +250,10 @@ because the order in which the validator groups run matters.
In each group, every single validator is executed.
If one of the validators in the group fails, the ``validate`` method executes the rest of the group and then
returns the error list without proceeding to the next group.
If all the validators in the error group succeed, the warning validators for this group are executed.
Here's the structure of the warnings validators::
If all the validators in the error group succeed, the post error validators for this group are executed.
Here's the structure of the post error validators::

WARNING_VALIDATOR_GROUPS = {
POST_ERROR_VALIDATOR_GROUPS = {
'commits': [
validators.commit_message_from_blacklist,
],
Expand All @@ -263,7 +263,7 @@ Here's the structure of the warnings validators::
]
}

The ``commits`` warning validator group is executed only if the ``commits`` error validator group passes successfully.
The ``commits`` post error validator group is executed only if the ``commits`` error validator group passes successfully.

Warning validators are not executed if none of the error validators are failed.
They just add more error messages in case the validation fails.
Expand Down
16 changes: 8 additions & 8 deletions fiasko_bro/code_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ def _construct_validator_arguments(project_path, **kwargs):
def determine_validators(**kwargs):
pre_validation_checks = kwargs.pop('pre_validation_checks', None) or defaults.PRE_VALIDATION_CHECKS
error_validator_groups = kwargs.pop('error_validator_groups', None)
warning_validator_groups = kwargs.pop('warning_validator_groups', None) or {}
post_error_validator_group = kwargs.pop('post_error_validator_groups', None) or {}
if not error_validator_groups:
error_validator_groups = defaults.ERROR_VALIDATOR_GROUPS
# use default warning groups only with default error groups
if not warning_validator_groups:
warning_validator_groups = defaults.WARNING_VALIDATOR_GROUPS
return pre_validation_checks, error_validator_groups, warning_validator_groups
if not post_error_validator_group:
post_error_validator_group = defaults.POST_ERROR_VALIDATOR_GROUPS
return pre_validation_checks, error_validator_groups, post_error_validator_group


def validate(project_path, original_project_path=None, **kwargs):
ensure_repo_tokens_mutually_exclusive(**kwargs)
pre_validation_checks, error_validator_groups, warning_validator_groups = determine_validators(**kwargs)
pre_validation_checks, error_validator_groups, post_error_validator_groups = determine_validators(**kwargs)
validator_arguments = _construct_validator_arguments(
project_path,
original_project_path=original_project_path,
Expand All @@ -88,7 +88,7 @@ def validate(project_path, original_project_path=None, **kwargs):
return run_validator_group(
validator_group=error_validator_groups,
validator_arguments=validator_arguments,
post_error_validator_group=warning_validator_groups
post_error_validator_group=post_error_validator_groups
)


Expand All @@ -98,11 +98,11 @@ def traverse_validator_groups(validator_groups, func):
func(validator)


def get_error_slugs(pre_validation_checks=None, error_validator_groups=None, warning_validator_groups=None):
def get_error_slugs(pre_validation_checks=None, error_validator_groups=None, post_error_validator_groups=None):
validators = determine_validators(
pre_validation_checks=pre_validation_checks,
error_validator_groups=error_validator_groups,
warning_validator_groups=warning_validator_groups
post_error_validator_groups=post_error_validator_groups
)
error_slugs = set()
for validator_groups in validators:
Expand Down
12 changes: 6 additions & 6 deletions fiasko_bro/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
{
'readme_filename': 'README.md',
'allowed_max_pep8_violations': 5,
'max_complexity': 7,
'minimum_name_length': 2,
'min_percent_of_another_language': 30,
'max_mccabe_complexity': 7,
'min_variable_name_length': 2,
'min_percent_of_another_language_in_readme': 30,
'last_commits_to_check_amount': 5,
'tab_size': 4,
'functions_with_docstrings_percent_limit': 80,
'max_pep8_line_length': 100,
'max_number_of_lines': 200,
'max_indentation_level': 4,
'max_num_of_py_files': 100,
'max_num_of_py_files_in_project': 100,
'directories_to_skip': frozenset(
[
'build',
Expand Down Expand Up @@ -268,7 +268,7 @@
)
)

WARNING_VALIDATOR_GROUPS = MappingProxyType(
POST_ERROR_VALIDATOR_GROUPS = MappingProxyType(
{
'commits': (
validators.commit_messages_from_blacklist,
Expand All @@ -280,5 +280,5 @@
}
)

for name in WARNING_VALIDATOR_GROUPS:
for name in POST_ERROR_VALIDATOR_GROUPS:
assert name in ERROR_VALIDATOR_GROUPS.keys()
6 changes: 3 additions & 3 deletions fiasko_bro/pre_validation_checks/repo_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
def repo_is_too_large(
project_path,
directories_to_skip,
max_num_of_py_files,
max_num_of_py_files_in_project,
original_project_path=None,
*args,
**kwargs
):
if code_helpers.is_repo_too_large(project_path, directories_to_skip, max_num_of_py_files):
if code_helpers.is_repo_too_large(project_path, directories_to_skip, max_num_of_py_files_in_project):
return ''
if original_project_path:
if code_helpers.is_repo_too_large(original_project_path, directories_to_skip, max_num_of_py_files):
if code_helpers.is_repo_too_large(original_project_path, directories_to_skip, max_num_of_py_files_in_project):
return ''


Expand Down
6 changes: 3 additions & 3 deletions fiasko_bro/utils/configparser_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ def _process_section_to_dict_config(config_section):
config_processors = {
'readme_filename': lambda x: x,
'allowed_max_pep8_violations': int,
'max_complexity': int,
'max_mccabe_complexity': int,
'minimum_name_length': int,
'min_percent_of_another_language': int,
'min_percent_of_another_language_in_readme': int,
'last_commits_to_check_amount': int,
'tab_size': int,
'functions_with_docstrings_percent_limit': int,
'max_pep8_line_length': int,
'max_number_of_lines': int,
'max_indentation_level': int,
'max_num_of_py_files': int,
'max_num_of_py_files_in_project': int,
'directories_to_skip': lambda x: x.split(',')
}
result_config = {}
Expand Down
4 changes: 2 additions & 2 deletions fiasko_bro/validators/code_inclusion.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from ..utils import code_helpers


def too_difficult_by_mccabe(project_folder, max_complexity, *args, **kwargs):
def too_difficult_by_mccabe(project_folder, max_mccabe_complexity, *args, **kwargs):
violations = []
for parsed_file in project_folder.get_parsed_py_files():
violations += code_helpers.get_mccabe_violations_for_file(parsed_file.path, max_complexity)
violations += code_helpers.get_mccabe_violations_for_file(parsed_file.path, max_mccabe_complexity)
if violations:
return ','.join(violations)

Expand Down
4 changes: 2 additions & 2 deletions fiasko_bro/validators/naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def has_local_var_named_as_global(project_folder, local_var_named_as_global_path
return message


def short_variable_name(project_folder, minimum_name_length, valid_short_variable_names, *args, **kwargs):
def short_variable_name(project_folder, min_variable_name_length, valid_short_variable_names, *args, **kwargs):
short_names = []
for parsed_file in project_folder.get_parsed_py_files():
names = ast_helpers.get_all_defined_names(parsed_file.ast_tree)
short_names += [n for n in names
if len(n) < minimum_name_length and n not in valid_short_variable_names]
if len(n) < min_variable_name_length and n not in valid_short_variable_names]
if short_names:
return ', '.join(list(set(short_names)))

Expand Down
4 changes: 2 additions & 2 deletions fiasko_bro/validators/readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def readme_not_changed(project_folder, readme_filename, original_project_folder=
return ''


def bilingual_readme(project_folder, readme_filename, min_percent_of_another_language, *args, **kwargs):
def bilingual_readme(project_folder, readme_filename, min_percent_of_another_language_in_readme, *args, **kwargs):
raw_readme = project_folder.get_file(readme_filename)
readme_no_code = re.sub(r'\s```[#!A-Za-z]*\n[\s\S]*?\n```\s', '', raw_readme)
clean_readme = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', '', readme_no_code)
Expand All @@ -35,5 +35,5 @@ def bilingual_readme(project_folder, readme_filename, min_percent_of_another_lan
return
another_language_percent = min([ru_letters_amount, en_letters_amount]) * 100
another_language_percent /= (ru_letters_amount + en_letters_amount)
if another_language_percent > min_percent_of_another_language:
if another_language_percent > min_percent_of_another_language_in_readme:
return ''
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

def test_short_variable_name_fail(test_repo):
expected_output = 'sv'
minimum_name_length = 3
min_variable_name_length = 3
output = validators.short_variable_name(
project_folder=test_repo,
valid_short_variable_names=defaults.VALIDATION_PARAMETERS['valid_short_variable_names'],
minimum_name_length=minimum_name_length,
min_variable_name_length=min_variable_name_length,
)
assert output == expected_output


def test_short_variable_name_ok(test_repo):
minimum_name_length = 3
min_variable_name_length = 3
output = validators.short_variable_name(
project_folder=test_repo,
valid_short_variable_names=['sv'],
minimum_name_length=minimum_name_length,
min_variable_name_length=min_variable_name_length,
)
assert output is None
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
def test_bilingual_readme_succeeds(test_repo):
readme_filename = 'readme_in_single_language.md'
min_percent = defaults.VALIDATION_PARAMETERS[
'min_percent_of_another_language'
'min_percent_of_another_language_in_readme'
]
output = validators.bilingual_readme(
project_folder=test_repo,
readme_filename=readme_filename,
min_percent_of_another_language=min_percent,
min_percent_of_another_language_in_readme=min_percent,
)
assert output is None

Expand All @@ -19,11 +19,11 @@ def test_bilingual_readme_fails(test_repo):
readme_filename = 'bilingual_readme.md'
expected_output = ''
min_percent = defaults.VALIDATION_PARAMETERS[
'min_percent_of_another_language'
'min_percent_of_another_language_in_readme'
]
output = validators.bilingual_readme(
project_folder=test_repo,
readme_filename=readme_filename,
min_percent_of_another_language=min_percent,
min_percent_of_another_language_in_readme=min_percent,
)
assert output == expected_output
2 changes: 1 addition & 1 deletion tests/test_general_validators/test_mccabe_difficulty.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ def test_mccabe_difficulty(test_repo):
expected_output = 'function_with_big_complexity'
output = validators.too_difficult_by_mccabe(
project_folder=test_repo,
max_complexity=max_complexity
max_mccabe_complexity=max_complexity
)
assert output == expected_output