From da12a70cbb3b3d9e2915b864742eee9f71e3e627 Mon Sep 17 00:00:00 2001 From: Iryna Cherniavska Date: Mon, 24 Aug 2015 17:46:40 -0700 Subject: [PATCH] Added read_from_file back into iospecial (it was removed but is still being used. Added integration test for editing a file. --- pgcli/packages/pgspecial/iocommands.py | 7 +++++ tests/features/crud_table.feature | 1 - tests/features/environment.py | 4 ++- tests/features/iocommands.feature | 11 +++++++ tests/features/steps/step_definitions.py | 37 ++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 tests/features/iocommands.feature diff --git a/pgcli/packages/pgspecial/iocommands.py b/pgcli/packages/pgspecial/iocommands.py index 621b5862..b48ceb69 100644 --- a/pgcli/packages/pgspecial/iocommands.py +++ b/pgcli/packages/pgspecial/iocommands.py @@ -1,6 +1,8 @@ import re import logging import click +import io +from os.path import expanduser from .namedqueries import namedqueries from . import export from .main import special_command @@ -66,6 +68,11 @@ def open_external_editor(filename=None, sql=''): return (query, message) +def read_from_file(path): + with io.open(expanduser(path), encoding='utf-8') as f: + contents = f.read() + return contents + @special_command('\\n', '\\n[+] [name]', 'List or execute named queries.') def execute_named_query(cur, pattern, **_): """Returns (title, rows, headers, status)""" diff --git a/tests/features/crud_table.feature b/tests/features/crud_table.feature index 05ddb0e9..2631372b 100644 --- a/tests/features/crud_table.feature +++ b/tests/features/crud_table.feature @@ -1,7 +1,6 @@ Feature: manipulate tables: create, insert, update, select, delete from, drop - @wip Scenario: create, insert, select from, update, drop table Given we have pgcli installed when we run pgcli diff --git a/tests/features/environment.py b/tests/features/environment.py index 25b3acd4..2db9fdd8 100644 --- a/tests/features/environment.py +++ b/tests/features/environment.py @@ -16,6 +16,7 @@ def before_all(context): os.environ['LINES'] = "100" os.environ['COLUMNS'] = "100" os.environ['PAGER'] = 'cat' + os.environ['EDITOR'] = 'nano' context.exit_sent = False @@ -30,6 +31,7 @@ def before_all(context): 'pass': context.config.userdata.get('pg_test_pass', None), 'dbname': db_name_full, 'dbname_tmp': db_name_full + '_tmp', + 'vi': vi } # Store old env vars. @@ -84,4 +86,4 @@ def after_scenario(context, _): if hasattr(context, 'cli') and not context.exit_sent: # Send Ctrl + D into cli context.cli.sendcontrol('d') - context.cli.expect(pexpect.EOF) + context.cli.expect(pexpect.EOF, timeout=5) diff --git a/tests/features/iocommands.feature b/tests/features/iocommands.feature new file mode 100644 index 00000000..f6f9bfa9 --- /dev/null +++ b/tests/features/iocommands.feature @@ -0,0 +1,11 @@ +Feature: I/O commands + + @wip + Scenario: edit sql in file with external editor + Given we have pgcli installed + when we run pgcli + and we wait for prompt + and we start external editor providing a file name + and we type sql in the editor + and we exit the editor + then we see the sql in prompt diff --git a/tests/features/steps/step_definitions.py b/tests/features/steps/step_definitions.py index 4b5da258..05dbcb51 100644 --- a/tests/features/steps/step_definitions.py +++ b/tests/features/steps/step_definitions.py @@ -8,6 +8,7 @@ from __future__ import unicode_literals import pip import pexpect +import os from behave import given, when, then @@ -133,6 +134,42 @@ def step_db_connect_test(context): context.cli.sendline('\connect {0}'.format(db_name)) +@when('we start external editor providing a file name') +def step_edit_file(context): + """ + Edit file with external editor. + """ + context.editor_file_name = 'test_file_{0}.sql'.format(context.conf['vi']) + if os.path.exists(context.editor_file_name): + os.remove(context.editor_file_name) + context.cli.sendline('\e {0}'.format(context.editor_file_name)) + context.cli.expect_exact('nano', timeout=2) + + +@when('we type sql in the editor') +def step_edit_type_sql(context): + context.cli.sendline('select * from abc') + # Write the file. + context.cli.sendcontrol('o') + # Confirm file name sending "enter". + context.cli.sendcontrol('m') + + +@when('we exit the editor') +def step_edit_quit(context): + context.cli.sendcontrol('x') + + +@then('we see the sql in prompt') +def step_edit_done_sql(context): + context.cli.expect_exact('select * from abc', timeout=2) + # Cleanup the command line. + context.cli.sendcontrol('u') + # Cleanup the edited file. + if context.editor_file_name and os.path.exists(context.editor_file_name): + os.remove(context.editor_file_name) + + @when('we connect to postgres') def step_db_connect_postgres(context): """