def test_outdated_local(tmpdir, local, remote): """Test with remote changes not pulled. :param tmpdir: pytest fixture. :param local: conftest fixture. :param remote: conftest fixture. """ # Commit to separate local repo and push to common remote. local_ahead = tmpdir.ensure_dir('local_ahead') pytest.run(local_ahead, ['git', 'clone', remote, '.']) local_ahead.join('README').write('changed') pytest.run(local_ahead, ['git', 'commit', '-am', 'Changed master']) pytest.run(local_ahead, ['git', 'checkout', 'feature']) local_ahead.join('README').write('changed') pytest.run(local_ahead, ['git', 'commit', '-am', 'Changed feature']) pytest.run(local_ahead, ['git', 'push', 'origin', 'master', 'feature']) # Commits not fetched. remotes = list_remote(str(local)) shas = [r[0] for r in remotes] with pytest.raises(GitError): filter_and_date(str(local), ['README'], shas) # Pull and retry. pytest.run(local, ['git', 'pull', 'origin', 'master']) pytest.run(local, ['git', 'checkout', 'feature']) pytest.run(local, ['git', 'pull', 'origin', 'feature']) dates = filter_and_date(str(local), ['README'], shas) assert len( dates ) == 3 # Original SHA is the same for everything. Plus above two commits.
def gather_git_info(root, conf_rel_paths, whitelist_branches, whitelist_tags): """Gather info about the remote git repository. Get list of refs. :raise HandledError: If function fails with a handled error. Will be logged before raising. :param str root: Root directory of repository. :param iter conf_rel_paths: List of possible relative paths (to git root) of Sphinx conf.py (e.g. docs/conf.py). :param iter whitelist_branches: Optional list of patterns to filter branches by. :param iter whitelist_tags: Optional list of patterns to filter tags by. :return: Commits with docs. A list of tuples: (sha, name, kind, date, conf_rel_path). :rtype: list """ log = logging.getLogger(__name__) # List remote. log.info('Getting list of all remote branches/tags...') try: remotes = list_remote(root) except GitError as exc: log.error(exc.message) log.error(exc.output) raise HandledError log.info('Found: %s', ' '.join(i[1] for i in remotes)) # Filter and date. try: try: dates_paths = filter_and_date(root, conf_rel_paths, (i[0] for i in remotes)) except GitError: log.info('Need to fetch from remote...') fetch_commits(root, remotes) try: dates_paths = filter_and_date(root, conf_rel_paths, (i[0] for i in remotes)) except GitError as exc: log.error(exc.message) log.error(exc.output) raise HandledError except subprocess.CalledProcessError as exc: log.debug(json.dumps(dict(command=exc.cmd, cwd=root, code=exc.returncode, output=exc.output))) log.error('Failed to get dates for all remote commits.') raise HandledError filtered_remotes = [[i[0], i[1], i[2], ] + dates_paths[i[0]] for i in remotes if i[0] in dates_paths] log.info('With docs: %s', ' '.join(i[1] for i in filtered_remotes)) if not whitelist_branches and not whitelist_tags: return filtered_remotes # Apply whitelist. whitelisted_remotes = list() for remote in filtered_remotes: if remote[2] == 'heads' and whitelist_branches: if not any(re.search(p, remote[1]) for p in whitelist_branches): continue if remote[2] == 'tags' and whitelist_tags: if not any(re.search(p, remote[1]) for p in whitelist_tags): continue whitelisted_remotes.append(remote) log.info('Passed whitelisting: %s', ' '.join(i[1] for i in whitelisted_remotes)) return whitelisted_remotes
def test_new_branch_tags(local, local_light, clone_branch): """Test with new branches and tags unknown to local repo. :param local: conftest fixture. :param local_light: conftest fixture. :param bool clone_branch: Test with local repo cloned with --branch. """ if clone_branch: local = local_light # Get SHAs and verify not fetched. remotes = list_remote(str(local)) assert len( remotes ) == 7 # master feature light_tag annotated_tag nb_tag orphaned_branch ob_at shas = {r[0] for r in remotes} assert len(shas) == 3 with pytest.raises(GitError): filter_and_date(str(local), ['README'], shas) # Fetch and verify. fetch_commits(str(local), remotes) dates = filter_and_date(str(local), ['README'], shas) assert len(dates) == 3 pytest.run(local, ['git', 'diff-index', '--quiet', 'HEAD', '--'])
def test_one_commit(local): """Test with one commit. :param local: conftest fixture. """ sha = pytest.run(local, ['git', 'rev-parse', 'HEAD']).strip() dates = filter_and_date(str(local), ['does_not_exist'], [sha]) assert not dates with pytest.raises(GitError): filter_and_date(str(local), ['README'], ['invalid']) # Test with existing conf_rel_path. dates = filter_and_date(str(local), ['README'], [sha]) assert list(dates) == [sha] assert dates[sha][0] >= pytest.ROOT_TS assert dates[sha][0] < time.time() assert dates[sha][1] == 'README' # Test duplicate SHAs. dates2 = filter_and_date(str(local), ['README'], [sha, sha, sha]) assert dates2 == dates
def test_new_branch_tags(local, local_light, clone_branch): """Test with new branches and tags unknown to local repo. :param local: conftest fixture. :param local_light: conftest fixture. :param bool clone_branch: Test with local repo cloned with --branch. """ if clone_branch: local = local_light # Get SHAs and verify not fetched. remotes = list_remote(str(local)) assert len(remotes) == 7 # master feature light_tag annotated_tag nb_tag orphaned_branch ob_at shas = {r[0] for r in remotes} assert len(shas) == 3 with pytest.raises(GitError): filter_and_date(str(local), ['README'], shas) # Fetch and verify. fetch_commits(str(local), remotes) dates = filter_and_date(str(local), ['README'], shas) assert len(dates) == 3 pytest.run(local, ['git', 'diff-index', '--quiet', 'HEAD', '--'])
def test_multiple_commits(local): """Test with multiple commits. :param local: conftest fixture. """ shas = {pytest.run(local, ['git', 'rev-parse', 'HEAD']).strip()} for _ in range(50): local.ensure('docs', 'conf.py').write('pass\n') pytest.run(local, ['git', 'add', 'docs/conf.py']) pytest.run(local, ['git', 'commit', '-m', 'add']) shas.add(pytest.run(local, ['git', 'rev-parse', 'HEAD']).strip()) pytest.run(local, ['git', 'rm', 'docs/conf.py']) pytest.run(local, ['git', 'commit', '-m', 'remove']) shas.add(pytest.run(local, ['git', 'rev-parse', 'HEAD']).strip()) assert len(shas) == 101 dates = filter_and_date(str(local), ['docs/conf.py'], list(shas)) assert len(dates) == 50
def test_three_commits_multiple_paths(local): """Test with two valid candidates and one ignored candidate. :param local: conftest fixture. """ shas = {pytest.run(local, ['git', 'rev-parse', 'HEAD']).strip()} pytest.run(local, ['git', 'checkout', 'feature']) local.ensure('conf.py').write('pass\n') pytest.run(local, ['git', 'add', 'conf.py']) pytest.run(local, ['git', 'commit', '-m', 'root']) shas.add(pytest.run(local, ['git', 'rev-parse', 'HEAD']).strip()) pytest.run(local, ['git', 'checkout', '-b', 'subdir', 'master']) local.ensure('docs', 'conf.py').write('pass\n') pytest.run(local, ['git', 'add', 'docs/conf.py']) pytest.run(local, ['git', 'commit', '-m', 'subdir']) shas.add(pytest.run(local, ['git', 'rev-parse', 'HEAD']).strip()) pytest.run(local, ['git', 'push', 'origin', 'feature', 'subdir']) assert len(shas) == 3 dates = filter_and_date(str(local), ['conf.py', 'docs/conf.py'], shas) assert len(dates) == 2