Skip to content

Test refactoring #753

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 12 commits into from
Mar 13, 2022
6 changes: 3 additions & 3 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
```yaml
session_name: Should not execute
windows:
- panes:
- shell_command: echo "___$((1 + 3))___"
enter: false
- panes:
- shell_command: echo "___$((1 + 3))___"
enter: false
```

- #701: `tmuxp freeze` now accepts `--quiet` and `--yes` along with the
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PY_FILES= find . -type f -not -path '*/\.*' | grep -i '.*[.]py$$' 2> /dev/null
TEST_FILES= find . -type f -not -path '*/\.*' | grep -i '.*[.]\(yaml\|py\)$$' 2> /dev/null
DOC_FILES= find . -type f -not -path '*/\.*' | grep -i '.*[.]rst\$\|.*[.]md\$\|.*[.]css\$\|.*[.]py\$\|mkdocs\.yml\|CHANGES\|TODO\|.*conf\.py' 2> /dev/null
SHELL := /bin/bash

Expand All @@ -23,7 +24,7 @@ start:
$(MAKE) test; poetry run ptw .

watch_test:
if command -v entr > /dev/null; then ${PY_FILES} | entr -c $(MAKE) test; else $(MAKE) test entr_warn; fi
if command -v entr > /dev/null; then ${TEST_FILES} | entr -c $(MAKE) test; else $(MAKE) test entr_warn; fi

build_docs:
$(MAKE) -C docs html
Expand Down
3 changes: 3 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ $ tmuxp load /path/to/folder/
```

Name of the config, assume `$HOME/.tmuxp/myconfig.yaml`:

```console
$ tmuxp load myconfig
```
Expand Down Expand Up @@ -236,11 +237,13 @@ All of these options can be preselected to skip the prompt:
```console
$ tmuxp load -y config
```

- Detached / open in background:

```console
$ tmuxp load -d config
```

- Append windows to existing session

```console
Expand Down
38 changes: 2 additions & 36 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,15 @@ exclude = .*/,.tox,*.egg,tmuxp/_compat.py,tmuxp/__*__.py,
select = E,W,F,N
max-line-length = 88
# Stuff we ignore thanks to black: https://github.com/ambv/black/issues/429
ignore = E111,
E121,
E122,
E123,
E124,
E125,
E126,
E201,
E202,
E203,
E221,
E222,
E225,
E226,
E227,
E231,
E241,
E251,
E261,
E262,
E265,
E271,
E272,
E302,
E303,
E306,
E502,
E701,
E702,
E703,
E704,
W291,
W292,
W293,
W391,
W503
extend-ignore = E203

[tool:pytest]
addopts = --reruns=0
filterwarnings =
ignore:distutils Version classes are deprecated. Use packaging.version instead.

[isort]
profile = black
combine_as_imports= true
default_section = THIRDPARTY
include_trailing_comma = true
Expand Down
8 changes: 4 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
~~~~~~~~~~~

"""
import os
import pathlib

current_dir = os.path.abspath(os.path.dirname(__file__))
example_dir = os.path.abspath(os.path.join(current_dir, "..", "examples"))
fixtures_dir = os.path.realpath(os.path.join(current_dir, "fixtures"))
TESTS_PATH = pathlib.Path(__file__).parent
EXAMPLE_PATH = TESTS_PATH.parent / "examples"
FIXTURES_PATH = TESTS_PATH / "fixtures"
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import getpass
import logging
import os
import pathlib

import pytest

Expand All @@ -10,6 +12,23 @@
logger = logging.getLogger(__name__)


@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.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 = [
Expand Down
124 changes: 73 additions & 51 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,95 +1,118 @@
"""Test for tmuxp configuration import, inlining, expanding and export."""
import os
import pathlib
import typing
from typing import Union

import pytest

import kaptan

from tmuxp import config, exc

from . import example_dir
from .fixtures import config as fixtures
from . import EXAMPLE_PATH

TMUXP_DIR = os.path.join(os.path.dirname(__file__), ".tmuxp")
if typing.TYPE_CHECKING:
from .fixtures import config as ConfigFixture


def load_yaml(yaml):
return kaptan.Kaptan(handler="yaml").import_config(yaml).get()
@pytest.fixture
def config_fixture():
"""Deferred import of tmuxp.tests.fixtures.*

