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): update_coverage_for_repo_version('broken', 'v1', rules_dir) assert 'failed to collect implemented rules for' in capsys.readouterr( ).out coverage = tmpdir.join('covered_rules.json') assert coverage.exists() cov = load_json(coverage) assert cov == {}
def read_canonical_rule_ids(rules_dir): ''' Map all the keys identifying a rule to its modern key (which is also its directory name). ''' print('Collecting the rule-id synonyms from ' + str(rules_dir)) canonical_id = {} rule_dirs = [entry for entry in os.scandir(rules_dir) if entry.is_dir()] for rule_dir in rule_dirs: for metadata_path in Path(rule_dir).rglob('metadata.json'): for alternative_key in read_all_alternative_keys( load_json(metadata_path)): canonical_id[alternative_key] = rule_dir.name return canonical_id
def test_update_coverage_for_repo_version(tmpdir, rules_dir: Path, mock_git_analyzer_repos): with pushd(tmpdir), patch('rspec_tools.coverage.Repo', mock_git_analyzer_repos): VER = '3.3.0.5702' REPO = 'SonarJS' update_coverage_for_repo_version(REPO, VER, rules_dir) coverage = tmpdir.join('covered_rules.json') assert coverage.exists() cov = load_json(coverage) assert 'JAVASCRIPT' in cov assert 'S100' in cov['JAVASCRIPT'] assert 'MethodName' not in cov[ 'JAVASCRIPT'] # MethodName is a legacy key for S100 assert 'S200' not in cov[ 'JAVASCRIPT'] # S200.json is not in the rules directory in mock assert cov['JAVASCRIPT']['S100'] == { 'since': REPO + ' ' + VER, 'until': REPO + ' ' + VER } # Running it again changes nothing update_coverage_for_repo_version(REPO, VER, rules_dir) assert cov == load_json(coverage) # Running it for a newer version doesn't change when the rules are first implemented VER2 = '5.0.0.6962' update_coverage_for_repo_version(REPO, VER2, rules_dir) cov_new = load_json(coverage) assert set(cov['JAVASCRIPT'].keys()).issubset( set(cov_new['JAVASCRIPT'].keys())) assert cov_new['JAVASCRIPT']['S100']['since'] == REPO + ' ' + VER assert cov_new['JAVASCRIPT']['S100']['until'] == REPO + ' ' + VER2 assert cov_new['JAVASCRIPT']['S1192']['since'] == REPO + ' ' + VER2 assert cov_new['JAVASCRIPT']['S1192']['until'] == REPO + ' ' + VER2 # For rules supported on master only the 'since' part is kept update_coverage_for_repo_version(REPO, 'master', rules_dir) assert load_json(coverage)['JAVASCRIPT']['S100'] == REPO + ' ' + VER
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 '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'
def get_implemented_rules(path, languages_from_sonarpedia): implemented_rules = {} for lang in languages_from_sonarpedia: implemented_rules[lang] = [] for filename in os.listdir(path): if filename.endswith(".json") and not filename.startswith("Sonar_way"): rule = load_json(os.path.join(path, filename)) rule_id = get_rule_id(filename) for language in compatible_languages(rule, languages_from_sonarpedia): if language not in implemented_rules: implemented_rules[language] = [] implemented_rules[language].append(rule_id) else: continue return implemented_rules
def all_implemented_rules(): implemented_rules = {} 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)) except Exception as e: print(f"failed to collect implemented rules for {sp_file}: {e}") continue return implemented_rules
def test_update_coverage_for_repo(tmpdir, rules_dir: Path, mock_git_analyzer_repos): with pushd(tmpdir), patch('rspec_tools.coverage.Repo', mock_git_analyzer_repos): REPO = 'SonarJS' update_coverage_for_repo(REPO, rules_dir) coverage = tmpdir.join('covered_rules.json') assert coverage.exists() cov = load_json(coverage) assert 'JAVASCRIPT' in cov assert 'TYPESCRIPT' in cov assert 'S100' in cov['JAVASCRIPT'] assert 'MethodName' not in cov[ 'JAVASCRIPT'] # MethodName is a legacy key for S100 assert cov['JAVASCRIPT']['S100'] == REPO + ' 3.3.0.5702' assert 'S1145' in cov['JAVASCRIPT'] assert cov['JAVASCRIPT']['S1145'] == { 'since': REPO + ' 3.3.0.5702', 'until': REPO + ' 6.7.0.14237' }
def __init__(self, filename, rules_dir): self.rules = {} self.canonical_ids = read_canonical_rule_ids(rules_dir) if os.path.exists(filename): self.rules = load_json(filename)