Skip to content

Refactor tests/ fixture and constants access #761

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 4 commits into from
Mar 20, 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
11 changes: 0 additions & 11 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
"""Tests for tmuxp.

tmuxp.tests
~~~~~~~~~~~

"""
import pathlib

TESTS_PATH = pathlib.Path(__file__).parent
EXAMPLE_PATH = TESTS_PATH.parent / "examples"
FIXTURES_PATH = TESTS_PATH / "fixtures"
5 changes: 5 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pathlib

TESTS_PATH = pathlib.Path(__file__).parent
EXAMPLE_PATH = TESTS_PATH.parent / "examples"
FIXTURE_PATH = TESTS_PATH / "fixtures"
2 changes: 1 addition & 1 deletion tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import _util # noqa
from . import utils # noqa
6 changes: 3 additions & 3 deletions tests/fixtures/config/expand2.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os

from .._util import load_fixture
from .. import utils as test_utils

unexpanded_yaml = load_fixture("config/expand2-unexpanded.yaml")
expanded_yaml = load_fixture("config/expand2-expanded.yaml").format(
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("~")
)
8 changes: 5 additions & 3 deletions tests/fixtures/config/shell_command_before_session.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from .._util import load_fixture
from .. import utils as test_utils

before = load_fixture("config/shell_command_before_session.yaml")
expected = load_fixture("config/shell_command_before_session-expected.yaml")
before = test_utils.read_config_file("config/shell_command_before_session.yaml")
expected = test_utils.read_config_file(
"config/shell_command_before_session-expected.yaml"
)
4 changes: 2 additions & 2 deletions tests/fixtures/config_teamocil/layouts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .._util import load_fixture
from .. import utils as test_utils

teamocil_yaml = load_fixture("config_teamocil/layouts.yaml")
teamocil_yaml = test_utils.read_config_file("config_teamocil/layouts.yaml")

