Skip to content

Commit 8fd5cfa

Browse files
authored
tests: Basic doctests support (#791)
This will help us with docs, testing, and also helping inspire better API aesthetics.
2 parents bd3aa03 + 47910b5 commit 8fd5cfa

File tree

11 files changed

+399
-288
lines changed

11 files changed

+399
-288
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ $ pipx install --suffix=@next 'tmuxp' --pip-args '\--pre' --force
2222
### Internal
2323

2424
- libtmux updated from v0.12 to v0.14 {issue}`790`
25+
- Add [doctest](https://docs.python.org/3/library/doctest.html) w/
26+
[pytest + doctest](https://docs.pytest.org/en/7.1.x/how-to/doctest.html), ({issue}`791`).
2527

2628
## tmuxp 1.12.1 (2022-08-04)
2729

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
import tmuxp
99

1010
# Get the project root dir, which is the parent dir of this
11-
cwd = Path.cwd()
11+
cwd = Path(__file__).parent
1212
project_root = cwd.parent
1313

1414
sys.path.insert(0, str(project_root))
1515
sys.path.insert(0, str(cwd / "_ext"))
1616

1717
# package data
1818
about = {}
19-
with open("../tmuxp/__about__.py") as fp:
19+
with open(project_root / "tmuxp" / "__about__.py") as fp:
2020
exec(fp.read(), about)
2121

2222
extensions = [

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ max-line-length = 88
66
extend-ignore = E203,W503
77

88
[tool:pytest]
9-
addopts = --reruns=0
109
filterwarnings =
1110
ignore:distutils Version classes are deprecated. Use packaging.version instead.
11+
addopts = --reruns=0 --tb=short --no-header --showlocals --doctest-modules
12+
doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE
1213

1314
[isort]
1415
profile = black

tests/conftest.py

Lines changed: 1 addition & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1 @@
1-
import getpass
2-
import logging
3-
import os
4-
import pathlib
5-
6-
import pytest
7-
8-
from libtmux import exc
9-
from libtmux.server import Server
10-
from libtmux.test import TEST_SESSION_PREFIX, get_test_session_name, namer
11-
12-
logger = logging.getLogger(__name__)
13-
USING_ZSH = "zsh" in os.getenv("SHELL", "")
14-
15-
16-
@pytest.fixture(autouse=True, scope="session")
17-
def home_path(tmp_path_factory: pytest.TempPathFactory):
18-
return tmp_path_factory.mktemp("home")
19-
20-
21-
@pytest.fixture(autouse=True, scope="session")
22-
def user_path(home_path: pathlib.Path):
23-
p = home_path / getpass.getuser()
24-
p.mkdir()
25-
return p
26-
27-
28-
@pytest.mark.skipif(USING_ZSH, reason="Using ZSH")
29-
@pytest.fixture(autouse=USING_ZSH, scope="session")
30-
def zshrc(user_path: pathlib.Path):
31-
"""This quiets ZSH default message.
32-
33-
Needs a startup file .zshenv, .zprofile, .zshrc, .zlogin.
34-
"""
35-
p = user_path / ".zshrc"
36-
p.touch()
37-
return p
38-
39-
40-
@pytest.fixture(autouse=True)
41-
def home_path_default(user_path: pathlib.Path):
42-
os.environ["HOME"] = str(user_path)
43-
44-
45-
@pytest.fixture(scope="function")
46-
def monkeypatch_plugin_test_packages(monkeypatch):
47-
paths = [
48-
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_bwb/",
49-
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_bs/",
50-
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_r/",
51-
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_owc/",
52-
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_awf/",
53-
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_fail/",
54-
]
55-
for path in paths:
56-
monkeypatch.syspath_prepend(os.path.abspath(os.path.relpath(path)))
57-
58-
59-
@pytest.fixture(scope="function")
60-
def socket_name(request):
61-
return "tmuxp_test%s" % next(namer)
62-
63-
64-
@pytest.fixture(scope="function")
65-
def server(request, socket_name):
66-
t = Server()
67-
t.socket_name = socket_name
68-
69-
def fin():
70-
t.kill_server()
71-
72-
request.addfinalizer(fin)
73-
74-
return t
75-
76-
77-
@pytest.fixture(scope="function")
78-
def session(server):
79-
session_name = "tmuxp"
80-
81-
if not server.has_session(session_name):
82-
server.cmd(
83-
"-f",
84-
"/dev/null", # use a blank config to reduce side effects
85-
"new-session",
86-
"-d", # detached
87-
"-s",
88-
session_name,
89-
"/bin/sh", # use /bin/sh as a shell to reduce side effects
90-
# normally, it'd be -c, but new-session is special
91-
)
92-
93-
# find current sessions prefixed with tmuxp
94-
old_test_sessions = [
95-
s.get("session_name")
96-
for s in server._sessions
97-
if s.get("session_name").startswith(TEST_SESSION_PREFIX)
98-
]
99-
100-
TEST_SESSION_NAME = get_test_session_name(server=server)
101-
102-
try:
103-
session = server.new_session(session_name=TEST_SESSION_NAME)
104-
except exc.LibTmuxException as e:
105-
raise e
106-
107-
"""
108-
Make sure that tmuxp can :ref:`test_builder_visually` and switches to
109-
the newly created session for that testcase.
110-
"""
111-
try:
112-
server.switch_client(session.get("session_id"))
113-
except exc.LibTmuxException:
114-
# server.attach_session(session.get('session_id'))
115-
pass
116-
117-
for old_test_session in old_test_sessions:
118-
logger.debug("Old test test session %s found. Killing it." % old_test_session)
119-
server.kill_session(old_test_session)
120-
assert TEST_SESSION_NAME == session.get("session_name")
121-
assert TEST_SESSION_NAME != "tmuxp"
122-
123-
return session
1+
from tmuxp.conftest import * # noqa F40

tests/fixtures/config/expand1.py

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,51 @@
2929
],
3030
}
3131

32-
after_config = {
33-
"session_name": "sampleconfig",
34-
"start_directory": os.path.expanduser("~"),
35-
"windows": [
36-
{
37-
"window_name": "editor",
38-
"panes": [
39-
{"shell_command": [{"cmd": "vim"}, {"cmd": "top"}]},
40-
{"shell_command": [{"cmd": "vim"}]},
41-
{"shell_command": [{"cmd": 'cowsay "hey"'}]},
42-
],
43-
"layout": "main-verticle",
44-
},
45-
{
46-
"window_name": "logging",
47-
"panes": [{"shell_command": [{"cmd": "tail -F /var/log/syslog"}]}],
48-
},
49-
{
50-
"start_directory": "/var/log",
51-
"options": {"automatic-rename": True},
52-
"panes": [
53-
{"shell_command": [{"cmd": "htop"}]},
54-
{"shell_command": [{"cmd": "vim"}]},
55-
],
56-
},
57-
{
58-
"start_directory": os.path.normpath(
59-
os.path.join(os.path.expanduser("~"), "./")
60-
),
61-
"panes": [{"shell_command": [{"cmd": "pwd"}]}],
62-
},
63-
{
64-
"start_directory": os.path.normpath(
65-
os.path.join(os.path.expanduser("~"), "./asdf")
66-
),
67-
"panes": [{"shell_command": [{"cmd": "pwd"}]}],
68-
},
69-
{
70-
"start_directory": os.path.normpath(
71-
os.path.join(os.path.expanduser("~"), "../")
72-
),
73-
"panes": [{"shell_command": [{"cmd": "pwd"}]}],
74-
},
75-
{"panes": [{"shell_command": [{"cmd": "top"}]}]},
76-
],
77-
}
32+
33+
def after_config():
34+
return {
35+
"session_name": "sampleconfig",
36+
"start_directory": os.path.expanduser("~"),
37+
"windows": [
38+
{
39+
"window_name": "editor",
40+
"panes": [
41+
{"shell_command": [{"cmd": "vim"}, {"cmd": "top"}]},
42+
{"shell_command": [{"cmd": "vim"}]},
43+
{"shell_command": [{"cmd": 'cowsay "hey"'}]},
44+
],
45+
"layout": "main-verticle",
46+
},
47+
{
48+
"window_name": "logging",
49+
"panes": [{"shell_command": [{"cmd": "tail -F /var/log/syslog"}]}],
50+
},
51+
{
52+
"start_directory": "/var/log",
53+
"options": {"automatic-rename": True},
54+
"panes": [
55+
{"shell_command": [{"cmd": "htop"}]},
56+
{"shell_command": [{"cmd": "vim"}]},
57+
],
58+
},
59+
{
60+
"start_directory": os.path.normpath(
61+
os.path.join(os.path.expanduser("~"), "./")
62+
),
63+
"panes": [{"shell_command": [{"cmd": "pwd"}]}],
64+
},
65+
{
66+
"start_directory": os.path.normpath(
67+
os.path.join(os.path.expanduser("~"), "./asdf")
68+
),
69+
"panes": [{"shell_command": [{"cmd": "pwd"}]}],
70+
},
71+
{
72+
"start_directory": os.path.normpath(
73+
os.path.join(os.path.expanduser("~"), "../")
74+
),
75+
"panes": [{"shell_command": [{"cmd": "pwd"}]}],
76+
},
77+
{"panes": [{"shell_command": [{"cmd": "top"}]}]},
78+
],
79+
}

tests/fixtures/config/expand2.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
from .. import utils as test_utils
44

5-
unexpanded_yaml = test_utils.read_config_file("config/expand2-unexpanded.yaml")
6-
expanded_yaml = test_utils.read_config_file("config/expand2-expanded.yaml").format(
7-
HOME=os.path.expanduser("~")
8-
)
5+
6+
def unexpanded_yaml():
7+
return test_utils.read_config_file("config/expand2-unexpanded.yaml")
8+
9+
10+
def expanded_yaml():
11+
return test_utils.read_config_file("config/expand2-expanded.yaml").format(
12+
HOME=os.path.expanduser("~")
13+
)

0 commit comments

Comments
 (0)