From 1ebb4370425fbf6776b87cac7c1cf9c8358b5fda Mon Sep 17 00:00:00 2001 From: Fred Tingaud <95592999+frederic-tingaud-sonarsource@users.noreply.github.com> Date: Fri, 2 Feb 2024 17:57:26 +0100 Subject: [PATCH] Allow free titles in 'How to fix it' --- .../rspec_tools/validation/description.py | 8 +++---- .../invalid-rules/S200/cobol/rule.adoc | 5 ---- .../invalid-rules/S200/cobol/rule.html | 21 ----------------- .../invalid-rules/S200/java/rule.adoc | 5 ---- .../invalid-rules/S200/java/rule.html | 15 ------------ .../validation/test_description_validation.py | 23 +++++++------------ 6 files changed, 12 insertions(+), 65 deletions(-) delete mode 100644 rspec-tools/tests/resources/invalid-rules/S200/cobol/rule.adoc delete mode 100644 rspec-tools/tests/resources/invalid-rules/S200/cobol/rule.html delete mode 100644 rspec-tools/tests/resources/invalid-rules/S200/java/rule.adoc delete mode 100644 rspec-tools/tests/resources/invalid-rules/S200/java/rule.html diff --git a/rspec-tools/rspec_tools/validation/description.py b/rspec-tools/rspec_tools/validation/description.py index 719064c25f..cf343f9e65 100644 --- a/rspec-tools/rspec_tools/validation/description.py +++ b/rspec-tools/rspec_tools/validation/description.py @@ -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}"') diff --git a/rspec-tools/tests/resources/invalid-rules/S200/cobol/rule.adoc b/rspec-tools/tests/resources/invalid-rules/S200/cobol/rule.adoc deleted file mode 100644 index 96c98479ca..0000000000 --- a/rspec-tools/tests/resources/invalid-rules/S200/cobol/rule.adoc +++ /dev/null @@ -1,5 +0,0 @@ -== Why is this an issue? -== How to fix it -// there's a typo, it's "Code examples" -=== Coding examples -== Resources diff --git a/rspec-tools/tests/resources/invalid-rules/S200/cobol/rule.html b/rspec-tools/tests/resources/invalid-rules/S200/cobol/rule.html deleted file mode 100644 index 230f82fa43..0000000000 --- a/rspec-tools/tests/resources/invalid-rules/S200/cobol/rule.html +++ /dev/null @@ -1,21 +0,0 @@ -
-

Why is this an issue?

-
- -
-
-
-

How to fix it

-
-
-

Coding examples

- -
-
-
-
-

Resources

-
- -
-
\ No newline at end of file diff --git a/rspec-tools/tests/resources/invalid-rules/S200/java/rule.adoc b/rspec-tools/tests/resources/invalid-rules/S200/java/rule.adoc deleted file mode 100644 index 0f89d78f43..0000000000 --- a/rspec-tools/tests/resources/invalid-rules/S200/java/rule.adoc +++ /dev/null @@ -1,5 +0,0 @@ -== Why is this an issue? -== How to fix it in Razor -=== Yolo (invalid section name) - - diff --git a/rspec-tools/tests/resources/invalid-rules/S200/java/rule.html b/rspec-tools/tests/resources/invalid-rules/S200/java/rule.html deleted file mode 100644 index adf59b4a8d..0000000000 --- a/rspec-tools/tests/resources/invalid-rules/S200/java/rule.html +++ /dev/null @@ -1,15 +0,0 @@ -
-

Why is this an issue?

-
- -
-
-
-

How to fix it in Razor

-
-
-

Yolo (invalid section name)

- -
-
-
\ No newline at end of file diff --git a/rspec-tools/tests/validation/test_description_validation.py b/rspec-tools/tests/validation/test_description_validation.py index 6aeec72380..b15a6b0422 100644 --- a/rspec-tools/tests/validation/test_description_validation.py +++ b/rspec-tools/tests/validation/test_description_validation.py @@ -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')