Skip to content

Basic doctests #791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ $ pipx install --suffix=@next 'tmuxp' --pip-args '\--pre' --force
### Internal

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

## tmuxp 1.12.1 (2022-08-04)

Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import tmuxp

# Get the project root dir, which is the parent dir of this
cwd = Path.cwd()
cwd = Path(__file__).parent
project_root = cwd.parent

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

# package data
about = {}
with open("../tmuxp/__about__.py") as fp:
with open(project_root / "tmuxp" / "__about__.py") as fp:
exec(fp.read(), about)

extensions = [
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ max-line-length = 88
extend-ignore = E203,W503

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

[isort]
profile = black
Expand Down
124 changes: 1 addition & 123 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,123 +1 @@
import getpass
import logging
import os
import pathlib

import pytest

from libtmux import exc
from libtmux.server import Server
from libtmux.test import TEST_SESSION_PREFIX, get_test_session_name, namer

logger = logging.getLogger(__name__)
USING_ZSH = "zsh" in os.getenv("SHELL", "")


@pytest.fixture(autouse=True, scope="session")
def home_path(tmp_path_factory: pytest.TempPathFactory):
return tmp_path_factory.mktemp("home")


@pytest.fixture(autouse=True, scope="session")
def user_path(home_path: pathlib.Path):
p = home_path / getpass.getuser()
p.mkdir()
return p


@pytest.mark.skipif(USING_ZSH, reason="Using ZSH")
@pytest.fixture(autouse=USING_ZSH, scope="session")
def zshrc(user_path: pathlib.Path):
"""This quiets ZSH default message.

Needs a startup file .zshenv, .zprofile, .zshrc, .zlogin.
"""
p = user_path / ".zshrc"
p.touch()
return p


@pytest.fixture(autouse=True)
def home_path_default(user_path: pathlib.Path):
os.environ["HOME"] = str(user_path)


@pytest.fixture(scope="function")
def monkeypatch_plugin_test_packages(monkeypatch):
paths = [
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_bwb/",
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_bs/",
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_r/",
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_owc/",
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_awf/",
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_fail/",
]
for path in paths:
monkeypatch.syspath_prepend(os.path.abspath(os.path.relpath(path)))


@pytest.fixture(scope="function")
def socket_name(request):
return "tmuxp_test%s" % next(namer)


@pytest.fixture(scope="function")
def server(request, socket_name):
t = Server()
t.socket_name = socket_name

def fin():
t.kill_server()

request.addfinalizer(fin)

return t


@pytest.fixture(scope="function")
def session(server):
session_name = "tmuxp"

if not server.has_session(session_name):
server.cmd(
"-f",
"/dev/null", # use a blank config to reduce side effects
"new-session",
"-d", # detached
"-s",
session_name,
"/bin/sh", # use /bin/sh as a shell to reduce side effects
# normally, it'd be -c, but new-session is special
)

# find current sessions prefixed with tmuxp
old_test_sessions = [
s.get("session_name")
for s in server._sessions
if s.get("session_name").startswith(TEST_SESSION_PREFIX)
]

TEST_SESSION_NAME = get_test_session_name(server=server)

try:
session = server.new_session(session_name=TEST_SESSION_NAME)
except exc.LibTmuxException as e:
raise e

"""
Make sure that tmuxp can :ref:`test_builder_visually` and switches to
the newly created session for that testcase.
"""
try:
server.switch_client(session.get("session_id"))
except exc.LibTmuxException:
# server.attach_session(session.get('session_id'))
pass

for old_test_session in old_test_sessions:
logger.debug("Old test test session %s found. Killing it." % old_test_session)
server.kill_session(old_test_session)
assert TEST_SESSION_NAME == session.get("session_name")
assert TEST_SESSION_NAME != "tmuxp"

return session
from tmuxp.conftest import * # noqa F40
94 changes: 48 additions & 46 deletions tests/fixtures/config/expand1.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,51 @@
],
}

after_config = {
"session_name": "sampleconfig",
"start_directory": os.path.expanduser("~"),
"windows": [
{
"window_name": "editor",
"panes": [
{"shell_command": [{"cmd": "vim"}, {"cmd": "top"}]},
{"shell_command": [{"cmd": "vim"}]},
{"shell_command": [{"cmd": 'cowsay "hey"'}]},
],
"layout": "main-verticle",
},
{
"window_name": "logging",
"panes": [{"shell_command": [{"cmd": "tail -F /var/log/syslog"}]}],
},
{
"start_directory": "/var/log",
"options": {"automatic-rename": True},
"panes": [
{"shell_command": [{"cmd": "htop"}]},
{"shell_command": [{"cmd": "vim"}]},
],
},
{
"start_directory": os.path.normpath(
os.path.join(os.path.expanduser("~"), "./")
),
"panes": [{"shell_command": [{"cmd": "pwd"}]}],
},
{
"start_directory": os.path.normpath(
os.path.join(os.path.expanduser("~"), "./asdf")
),
"panes": [{"shell_command": [{"cmd": "pwd"}]}],
},
{
"start_directory": os.path.normpath(
os.path.join(os.path.expanduser("~"), "../")
),
"panes": [{"shell_command": [{"cmd": "pwd"}]}],
},
{"panes": [{"shell_command": [{"cmd": "top"}]}]},
],
}

def after_config():
return {
"session_name": "sampleconfig",
"start_directory": os.path.expanduser("~"),
"windows": [
{
"window_name": "editor",
"panes": [
{"shell_command": [{"cmd": "vim"}, {"cmd": "top"}]},
{"shell_command": [{"cmd": "vim"}]},
{"shell_command": [{"cmd": 'cowsay "hey"'}]},
],
"layout": "main-verticle",
},
{
"window_name": "logging",
"panes": [{"shell_command": [{"cmd": "tail -F /var/log/syslog"}]}],
},
{
"start_directory": "/var/log",
"options": {"automatic-rename": True},
"panes": [
{"shell_command": [{"cmd": "htop"}]},
{"shell_command": [{"cmd": "vim"}]},
],
},
{
"start_directory": os.path.normpath(
os.path.join(os.path.expanduser("~"), "./")
),
"panes": [{"shell_command": [{"cmd": "pwd"}]}],
},
{
"start_directory": os.path.normpath(
os.path.join(os.path.expanduser("~"), "./asdf")
),
"panes": [{"shell_command": [{"cmd": "pwd"}]}],
},
{
"start_directory": os.path.normpath(
os.path.join(os.path.expanduser("~"), "../")
),
"panes": [{"shell_command": [{"cmd": "pwd"}]}],
},
{"panes": [{"shell_command": [{"cmd": "top"}]}]},
],
}
13 changes: 9 additions & 4 deletions tests/fixtures/config/expand2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

from .. import utils as test_utils

unexpanded_yaml = test_utils.read_config_file("config/expand2-unexpanded.yaml")
expanded_yaml = test_utils.read_config_file("config/expand2-expanded.yaml").format(
HOME=os.path.expanduser("~")
)

def unexpanded_yaml():
return test_utils.read_config_file("config/expand2-unexpanded.yaml")


def expanded_yaml():
return test_utils.read_config_file("config/expand2-expanded.yaml").format(
HOME=os.path.expanduser("~")
)
Loading