def test_update_coverage_nonexisting_versio(tmpdir, rules_dir: Path, mock_git_analyzer_repos, capsys): with pushd(tmpdir), patch('rspec_tools.coverage.Repo', mock_git_analyzer_repos): with pytest.raises(Exception): update_coverage_for_repo_version('broken', 'non-existing', rules_dir) assert 'checkout failed' in capsys.readouterr().out
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 collect_coverage_for_version(repo_name, git_repo, version, coverage): g = Git(git_repo) print(f"{repo_name} {version}") repo_dir = git_repo.working_tree_dir try: with pushd(repo_dir): git_repo.head.reference = git_repo.commit(version) git_repo.head.reset(index=True, working_tree=True) g.checkout(version) implemented_rules = all_implemented_rules() coverage.add_analyzer_version(repo_name, version, implemented_rules) except Exception as e: print(f"{repo_name} {version} checkout failed: {e}") raise
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 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 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