コード例 #1
0
async def test_review_branch_not_yet_pushed(mock_input):
    """
    Test case for a new review branch that does not yet track any remote branch
    (presumably, it hasn't yet been pushed)
    """
    with temp_repo() as upstream:
        merge_base = upstream.active_branch.commit
        assert merge_base is not None
        with temp_repo_clone(upstream) as downstream:
            new_branch = downstream.create_head("feature-123")
            new_branch.checkout()
            assert downstream.active_branch.name == "feature-123"
            create_git_history(
                downstream,
                [(('my-file.txt', 'sample content'), 'second commit')])
            assert downstream.heads['feature-123'] is not None
            downstream_default_branch = git_default_branch(downstream)
            assert downstream_default_branch == upstream.active_branch.name
            with fake_cliux(log_level=INFO) as (cli, get_lines):
                opts = ValidateOptions(
                    ValidateCLIOptions(verbose=True,
                                       cwd=downstream.working_dir))
                await do_validate(cli=cli, opts=opts)
                assert strip_ansi(
                    "".join(get_lines())
                ) == """git_guardrails has completed without taking any action.
コード例 #2
0
async def test_new_upstream_commits_to_pull_down(mock_input_a):
    """
    Test case for "origin/review-branch has new commits that I must pull down"
    (no new local commits that origin doesn't have yet)
    """
    with temp_repo() as upstream:
        upstream_default_branch = upstream.active_branch
        upstream_feature_branch = upstream.create_head("feature-123")
        # upstream_feature_branch.checkout()
        with temp_repo_clone(upstream, ['feature-123']) as downstream:
            assert ", ".join(sorted_repo_branch_names(
                downstream)) == f"feature-123, {upstream_default_branch.name}"
            upstream_feature_branch.checkout()
            create_git_history(upstream, [
                (('file_0.txt', 'content for file 0'), 'second commit'),
                (('file_1.txt', 'content for file 1'), 'third commit'),
                (('file_2.txt', 'content for file 2'), 'fourth commit'),
            ])
            upstream_default_branch.checkout()
            assert upstream.active_branch.name in ['main', 'master']
            downstream.heads['feature-123'].checkout()
            opts = ValidateOptions(
                ValidateCLIOptions(verbose=False,
                                   cwd=downstream.working_dir,
                                   auto_fetch=True))
            assert opts.is_verbose() == False
            with fake_cliux(log_level=INFO) as (cli, get_lines):
                assert cli.log_level == INFO
                await do_validate(cli=cli, opts=opts)
                assert strip_ansi(
                    "".join(get_lines())
                ) == """determined that local branch feature-123 tracks upstream branch feature-123 on remote origin
コード例 #3
0
def test_cliux_logging():
    with fake_cliux(log_level=DEBUG) as (cli, get_lines):
        cli.info('grapes')
        cli.debug('apple')
        cli.warning('pear')
        cli.error('orange')
        lines = "".join(get_lines())
        assert strip_ansi(lines) == """[INFO]: grapes
コード例 #4
0
async def test_do_validate_no_remote(mock_input):
    """
    Test case for repo w/o any git remotes
    """
    with temp_repo() as upstream:
        with fake_cliux() as (cli, get_lines):
            opts = ValidateOptions(
                ValidateCLIOptions(verbose=True, cwd=upstream.working_dir))
            await do_validate(cli=cli, opts=opts)
            assert strip_ansi("".join(get_lines(
            ))) == """git_guardrails has completed without taking any action.
コード例 #5
0
async def test_need_to_fetch_only_upstream_commits(mock_input):
    with setup_need_to_fetch_scenario() as (upstream, downstream):
        assert upstream.is_dirty() == False
        downstream.heads['review-999'].checkout()
        with fake_cliux() as (cli, get_lines):
            opts = ValidateOptions(
                ValidateCLIOptions(verbose=True,
                                   auto_fetch=True,
                                   cwd=downstream.working_dir))
            await do_validate(cli=cli, opts=opts)
            assert "[WARNING]: New commits on origin/review-999 were detected" in strip_ansi(
                "".join(get_lines()))
コード例 #6
0
def test_non_applicable_situation_exception():
    with fake_cliux() as (cli, get_lines):
        try:
            raise NonApplicableSituationException(
                situation_title="Example title",
                situation_details="Some example details")
        except NonApplicableSituationException as ex:
            cli.handle_non_applicable_situation_exception(ex)
            log_lines = "".join(get_lines())
            assert strip_ansi(
                log_lines
            ) == """git_guardrails has completed without taking any action.
コード例 #7
0
def test_unhandled_situation_exception_user_invalid_response(_mock_input):
    with fake_cliux() as (cli, get_lines):
        try:
            raise UnhandledSituationException(
                situation_title="Example title",
                situation_details="Some example details")
        except UnhandledSituationException as ex:
            cli.handle_unhandled_situation_exception(ex, retry_prompt=False)
            log_lines = "".join(get_lines())
            assert strip_ansi(
                log_lines
            ) == """[WARNING]: git_guardrails found your workspace in an unexpected state
コード例 #8
0
async def test_need_to_fetch_only_upstream_commits_user_refuses(mock_input):
    with setup_need_to_fetch_scenario() as (upstream, downstream):
        assert upstream.is_dirty() == False
        downstream.heads['review-999'].checkout()
        with fake_cliux() as (cli, get_lines):
            opts = ValidateOptions(
                ValidateCLIOptions(verbose=True,
                                   auto_fetch=False,
                                   cwd=downstream.working_dir))
            await do_validate(cli=cli, opts=opts)
            assert strip_ansi(
                "".join(get_lines())
            ) == """determined that local branch review-999 tracks upstream branch review-999 on remote origin
コード例 #9
0
async def test_push_from_default_branch(mock_input):
    """
    Test case for "push while on the default branch instead of a review branch"
    """
    with temp_repo() as upstream:
        with temp_repo_clone(upstream) as downstream:
            with fake_cliux(log_level=INFO) as (cli, get_lines):
                opts = ValidateOptions(
                    ValidateCLIOptions(verbose=False,
                                       cwd=downstream.working_dir))
                await do_validate(cli=cli, opts=opts)
                assert strip_ansi(
                    "".join(get_lines())
                ) == """git_guardrails has completed without taking any action.
コード例 #10
0
async def test_no_connect_to_remote(mock_input):
    """
    Test case for "can't connect to git remote"
    """
    with pytest.raises(GitRemoteConnectivityException):
        with temp_repo() as upstream:
            with temp_repo_clone(upstream) as downstream:
                downstream_origin: Remote = downstream.remotes['origin']
                assert downstream_origin is not None
                downstream_origin.set_url(new_url='https://example.com')
                with fake_cliux(log_level=INFO) as (cli, get_lines):
                    opts = ValidateOptions(
                        ValidateCLIOptions(verbose=False,
                                           cwd=downstream.working_dir))
                    await do_validate(cli=cli, opts=opts)
                    assert strip_ansi("".join(get_lines())) == ""
コード例 #11
0
async def test_review_branch_commit_count_hard_fail():
    try:
        with temp_repo() as upstream:
            upstream_default_branch = upstream.active_branch
            upstream.create_head('mnorth-review-111')
            create_git_history(upstream, [
                (("demo_0.txt", "content for demo_0"), "demo 0 commit"),
                (("demo_1.txt", "content for demo_1"), "demo 1 commit"),
                (("demo_2.txt", "content for demo_2"), "demo 2 commit"),
                (("demo_3.txt", "content for demo_3"), "demo 3 commit"),
            ])
            upstream_default_branch.checkout()
            with temp_repo_clone(upstream,
                                 ['mnorth-review-111']) as downstream:
                # downstream_default_branch = downstream.active_branch
                downstream_review_branch = downstream.heads[
                    'mnorth-review-111']
                assert downstream_review_branch is not None
                downstream_review_branch.checkout()
                create_git_history(downstream, [
                    (("demo_4.txt", "content for demo_4"), "demo 4 commit"),
                    (("demo_5.txt", "content for demo_5"), "demo 5 commit"),
                    (("demo_6.txt", "content for demo_6"), "demo 6 commit"),
                    (("demo_7.txt", "content for demo_7"), "demo 7 commit"),
                    (("demo_8.txt", "content for demo_8"), "demo 8 commit"),
                    (("demo_9.txt", "content for demo_9"), "demo 9 commit"),
                    (("demo_10.txt", "content for demo_10"), "demo 10 commit"),
                    (("demo_11.txt", "content for demo_11"), "demo 11 commit"),
                    (("demo_12.txt", "content for demo_12"), "demo 12 commit"),
                    (("demo_13.txt", "content for demo_13"), "demo 13 commit"),
                    (("demo_14.txt", "content for demo_14"), "demo 14 commit"),
                    (("demo_15.txt", "content for demo_15"), "demo 15 commit"),
                ])
                with fake_cliux(log_level=INFO) as (cli, get_lines):
                    opts = ValidateOptions(
                        ValidateCLIOptions(verbose=True,
                                           commit_count_soft_fail_threshold=5,
                                           commit_count_hard_fail_threshold=10,
                                           cwd=downstream.working_dir))
                    await do_validate(cli=cli, opts=opts)
                    assert False, 'Error should have already been thrown by this point'
    except LikelyUserErrorException as ex:
        assert ex is not None
        assert strip_ansi(
            str(ex)) == """DANGER: VERY LARGE NUMBER OF REVIEW BRANCH COMMITS
コード例 #12
0
async def test_review_branch_commit_count_soft_fail():
    with temp_repo() as upstream:
        upstream_default_branch = upstream.active_branch
        upstream.create_head('mnorth-review-111')
        create_git_history(upstream, [
            (("demo_0.txt", "content for demo_0"), "demo 0 commit"),
            (("demo_1.txt", "content for demo_1"), "demo 1 commit"),
            (("demo_2.txt", "content for demo_2"), "demo 2 commit"),
            (("demo_3.txt", "content for demo_3"), "demo 3 commit"),
        ])
        upstream_default_branch.checkout()
        with temp_repo_clone(upstream, ['mnorth-review-111']) as downstream:
            # downstream_default_branch = downstream.active_branch
            downstream_review_branch = downstream.heads['mnorth-review-111']
            assert downstream_review_branch is not None
            downstream_review_branch.checkout()
            create_git_history(downstream, [
                (("demo_4.txt", "content for demo_4"), "demo 4 commit"),
                (("demo_5.txt", "content for demo_5"), "demo 5 commit"),
                (("demo_6.txt", "content for demo_6"), "demo 6 commit"),
                (("demo_7.txt", "content for demo_7"), "demo 7 commit"),
                (("demo_8.txt", "content for demo_8"), "demo 8 commit"),
                (("demo_9.txt", "content for demo_9"), "demo 9 commit"),
                (("demo_10.txt", "content for demo_10"), "demo 10 commit"),
                (("demo_11.txt", "content for demo_11"), "demo 11 commit"),
                (("demo_12.txt", "content for demo_12"), "demo 12 commit"),
                (("demo_13.txt", "content for demo_13"), "demo 13 commit"),
                (("demo_14.txt", "content for demo_14"), "demo 14 commit"),
                (("demo_15.txt", "content for demo_15"), "demo 15 commit"),
            ])
            with fake_cliux(log_level=INFO) as (cli, get_lines):
                opts = ValidateOptions(
                    ValidateCLIOptions(verbose=True,
                                       commit_count_soft_fail_threshold=10,
                                       commit_count_auto_bypass_soft_fail=True,
                                       cwd=downstream.working_dir))
                await do_validate(cli=cli, opts=opts)
                assert strip_ansi(
                    "".join(get_lines())
                ) == """determined that local branch mnorth-review-111 tracks upstream branch mnorth-review-111 on remote origin
コード例 #13
0
async def test_need_to_fetch_upstream_and_downstream_commits(mock_input):
    with setup_need_to_fetch_scenario() as (upstream, downstream):
        assert upstream.is_dirty() == False
        review_branch: Head = downstream.heads['review-999']
        review_branch.checkout()
        tracked_branch: Head = review_branch.tracking_branch()
        head_before_fetch = tracked_branch.commit
        assert head_before_fetch.hexsha == tracked_branch.commit.hexsha
        with fake_cliux(log_level=INFO) as (cli, get_lines):
            opts = ValidateOptions(
                ValidateCLIOptions(verbose=False,
                                   auto_fetch=True,
                                   cwd=downstream.working_dir))
            await do_validate(cli=cli, opts=opts)
            assert strip_ansi(
                "".join(get_lines())
            ) == """determined that local branch review-999 tracks upstream branch review-999 on remote origin
[WARNING]: New commits on origin/review-999 were detected, which have not yet been pulled down to review-999
Fetching new commits for branch origin/review-999
Fetch from origin complete
Comparing review-999 against origin/review-999
"""
            assert head_before_fetch.hexsha != tracked_branch.commit.hexsha, 'New commits have been pulled down'
コード例 #14
0
async def test_in_non_git_directory():
    with temp_dir() as dir:
        with fake_cliux() as (cli, get_lines):
            opts = ValidateOptions(ValidateCLIOptions(verbose=True, auto_fetch=True, cwd=dir))
            await do_validate(cli=cli, opts=opts)
            assert strip_ansi("".join(get_lines())) == f"""[ERROR]: git_guardrails is only intended for use within a git repository directory