1
0
Fork 0

Fix for pgcli --list. (#952)

Fix for pgcli --list
This commit is contained in:
Irina Truong 2018-10-02 17:10:43 -07:00 committed by GitHub
parent ec5131e1da
commit fcf0eb022e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 11 deletions

View File

@ -1,7 +1,7 @@
Upcoming:
=========
TODO
* Fix for error listing databases (#951). (Thanks: `Irina Truong`_)
2.0.0:
======

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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')