Skip to content

Commit 25d0722

Browse files
committed
tests for config
1 parent a106d25 commit 25d0722

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

tests/test_config.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from pathlib import Path
2+
3+
from qbpm.config import (
4+
DEFAULT_CONFIG_FILE,
5+
Config,
6+
find_config,
7+
find_qutebrowser_config_dir,
8+
)
9+
10+
from . import no_homedir_fixture # noqa: F401
11+
12+
13+
def test_no_config():
14+
assert find_config(None) == Config.load(DEFAULT_CONFIG_FILE)
15+
16+
17+
def test_empty_config(tmp_path: Path):
18+
file = tmp_path / "config.toml"
19+
file.touch()
20+
assert find_config(file) == Config()
21+
22+
23+
def test_default_config_location(tmp_path: Path):
24+
(tmp_path / "qbpm").mkdir()
25+
(tmp_path / "qbpm" / "config.toml").touch()
26+
assert find_config(None) == Config()
27+
28+
29+
def test_minimal_config(tmp_path: Path):
30+
file = tmp_path / "config.toml"
31+
file.write_text("""config_py_template = 'template'""")
32+
assert find_config(file) == Config(config_py_template="template")
33+
34+
35+
def test_full_config(tmp_path: Path):
36+
file = tmp_path / "config.toml"
37+
file.write_text("""
38+
config_py_template = \"""
39+
config.load_autoconfig()
40+
\"""
41+
qutebrowser_config_directory = "~/.config/qutebrowser"
42+
profile_directory = "profile"
43+
generate_desktop_file = false
44+
desktop_file_directory = "desktop"
45+
menu = "~/bin/my-dmenu"
46+
menu_prompt = "qbpm"
47+
""")
48+
assert find_config(file) == Config(
49+
config_py_template="config.load_autoconfig()\n",
50+
qutebrowser_config_directory=Path("~/.config/qutebrowser").expanduser(),
51+
profile_directory=Path("profile"),
52+
desktop_file_directory=Path("desktop"),
53+
generate_desktop_file=False,
54+
menu="~/bin/my-dmenu",
55+
menu_prompt="qbpm",
56+
)
57+
58+
59+
def test_find_qb_config(tmp_path: Path):
60+
qb_dir = tmp_path / "qb"
61+
qb_conf_dir = qb_dir / "config"
62+
qb_conf_dir.mkdir(parents=True)
63+
(qb_conf_dir / "config.py").touch()
64+
assert find_qutebrowser_config_dir(qb_dir) == qb_conf_dir
65+
assert find_qutebrowser_config_dir(qb_dir / "config") == qb_conf_dir
66+
67+
68+
def test_find_qb_config_default(tmp_path: Path):
69+
(tmp_path / "config.py").touch()
70+
assert find_qutebrowser_config_dir(None) == tmp_path
71+
72+
73+
def test_find_qutebrowser_none(tmp_path: Path):
74+
assert find_qutebrowser_config_dir(None) is None
75+
assert find_qutebrowser_config_dir(tmp_path / "config") is None

tests/test_main.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def test_profile_dir_option(tmp_path: Path):
1818
assert result.exit_code == 0
1919
assert result.output.strip() == str(tmp_path / "test")
2020
assert tmp_path / "test" in list(tmp_path.iterdir())
21+
assert (tmp_path / "applications" / "qbpm" / "test.desktop").exists()
2122

2223

2324
def test_profile_dir_env(tmp_path: Path):
@@ -57,3 +58,37 @@ def test_from_session(tmp_path: Path):
5758
assert result.exit_code == 0
5859
assert result.output.strip() == str(tmp_path / "test")
5960
assert (tmp_path / "test/data/sessions/_autosave.yml").read_text() == ("windows:\n")
61+
62+
63+
def test_config_file(tmp_path: Path):
64+
environ["QBPM_PROFILE_DIR"] = str(tmp_path)
65+
(tmp_path / "config.py").touch()
66+
config_file = tmp_path / "config.toml"
67+
config_file.write_text("config_py_template = '# Custom template {profile_name}'")
68+
result = run("-c", str(config_file), "new", "test")
69+
assert result.exit_code == 0
70+
profile_config = tmp_path / "test" / "config" / "config.py"
71+
assert "# Custom template test" in profile_config.read_text()
72+
73+
74+
def test_bad_config_file():
75+
result = run("-c", "/nonexistent/config.toml", "list")
76+
assert result.exit_code == 1
77+
assert "not a file" in result.output
78+
79+
80+
def test_no_desktop_file(tmp_path: Path):
81+
environ["QBPM_PROFILE_DIR"] = str(tmp_path)
82+
(tmp_path / "config.py").touch()
83+
run("-P", str(tmp_path), "new", "--no-desktop-file", "-C", str(tmp_path), "test")
84+
assert not (tmp_path / "applications" / "qbpm" / "test.desktop").exists()
85+
86+
87+
def test_desktop_file_directory(tmp_path: Path):
88+
environ["QBPM_PROFILE_DIR"] = str(tmp_path)
89+
(tmp_path / "config.py").touch()
90+
config_file = tmp_path / "config.toml"
91+
config_file.write_text(f'''config_py_template = ""
92+
desktop_file_directory="{tmp_path}"''')
93+
run("-P", str(tmp_path), "new", "-C", str(tmp_path), "test")
94+
assert not (tmp_path / "test.desktop").exists()

tests/test_menu.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from qbpm.menus import Dmenu, custom_dmenu
2+
3+
4+
def test_menu_prompt_formatting():
5+
dmenu = Dmenu(["my-menu", "--prompt", "{prompt}"])
6+
cmd = dmenu.command(["p1"], "qb ({qb_args})", "example.com")
7+
assert "--prompt" in cmd
8+
assert "qb (example.com)" in cmd
9+
10+
11+
def test_known_custom_menu():
12+
assert custom_dmenu(["fuzzel"]).menu_command == ["fuzzel", "--dmenu"]
13+
assert custom_dmenu("fuzzel").menu_command == ["fuzzel", "--dmenu"]
14+
assert "--dmenu" in custom_dmenu("~/bin/fuzzel").menu_command
15+
16+
17+
def test_custom_menu_list():
18+
menu = ["fuzzel", "--dmenu", "--prompt", "{prompt}>"]
19+
assert custom_dmenu(menu).menu_command == menu

tests/test_profiles.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,25 @@ def test_new_profile(tmp_path: Path):
8484
config.generate_desktop_file = False
8585
assert profiles.new_profile(profile, config)
8686
check_new_profile(profile)
87+
88+
89+
def test_config_template(tmp_path: Path):
90+
profile = Profile("test", tmp_path)
91+
template = "# Profile: {profile_name}\nconfig.source('{source_config_py}')"
92+
93+
profiles.create_profile(profile)
94+
profiles.create_config(profile, tmp_path / "config", template)
95+
96+
config_content = (profile.root / "config" / "config.py").read_text()
97+
assert "# Profile: test" in config_content
98+
assert f"config.source('{tmp_path / 'config' / 'config.py'}')" in config_content
99+
100+
101+
def test_missing_qb_config(tmp_path: Path):
102+
profile = Profile("test", tmp_path / "test")
103+
config = Config.load(None)
104+
config.qutebrowser_config_directory = tmp_path
105+
config.generate_desktop_file = False
106+
assert not profiles.new_profile(profile, config)
107+
config.qutebrowser_config_directory = tmp_path / "nonexistent"
108+
assert not profiles.new_profile(profile, config)

0 commit comments

Comments
 (0)