File size: 2,047 Bytes
b3601d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import annotations

from hughub import cli
from hughub.config import RepoConfig, load_config, save_config
from hughub.process import Result

from .conftest import FakeRunner


def configured_runner(tmp_path, *, mode="github", scripted=None):
    git_dir = tmp_path / ".git"
    git_dir.mkdir()
    runner = FakeRunner(git_dir, scripted)
    save_config(RepoConfig("acme/widget", "acme/widget", mode=mode), runner, tmp_path)
    return runner


def test_successful_command_is_passed_to_gh(tmp_path, capsys):
    command = ("gh", "pr", "list", "--json", "number")
    runner = configured_runner(
        tmp_path, scripted={command: Result(command, 0, '[{"number":1}]\n', "")}
    )

    result = cli.run(["pr", "list", "--json", "number"], runner=runner, cwd=tmp_path)

    assert result == 0
    assert capsys.readouterr().out == '[{"number":1}]\n'
    assert command in runner.calls


def test_transient_github_failure_automatically_uses_hughub(tmp_path, monkeypatch, capsys):
    command = ("gh", "pr", "create", "--fill")
    runner = configured_runner(
        tmp_path, scripted={command: Result(command, 1, "", "HTTP 503: Service Unavailable\n")}
    )
    dispatched: list[list[str]] = []

    def fake_dispatch(args, _runner, _cwd):
        dispatched.append(args)
        return 0

    monkeypatch.setattr(cli, "_hub_dispatch", fake_dispatch)

    result = cli.run(["pr", "create", "--fill"], runner=runner, cwd=tmp_path)

    assert result == 0
    assert dispatched == [["pr", "create", "--fill"]]
    assert load_config(runner, tmp_path).mode == "overlay"
    assert "continuing on HugHub" in capsys.readouterr().err


def test_user_error_does_not_trigger_failover(tmp_path):
    command = ("gh", "pr", "create", "--fill")
    runner = configured_runner(
        tmp_path, scripted={command: Result(command, 1, "", "GraphQL: Head sha can't be blank")}
    )

    result = cli.run(["pr", "create", "--fill"], runner=runner, cwd=tmp_path)

    assert result == 1
    assert load_config(runner, tmp_path).mode == "github"