Skip to content

RULEAPI-770 Fix bug due to multiple sonarpedia.json files #3983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 11, 2024
Merged
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
8 changes: 6 additions & 2 deletions rspec-tools/rspec_tools/coverage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
import json
import collections
from git import Repo
from git import Git
from pathlib import Path
Expand Down Expand Up @@ -133,15 +134,18 @@ def add_analyzer_version(self, analyzer, version, implemented_rules_per_language
self.rule_implemented(rule_id, language, analyzer, version)

def all_implemented_rules():
implemented_rules = {}
implemented_rules = collections.defaultdict(list)
for sp_file in Path('.').rglob('sonarpedia.json'):
print(sp_file)
sonarpedia_path=sp_file.parents[0]
try:
sonarpedia = load_json(sp_file)
path = str(sonarpedia_path) + '/' + sonarpedia['rules-metadata-path'].replace('\\', '/')
languages = sonarpedia['languages']
implemented_rules.update(get_implemented_rules(path, languages))

implemented_rules_in_path = get_implemented_rules(path, languages)
for lang, rules in implemented_rules_in_path.items():
implemented_rules[lang] += rules
except Exception as e:
print(f"failed to collect implemented rules for {sp_file}: {e}")
continue
Expand Down
16 changes: 14 additions & 2 deletions rspec-tools/tests/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ def rules_dir():
['sonarpedia.json', '{"rules-metadata-path": "rules", "languages":["XML"]}'],
['rules/S103.json', '{}']]}
]},
{'name':'sonar-java',
'versions': [
{'name': '1.2.0.123',
'date': '2020-01-02 10:00:00',
'files': [['module1/rules/Sonar_way_profile.json', '{}'],
['module1/sonarpedia.json', '{"rules-metadata-path": "rules", "languages":["JAVA"]}'],
['module1/rules/S100.json', '{}'],
['module2/rules/Sonar_way_profile.json', '{}'],
['module2/sonarpedia.json', '{"rules-metadata-path": "rules", "languages":["JAVA"]}'],
['module2/rules/S101.json', '{}']]}
]},
{'name':'broken',
'versions': [
{'name': 'v1',
Expand Down Expand Up @@ -159,19 +170,20 @@ def test_update_coverage_for_repo(tmpdir, rules_dir: Path, mock_git_analyzer_rep
assert cov['JAVASCRIPT']['S1145'] == {'since': REPO + ' 3.3.0.5702', 'until': REPO + ' 6.7.0.14237'}


@patch('rspec_tools.coverage.REPOS', ['SonarJS', 'sonar-xml'])
@patch('rspec_tools.coverage.REPOS', ['SonarJS', 'sonar-xml', 'sonar-java'])
def test_update_coverage_for_all_repos(tmpdir, rules_dir: Path, mock_git_analyzer_repos):
with pushd(tmpdir), patch('rspec_tools.coverage.Repo', mock_git_analyzer_repos):
update_coverage_for_all_repos(rules_dir)
coverage = tmpdir.join('covered_rules.json')
assert coverage.exists()
cov = load_json(coverage)
assert {'JAVASCRIPT', 'TYPESCRIPT', 'XML', 'CSS'} == set(cov.keys())
assert {'JAVASCRIPT', 'TYPESCRIPT', 'XML', 'CSS', 'JAVA'} == set(cov.keys())
assert 'S100' in cov['JAVASCRIPT']
assert 'MethodName' not in cov['JAVASCRIPT'] # MethodName is a legacy key for S100
assert {'S100'} == set(cov['CSS'].keys())
assert {'S103', 'S1000'} == set(cov['XML'].keys())
assert cov['XML']['S1000'] == 'SonarJS 7.0.0.14528'
assert {'S100', 'S101'} == set(cov['JAVA'].keys())

def test_update_coverage_no_sonarpedia(tmpdir, rules_dir: Path, mock_git_analyzer_repos, capsys):
with pushd(tmpdir), patch('rspec_tools.coverage.Repo', mock_git_analyzer_repos):
Expand Down