2022-08-03 19:09:43 +05:30
|
|
|
import os
|
|
|
|
|
2021-06-11 04:54:24 +05:30
|
|
|
import pytest
|
|
|
|
|
|
|
|
from xonsh.prompt import gitstatus
|
2022-04-23 07:01:04 +05:30
|
|
|
from xonsh.prompt.base import _format_value
|
2021-06-11 04:54:24 +05:30
|
|
|
|
|
|
|
|
2022-01-08 04:03:22 +05:30
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def git_no_stash(mocker):
|
2022-03-21 20:48:36 +05:30
|
|
|
return mocker.patch.object(gitstatus, "get_stash_count", return_value=0)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def prompts(xession):
|
|
|
|
fields = xession.env["PROMPT_FIELDS"]
|
|
|
|
yield fields
|
|
|
|
fields.clear()
|
2022-08-03 19:09:43 +05:30
|
|
|
fields.reset()
|
2022-03-21 20:48:36 +05:30
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fake_proc(fake_process):
|
|
|
|
def wrap(map: "dict"):
|
|
|
|
for command, stdout in map.items():
|
|
|
|
fake_process.register_subprocess(command=command, stdout=stdout)
|
|
|
|
return fake_process
|
|
|
|
|
|
|
|
return wrap
|
2022-01-08 04:03:22 +05:30
|
|
|
|
|
|
|
|
2021-06-11 04:54:24 +05:30
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"hidden, exp",
|
|
|
|
[
|
|
|
|
(
|
|
|
|
(),
|
|
|
|
"{CYAN}gitstatus-opt↑·7↓·2{RESET}|{RED}●1{RESET}{BLUE}+3{RESET}{BLUE}+49{RESET}{RED}-26{RESET}",
|
|
|
|
),
|
|
|
|
(
|
2022-03-21 20:48:36 +05:30
|
|
|
(".lines_added", ".lines_removed"),
|
2021-06-11 04:54:24 +05:30
|
|
|
"{CYAN}gitstatus-opt↑·7↓·2{RESET}|{RED}●1{RESET}{BLUE}+3{RESET}",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2022-03-21 20:48:36 +05:30
|
|
|
def test_gitstatus_dirty(prompts, fake_proc, hidden, exp, xession):
|
|
|
|
prompts["gitstatus"].hidden = hidden
|
|
|
|
dirty = {
|
|
|
|
"git status --porcelain --branch": b"""\
|
2021-06-11 04:54:24 +05:30
|
|
|
## gitstatus-opt...origin/gitstatus-opt [ahead 7, behind 2]
|
|
|
|
M requirements/tests.txt
|
|
|
|
AM tests/prompt/test_gitstatus.py
|
2022-03-21 20:48:36 +05:30
|
|
|
M tests/prompt/test_vc.py""",
|
|
|
|
"git rev-parse --git-dir": b".git",
|
|
|
|
"git diff --numstat": b"""\
|
2021-06-11 04:54:24 +05:30
|
|
|
1 0 requirements/tests.txt
|
|
|
|
26 0 tests/prompt/test_gitstatus.py
|
2022-03-21 20:48:36 +05:30
|
|
|
22 26 tests/prompt/test_vc.py""",
|
|
|
|
}
|
|
|
|
fake_proc(dirty)
|
2021-06-11 04:54:24 +05:30
|
|
|
|
2022-03-21 20:48:36 +05:30
|
|
|
# finally assert
|
|
|
|
assert format(prompts.pick("gitstatus")) == exp
|
|
|
|
|
|
|
|
|
|
|
|
def test_gitstatus_clean(prompts, fake_proc):
|
|
|
|
clean = {
|
|
|
|
"git status --porcelain --branch": b"## gitstatus-opt...origin/gitstatus-opt [ahead 7, behind 2]",
|
|
|
|
"git rev-parse --git-dir": b".git",
|
|
|
|
"git diff --numstat": b"",
|
|
|
|
}
|
|
|
|
fake_proc(clean)
|
2021-06-11 04:54:24 +05:30
|
|
|
|
|
|
|
exp = "{CYAN}gitstatus-opt↑·7↓·2{RESET}|{BOLD_GREEN}✓{RESET}"
|
2022-03-21 20:48:36 +05:30
|
|
|
assert format(prompts.pick("gitstatus")) == exp
|
2022-04-23 07:01:04 +05:30
|
|
|
assert _format_value(prompts.pick("gitstatus"), None, None) == exp
|
|
|
|
assert _format_value(prompts.pick("gitstatus"), "{}", None) == exp
|
2022-08-03 19:09:43 +05:30
|
|
|
|
|
|
|
|
|
|
|
def test_no_git(prompts, fake_process, tmp_path):
|
|
|
|
os.chdir(tmp_path)
|
|
|
|
err = b"fatal: not a git repository (or any of the parent directories): .git"
|
2022-08-17 00:38:02 -04:00
|
|
|
fake_process.register_subprocess(
|
|
|
|
command="git rev-parse --git-dir", stderr=err, returncode=128
|
|
|
|
)
|
|
|
|
|
|
|
|
# test that all gitstatus fields (gitstatus, gitstatus.branch,
|
|
|
|
# gitstatus.porceclain, etc) are None and are formatted correctly in a
|
|
|
|
# format string like {gitstatus: hello {}}
|
|
|
|
for field in prompts.get_fields(gitstatus):
|
|
|
|
assert prompts.pick_val(field) is None
|
|
|
|
assert _format_value(prompts.pick(field), None, None) == ""
|
|
|
|
assert _format_value(prompts.pick(field), "hello {}", None) == ""
|