def test_in_session_scope(): sconfig = load_yaml(fixtures.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)
def test_in_session_scope(): sconfig = load_yaml(fixtures.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)
def test_load_workspace_sleep( tmp_path: pathlib.Path, server: libtmux.Server, monkeypatch: pytest.MonkeyPatch, yaml, sleep: int, output, ): yaml_config = tmp_path / "simple.yaml" yaml_config.write_text( yaml, encoding="utf-8", ) sconfig = kaptan.Kaptan(handler="yaml") sconfig = sconfig.import_config(str(yaml_config)).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig, server=server) t = time.process_time() builder.build() time.sleep(0.5) session = builder.session pane = session.attached_pane while (time.process_time() - t) * 1000 < sleep: captured_pane = "\n".join(pane.capture_pane()) assert output not in captured_pane time.sleep(0.1) captured_pane = "\n".join(pane.capture_pane()) assert output in captured_pane
def test_window_options(session): yaml_config = test_utils.read_config_file( "workspacebuilder/window_options.yaml") s = session sconfig = kaptan.Kaptan(handler="yaml") sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) if has_gte_version("2.3"): sconfig["windows"][0]["options"]["pane-border-format"] = " #P " builder = WorkspaceBuilder(sconf=sconfig) window_count = len(session._windows) # current window count assert len(s._windows) == window_count for w, wconf in builder.iter_create_windows(s): for p in builder.iter_create_panes(w, wconf): w.select_layout("tiled") # fix glitch with pane size p = p assert len(s._windows) == window_count assert isinstance(w, Window) assert w.show_window_option("main-pane-height") == 5 if has_gte_version("2.3"): assert w.show_window_option("pane-border-format") == " #P " assert len(s._windows) == window_count window_count += 1 w.select_layout(wconf["layout"])
def test_window_options_after(session): yaml_config = test_utils.read_config_file( "workspacebuilder/window_options_after.yaml") sconfig = kaptan.Kaptan(handler="yaml") sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) def assert_last_line(p, s): def f(): pane_out = p.cmd("capture-pane", "-p", "-J").stdout while not pane_out[-1].strip(): # delete trailing lines tmux 1.8 pane_out.pop() return len(pane_out) > 1 and pane_out[-2].strip() == s # Print output for easier debugging if assertion fails return retry_until(f, raises=False) for i, pane in enumerate(session.attached_window.panes): assert assert_last_line( pane, str(i)), "Initial command did not execute properly/" + str(i) pane.cmd("send-keys", "Up") # Will repeat echo pane.enter() # in each iteration assert assert_last_line( pane, str(i)), "Repeated command did not execute properly/" + str(i) session.cmd("send-keys", " echo moo") session.cmd("send-keys", "Enter") for pane in session.attached_window.panes: assert assert_last_line( pane, "moo"), "Synchronized command did not execute properly"
def test_window_options(session): yaml_config = loadfixture("workspacebuilder/window_options.yaml") s = session sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) if has_gte_version('2.3'): sconfig['windows'][0]['options']['pane-border-format'] = ' #P ' builder = WorkspaceBuilder(sconf=sconfig) window_count = len(session._windows) # current window count assert len(s._windows) == window_count for w, wconf in builder.iter_create_windows(s): for p in builder.iter_create_panes(w, wconf): w.select_layout('tiled') # fix glitch with pane size p = p assert len(s._windows) == window_count assert isinstance(w, Window) assert w.show_window_option('main-pane-height') == 5 if has_gte_version('2.3'): assert w.show_window_option('pane-border-format') == ' #P ' assert len(s._windows) == window_count window_count += 1 w.select_layout(wconf['layout'])
def test_replaces_start_directory(): env_key = "TESTHEY92" env_value = "HEYO1" yaml_config = """ start_directory: {TEST_VAR}/test shell_command_before: {TEST_VAR}/test2 before_script: {TEST_VAR}/test3 session_name: hi - {TEST_VAR} windows: - window_name: editor panes: - shell_command: - tail -F /var/log/syslog start_directory: /var/log - window_name: logging @ {TEST_VAR} automatic_rename: true panes: - shell_command: - htop """.format( TEST_VAR="${%s}" % env_key ) sconfig = load_yaml(yaml_config) with EnvironmentVarGuard() as env: env.set(env_key, env_value) sconfig = config.expand(sconfig) assert "%s/test" % env_value == sconfig['start_directory'] assert "%s/test2" % env_value in sconfig['shell_command_before'] assert "%s/test3" % env_value == sconfig['before_script'] assert "hi - %s" % env_value == sconfig['session_name'] assert "logging @ %s" % env_value == \ sconfig['windows'][1]['window_name']
def test_expands_blank_panes(config_fixture: "ConfigFixture"): """Expand blank config into full form. Handle ``NoneType`` and 'blank':: # nothing, None, 'blank' 'panes': [ None, 'blank' ] # should be blank 'panes': [ 'shell_command': [] ] Blank strings:: panes: [ '' ] # should output to: panes: 'shell_command': [''] """ yaml_config_file = EXAMPLE_PATH / "blank-panes.yaml" test_config = load_config(yaml_config_file) assert config.expand(test_config) == config_fixture.expand_blank.expected
def test_before_load_throw_error_if_file_not_exists(server): config_script_not_exists = loadfixture( "workspacebuilder/config_script_not_exists.yaml" ) 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' ) ) sconfig = sconfig.import_config(yaml).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) with temp_session(server) as sess: session_name = sess.name temp_session_exists = server.has_session( sess.name ) assert temp_session_exists with pytest.raises( (exc.BeforeLoadScriptNotExists, OSError), ) as excinfo: builder.build(session=sess) excinfo.match(r'No such file or directory') result = server.has_session(session_name) assert not result, "Kills session if before_script doesn't exist"
def test_before_load_throw_error_if_retcode_error(server): config_script_fails = loadfixture( "workspacebuilder/config_script_fails.yaml" ) sconfig = kaptan.Kaptan(handler='yaml') yaml = config_script_fails.format( fixtures_dir=fixtures_dir, script_failed=os.path.join(fixtures_dir, 'script_failed.sh') ) sconfig = sconfig.import_config(yaml).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) with temp_session(server) as sess: session_name = sess.name with pytest.raises(exc.BeforeLoadScriptError): builder.build(session=sess) result = server.has_session(session_name) assert not result, \ "Kills session if before_script exits with errcode"
def test_start_directory(session, tmpdir): yaml_config = loadfixture("workspacebuilder/start_directory.yaml") test_dir = str(tmpdir.mkdir('foo bar')) test_config = yaml_config.format(TMP_DIR=str(tmpdir), TEST_DIR=test_dir) sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(test_config).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) assert session == builder.session dirs = ['/usr/bin', '/dev', test_dir, '/usr', '/usr'] for path, window in zip(dirs, session.windows): for p in window.panes: while retry(): p.server._update_panes() pane_path = p.current_path if pane_path is None: pass elif path in pane_path or pane_path == path: result = path == pane_path or path in pane_path break # handle case with OS X adding /private/ to /tmp/ paths assert result
def test_expands_blank_panes(): """Expand blank config into full form. Handle ``NoneType`` and 'blank':: # nothing, None, 'blank' 'panes': [ None, 'blank' ] # should be blank 'panes': [ 'shell_command': [] ] Blank strings:: panes: [ '' ] # should output to: panes: 'shell_command': [''] """ yaml_config_file = os.path.join(example_dir, 'blank-panes.yaml') test_config = load_config(yaml_config_file) assert config.expand(test_config) == fixtures.expand_blank.expected
def test_replaces_start_directory(): env_key = "TESTHEY92" env_value = "HEYO1" yaml_config = """ start_directory: {TEST_VAR}/test shell_command_before: {TEST_VAR}/test2 before_script: {TEST_VAR}/test3 session_name: hi - {TEST_VAR} windows: - window_name: editor panes: - shell_command: - tail -F /var/log/syslog start_directory: /var/log - window_name: logging @ {TEST_VAR} automatic_rename: true panes: - shell_command: - htop """.format(TEST_VAR="${%s}" % env_key) sconfig = load_yaml(yaml_config) with EnvironmentVarGuard() as env: env.set(env_key, env_value) sconfig = config.expand(sconfig) assert "%s/test" % env_value == sconfig['start_directory'] assert "%s/test2" % env_value in sconfig['shell_command_before'] assert "%s/test3" % env_value == sconfig['before_script'] assert "hi - %s" % env_value == sconfig['session_name'] assert "logging @ %s" % env_value == \ sconfig['windows'][1]['window_name']
def test_suppress_history(session): yaml_config = loadfixture("workspacebuilder/suppress_history.yaml") sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) inHistoryWindow = session.find_where({'window_name': 'inHistory'}) isMissingWindow = session.find_where({'window_name': 'isMissing'}) def assertHistory(cmd, hist): return 'inHistory' in cmd and cmd.endswith(hist) def assertIsMissing(cmd, hist): return 'isMissing' in cmd and not cmd.endswith(hist) for w, window_name, assertCase in [( inHistoryWindow, 'inHistory', assertHistory, ), ( isMissingWindow, 'isMissing', assertIsMissing, )]: assert w.name == window_name correct = False w.select_window() p = w.attached_pane p.select_pane() # Print the last-in-history command in the pane p.cmd('send-keys', ' fc -ln -1') p.cmd('send-keys', 'Enter') buffer_name = 'test' for _ in range(10): time.sleep(0.1) # from v0.7.4 libtmux session.cmd adds target -t self.id by default # show-buffer doesn't accept -t, use global cmd. # Get the contents of the pane p.cmd('capture-pane', '-b', buffer_name) captured_pane = session.server.cmd('show-buffer', '-b', buffer_name) session.server.cmd('delete-buffer', '-b', buffer_name) # Parse the sent and last-in-history commands sent_cmd = captured_pane.stdout[0].strip() history_cmd = captured_pane.stdout[-2].strip() if assertCase(sent_cmd, history_cmd): correct = True break assert correct, "Unknown sent command: [%s] in %s" % (sent_cmd, assertCase)
def test_load_workspace_enter( tmp_path: pathlib.Path, server: libtmux.Server, monkeypatch: pytest.MonkeyPatch, yaml, output, should_see, ): yaml_config = tmp_path / "simple.yaml" yaml_config.write_text( yaml, encoding="utf-8", ) sconfig = kaptan.Kaptan(handler="yaml") sconfig = sconfig.import_config(str(yaml_config)).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig, server=server) builder.build() session = builder.session pane = session.attached_pane def fn(): captured_pane = "\n".join(pane.capture_pane()) if should_see: return output in captured_pane else: return output not in captured_pane assert retry_until( fn, 1), f'Should{" " if should_see else "not "} output in captured pane'
def test_start_directory(session, tmp_path: pathlib.Path): yaml_config = test_utils.read_config_file( "workspacebuilder/start_directory.yaml") test_dir = tmp_path / "foo bar" test_dir.mkdir() test_config = yaml_config.format(TEST_DIR=test_dir) sconfig = kaptan.Kaptan(handler="yaml") sconfig = sconfig.import_config(test_config).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) assert session == builder.session dirs = ["/usr/bin", "/dev", str(test_dir), "/usr", "/usr"] for path, window in zip(dirs, session.windows): for p in window.panes: def f(): p.server._update_panes() pane_path = p.current_path return path in pane_path or pane_path == path # handle case with OS X adding /private/ to /tmp/ paths assert retry_until(f)
def test_config_expand2(): """Expand shell commands from string to list.""" unexpanded_dict = load_yaml(fixtures.expand2.unexpanded_yaml) expanded_dict = load_yaml(fixtures.expand2.expanded_yaml) assert config.expand(unexpanded_dict) == expanded_dict
def test_shell_command_before(): """Config inheritance for the nested 'start_command'.""" test_config = fixtures.shell_command_before.config_unexpanded test_config = config.expand(test_config) assert test_config == fixtures.shell_command_before.config_expanded test_config = config.trickle(test_config) assert test_config == fixtures.shell_command_before.config_after
def test_start_directory_relative(session, tmpdir): """Same as above test, but with relative start directory, mimicing loading it from a location of project file. Like:: $ tmuxp load ~/workspace/myproject/.tmuxp.yaml instead of:: $ cd ~/workspace/myproject/.tmuxp.yaml $ tmuxp load . """ yaml_config = \ loadfixture("workspacebuilder/start_directory_relative.yaml") test_dir = str(tmpdir.mkdir('foo bar')) config_dir = str(tmpdir.mkdir('testRelConfigDir')) test_config = yaml_config.format(TEST_DIR=test_dir, ) sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(test_config).get() # the second argument of os.getcwd() mimics the behavior # the CLI loader will do, but it passes in the config file's location. sconfig = config.expand(sconfig, config_dir) sconfig = config.trickle(sconfig) assert os.path.exists(config_dir) assert os.path.exists(test_dir) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) assert session == builder.session dirs = [ '/usr/bin', '/dev', test_dir, config_dir, config_dir, ] for path, window in zip(dirs, session.windows): for p in window.panes: for _ in range(60): p.server._update_panes() # Handle case where directories resolve to /private/ in OSX pane_path = p.current_path if pane_path is None: pass elif (path in pane_path or pane_path == path): result = (path == pane_path or path in pane_path) break time.sleep(.2) assert result
def test_suppress_history(session): yaml_config = test_utils.read_config_file( "workspacebuilder/suppress_history.yaml") sconfig = kaptan.Kaptan(handler="yaml") sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) inHistoryWindow = session.find_where({"window_name": "inHistory"}) isMissingWindow = session.find_where({"window_name": "isMissing"}) def assertHistory(cmd, hist): return "inHistory" in cmd and cmd.endswith(hist) def assertIsMissing(cmd, hist): return "isMissing" in cmd and not cmd.endswith(hist) for w, window_name, assertCase in [ (inHistoryWindow, "inHistory", assertHistory), (isMissingWindow, "isMissing", assertIsMissing), ]: assert w.name == window_name w.select_window() p = w.attached_pane p.select_pane() # Print the last-in-history command in the pane p.cmd("send-keys", " fc -ln -1") p.cmd("send-keys", "Enter") buffer_name = "test" sent_cmd = None def f(): # from v0.7.4 libtmux session.cmd adds target -t self.id by default # show-buffer doesn't accept -t, use global cmd. # Get the contents of the pane p.cmd("capture-pane", "-b", buffer_name) captured_pane = session.server.cmd("show-buffer", "-b", buffer_name) session.server.cmd("delete-buffer", "-b", buffer_name) # Parse the sent and last-in-history commands sent_cmd = captured_pane.stdout[0].strip() history_cmd = captured_pane.stdout[-2].strip() return assertCase(sent_cmd, history_cmd) assert retry_until( f), f"Unknown sent command: [{sent_cmd}] in {assertCase}"
def test_pane_order(session): """Pane ordering based on position in config and ``pane_index``. Regression test for https://github.com/tmux-python/tmuxp/issues/15. """ yaml_config = loadfixture("workspacebuilder/pane_ordering.yaml").format( HOME=os.path.realpath(os.path.expanduser('~')) ) # test order of `panes` (and pane_index) above aganist pane_dirs pane_paths = [ '/usr/bin', '/usr', '/usr/sbin', os.path.realpath(os.path.expanduser('~')), ] s = session sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) window_count = len(session._windows) # current window count assert len(s._windows) == window_count for w, wconf in builder.iter_create_windows(s): for p in builder.iter_create_panes(w, wconf): w.select_layout('tiled') # fix glitch with pane size p = p assert len(s._windows) == window_count assert isinstance(w, Window) assert len(s._windows) == window_count window_count += 1 for w in session.windows: pane_base_index = w.show_window_option('pane-base-index', g=True) for p_index, p in enumerate(w.list_panes(), start=pane_base_index): assert int(p_index) == int(p.index) # pane-base-index start at base-index, pane_paths always start # at 0 since python list. pane_path = pane_paths[p_index - pane_base_index] while retry(): p.server._update_panes() if p.current_path == pane_path: break assert p.current_path, pane_path
def test_suppress_history(session): yaml_config = loadfixture("workspacebuilder/suppress_history.yaml") sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) inHistoryPane = session.find_where({ 'window_name': 'inHistory' }).attached_pane isMissingPane = session.find_where({ 'window_name': 'isMissing' }).attached_pane def assertHistory(cmd, hist): return 'inHistory' in cmd and cmd.endswith(hist) def assertIsMissing(cmd, hist): return 'isMissing' in cmd and not cmd.endswith(hist) for p, assertCase in [( inHistoryPane, assertHistory, ), ( isMissingPane, assertIsMissing, )]: correct = False p.window.select_window() p.select_pane() # Print the last-in-history command in the pane session.cmd('send-keys', ' fc -ln -1') session.cmd('send-keys', 'Enter') for _ in range(10): time.sleep(0.1) # Get the contents of the pane session.cmd('capture-pane') captured_pane = session.cmd('show-buffer') session.cmd('delete-buffer') # Parse the sent and last-in-history commands sent_cmd = captured_pane.stdout[0].strip() history_cmd = captured_pane.stdout[-2].strip() if assertCase(sent_cmd, history_cmd): correct = True break assert correct, "Unknown sent command: [%s]" % sent_cmd
def test_environment_variables(session): yaml_config = loadfixture("workspacebuilder/environment_vars.yaml") sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session) assert session.show_environment('FOO') == 'BAR' assert session.show_environment('PATH') == '/tmp'
def test_pane_order(session): """Pane ordering based on position in config and ``pane_index``. Regression test for https://github.com/tmux-python/tmuxp/issues/15. """ yaml_config = test_utils.read_config_file( "workspacebuilder/pane_ordering.yaml").format( HOME=os.path.realpath(os.path.expanduser("~"))) # test order of `panes` (and pane_index) above against pane_dirs pane_paths = [ "/usr/bin", "/usr", "/etc", os.path.realpath(os.path.expanduser("~")), ] s = session sconfig = kaptan.Kaptan(handler="yaml") sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) window_count = len(session._windows) # current window count assert len(s._windows) == window_count for w, wconf in builder.iter_create_windows(s): for p in builder.iter_create_panes(w, wconf): w.select_layout("tiled") # fix glitch with pane size assert len(s._windows) == window_count assert isinstance(w, Window) assert len(s._windows) == window_count window_count += 1 for w in session.windows: pane_base_index = w.show_window_option("pane-base-index", g=True) for p_index, p in enumerate(w.list_panes(), start=pane_base_index): assert int(p_index) == int(p.index) # pane-base-index start at base-index, pane_paths always start # at 0 since python list. pane_path = pane_paths[p_index - pane_base_index] def f(): p.server._update_panes() return p.current_path == pane_path assert retry_until(f)
def test_start_directory_relative(session, tmp_path: pathlib.Path): """Same as above test, but with relative start directory, mimicking loading it from a location of project file. Like:: $ tmuxp load ~/workspace/myproject/.tmuxp.yaml instead of:: $ cd ~/workspace/myproject/.tmuxp.yaml $ tmuxp load . """ yaml_config = test_utils.read_config_file( "workspacebuilder/start_directory_relative.yaml") test_dir = tmp_path / "foo bar" test_dir.mkdir() config_dir = tmp_path / "testRelConfigDir" config_dir.mkdir() test_config = yaml_config.format(TEST_DIR=test_dir) sconfig = kaptan.Kaptan(handler="yaml") sconfig = sconfig.import_config(test_config).get() # the second argument of os.getcwd() mimics the behavior # the CLI loader will do, but it passes in the config file's location. sconfig = config.expand(sconfig, config_dir) sconfig = config.trickle(sconfig) assert os.path.exists(config_dir) assert os.path.exists(test_dir) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) assert session == builder.session dirs = [ "/usr/bin", "/dev", str(test_dir), str(config_dir), str(config_dir) ] for path, window in zip(dirs, session.windows): for p in window.panes: def f(): p.server._update_panes() # Handle case where directories resolve to /private/ in OSX pane_path = p.current_path return path in pane_path or pane_path == path assert retry_until(f)
def test_session_options(session): yaml_config = loadfixture("workspacebuilder/session_options.yaml") s = session sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) assert "/bin/sh" in s.show_option('default-shell') assert "/bin/sh" in s.show_option('default-command')
def test_global_options(session): yaml_config = loadfixture("workspacebuilder/global_options.yaml") s = session sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) assert "top" in s.show_option('status-position', _global=True) assert 493 == s.show_option('repeat-time', _global=True)
def test_environment_variables(session): yaml_config = test_utils.read_config_file( "workspacebuilder/environment_vars.yaml") sconfig = kaptan.Kaptan(handler="yaml") sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session) assert session.show_environment("FOO") == "BAR" assert session.show_environment("PATH") == "/tmp"
def test_suppress_history(session): yaml_config = loadfixture("workspacebuilder/suppress_history.yaml") sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) inHistoryWindow = session.find_where({'window_name': 'inHistory'}) isMissingWindow = session.find_where({'window_name': 'isMissing'}) def assertHistory(cmd, hist): return 'inHistory' in cmd and cmd.endswith(hist) def assertIsMissing(cmd, hist): return 'isMissing' in cmd and not cmd.endswith(hist) for w, window_name, assertCase in [ (inHistoryWindow, 'inHistory', assertHistory), (isMissingWindow, 'isMissing', assertIsMissing), ]: assert w.name == window_name correct = False w.select_window() p = w.attached_pane p.select_pane() # Print the last-in-history command in the pane p.cmd('send-keys', ' fc -ln -1') p.cmd('send-keys', 'Enter') buffer_name = 'test' while retry(): # from v0.7.4 libtmux session.cmd adds target -t self.id by default # show-buffer doesn't accept -t, use global cmd. # Get the contents of the pane p.cmd('capture-pane', '-b', buffer_name) captured_pane = session.server.cmd('show-buffer', '-b', buffer_name) session.server.cmd('delete-buffer', '-b', buffer_name) # Parse the sent and last-in-history commands sent_cmd = captured_pane.stdout[0].strip() history_cmd = captured_pane.stdout[-2].strip() if assertCase(sent_cmd, history_cmd): correct = True break assert correct, "Unknown sent command: [%s] in %s" % (sent_cmd, assertCase)
def test_global_options(session): yaml_config = test_utils.read_config_file( "workspacebuilder/global_options.yaml") s = session sconfig = kaptan.Kaptan(handler="yaml") sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) assert "top" in s.show_option("status-position", _global=True) assert 493 == s.show_option("repeat-time", _global=True)
def test_session_options(session): yaml_config = test_utils.read_config_file( "workspacebuilder/session_options.yaml") s = session sconfig = kaptan.Kaptan(handler="yaml") sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) assert "/bin/sh" in s.show_option("default-shell") assert "/bin/sh" in s.show_option("default-command")
def test_plugin_system_after_window_finished(session): config_plugins = loadfixture("workspacebuilder/plugin_awf.yaml") sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(config_plugins).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig, plugins=load_plugins(sconfig)) assert len(builder.plugins) > 0 builder.build(session=session) proc = session.cmd('display-message', '-p', "'#W'") assert proc.stdout[0] == "'plugin_test_awf'"
def test_plugin_system_on_window_create(monkeypatch_plugin_test_packages, session): config_plugins = loadfixture("workspacebuilder/plugin_owc.yaml") sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(config_plugins).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig, plugins=load_plugins(sconfig)) assert len(builder.plugins) > 0 builder.build(session=session) proc = session.cmd('display-message', '-p', "'#W'") assert proc.stdout[0] == "'plugin_test_owc'"
def test_start_directory_relative(session, tmpdir): """Same as above test, but with relative start directory, mimicing loading it from a location of project file. Like:: $ tmuxp load ~/workspace/myproject/.tmuxp.yaml instead of:: $ cd ~/workspace/myproject/.tmuxp.yaml $ tmuxp load . """ yaml_config = loadfixture("workspacebuilder/start_directory_relative.yaml") test_dir = str(tmpdir.mkdir('foo bar')) config_dir = str(tmpdir.mkdir('testRelConfigDir')) test_config = yaml_config.format(TEST_DIR=test_dir) sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(test_config).get() # the second argument of os.getcwd() mimics the behavior # the CLI loader will do, but it passes in the config file's location. sconfig = config.expand(sconfig, config_dir) sconfig = config.trickle(sconfig) assert os.path.exists(config_dir) assert os.path.exists(test_dir) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) assert session == builder.session dirs = ['/usr/bin', '/dev', test_dir, config_dir, config_dir] for path, window in zip(dirs, session.windows): for p in window.panes: while retry(): p.server._update_panes() # Handle case where directories resolve to /private/ in OSX pane_path = p.current_path if pane_path is None: pass elif path in pane_path or pane_path == path: result = path == pane_path or path in pane_path break assert result
def test_suppress_history(session): yaml_config = loadfixture("workspacebuilder/suppress_history.yaml") sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) inHistoryPane = session.find_where( {'window_name': 'inHistory'}).attached_pane isMissingPane = session.find_where( {'window_name': 'isMissing'}).attached_pane def assertHistory(cmd, hist): return 'inHistory' in cmd and cmd == hist def assertIsMissing(cmd, hist): return 'isMissing' in cmd and cmd != hist for p, assertCase in [ (inHistoryPane, assertHistory,), (isMissingPane, assertIsMissing,) ]: correct = False p.window.select_window() p.select_pane() # Print the last-in-history command in the pane session.cmd('send-keys', ' fc -ln -1') session.cmd('send-keys', 'Enter') for _ in range(10): time.sleep(0.1) # Get the contents of the pane session.cmd('capture-pane') captured_pane = session.cmd('show-buffer') session.cmd('delete-buffer') # Parse the sent and last-in-history commands sent_cmd = captured_pane.stdout[0].strip() history_cmd = captured_pane.stdout[-2].strip() if assertCase(sent_cmd, history_cmd): correct = True break assert correct, "Unknown sent command: [%s]" % sent_cmd
def test_window_index(session): yaml_config = loadfixture("workspacebuilder/window_index.yaml") proc = session.cmd('show-option', '-gv', 'base-index') base_index = int(proc.stdout[0]) name_index_map = {'zero': 0 + base_index, 'one': 1 + base_index, 'five': 5} sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) for window, _ in builder.iter_create_windows(session): expected_index = name_index_map[window['window_name']] assert int(window['window_index']) == expected_index
def test_trickle_window_with_no_pane_config(): test_yaml = """ session_name: test_session windows: - window_name: test_1 panes: - shell_command: - ls -l - window_name: test_no_panes """ sconfig = load_yaml(test_yaml) config.validate_schema(sconfig) assert config.expand(config.trickle(sconfig))['windows'][1]['panes'][0] == { 'shell_command': [] }
def test_before_load_true_if_test_passes(server): config_script_completes = loadfixture( "workspacebuilder/config_script_completes.yaml" ) assert os.path.exists(os.path.join(fixtures_dir, 'script_complete.sh')) sconfig = kaptan.Kaptan(handler='yaml') yaml = config_script_completes.format( fixtures_dir=fixtures_dir, script_complete=os.path.join(fixtures_dir, 'script_complete.sh'), ) sconfig = sconfig.import_config(yaml).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) with temp_session(server) as session: builder.build(session=session)
def test_window_shell(session): yaml_config = loadfixture("workspacebuilder/window_shell.yaml") s = session sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig) for w, wconf in builder.iter_create_windows(s): if 'window_shell' in wconf: assert wconf['window_shell'] == text_type('top') while retry(): session.server._update_windows() if w['window_name'] != 'top': break assert w.name != text_type('top')
def test_window_options_after(session): yaml_config = loadfixture("workspacebuilder/window_options_after.yaml") sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) def assert_last_line(p, s): correct = False while retry(): pane_out = p.cmd('capture-pane', '-p', '-J').stdout while not pane_out[-1].strip(): # delete trailing lines tmux 1.8 pane_out.pop() if len(pane_out) > 1 and pane_out[-2].strip() == s: correct = True break # Print output for easier debugging if assertion fails if not correct: print('\n'.join(pane_out)) return correct for i, pane in enumerate(session.attached_window.panes): assert assert_last_line( pane, str(i) ), "Initial command did not execute properly/" + str(i) pane.cmd('send-keys', 'Up') # Will repeat echo pane.enter() # in each iteration assert assert_last_line( pane, str(i) ), "Repeated command did not execute properly/" + str(i) session.cmd('send-keys', ' echo moo') session.cmd('send-keys', 'Enter') for pane in session.attached_window.panes: assert assert_last_line( pane, 'moo' ), "Synchronized command did not execute properly"
def test_global_session_env_options(session, monkeypatch): visual_silence = 'on' monkeypatch.setenv(str('VISUAL_SILENCE'), str(visual_silence)) repeat_time = 738 monkeypatch.setenv(str('REPEAT_TIME'), str(repeat_time)) main_pane_height = 8 monkeypatch.setenv(str('MAIN_PANE_HEIGHT'), str(main_pane_height)) yaml_config = loadfixture("workspacebuilder/env_var_options.yaml") s = session sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) assert visual_silence in s.show_option('visual-silence', _global=True) assert repeat_time == s.show_option('repeat-time') assert main_pane_height == s.attached_window.show_window_option('main-pane-height')
def test_window_options(session): yaml_config = loadfixture("workspacebuilder/window_options.yaml") s = session sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) builder = WorkspaceBuilder(sconf=sconfig) window_count = len(session._windows) # current window count assert len(s._windows) == window_count for w, wconf in builder.iter_create_windows(s): for p in builder.iter_create_panes(w, wconf): p = p assert len(s._windows) == window_count assert isinstance(w, Window) assert w.show_window_option('main-pane-height') == 5 assert len(s._windows) == window_count window_count += 1 w.select_layout(wconf['layout'])
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() test_config = config.expand(test_config) builder = WorkspaceBuilder(sconf=test_config) builder.build(session=session) assert session == builder.session window1 = session.find_where({'window_name': 'Blank pane test'}) assert len(window1._panes) == 3 window2 = session.find_where({'window_name': 'More blank panes'}) assert len(window2._panes) == 3 window3 = session.find_where({'window_name': 'Empty string (return)'}) assert len(window3._panes) == 3 window4 = session.find_where({'window_name': 'Blank with options'}) assert len(window4._panes) == 2
def test_replaces_env_variables(monkeypatch): env_key = "TESTHEY92" env_val = "HEYO1" yaml_config = """ start_directory: {TEST_VAR}/test shell_command_before: {TEST_VAR}/test2 before_script: {TEST_VAR}/test3 session_name: hi - {TEST_VAR} options: default-command: {TEST_VAR}/lol global_options: default-shell: {TEST_VAR}/moo windows: - window_name: editor panes: - shell_command: - tail -F /var/log/syslog start_directory: /var/log - window_name: logging @ {TEST_VAR} automatic-rename: true panes: - shell_command: - htop """.format( TEST_VAR="${%s}" % env_key ) sconfig = load_yaml(yaml_config) monkeypatch.setenv(env_key, env_val) sconfig = config.expand(sconfig) assert "%s/test" % env_val == sconfig['start_directory'] assert "%s/test2" % env_val in sconfig['shell_command_before'] assert "%s/test3" % env_val == sconfig['before_script'] assert "hi - %s" % env_val == sconfig['session_name'] assert "%s/moo" % env_val == sconfig['global_options']['default-shell'] assert "%s/lol" % env_val == sconfig['options']['default-command'] assert "logging @ %s" % env_val == sconfig['windows'][1]['window_name']
def test_config_expand1(): """Expand shell commands from string to list.""" test_config = config.expand(fixtures.expand1.before_config) assert test_config == fixtures.expand1.after_config
def test_focus_pane_index(session): yaml_config = loadfixture('workspacebuilder/focus_and_pane.yaml') sconfig = kaptan.Kaptan(handler='yaml') sconfig = sconfig.import_config(yaml_config).get() sconfig = config.expand(sconfig) sconfig = config.trickle(sconfig) builder = WorkspaceBuilder(sconf=sconfig) builder.build(session=session) assert session.attached_window.name == 'focused window' pane_base_index = int( session.attached_window.show_window_option('pane-base-index', g=True) ) if not pane_base_index: pane_base_index = 0 else: pane_base_index = int(pane_base_index) # get the pane index for each pane pane_base_indexes = [] for pane in session.attached_window.panes: pane_base_indexes.append(int(pane.index)) pane_indexes_should_be = [pane_base_index + x for x in range(0, 3)] assert pane_indexes_should_be == pane_base_indexes w = session.attached_window assert w.name != 'man' pane_path = '/usr' while retry(): p = w.attached_pane p.server._update_panes() if p.current_path == pane_path: break assert p.current_path == pane_path proc = session.cmd('show-option', '-gv', 'base-index') base_index = int(proc.stdout[0]) session.server._update_windows() window3 = session.find_where({'window_index': str(base_index + 2)}) assert isinstance(window3, Window) p = None pane_path = '/' while retry(): p = window3.attached_pane p.server._update_panes() if p.current_path == pane_path: break assert p.current_path == pane_path