Allow free titles in 'How to fix it'

This commit is contained in:
Fred Tingaud 2024-02-02 17:57:26 +01:00 committed by GitHub
parent d9656053c0
commit 1ebb437042
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 12 additions and 65 deletions

View File

@ -1,12 +1,12 @@
from bs4 import BeautifulSoup
import re
from pathlib import Path
from typing import Final, Dict, List
from bs4 import BeautifulSoup
from rspec_tools.errors import RuleValidationError
from rspec_tools.rules import LanguageSpecificRule
from rspec_tools.utils import LANG_TO_SOURCE
import re
def read_file(path):
section_names_path = Path(__file__).parent.parent.parent.parent.joinpath(path)
@ -60,7 +60,7 @@ MANDATORY_SECTIONS = ['Why is this an issue?']
CODE_EXAMPLES='Code examples'
OPTIONAL_SECTIONS = {
# Also covers 'How to fix it in {Framework Display Name}'
'How to fix it': [CODE_EXAMPLES, 'How does this work?', 'Pitfalls', 'Going the extra mile'],
HOW_TO_FIX_IT: [], # Empty list because we now accept anything as sub-section
'Resources': ['Documentation', 'Articles & blog posts', 'Conference presentations', 'Standards', 'External coding guidelines', 'Benchmarks', 'Related rules']
}
SUBSECTIONS = {
@ -219,7 +219,7 @@ def validate_subsections_for_section(rule_language: LanguageSpecificRule, sectio
subsections_seen = set()
for title in titles:
name = title.text.strip()
if name not in allowed_subsections:
if allowed_subsections and name not in allowed_subsections:
raise RuleValidationError(f'Rule {rule_language.id} has a "{section_name}" subsection with an unallowed name: "{name}"')
if name in subsections_seen and not is_duplicate_allowed:
raise RuleValidationError(f'Rule {rule_language.id} has duplicate "{section_name}" subsections. There are 2 occurences of "{name}"')

View File

@ -1,5 +0,0 @@
== Why is this an issue?
== How to fix it
// there's a typo, it's "Code examples"
=== Coding examples
== Resources

View File

@ -1,21 +0,0 @@
<div class="sect1">
<h2 id="_why_is_this_an_issue">Why is this an issue?</h2>
<div class="sectionbody">
</div>
</div>
<div class="sect1">
<h2 id="_how_to_fix_it">How to fix it</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_coding_examples">Coding examples</h3>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_resources">Resources</h2>
<div class="sectionbody">
</div>
</div>

View File

@ -1,5 +0,0 @@
== Why is this an issue?
== How to fix it in Razor
=== Yolo (invalid section name)

View File

@ -1,15 +0,0 @@
<div class="sect1">
<h2 id="_why_is_this_an_issue">Why is this an issue?</h2>
<div class="sectionbody">
</div>
</div>
<div class="sect1">
<h2 id="_how_to_fix_it_in_razor">How to fix it in Razor</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_yolo_invalid_section_name">Yolo (invalid section name)</h3>
</div>
</div>
</div>

View File

@ -4,9 +4,14 @@ from pathlib import Path
import pytest
from rspec_tools.errors import RuleValidationError
from rspec_tools.rules import RulesRepository
from rspec_tools.validation.description import validate_section_names, \
validate_section_levels, validate_parameters, validate_source_language, \
validate_subsections, validate_security_standard_links
from rspec_tools.validation.description import (
validate_parameters,
validate_section_levels,
validate_section_names,
validate_security_standard_links,
validate_source_language,
validate_subsections,
)
@pytest.fixture
@ -120,12 +125,6 @@ def test_wrong_format_how_to_fix_it_section_validation(invalid_rule):
with pytest.raises(RuleValidationError, match=f'Rule typescript:S200 has a "How to fix it" section with an unsupported format: "How to fix it wrong format". Either use "How to fix it" or "How to fix it in FRAMEWORK NAME"'):
validate_section_names(rule)
def test_unallowed_subsections_in_how_to_fix_it_validation(invalid_rule):
'''Check that having "How to fix it" subsections with unallowed names breaks validation'''
rule = invalid_rule('S200', 'java')
with pytest.raises(RuleValidationError, match=f'Rule java:S200 has a "How to fix it" subsection with an unallowed name: "Yolo \\(invalid section name\\)"'):
validate_subsections(rule)
def test_duplicate_subsections_in_how_to_fix_it_validation(invalid_rule):
'''Check that having duplicate "How to fix it" subsections breaks validation'''
rule = invalid_rule('S200', 'csharp')
@ -150,12 +149,6 @@ def test_education_format_missing_mandatory_sections_validation(invalid_rule):
with pytest.raises(RuleValidationError, match=f'Rule common:S200 is missing the "Why is this an issue\\?" section'):
validate_section_names(rule)
def test_code_examples_with_typo_validation(invalid_rule):
'''Check that the "Code examples" subsection with a typo in the education format breaks validation'''
rule = invalid_rule('S200', 'cobol')
with pytest.raises(RuleValidationError, match=f'Rule cobol:S200 has a "How to fix it" subsection with an unallowed name: "Coding examples"'):
validate_subsections(rule)
def test_noncompliant_examples_with_typo_validation(invalid_rule):
'''Check that the "Non-compliant examples" sub-subsection with a typo in the education format breaks validation'''
rule = invalid_rule('S200', 'apex')