xonsh/tests/prompt/test_gitstatus.py

98 lines
2.8 KiB
Python
Raw Normal View History

import os
import pytest
from xonsh.prompt import gitstatus
2022-04-23 07:01:04 +05:30
from xonsh.prompt.base import _format_value
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
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()
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
update test xsh usage (#4581) * todo * test: remove usage of DummyEnv and setting .env attribute on xession fixture one step closer to making too much of tweaking to xession during tests * test: fix tests vox and gitstatus-prompt * docs: update test-fixture usage * fix: import flake8 error * test: remove direct access to XSH in tests * test: remove usage of XSH in test files * todo * test: use tmp-dir to create stubs * refactor: use fixture factory to mock out XonshSession * refactor: remove direct access of XSH from functions * refactor: remove direct access of XSH from functions * fix: qa checks * refactor: rename variables to match their values * test: update failing tests because it had no PATH set previously * fix: remove builtins usage from pyghooks.py * style: * refactor: update tests to use fixtures * fix: env varialbe is setup per function some tests accidentally update the env variables and that is leaking into next tests * fix: failing vox tests * test: set commands_cache per test * test: fix failing tests * fix: failing tests on linux ptk-highlight * fix: failing tests on Windows cwd-prompt * test: copy env as to not affect original object * fix: lazily evaluate cmds-cache in pyghooks * test: fix failing tests * fix: qa errors import * test: set commands-cache per test while caching path results * test: speedup test_thread_local_swap * fix: failing tests on windows * refactor: Execer doesn't control session * refactor: XSH.unload will take care of reversing builtins attrs set * test: use env.update over monkeypatch * Revert "test: use env.update over monkeypatch" This reverts commit 010a5022247a098f1741966b8af1bf758663480e.
2022-01-08 04:03:22 +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"),
"{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"""\
## 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"""\
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)
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)
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
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"
for cmd in (
"git status --porcelain --branch",
"git rev-parse --git-dir",
"git diff --numstat",
):
fake_process.register_subprocess(
command=cmd,
stderr=err,
returncode=128,
)
exp = ""
assert prompts.pick_val("gitstatus.repo_path") == ""
assert format(prompts.pick("gitstatus")) == exp
assert _format_value(prompts.pick("gitstatus"), None, None) == exp
assert _format_value(prompts.pick("gitstatus"), "{}", None) == exp