pytest setup (conftest.py) patches os.environ["HOME"], delay execution of
os.path.expanduser until here.
"""
from .fixtures import config as config_fixture

return config_fixture


def load_config(_file):
return kaptan.Kaptan().import_config(_file).get()
def load_yaml(path: Union[str, pathlib.Path]) -> str:
return (
kaptan.Kaptan(handler="yaml")
.import_config(str(path) if isinstance(path, pathlib.Path) else path)
.get()
)


def load_config(path: Union[str, pathlib.Path]) -> str:
return (
kaptan.Kaptan()
.import_config(str(path) if isinstance(path, pathlib.Path) else path)
.get()
)

def test_export_json(tmpdir):
json_config_file = tmpdir.join("config.json")

def test_export_json(tmp_path: pathlib.Path, config_fixture: "ConfigFixture"):
json_config_file = tmp_path / "config.json"

configparser = kaptan.Kaptan()
configparser.import_config(fixtures.sampleconfig.sampleconfigdict)
configparser.import_config(config_fixture.sampleconfig.sampleconfigdict)

json_config_data = configparser.export("json", indent=2)

json_config_file.write(json_config_data)
json_config_file.write_text(json_config_data, encoding="utf-8")

new_config = kaptan.Kaptan()
new_config_data = new_config.import_config(str(json_config_file)).get()
assert fixtures.sampleconfig.sampleconfigdict == new_config_data
assert config_fixture.sampleconfig.sampleconfigdict == new_config_data


def test_export_yaml(tmpdir):
yaml_config_file = tmpdir.join("config.yaml")
def test_export_yaml(tmp_path: pathlib.Path, config_fixture: "ConfigFixture"):
yaml_config_file = tmp_path / "config.yaml"

configparser = kaptan.Kaptan()
sampleconfig = config.inline(fixtures.sampleconfig.sampleconfigdict)
sampleconfig = config.inline(config_fixture.sampleconfig.sampleconfigdict)
configparser.import_config(sampleconfig)

yaml_config_data = configparser.export("yaml", indent=2, default_flow_style=False)

yaml_config_file.write(yaml_config_data)
yaml_config_file.write_text(yaml_config_data, encoding="utf-8")

new_config_data = load_config(str(yaml_config_file))
assert fixtures.sampleconfig.sampleconfigdict == new_config_data
assert config_fixture.sampleconfig.sampleconfigdict == new_config_data


def test_scan_config(tmpdir):
def test_scan_config(tmp_path: pathlib.Path):
configs = []

garbage_file = tmpdir.join("config.psd")
garbage_file.write("wat")
garbage_file = tmp_path / "config.psd"
garbage_file.write_text("wat", encoding="utf-8")

for r, d, f in os.walk(str(tmpdir)):
for r, d, f in os.walk(str(tmp_path)):
for filela in (x for x in f if x.endswith((".json", ".ini", "yaml"))):
configs.append(str(tmpdir.join(filela)))
configs.append(str(tmp_path / filela))

files = 0
if tmpdir.join("config.json").check():
config_json = tmp_path / "config.json"
config_yaml = tmp_path / "config.yaml"
config_ini = tmp_path / "config.ini"
if config_json.exists():
files += 1
assert str(tmpdir.join("config.json")) in configs
assert str(config_json) in configs

if tmpdir.join("config.yaml").check():
if config_yaml.exists():
files += 1
assert str(tmpdir.join("config.yaml")) in configs
assert str(config_yaml) in configs

if tmpdir.join("config.ini").check():
if config_ini.exists():
files += 1
assert str(tmpdir.join("config.ini")) in configs
assert str(config_ini) in configs

assert len(configs) == files


def test_config_expand1():
def test_config_expand1(config_fixture: "ConfigFixture"):
"""Expand shell commands from string to list."""
test_config = config.expand(fixtures.expand1.before_config)
assert test_config == fixtures.expand1.after_config
test_config = config.expand(config_fixture.expand1.before_config)
assert test_config == config_fixture.expand1.after_config


def test_config_expand2():
def test_config_expand2(config_fixture: "ConfigFixture"):
"""Expand shell commands from string to list."""

unexpanded_dict = load_yaml(fixtures.expand2.unexpanded_yaml)

expanded_dict = load_yaml(fixtures.expand2.expanded_yaml)

unexpanded_dict = load_yaml(config_fixture.expand2.unexpanded_yaml)
expanded_dict = load_yaml(config_fixture.expand2.expanded_yaml)
assert config.expand(unexpanded_dict) == expanded_dict


Expand Down Expand Up @@ -205,31 +228,31 @@ def test_inheritance_config():
assert config == inheritance_config_after


def test_shell_command_before():
def test_shell_command_before(config_fixture: "ConfigFixture"):
"""Config inheritance for the nested 'start_command'."""
test_config = fixtures.shell_command_before.config_unexpanded
test_config = config_fixture.shell_command_before.config_unexpanded
test_config = config.expand(test_config)

assert test_config == fixtures.shell_command_before.config_expanded
assert test_config == config_fixture.shell_command_before.config_expanded

test_config = config.trickle(test_config)
assert test_config == fixtures.shell_command_before.config_after
assert test_config == config_fixture.shell_command_before.config_after


def test_in_session_scope():
sconfig = load_yaml(fixtures.shell_command_before_session.before)
def test_in_session_scope(config_fixture: "ConfigFixture"):
sconfig = load_yaml(config_fixture.shell_command_before_session.before)

config.validate_schema(sconfig)

assert config.expand(sconfig) == sconfig
assert config.expand(config.trickle(sconfig)) == load_yaml(
fixtures.shell_command_before_session.expected
config_fixture.shell_command_before_session.expected
)


def test_trickle_relative_start_directory():
test_config = config.trickle(fixtures.trickle.before)
assert test_config == fixtures.trickle.expected
def test_trickle_relative_start_directory(config_fixture: "ConfigFixture"):
test_config = config.trickle(config_fixture.trickle.before)
assert test_config == config_fixture.trickle.expected


def test_trickle_window_with_no_pane_config():
Expand All @@ -250,7 +273,7 @@ def test_trickle_window_with_no_pane_config():
}


def test_expands_blank_panes():
def test_expands_blank_panes(config_fixture: "ConfigFixture"):
"""Expand blank config into full form.

