+1
pyproject.toml
+1
pyproject.toml
+83
tests/test_choose.py
+83
tests/test_choose.py
···
1
+
from os import environ
2
+
from pathlib import Path
3
+
4
+
from qbpm.choose import choose_profile, find_menu
5
+
6
+
7
+
def write_script(parent_dir: Path, name: str = "menu", contents: str = "") -> Path:
8
+
parent_dir.mkdir(exist_ok=True)
9
+
menu = parent_dir / name
10
+
menu.write_text(f"#!/bin/sh\n{contents}")
11
+
menu.chmod(0o700)
12
+
return menu
13
+
14
+
15
+
def test_choose(tmp_path: Path):
16
+
log = tmp_path / "log"
17
+
log.touch()
18
+
menu = write_script(tmp_path / "bin", contents=f"cat > {log}\necho p1")
19
+
write_script(
20
+
tmp_path / "bin",
21
+
name="qutebrowser",
22
+
contents=f'echo qutebrowser "$@" >> {log}',
23
+
)
24
+
environ["PATH"] = str(tmp_path / "bin") + ":" + environ["PATH"]
25
+
26
+
profile_dir = tmp_path / "profiles"
27
+
profile_dir.mkdir()
28
+
(profile_dir / "p1").mkdir()
29
+
(profile_dir / "p2").mkdir()
30
+
assert choose_profile(profile_dir, str(menu), False, ())
31
+
assert log.read_text().startswith(
32
+
f"""p1
33
+
p2
34
+
qutebrowser
35
+
qutebrowser -B {profile_dir / "p1"}"""
36
+
)
37
+
38
+
39
+
def test_find_installed_menu(tmp_path: Path):
40
+
write_script(tmp_path / "bin", name="dmenu")
41
+
environ["PATH"] = str(tmp_path / "bin")
42
+
environ["DISPLAY"] = ":1"
43
+
assert getattr(find_menu(None), "name", None) == "dmenu"
44
+
45
+
46
+
def test_override_menu_priority(tmp_path: Path):
47
+
write_script(tmp_path / "bin", name="fuzzel")
48
+
write_script(tmp_path / "bin", name="dmenu-wl")
49
+
environ["PATH"] = str(tmp_path / "bin")
50
+
environ["WAYLAND_DISPLAY"] = "wayland-2"
51
+
assert getattr(find_menu(None), "name", None) == "fuzzel"
52
+
assert getattr(find_menu("dmenu-wl"), "name", None) == "dmenu-wl"
53
+
54
+
55
+
def test_custom_menu():
56
+
dmenu = find_menu("/bin/sh -c")
57
+
assert dmenu is not None
58
+
assert (
59
+
dmenu.commandline(["p1", "p2"], "", "").strip() == 'echo "p1\np2" | /bin/sh -c'
60
+
)
61
+
62
+
63
+
def test_invalid_custom_menu():
64
+
assert find_menu("fake_command") is None
65
+
66
+
67
+
def test_custom_menu_default_args(tmp_path: Path):
68
+
menu = write_script(tmp_path / "bin", name="rofi")
69
+
environ["PATH"] = str(tmp_path / "bin")
70
+
environ["DISPLAY"] = ":1"
71
+
dmenu = find_menu(str(menu))
72
+
assert dmenu is not None
73
+
assert f"{menu} -dmenu" in dmenu.commandline(["p1", "p2"], "", "")
74
+
75
+
76
+
def test_custom_menu_custom_args(tmp_path: Path):
77
+
menu = write_script(tmp_path / "bin", name="rofi")
78
+
command = f"{menu} -custom -dmenu"
79
+
environ["PATH"] = str(tmp_path / "bin")
80
+
environ["DISPLAY"] = ":1"
81
+
dmenu = find_menu(command)
82
+
assert dmenu is not None
83
+
assert command in dmenu.commandline(["p1", "p2"], "", "")