From fcf0eb022ee81cc8e961c08c4a89742a9714cd63 Mon Sep 17 00:00:00 2001 From: Irina Truong Date: Tue, 2 Oct 2018 17:10:43 -0700 Subject: Fix for pgcli --list. (#952) Fix for pgcli --list --- changelog.rst | 2 +- pgcli/main.py | 12 ++++++++++-- tests/features/basic_commands.feature | 5 +++++ tests/features/environment.py | 21 +++++++++++++-------- tests/features/steps/basic_commands.py | 16 ++++++++++++++++ tests/features/steps/iocommands.py | 1 + 6 files changed, 46 insertions(+), 11 deletions(-) diff --git a/changelog.rst b/changelog.rst index 8d7539cf..54a4acd6 100644 --- a/changelog.rst +++ b/changelog.rst @@ -1,7 +1,7 @@ Upcoming: ========= -TODO +* Fix for error listing databases (#951). (Thanks: `Irina Truong`_) 2.0.0: ====== diff --git a/pgcli/main.py b/pgcli/main.py index f0c2d430..53ed99f7 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -905,10 +905,18 @@ class PGCli(object): """Get the last query executed or None.""" return self.query_history[-1][0] if self.query_history else None - def is_wide_line(self, line): + def is_too_wide(self, line): """Will this line be too wide to fit into terminal?""" + if not self.prompt_app: + return False return len(COLOR_CODE_REGEX.sub('', line)) > self.prompt_app.output.get_size().columns + def is_too_tall(self, lines): + """Are there too many lines to fit into terminal?""" + if not self.prompt_app: + return False + return len(lines) >= (self.prompt_app.output.get_size().rows - 4) + def echo_via_pager(self, text, color=None): if self.pgspecial.pager_config == PAGER_OFF or self.watch_command: click.echo(text, color=color) @@ -916,7 +924,7 @@ class PGCli(object): lines = text.split('\n') # The last 4 lines are reserved for the pgcli menu and padding - if len(lines) >= self.prompt_app.output.get_size().rows - 4 or any(self.is_wide_line(l) for l in lines): + if self.is_too_tall(lines) or any(self.is_too_wide(l) for l in lines): click.echo_via_pager(text, color=color) else: click.echo(text, color=color) diff --git a/tests/features/basic_commands.feature b/tests/features/basic_commands.feature index a12e8992..8c9ce437 100644 --- a/tests/features/basic_commands.feature +++ b/tests/features/basic_commands.feature @@ -17,3 +17,8 @@ Feature: run the cli, Scenario: run the cli and exit When we send "ctrl + d" then dbcli exits + + Scenario: list databases + When we list databases + then we see list of databases + diff --git a/tests/features/environment.py b/tests/features/environment.py index 5fa8b299..dedbe093 100644 --- a/tests/features/environment.py +++ b/tests/features/environment.py @@ -9,6 +9,7 @@ import fixture_utils as fixutils import pexpect import tempfile import shutil +import signal from steps import wrappers @@ -146,24 +147,28 @@ def before_step(context, _): context.atprompt = False -def before_scenario(context, _): +def before_scenario(context, scenario): + if scenario.name == 'list databases': + # not using the cli for that + return wrappers.run_cli(context) wrappers.wait_prompt(context) -def after_scenario(context, _): +def after_scenario(context, scenario): """Cleans up after each scenario completes.""" - if hasattr(context, 'cli') and not context.exit_sent: + if hasattr(context, 'cli') and context.cli and not context.exit_sent: # Quit nicely. if not context.atprompt: dbname = context.currentdb - context.cli.expect_exact( - '{0}> '.format(dbname), - timeout=5 - ) + context.cli.expect_exact('{0}> '.format(dbname), timeout=15) context.cli.sendcontrol('c') context.cli.sendcontrol('d') - context.cli.expect_exact(pexpect.EOF, timeout=10) + try: + context.cli.expect_exact(pexpect.EOF, timeout=15) + except pexpect.TIMEOUT: + print('--- after_scenario {}: kill cli'.format(scenario.name)) + context.cli.kill(signal.SIGKILL) if hasattr(context, 'tmpfile_sql_help') and context.tmpfile_sql_help: context.tmpfile_sql_help.close() context.tmpfile_sql_help = None diff --git a/tests/features/steps/basic_commands.py b/tests/features/steps/basic_commands.py index 9bafb61d..ab46089e 100644 --- a/tests/features/steps/basic_commands.py +++ b/tests/features/steps/basic_commands.py @@ -6,6 +6,8 @@ This string is used to call the step in "*.feature" file. """ from __future__ import unicode_literals, print_function +import pexpect +import subprocess import tempfile from behave import when, then @@ -13,6 +15,19 @@ from textwrap import dedent import wrappers +@when('we list databases') +def step_list_databases(context): + cmd = ['pgcli', '--list'] + context.cmd_output = subprocess.check_output(cmd, cwd=context.package_root) + + +@then('we see list of databases') +def step_see_list_databases(context): + assert b'List of databases' in context.cmd_output + assert b'postgres' in context.cmd_output + context.cmd_output = None + + @when('we run dbcli') def step_run_cli(context): wrappers.run_cli(context) @@ -32,6 +47,7 @@ def step_ctrl_d(context): context.cli.sendline('\pset pager off') wrappers.wait_prompt(context) context.cli.sendcontrol('d') + context.cli.expect_exact(pexpect.EOF, timeout=15) context.exit_sent = True diff --git a/tests/features/steps/iocommands.py b/tests/features/steps/iocommands.py index 95bf8652..1a1a4c5c 100644 --- a/tests/features/steps/iocommands.py +++ b/tests/features/steps/iocommands.py @@ -44,6 +44,7 @@ def step_edit_done_sql(context): # Cleanup the edited file. if context.editor_file_name and os.path.exists(context.editor_file_name): os.remove(context.editor_file_name) + context.atprompt = True @when(u'we tee output') -- cgit v1.2.3