Handle ``NoneType`` and 'blank'::
Expand All @@ -277,10 +300,9 @@ def test_expands_blank_panes():
'shell_command': ['']

"""

yaml_config_file = os.path.join(example_dir, "blank-panes.yaml")
yaml_config_file = EXAMPLE_PATH / "blank-panes.yaml"
test_config = load_config(yaml_config_file)
assert config.expand(test_config) == fixtures.expand_blank.expected
assert config.expand(test_config) == config_fixture.expand_blank.expected


def test_no_session_name():
Expand Down
4 changes: 0 additions & 4 deletions tests/test_config_teamocil.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Test for tmuxp teamocil configuration."""
import os

import pytest

import kaptan
Expand All @@ -9,8 +7,6 @@

from .fixtures import config_teamocil as fixtures

TMUXP_DIR = os.path.join(os.path.dirname(__file__), ".tmuxp")


@pytest.mark.parametrize(
"teamocil_yaml,teamocil_dict,tmuxp_dict",
Expand Down
4 changes: 0 additions & 4 deletions tests/test_config_tmuxinator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Test for tmuxp tmuxinator configuration."""
import os

import pytest

import kaptan
Expand All @@ -9,8 +7,6 @@

from .fixtures import config_tmuxinator as fixtures

TMUXP_DIR = os.path.join(os.path.dirname(__file__), ".tmuxp")


@pytest.mark.parametrize(
"tmuxinator_yaml,tmuxinator_dict,tmuxp_dict",
Expand Down
Loading