teamocil_dict = {
"two-windows": {
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/config_teamocil/test1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .._util import load_fixture
from .. import utils as test_utils

teamocil_yaml = load_fixture("config_teamocil/test1.yaml")
teamocil_yaml = test_utils.read_config_file("config_teamocil/test1.yaml")
teamocil_conf = {
"windows": [
{
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/config_teamocil/test2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .._util import load_fixture
from .. import utils as test_utils

teamocil_yaml = load_fixture("config_teamocil/test2.yaml")
teamocil_yaml = test_utils.read_config_file("config_teamocil/test2.yaml")
teamocil_dict = {
"windows": [
{
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/config_teamocil/test3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .._util import load_fixture
from .. import utils as test_utils

teamocil_yaml = load_fixture("config_teamocil/test3.yaml")
teamocil_yaml = test_utils.read_config_file("config_teamocil/test3.yaml")

teamocil_dict = {
"windows": [
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/config_teamocil/test4.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .._util import load_fixture
from .. import utils as test_utils

teamocil_yaml = load_fixture("config_teamocil/test4.yaml")
teamocil_yaml = test_utils.read_config_file("config_teamocil/test4.yaml")

teamocil_dict = {
"windows": [
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/config_tmuxinator/test1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .._util import load_fixture
from .. import utils as test_utils

tmuxinator_yaml = load_fixture("config_tmuxinator/test1.yaml")
tmuxinator_yaml = test_utils.read_config_file("config_tmuxinator/test1.yaml")
tmuxinator_dict = {
"windows": [
{"editor": {"layout": "main-vertical", "panes": ["vim", "guard"]}},
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/config_tmuxinator/test2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .._util import load_fixture
from .. import utils as test_utils

tmuxinator_yaml = load_fixture("config_tmuxinator/test2.yaml")
tmuxinator_yaml = test_utils.read_config_file("config_tmuxinator/test2.yaml")

tmuxinator_dict = {
"project_name": "sample",
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/config_tmuxinator/test3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .._util import load_fixture
from .. import utils as test_utils

tmuxinator_yaml = load_fixture("config_tmuxinator/test3.yaml")
tmuxinator_yaml = test_utils.read_config_file("config_tmuxinator/test3.yaml")

tmuxinator_dict = {
"name": "sample",
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/_util.py → tests/fixtures/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pathlib

FIXTURE_PATH = pathlib.Path(__file__).parent
from ..constants import FIXTURE_PATH


def load_fixture(_file): # return fixture data, relative to __file__
def read_config_file(_file): # return fixture data, relative to __file__
return open(FIXTURE_PATH / _file).read()


Expand Down
21 changes: 11 additions & 10 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
)
from tmuxp.workspacebuilder import WorkspaceBuilder

from .fixtures._util import FIXTURE_PATH, load_fixture
from .constants import FIXTURE_PATH
from .fixtures import utils as test_utils


def test_creates_config_dir_not_exists(tmp_path: pathlib.Path):
Expand Down Expand Up @@ -861,7 +862,7 @@ def test_help(cli_args, monkeypatch):
],
)
def test_import_teamocil(cli_args, inputs, tmp_path, monkeypatch):
teamocil_config = load_fixture("config_teamocil/test4.yaml")
teamocil_config = test_utils.read_config_file("config_teamocil/test4.yaml")

teamocil_path = tmp_path / ".teamocil"
teamocil_path.mkdir()
Expand Down Expand Up @@ -900,7 +901,7 @@ def test_import_teamocil(cli_args, inputs, tmp_path, monkeypatch):
],
)
def test_import_tmuxinator(cli_args, inputs, tmp_path, monkeypatch):
tmuxinator_config = load_fixture("config_tmuxinator/test3.yaml")
tmuxinator_config = test_utils.read_config_file("config_tmuxinator/test3.yaml")

tmuxinator_path = tmp_path / ".tmuxinator"
tmuxinator_path.mkdir()
Expand Down Expand Up @@ -1103,7 +1104,7 @@ def test_ls_cli(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path):
def test_load_plugins(monkeypatch_plugin_test_packages):
from tmuxp_test_plugin_bwb.plugin import PluginBeforeWorkspaceBuilder

plugins_config = load_fixture("workspacebuilder/plugin_bwb.yaml")
plugins_config = test_utils.read_config_file("workspacebuilder/plugin_bwb.yaml")

sconfig = kaptan.Kaptan(handler="yaml")
sconfig = sconfig.import_config(plugins_config).get()
Expand Down Expand Up @@ -1186,7 +1187,7 @@ def test_plugin_system_before_script(


def test_reattach_plugins(monkeypatch_plugin_test_packages, server):
config_plugins = load_fixture("workspacebuilder/plugin_r.yaml")
config_plugins = test_utils.read_config_file("workspacebuilder/plugin_r.yaml")

sconfig = kaptan.Kaptan(handler="yaml")
sconfig = sconfig.import_config(config_plugins).get()
Expand Down Expand Up @@ -1217,7 +1218,7 @@ def test_load_attached(server, monkeypatch):

monkeypatch.setattr("libtmux.session.Session.attach_session", attach_session_mock)

yaml_config = load_fixture("workspacebuilder/two_pane.yaml")
yaml_config = test_utils.read_config_file("workspacebuilder/two_pane.yaml")
sconfig = kaptan.Kaptan(handler="yaml")
sconfig = sconfig.import_config(yaml_config).get()

Expand All @@ -1237,7 +1238,7 @@ def test_load_attached_detached(server, monkeypatch):

monkeypatch.setattr("libtmux.session.Session.attach_session", attach_session_mock)

yaml_config = load_fixture("workspacebuilder/two_pane.yaml")
yaml_config = test_utils.read_config_file("workspacebuilder/two_pane.yaml")
sconfig = kaptan.Kaptan(handler="yaml")
sconfig = sconfig.import_config(yaml_config).get()

Expand All @@ -1257,7 +1258,7 @@ def test_load_attached_within_tmux(server, monkeypatch):

monkeypatch.setattr("libtmux.session.Session.switch_client", switch_client_mock)

yaml_config = load_fixture("workspacebuilder/two_pane.yaml")
yaml_config = test_utils.read_config_file("workspacebuilder/two_pane.yaml")
sconfig = kaptan.Kaptan(handler="yaml")
sconfig = sconfig.import_config(yaml_config).get()

Expand All @@ -1277,7 +1278,7 @@ def test_load_attached_within_tmux_detached(server, monkeypatch):

monkeypatch.setattr("libtmux.session.Session.switch_client", switch_client_mock)

yaml_config = load_fixture("workspacebuilder/two_pane.yaml")
yaml_config = test_utils.read_config_file("workspacebuilder/two_pane.yaml")
sconfig = kaptan.Kaptan(handler="yaml")
sconfig = sconfig.import_config(yaml_config).get()

Expand All @@ -1289,7 +1290,7 @@ def test_load_attached_within_tmux_detached(server, monkeypatch):


def test_load_append_windows_to_current_session(server, monkeypatch):
yaml_config = load_fixture("workspacebuilder/two_pane.yaml")
yaml_config = test_utils.read_config_file("workspacebuilder/two_pane.yaml")
sconfig = kaptan.Kaptan(handler="yaml")
sconfig = sconfig.import_config(yaml_config).get()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from tmuxp import config, exc

from . import EXAMPLE_PATH
from .constants import EXAMPLE_PATH

if typing.TYPE_CHECKING:
from .fixtures import config as ConfigFixture
Expand Down
12 changes: 6 additions & 6 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from tmuxp.exc import BeforeLoadScriptError, BeforeLoadScriptNotExists
from tmuxp.util import get_session, run_before_script

from . import FIXTURES_PATH
from .constants import FIXTURE_PATH


def test_raise_BeforeLoadScriptNotExists_if_not_exists():
script_file = FIXTURES_PATH / "script_noexists.sh"
script_file = FIXTURE_PATH / "script_noexists.sh"

with pytest.raises(BeforeLoadScriptNotExists):
run_before_script(script_file)
Expand All @@ -19,30 +19,30 @@ def test_raise_BeforeLoadScriptNotExists_if_not_exists():


def test_raise_BeforeLoadScriptError_if_retcode():
script_file = FIXTURES_PATH / "script_failed.sh"
script_file = FIXTURE_PATH / "script_failed.sh"

with pytest.raises(BeforeLoadScriptError):
run_before_script(script_file)


def test_return_stdout_if_ok(capsys):
script_file = FIXTURES_PATH / "script_complete.sh"
script_file = FIXTURE_PATH / "script_complete.sh"

run_before_script(script_file)
out, err = capsys.readouterr()
assert "hello" in out


def test_beforeload_returncode():
script_file = FIXTURES_PATH / "script_failed.sh"
script_file = FIXTURE_PATH / "script_failed.sh"

with pytest.raises(exc.BeforeLoadScriptError) as excinfo:
run_before_script(script_file)
assert excinfo.match(r"113")


def test_beforeload_returns_stderr_messages():
script_file = FIXTURES_PATH / "script_failed.sh"
script_file = FIXTURE_PATH / "script_failed.sh"

with pytest.raises(exc.BeforeLoadScriptError) as excinfo:
run_before_script(script_file)
Expand Down
Loading