From 199c32e9e02573eaed16b2a303ae16e8a1824ae8 Mon Sep 17 00:00:00 2001 From: Will Ockmore Date: Tue, 12 Jan 2021 20:18:24 +0000 Subject: [PATCH 1/5] fix cli freeze test and remove duplicate cases --- tests/test_cli.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index cf0ec046b4c..68f63fac6fa 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -868,9 +868,9 @@ def test_import_tmuxinator(cli_args, inputs, tmpdir, monkeypatch): @pytest.mark.parametrize( "cli_args,inputs", [ - (['freeze', 'mysession'], ['\n', 'y\n', './la.yaml\n', 'y\n']), + (['freeze', 'myfrozensession'], ['\n', 'y\n', './la.yaml\n', 'y\n']), ( # Exists - ['freeze', 'mysession'], + ['freeze', 'myfrozensession'], ['\n', 'y\n', './exists.yaml\n', './la.yaml\n', 'y\n'], ), ( # Imply current session if not entered @@ -878,21 +878,21 @@ def test_import_tmuxinator(cli_args, inputs, tmpdir, monkeypatch): ['\n', 'y\n', './la.yaml\n', 'y\n'], ), (['freeze'], ['\n', 'y\n', './exists.yaml\n', './la.yaml\n', 'y\n']), # Exists - ( # Create a new one - ['freeze', 'mysession', '--force'], - ['\n', 'y\n', './la.yaml\n', 'y\n'], - ), - ( # Imply current session if not entered - ['freeze', '--force'], - ['\n', 'y\n', './la.yaml\n', 'y\n'], - ), ], ) def test_freeze(server, cli_args, inputs, tmpdir, monkeypatch): monkeypatch.setenv('HOME', str(tmpdir)) tmpdir.join('exists.yaml').ensure() - server.new_session(session_name='mysession') + server.new_session(session_name='myfirstsession') + server.new_session(session_name='myfrozensession') + + # Assign an active pane to the session + second_session = server.list_sessions()[1] + first_pane_on_second_session_id = ( + second_session.list_windows()[0].list_panes()[0]["pane_id"] + ) + monkeypatch.setenv("TMUX_PANE", first_pane_on_second_session_id) with tmpdir.as_cwd(): runner = CliRunner() @@ -902,6 +902,11 @@ def test_freeze(server, cli_args, inputs, tmpdir, monkeypatch): print(out.output) assert tmpdir.join('la.yaml').check() + yaml_config = tmpdir.join('la.yaml').open().read() + frozen_config = kaptan.Kaptan(handler='yaml').import_config(yaml_config).get() + + assert frozen_config['session_name'] == 'myfrozensession' + @pytest.mark.parametrize( "cli_args,inputs", From bfc0e07ce812c25c009511d23ed57d3ea0049523 Mon Sep 17 00:00:00 2001 From: Will Ockmore Date: Tue, 12 Jan 2021 20:29:42 +0000 Subject: [PATCH 2/5] get_session should return attached local session by default --- tests/test_util.py | 22 +++++++++++++++++++++- tmuxp/cli.py | 2 +- tmuxp/util.py | 11 +++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index 17568ce7c13..4fde2fb62ae 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -5,7 +5,7 @@ from tmuxp import exc from tmuxp.exc import BeforeLoadScriptError, BeforeLoadScriptNotExists -from tmuxp.util import run_before_script +from tmuxp.util import run_before_script, get_session from . import fixtures_dir @@ -49,3 +49,23 @@ def test_beforeload_returns_stderr_messages(): with pytest.raises(exc.BeforeLoadScriptError) as excinfo: run_before_script(script_file) assert excinfo.match(r'failed with returncode') + + +def test_get_session_should_default_to_local_attached_session(server, monkeypatch): + server.new_session(session_name='myfirstsession') + second_session = server.new_session(session_name='mysecondsession') + + # Assign an active pane to the session + first_pane_on_second_session_id = second_session.list_windows()[0].list_panes()[0][ + 'pane_id' + ] + monkeypatch.setenv('TMUX_PANE', first_pane_on_second_session_id) + + assert get_session(server) == second_session + + +def test_get_session_should_raise_exception_if_no_session(server): + first_session = server.new_session(session_name='myfirstsession') + server.new_session(session_name='mysecondsession') + + assert get_session(server) == first_session diff --git a/tmuxp/cli.py b/tmuxp/cli.py index 201d72282e0..9605e5d0eca 100644 --- a/tmuxp/cli.py +++ b/tmuxp/cli.py @@ -924,7 +924,7 @@ def command_freeze(session_name, socket_name, socket_path, force): if session_name: session = t.find_where({'session_name': session_name}) else: - session = t.list_sessions()[0] + session = util.get_session(t) if not session: raise exc.TmuxpException('Session not found.') diff --git a/tmuxp/util.py b/tmuxp/util.py index 7fc32f713da..725dcde8bdc 100644 --- a/tmuxp/util.py +++ b/tmuxp/util.py @@ -111,10 +111,17 @@ def get_session(server, session_name=None, current_pane=None): elif current_pane is not None: session = server.find_where({'session_id': current_pane['session_id']}) else: - session = server.list_sessions()[0] + current_pane = get_current_pane(server) + if current_pane: + session = server.find_where({'session_id': current_pane['session_id']}) + else: + session = server.list_sessions()[0] if not session: - raise exc.TmuxpException('Session not found: %s' % session_name) + if session_name: + raise exc.TmuxpException('Session not found: %s' % session_name) + else: + raise exc.TmuxpException('Session not found') return session From aa48143242b6b391f3f005b91f960ebc9e727cd9 Mon Sep 17 00:00:00 2001 From: Will Ockmore Date: Sat, 29 Jan 2022 13:54:39 +0000 Subject: [PATCH 3/5] add additional documentation on the `freeze` command --- docs/cli.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/cli.md b/docs/cli.md index 5c4ccf7fe5c..be9f1adc302 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -130,13 +130,19 @@ my_window ## Freeze sessions ``` +tmuxp freeze tmuxp freeze +tmuxp freeze --force ``` You can save the state of your tmux session by freezing it. Tmuxp will offer to save your session state to `.json` or `.yaml`. +If no session is specified, it will default to the attached session. + +If the `--force` argument is passed, it will overwrite any existing config file with the same name. + (cli-load)= ## Load session From 47857300f979c20168a628d485356adc6b64a4a9 Mon Sep 17 00:00:00 2001 From: Will Ockmore Date: Mon, 25 Jan 2021 09:04:03 +0000 Subject: [PATCH 4/5] fix test naming --- tests/test_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_util.py b/tests/test_util.py index 4fde2fb62ae..1d7d17f2952 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -64,7 +64,7 @@ def test_get_session_should_default_to_local_attached_session(server, monkeypatc assert get_session(server) == second_session -def test_get_session_should_raise_exception_if_no_session(server): +def test_get_session_should_return_first_session_if_no_active_session(server): first_session = server.new_session(session_name='myfirstsession') server.new_session(session_name='mysecondsession') From 6c5ffe0edbc1b5d93999f45736a5aa6699716dd5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 29 Jan 2022 13:56:28 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_cli.py | 6 +++--- tests/test_util.py | 2 +- tmuxp/workspacebuilder.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 68f63fac6fa..feb5a1f9710 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -889,9 +889,9 @@ def test_freeze(server, cli_args, inputs, tmpdir, monkeypatch): # Assign an active pane to the session second_session = server.list_sessions()[1] - first_pane_on_second_session_id = ( - second_session.list_windows()[0].list_panes()[0]["pane_id"] - ) + first_pane_on_second_session_id = second_session.list_windows()[0].list_panes()[0][ + "pane_id" + ] monkeypatch.setenv("TMUX_PANE", first_pane_on_second_session_id) with tmpdir.as_cwd(): diff --git a/tests/test_util.py b/tests/test_util.py index 1d7d17f2952..420e1abfd1f 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -5,7 +5,7 @@ from tmuxp import exc from tmuxp.exc import BeforeLoadScriptError, BeforeLoadScriptNotExists -from tmuxp.util import run_before_script, get_session +from tmuxp.util import get_session, run_before_script from . import fixtures_dir diff --git a/tmuxp/workspacebuilder.py b/tmuxp/workspacebuilder.py index 16ab6e90a1a..c38b1c5b01b 100644 --- a/tmuxp/workspacebuilder.py +++ b/tmuxp/workspacebuilder.py @@ -348,7 +348,7 @@ def get_pane_shell(): attach=True, start_directory=get_pane_start_directory(), shell=get_pane_shell(), - target=p.id + target=p.id, ) assert isinstance(p, Pane)