Esempio n. 1
0
def test_get_metadata(mockrules: Path):
    '''Check that language metadata are correctly overriden.'''
    rule = RulesRepository(rules_path=mockrules).get_rule('S120')
    plsql = rule.get_language('plsql')
    assert plsql.metadata['sqKey'] == 'PlSql.PackageNaming'
    java = rule.get_language('java')
    assert java.metadata['sqKey'] == 'S120'
Esempio n. 2
0
def test_open_rule_with_no_language_in_exception_list(
        invalid_rules: RulesRepository):
    s501 = invalid_rules.get_rule('S501')
    with pytest.raises(RuleValidationError,
                       match=fr'^Rule S501 should be closed or deprecated'):
        with patch.dict(s501.generic_metadata, [('status', 'ready')]):
            validate_rule_metadata(s501)
Esempio n. 3
0
def validate_rules_metadata(rules):
    '''Validate rules metadata.'''
    rule_repository = RulesRepository()
    error_counter = 0

    for rule_id in rules:
        try:
            rule = rule_repository.get_rule(rule_id)
            validate_rule_metadata(rule)
        except RuleValidationError as e:
            click.echo(e.message, err=True)
            error_counter += 1

    if error_counter > 0:
        _fatal_error(
            f"Validation failed due to {error_counter} errors out of {len(rules)} analyzed rules"
        )
Esempio n. 4
0
def test_rule_with_invalid_language(invalid_rules: RulesRepository):
    s502 = invalid_rules.get_rule('S502')
    with pytest.raises(
            RuleValidationError,
            match=
            fr'^Rule S502 failed validation for these reasons:\n - Rule scala:S502 has invalid metadata'
    ):
        validate_rule_metadata(s502)
Esempio n. 5
0
def check_description(d, rules):
    '''Validate the rule.adoc description.'''
    out_dir = Path(__file__).parent.parent.joinpath(d)
    rule_repository = RulesRepository(rules_path=out_dir)
    error_counter = 0
    for rule in rule_repository.rules:
        if rules and rule.id not in rules:
            continue
        for lang_spec_rule in rule.specializations:
            error_counter += _validate_rule_specialization(lang_spec_rule)
    if error_counter > 0:
        _fatal_error(f"Validation failed due to {error_counter} errors")
 def _invalid_rule(rule_id, language):
     rule = RulesRepository(rules_path=mockinvalidrules).get_rule(rule_id)
     return rule.get_language(language)
Esempio n. 7
0
def test_list_rules(mockrules: Path):
    '''Check that rules are all listed.'''
    rules = {rule.id for rule in RulesRepository(rules_path=mockrules).rules}
    assert rules == {'S100', 'S120', 'S4727', 'S1033'}
Esempio n. 8
0
def test_nonexisting_rule(mockrules: Path):
    '''Check that a nonexisting rule is reported.'''
    with pytest.raises(RuleNotFoundError, match=fr'^Cannot find rule S200'):
        RulesRepository(rules_path=mockrules).get_rule('S200')
Esempio n. 9
0
def test_list_languages(mockrules: Path):
    '''Check that languages are all listed.'''
    rule = RulesRepository(rules_path=mockrules).get_rule('S120')
    languages = {lang.language for lang in rule.specializations}
    assert languages == {'flex', 'java', 'plsql'}
Esempio n. 10
0
def test_rule_that_is_fully_valid(mockrules: Path):
    valid_rule = RulesRepository(rules_path=mockrules).get_rule('S120')
    validate_rule_metadata(valid_rule)
Esempio n. 11
0
def test_rule_expected_to_have_no_language(mockrules: Path):
    valid_rule = RulesRepository(rules_path=mockrules).get_rule('S120')
    with pytest.raises(RuleValidationError,
                       match=fr'^Rule S120 should have no specializations'):
        validate_rule_metadata(valid_rule)
Esempio n. 12
0
def test_rule_with_no_language_in_exception_list(
        invalid_rules: RulesRepository):
    s501 = invalid_rules.get_rule('S501')
    validate_rule_metadata(s501)
    with patch.dict(s501.generic_metadata, [('status', 'deprecated')]):
        validate_rule_metadata(s501)
Esempio n. 13
0
def test_rule_with_no_language(invalid_rules: RulesRepository):
    s501 = invalid_rules.get_rule('S501')
    with pytest.raises(RuleValidationError,
                       match=fr'^Rule S501 has no language-specific data'):
        validate_rule_metadata(s501)
Esempio n. 14
0
def invalid_rules():
    invalid_rules_path = Path(__file__).parent.parent.joinpath(
        'resources', 'invalid-rules')
    return RulesRepository(rules_path=invalid_rules_path)
Esempio n. 15
0
def rule_language(mockrules: Path):
    rule = RulesRepository(rules_path=mockrules).get_rule('S100')
    return rule.get_language('kotlin')