From 8238ddf11a4cf7370cb8e0928c32ebe282079a0d Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 09:15:10 -0500 Subject: [PATCH 01/12] config(flake8): Update to latest black formatting suggestion See also: https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html?highlight=compatible#id2 --- setup.cfg | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/setup.cfg b/setup.cfg index 84854c63997..67686ffa91c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,42 +3,7 @@ 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 From 58e426c8d565d30203115fe781235f6ca8968241 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 09:17:05 -0500 Subject: [PATCH 02/12] ci(isort): Use black config See also: https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html?highlight=compatible#isort --- setup.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.cfg b/setup.cfg index 67686ffa91c..c271bb3b4f3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -11,6 +11,7 @@ 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 From 8f057fd668a31b9841b59b0e1ab9a97be15ae197 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 08:42:41 -0500 Subject: [PATCH 03/12] tests(conf): Refactor tmpdir -> tmp_path --- tests/test_config.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 181751684bf..61ce2debe7a 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,5 +1,6 @@ """Test for tmuxp configuration import, inlining, expanding and export.""" import os +import pathlib import pytest @@ -10,7 +11,7 @@ from . import example_dir from .fixtures import config as fixtures -TMUXP_DIR = os.path.join(os.path.dirname(__file__), ".tmuxp") +TMUXP_DIR = pathlib.Path(__file__).parent / ".tmuxp" def load_yaml(yaml): @@ -21,23 +22,23 @@ def load_config(_file): return kaptan.Kaptan().import_config(_file).get() -def test_export_json(tmpdir): - json_config_file = tmpdir.join("config.json") +def test_export_json(tmp_path: pathlib.Path): + json_config_file = tmp_path / "config.json" configparser = kaptan.Kaptan() configparser.import_config(fixtures.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 -def test_export_yaml(tmpdir): - yaml_config_file = tmpdir.join("config.yaml") +def test_export_yaml(tmp_path: pathlib.Path): + yaml_config_file = tmp_path / "config.yaml" configparser = kaptan.Kaptan() sampleconfig = config.inline(fixtures.sampleconfig.sampleconfigdict) @@ -45,34 +46,37 @@ def test_export_yaml(tmpdir): 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 -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 From a1d56e3bc2c55ee372c93e2425f0f036f0749693 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 09:07:57 -0500 Subject: [PATCH 04/12] refactor(tests): Move test dir constants to pathlib.Path --- tests/__init__.py | 8 ++++---- tests/test_config.py | 19 ++++++++++++++----- tests/test_util.py | 12 +++++------- tests/test_workspacebuilder.py | 20 ++++++++++---------- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 89d3f1684f0..1872e36b130 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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_dir = pathlib.Path(__file__).parent +example_dir = tests_dir.parent / "examples" +fixtures_dir = tests_dir / "fixtures" diff --git a/tests/test_config.py b/tests/test_config.py index 61ce2debe7a..ba175e8bc20 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,6 +1,7 @@ """Test for tmuxp configuration import, inlining, expanding and export.""" import os import pathlib +from typing import Union import pytest @@ -14,12 +15,20 @@ TMUXP_DIR = pathlib.Path(__file__).parent / ".tmuxp" -def load_yaml(yaml): - return kaptan.Kaptan(handler="yaml").import_config(yaml).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(_file): - return kaptan.Kaptan().import_config(_file).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(tmp_path: pathlib.Path): @@ -282,7 +291,7 @@ def test_expands_blank_panes(): """ - yaml_config_file = os.path.join(example_dir, "blank-panes.yaml") + yaml_config_file = example_dir / "blank-panes.yaml" test_config = load_config(yaml_config_file) assert config.expand(test_config) == fixtures.expand_blank.expected diff --git a/tests/test_util.py b/tests/test_util.py index e87c3bf0f96..aa7b79f3f75 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1,6 +1,4 @@ """Tests for utility functions in tmux.""" -import os - import pytest from tmuxp import exc @@ -11,7 +9,7 @@ def test_raise_BeforeLoadScriptNotExists_if_not_exists(): - script_file = os.path.join(fixtures_dir, "script_noexists.sh") + script_file = fixtures_dir / "script_noexists.sh" with pytest.raises(BeforeLoadScriptNotExists): run_before_script(script_file) @@ -21,14 +19,14 @@ def test_raise_BeforeLoadScriptNotExists_if_not_exists(): def test_raise_BeforeLoadScriptError_if_retcode(): - script_file = os.path.join(fixtures_dir, "script_failed.sh") + script_file = fixtures_dir / "script_failed.sh" with pytest.raises(BeforeLoadScriptError): run_before_script(script_file) def test_return_stdout_if_ok(capsys): - script_file = os.path.join(fixtures_dir, "script_complete.sh") + script_file = fixtures_dir / "script_complete.sh" run_before_script(script_file) out, err = capsys.readouterr() @@ -36,7 +34,7 @@ def test_return_stdout_if_ok(capsys): def test_beforeload_returncode(): - script_file = os.path.join(fixtures_dir, "script_failed.sh") + script_file = fixtures_dir / "script_failed.sh" with pytest.raises(exc.BeforeLoadScriptError) as excinfo: run_before_script(script_file) @@ -44,7 +42,7 @@ def test_beforeload_returncode(): def test_beforeload_returns_stderr_messages(): - script_file = os.path.join(fixtures_dir, "script_failed.sh") + script_file = fixtures_dir / "script_failed.sh" with pytest.raises(exc.BeforeLoadScriptError) as excinfo: run_before_script(script_file) diff --git a/tests/test_workspacebuilder.py b/tests/test_workspacebuilder.py index c5771a6024a..737a59f9d08 100644 --- a/tests/test_workspacebuilder.py +++ b/tests/test_workspacebuilder.py @@ -405,8 +405,8 @@ def test_automatic_rename_option(session): def test_blank_pane_count(session): """:todo: Verify blank panes of various types build into workspaces.""" - yaml_config_file = os.path.join(example_dir, "blank-panes.yaml") - test_config = kaptan.Kaptan().import_config(yaml_config_file).get() + yaml_config_file = example_dir / "blank-panes.yaml" + test_config = kaptan.Kaptan().import_config(str(yaml_config_file)).get() test_config = config.expand(test_config) builder = WorkspaceBuilder(sconf=test_config) builder.build(session=session) @@ -590,7 +590,7 @@ def test_before_load_throw_error_if_retcode_error(server): sconfig = kaptan.Kaptan(handler="yaml") yaml = config_script_fails.format( fixtures_dir=fixtures_dir, - script_failed=os.path.join(fixtures_dir, "script_failed.sh"), + script_failed=fixtures_dir / "script_failed.sh", ) sconfig = sconfig.import_config(yaml).get() @@ -616,7 +616,7 @@ def test_before_load_throw_error_if_file_not_exists(server): sconfig = kaptan.Kaptan(handler="yaml") yaml = config_script_not_exists.format( fixtures_dir=fixtures_dir, - script_not_exists=os.path.join(fixtures_dir, "script_not_exists.sh"), + script_not_exists=fixtures_dir / "script_not_exists.sh", ) sconfig = sconfig.import_config(yaml).get() sconfig = config.expand(sconfig) @@ -639,11 +639,12 @@ def test_before_load_true_if_test_passes(server): config_script_completes = load_fixture( "workspacebuilder/config_script_completes.yaml" ) - assert os.path.exists(os.path.join(fixtures_dir, "script_complete.sh")) + script_complete_sh = fixtures_dir / "script_complete.sh" + assert script_complete_sh.exists() sconfig = kaptan.Kaptan(handler="yaml") yaml = config_script_completes.format( fixtures_dir=fixtures_dir, - script_complete=os.path.join(fixtures_dir, "script_complete.sh"), + script_complete=script_complete_sh, ) sconfig = sconfig.import_config(yaml).get() @@ -660,12 +661,11 @@ def test_before_load_true_if_test_passes_with_args(server): config_script_completes = load_fixture( "workspacebuilder/config_script_completes.yaml" ) - - assert os.path.exists(os.path.join(fixtures_dir, "script_complete.sh")) + script_complete_sh = fixtures_dir / "script_complete.sh" + assert script_complete_sh.exists() sconfig = kaptan.Kaptan(handler="yaml") yaml = config_script_completes.format( - fixtures_dir=fixtures_dir, - script_complete=os.path.join(fixtures_dir, "script_complete.sh") + " -v", + fixtures_dir=fixtures_dir, script_complete=script_complete_sh ) sconfig = sconfig.import_config(yaml).get() From 05bb102175e58106f5969ac332f405a568dfc069 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 09:10:31 -0500 Subject: [PATCH 05/12] refactor(tests): Rename constants to _dir -> _PATH and uppercase --- tests/__init__.py | 6 +++--- tests/test_config.py | 4 ++-- tests/test_util.py | 12 ++++++------ tests/test_workspacebuilder.py | 20 ++++++++++---------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 1872e36b130..6233b234811 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -6,6 +6,6 @@ """ import pathlib -tests_dir = pathlib.Path(__file__).parent -example_dir = tests_dir.parent / "examples" -fixtures_dir = tests_dir / "fixtures" +TESTS_PATH = pathlib.Path(__file__).parent +EXAMPLE_PATH = TESTS_PATH.parent / "examples" +FIXTURES_PATH = TESTS_PATH / "fixtures" diff --git a/tests/test_config.py b/tests/test_config.py index ba175e8bc20..7e3a0785d09 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -9,7 +9,7 @@ from tmuxp import config, exc -from . import example_dir +from . import EXAMPLE_PATH from .fixtures import config as fixtures TMUXP_DIR = pathlib.Path(__file__).parent / ".tmuxp" @@ -291,7 +291,7 @@ def test_expands_blank_panes(): """ - yaml_config_file = 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 diff --git a/tests/test_util.py b/tests/test_util.py index aa7b79f3f75..c5f5b53824a 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -5,11 +5,11 @@ from tmuxp.exc import BeforeLoadScriptError, BeforeLoadScriptNotExists from tmuxp.util import get_session, run_before_script -from . import fixtures_dir +from . import FIXTURES_PATH def test_raise_BeforeLoadScriptNotExists_if_not_exists(): - script_file = fixtures_dir / "script_noexists.sh" + script_file = FIXTURES_PATH / "script_noexists.sh" with pytest.raises(BeforeLoadScriptNotExists): run_before_script(script_file) @@ -19,14 +19,14 @@ def test_raise_BeforeLoadScriptNotExists_if_not_exists(): def test_raise_BeforeLoadScriptError_if_retcode(): - script_file = fixtures_dir / "script_failed.sh" + script_file = FIXTURES_PATH / "script_failed.sh" with pytest.raises(BeforeLoadScriptError): run_before_script(script_file) def test_return_stdout_if_ok(capsys): - script_file = fixtures_dir / "script_complete.sh" + script_file = FIXTURES_PATH / "script_complete.sh" run_before_script(script_file) out, err = capsys.readouterr() @@ -34,7 +34,7 @@ def test_return_stdout_if_ok(capsys): def test_beforeload_returncode(): - script_file = fixtures_dir / "script_failed.sh" + script_file = FIXTURES_PATH / "script_failed.sh" with pytest.raises(exc.BeforeLoadScriptError) as excinfo: run_before_script(script_file) @@ -42,7 +42,7 @@ def test_beforeload_returncode(): def test_beforeload_returns_stderr_messages(): - script_file = fixtures_dir / "script_failed.sh" + script_file = FIXTURES_PATH / "script_failed.sh" with pytest.raises(exc.BeforeLoadScriptError) as excinfo: run_before_script(script_file) diff --git a/tests/test_workspacebuilder.py b/tests/test_workspacebuilder.py index 737a59f9d08..33c368fbcd6 100644 --- a/tests/test_workspacebuilder.py +++ b/tests/test_workspacebuilder.py @@ -16,7 +16,7 @@ from tmuxp.cli import load_plugins from tmuxp.workspacebuilder import WorkspaceBuilder -from . import example_dir, fixtures_dir +from . import EXAMPLE_PATH, FIXTURES_PATH from .fixtures._util import load_fixture @@ -405,7 +405,7 @@ def test_automatic_rename_option(session): def test_blank_pane_count(session): """:todo: Verify blank panes of various types build into workspaces.""" - yaml_config_file = example_dir / "blank-panes.yaml" + yaml_config_file = EXAMPLE_PATH / "blank-panes.yaml" test_config = kaptan.Kaptan().import_config(str(yaml_config_file)).get() test_config = config.expand(test_config) builder = WorkspaceBuilder(sconf=test_config) @@ -589,8 +589,8 @@ def test_before_load_throw_error_if_retcode_error(server): config_script_fails = load_fixture("workspacebuilder/config_script_fails.yaml") sconfig = kaptan.Kaptan(handler="yaml") yaml = config_script_fails.format( - fixtures_dir=fixtures_dir, - script_failed=fixtures_dir / "script_failed.sh", + fixtures_dir=FIXTURES_PATH, + script_failed=FIXTURES_PATH / "script_failed.sh", ) sconfig = sconfig.import_config(yaml).get() @@ -615,8 +615,8 @@ def test_before_load_throw_error_if_file_not_exists(server): ) sconfig = kaptan.Kaptan(handler="yaml") yaml = config_script_not_exists.format( - fixtures_dir=fixtures_dir, - script_not_exists=fixtures_dir / "script_not_exists.sh", + fixtures_dir=FIXTURES_PATH, + script_not_exists=FIXTURES_PATH / "script_not_exists.sh", ) sconfig = sconfig.import_config(yaml).get() sconfig = config.expand(sconfig) @@ -639,11 +639,11 @@ def test_before_load_true_if_test_passes(server): config_script_completes = load_fixture( "workspacebuilder/config_script_completes.yaml" ) - script_complete_sh = fixtures_dir / "script_complete.sh" + script_complete_sh = FIXTURES_PATH / "script_complete.sh" assert script_complete_sh.exists() sconfig = kaptan.Kaptan(handler="yaml") yaml = config_script_completes.format( - fixtures_dir=fixtures_dir, + fixtures_dir=FIXTURES_PATH, script_complete=script_complete_sh, ) @@ -661,11 +661,11 @@ def test_before_load_true_if_test_passes_with_args(server): config_script_completes = load_fixture( "workspacebuilder/config_script_completes.yaml" ) - script_complete_sh = fixtures_dir / "script_complete.sh" + script_complete_sh = FIXTURES_PATH / "script_complete.sh" assert script_complete_sh.exists() sconfig = kaptan.Kaptan(handler="yaml") yaml = config_script_completes.format( - fixtures_dir=fixtures_dir, script_complete=script_complete_sh + fixtures_dir=FIXTURES_PATH, script_complete=script_complete_sh ) sconfig = sconfig.import_config(yaml).get() From 38c5179ad782653f982263ce2cce13fe9f2129d9 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 09:13:01 -0500 Subject: [PATCH 06/12] tests(workspacebuilder): Remove unused formatting --- tests/test_workspacebuilder.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/test_workspacebuilder.py b/tests/test_workspacebuilder.py index 33c368fbcd6..75000050a75 100644 --- a/tests/test_workspacebuilder.py +++ b/tests/test_workspacebuilder.py @@ -589,7 +589,6 @@ def test_before_load_throw_error_if_retcode_error(server): config_script_fails = load_fixture("workspacebuilder/config_script_fails.yaml") sconfig = kaptan.Kaptan(handler="yaml") yaml = config_script_fails.format( - fixtures_dir=FIXTURES_PATH, script_failed=FIXTURES_PATH / "script_failed.sh", ) @@ -615,7 +614,6 @@ def test_before_load_throw_error_if_file_not_exists(server): ) sconfig = kaptan.Kaptan(handler="yaml") yaml = config_script_not_exists.format( - fixtures_dir=FIXTURES_PATH, script_not_exists=FIXTURES_PATH / "script_not_exists.sh", ) sconfig = sconfig.import_config(yaml).get() @@ -643,7 +641,6 @@ def test_before_load_true_if_test_passes(server): assert script_complete_sh.exists() sconfig = kaptan.Kaptan(handler="yaml") yaml = config_script_completes.format( - fixtures_dir=FIXTURES_PATH, script_complete=script_complete_sh, ) @@ -664,9 +661,7 @@ def test_before_load_true_if_test_passes_with_args(server): script_complete_sh = FIXTURES_PATH / "script_complete.sh" assert script_complete_sh.exists() sconfig = kaptan.Kaptan(handler="yaml") - yaml = config_script_completes.format( - fixtures_dir=FIXTURES_PATH, script_complete=script_complete_sh - ) + yaml = config_script_completes.format(script_complete=script_complete_sh) sconfig = sconfig.import_config(yaml).get() sconfig = config.expand(sconfig) From 8e4411e9a5352dc5d84766979914d5be10b69375 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 09:50:05 -0500 Subject: [PATCH 07/12] tests: Use home default directory --- tests/conftest.py | 19 +++++++++++++++++++ tests/test_config.py | 17 ++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 7ab1d4ae72a..da5d1713a00 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,7 @@ +import getpass import logging import os +import pathlib import pytest @@ -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 = [ diff --git a/tests/test_config.py b/tests/test_config.py index 7e3a0785d09..1b1b2b93944 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -10,7 +10,6 @@ from tmuxp import config, exc from . import EXAMPLE_PATH -from .fixtures import config as fixtures TMUXP_DIR = pathlib.Path(__file__).parent / ".tmuxp" @@ -32,6 +31,8 @@ def load_config(path: Union[str, pathlib.Path]) -> str: def test_export_json(tmp_path: pathlib.Path): + from .fixtures import config as fixtures + json_config_file = tmp_path / "config.json" configparser = kaptan.Kaptan() @@ -47,6 +48,8 @@ def test_export_json(tmp_path: pathlib.Path): def test_export_yaml(tmp_path: pathlib.Path): + from .fixtures import config as fixtures + yaml_config_file = tmp_path / "config.yaml" configparser = kaptan.Kaptan() @@ -92,17 +95,18 @@ def test_scan_config(tmp_path: pathlib.Path): def test_config_expand1(): """Expand shell commands from string to list.""" + from .fixtures import config as fixtures + test_config = config.expand(fixtures.expand1.before_config) assert test_config == fixtures.expand1.after_config def test_config_expand2(): """Expand shell commands from string to list.""" + from .fixtures import config as fixtures unexpanded_dict = load_yaml(fixtures.expand2.unexpanded_yaml) - expanded_dict = load_yaml(fixtures.expand2.expanded_yaml) - assert config.expand(unexpanded_dict) == expanded_dict @@ -220,6 +224,8 @@ def test_inheritance_config(): def test_shell_command_before(): """Config inheritance for the nested 'start_command'.""" + from .fixtures import config as fixtures + test_config = fixtures.shell_command_before.config_unexpanded test_config = config.expand(test_config) @@ -230,6 +236,8 @@ def test_shell_command_before(): def test_in_session_scope(): + from .fixtures import config as fixtures + sconfig = load_yaml(fixtures.shell_command_before_session.before) config.validate_schema(sconfig) @@ -241,6 +249,8 @@ def test_in_session_scope(): def test_trickle_relative_start_directory(): + from .fixtures import config as fixtures + test_config = config.trickle(fixtures.trickle.before) assert test_config == fixtures.trickle.expected @@ -290,6 +300,7 @@ def test_expands_blank_panes(): 'shell_command': [''] """ + from .fixtures import config as fixtures yaml_config_file = EXAMPLE_PATH / "blank-panes.yaml" test_config = load_config(yaml_config_file) From e2bd17df517d4dba80524b2255197e63e1b6a120 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 09:55:21 -0500 Subject: [PATCH 08/12] refactor(test): Move fixtures to config_fixture fixture (say fixture TEN times) --- tests/test_config.py | 76 +++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 1b1b2b93944..48a11a8d7f2 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -14,6 +14,18 @@ TMUXP_DIR = pathlib.Path(__file__).parent / ".tmuxp" +@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_yaml(path: Union[str, pathlib.Path]) -> str: return ( kaptan.Kaptan(handler="yaml") @@ -30,13 +42,11 @@ def load_config(path: Union[str, pathlib.Path]) -> str: ) -def test_export_json(tmp_path: pathlib.Path): - from .fixtures import config as fixtures - +def test_export_json(tmp_path: pathlib.Path, config_fixture): 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) @@ -44,16 +54,14 @@ def test_export_json(tmp_path: pathlib.Path): 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(tmp_path: pathlib.Path): - from .fixtures import config as fixtures +def test_export_yaml(tmp_path: pathlib.Path, config_fixture): 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) @@ -61,7 +69,7 @@ def test_export_yaml(tmp_path: pathlib.Path): 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(tmp_path: pathlib.Path): @@ -93,20 +101,16 @@ def test_scan_config(tmp_path: pathlib.Path): assert len(configs) == files -def test_config_expand1(): +def test_config_expand1(config_fixture): """Expand shell commands from string to list.""" - from .fixtures import config as fixtures + test_config = config.expand(config_fixture.expand1.before_config) + assert test_config == config_fixture.expand1.after_config - test_config = config.expand(fixtures.expand1.before_config) - assert test_config == fixtures.expand1.after_config - -def test_config_expand2(): +def test_config_expand2(config_fixture): """Expand shell commands from string to list.""" - from .fixtures import config as fixtures - - 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 @@ -222,37 +226,31 @@ def test_inheritance_config(): assert config == inheritance_config_after -def test_shell_command_before(): +def test_shell_command_before(config_fixture): """Config inheritance for the nested 'start_command'.""" - from .fixtures import config as fixtures - - 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(): - from .fixtures import config as fixtures - - sconfig = load_yaml(fixtures.shell_command_before_session.before) +def test_in_session_scope(config_fixture): + 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(): - from .fixtures import config as fixtures - - test_config = config.trickle(fixtures.trickle.before) - assert test_config == fixtures.trickle.expected +def test_trickle_relative_start_directory(config_fixture): + test_config = config.trickle(config_fixture.trickle.before) + assert test_config == config_fixture.trickle.expected def test_trickle_window_with_no_pane_config(): @@ -273,7 +271,7 @@ def test_trickle_window_with_no_pane_config(): } -def test_expands_blank_panes(): +def test_expands_blank_panes(config_fixture): """Expand blank config into full form. Handle ``NoneType`` and 'blank':: @@ -300,11 +298,9 @@ def test_expands_blank_panes(): 'shell_command': [''] """ - from .fixtures import config as fixtures - 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(): From 4507f6c8f1a359565c90d2ad5a8122fdd45d1a08 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 09:57:53 -0500 Subject: [PATCH 09/12] tests: Delete unused constant --- tests/test_config.py | 2 -- tests/test_config_teamocil.py | 4 ---- tests/test_config_tmuxinator.py | 4 ---- 3 files changed, 10 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 48a11a8d7f2..032c48589ad 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -11,8 +11,6 @@ from . import EXAMPLE_PATH -TMUXP_DIR = pathlib.Path(__file__).parent / ".tmuxp" - @pytest.fixture def config_fixture(): diff --git a/tests/test_config_teamocil.py b/tests/test_config_teamocil.py index 721e3878abf..dc040261c38 100644 --- a/tests/test_config_teamocil.py +++ b/tests/test_config_teamocil.py @@ -1,6 +1,4 @@ """Test for tmuxp teamocil configuration.""" -import os - import pytest import kaptan @@ -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", diff --git a/tests/test_config_tmuxinator.py b/tests/test_config_tmuxinator.py index 88241c22a93..ddcf34e68ac 100644 --- a/tests/test_config_tmuxinator.py +++ b/tests/test_config_tmuxinator.py @@ -1,6 +1,4 @@ """Test for tmuxp tmuxinator configuration.""" -import os - import pytest import kaptan @@ -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", From 2e16cabee8399e1fe8e616695e2dccd1472f1d88 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 10:23:55 -0500 Subject: [PATCH 10/12] Type annotations for module Raises an error in pyright, but completion works as expected. Potentially reach out to: - https://github.com/microsoft/pylance-release - https://github.com/microsoft/pyright about this. --- tests/test_config.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 032c48589ad..a89b3631548 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,6 +1,7 @@ """Test for tmuxp configuration import, inlining, expanding and export.""" import os import pathlib +import typing from typing import Union import pytest @@ -11,6 +12,9 @@ from . import EXAMPLE_PATH +if typing.TYPE_CHECKING: + from .fixtures import config as ConfigFixture + @pytest.fixture def config_fixture(): @@ -40,7 +44,7 @@ def load_config(path: Union[str, pathlib.Path]) -> str: ) -def test_export_json(tmp_path: pathlib.Path, config_fixture): +def test_export_json(tmp_path: pathlib.Path, config_fixture: "ConfigFixture"): json_config_file = tmp_path / "config.json" configparser = kaptan.Kaptan() @@ -55,7 +59,7 @@ def test_export_json(tmp_path: pathlib.Path, config_fixture): assert config_fixture.sampleconfig.sampleconfigdict == new_config_data -def test_export_yaml(tmp_path: pathlib.Path, config_fixture): +def test_export_yaml(tmp_path: pathlib.Path, config_fixture: "ConfigFixture"): yaml_config_file = tmp_path / "config.yaml" configparser = kaptan.Kaptan() @@ -99,13 +103,13 @@ def test_scan_config(tmp_path: pathlib.Path): assert len(configs) == files -def test_config_expand1(config_fixture): +def test_config_expand1(config_fixture: "ConfigFixture"): """Expand shell commands from string to list.""" test_config = config.expand(config_fixture.expand1.before_config) assert test_config == config_fixture.expand1.after_config -def test_config_expand2(config_fixture): +def test_config_expand2(config_fixture: "ConfigFixture"): """Expand shell commands from string to list.""" unexpanded_dict = load_yaml(config_fixture.expand2.unexpanded_yaml) expanded_dict = load_yaml(config_fixture.expand2.expanded_yaml) @@ -224,7 +228,7 @@ def test_inheritance_config(): assert config == inheritance_config_after -def test_shell_command_before(config_fixture): +def test_shell_command_before(config_fixture: "ConfigFixture"): """Config inheritance for the nested 'start_command'.""" test_config = config_fixture.shell_command_before.config_unexpanded test_config = config.expand(test_config) @@ -235,7 +239,7 @@ def test_shell_command_before(config_fixture): assert test_config == config_fixture.shell_command_before.config_after -def test_in_session_scope(config_fixture): +def test_in_session_scope(config_fixture: "ConfigFixture"): sconfig = load_yaml(config_fixture.shell_command_before_session.before) config.validate_schema(sconfig) @@ -246,7 +250,7 @@ def test_in_session_scope(config_fixture): ) -def test_trickle_relative_start_directory(config_fixture): +def test_trickle_relative_start_directory(config_fixture: "ConfigFixture"): test_config = config.trickle(config_fixture.trickle.before) assert test_config == config_fixture.trickle.expected @@ -269,7 +273,7 @@ def test_trickle_window_with_no_pane_config(): } -def test_expands_blank_panes(config_fixture): +def test_expands_blank_panes(config_fixture: "ConfigFixture"): """Expand blank config into full form. Handle ``NoneType`` and 'blank':: From 44e7670d038e00735aee7b420e55a7a5b9d8d4e1 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 12:13:36 -0500 Subject: [PATCH 11/12] docs: Format --- CHANGES | 6 +++--- docs/cli.md | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 71ce18513ec..ca6b363a9f9 100644 --- a/CHANGES +++ b/CHANGES @@ -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 diff --git a/docs/cli.md b/docs/cli.md index 332738e4479..01ae7255215 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -188,6 +188,7 @@ $ tmuxp load /path/to/folder/ ``` Name of the config, assume `$HOME/.tmuxp/myconfig.yaml`: + ```console $ tmuxp load myconfig ``` @@ -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 From 733d0ed20dd1c8aed0ee7552e71904046e89a9df Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 12:24:52 -0500 Subject: [PATCH 12/12] Makefile: Include yaml files in test watching --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9b02c94f1de..811a7aded17 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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