Skip to content

Commit

Permalink
fixed bug that caused crash to crash on startup when using python 2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
chaudum committed Jul 6, 2017
1 parent 4019fe3 commit 48c58df
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Changes for crash
Unreleased
==========

- Fixed bug that caused ``crash`` to crash on startup when using Python 2.7.

2017/07/04 0.21.1
=================

Expand Down
23 changes: 14 additions & 9 deletions src/crate/crash/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def __init__(self, cmd):
self.cmd = cmd
self.last_changed = None

def __call__(self, buffer):
def apply_capitalization(self, buffer):
if not self.cmd.should_autocapitalize():
return

Expand Down Expand Up @@ -260,6 +260,18 @@ def keyword_replacer(self, match):
def is_prefix(self, string, prefix):
return string.startswith(prefix) and string != prefix


def create_buffer(cmd, history_file):
buffer = CrashBuffer(
history=TruncatedFileHistory(history_file, max_length=MAX_HISTORY_LENGTH),
accept_action=AcceptAction.RETURN_DOCUMENT,
completer=SQLCompleter(cmd),
on_text_insert=Capitalizer(cmd).apply_capitalization
)
buffer.complete_while_typing = lambda cli=None: cmd.should_autocomplete()
return buffer


def loop(cmd, history_file):
key_binding_manager = KeyBindingManager(
enable_search=True,
Expand All @@ -277,16 +289,9 @@ def loop(cmd, history_file):
filter=HasFocus(DEFAULT_BUFFER) & ~IsDone())
]
)
buffer = CrashBuffer(
history=TruncatedFileHistory(history_file, max_length=MAX_HISTORY_LENGTH),
accept_action=AcceptAction.RETURN_DOCUMENT,
completer=SQLCompleter(cmd),
on_text_insert=Capitalizer(cmd)
)
buffer.complete_while_typing = lambda cli=None: cmd.should_autocomplete()
application = Application(
layout=layout,
buffer=buffer,
buffer=create_buffer(cmd, history_file),
style=PygmentsStyle.from_defaults(pygments_style_cls=CrateStyle),
key_bindings_registry=key_binding_manager.registry,
editing_mode=_get_editing_mode(),
Expand Down
36 changes: 22 additions & 14 deletions src/crate/crash/test_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
# with Crate these terms will supersede the license and you may use the
# software solely pursuant to the terms of the relevant commercial agreement.

import re
from unittest import TestCase
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.document import Document
from .repl import SQLCompleter, Capitalizer
from .repl import SQLCompleter, Capitalizer, create_buffer
from .command import CrateCmd
import re

class SQLCompleterTest(TestCase):

Expand All @@ -39,6 +39,14 @@ def test_get_command_completions_format(self):
self.assertEqual(result, ['dynamic'])


class CrashBufferTest(TestCase):

def test_create_buffer(self):
cmd = CrateCmd()
buffer = create_buffer(cmd, '/tmp/history')
self.assertEquals(buffer.on_text_insert.fire(), None)


class AutoCapitalizeTest(TestCase):

def setUp(self):
Expand All @@ -50,65 +58,65 @@ def test_capitalize(self):

text = u'selec'
buffer.set_document(Document(text, len(text)))
self.capitalizer(buffer)
self.capitalizer.apply_capitalization(buffer)
self.assertEqual(u'selec', buffer.text)

text = u'select'
buffer.set_document(Document(text, len(text)))
self.capitalizer(buffer)
self.capitalizer.apply_capitalization(buffer)
self.assertEqual(u'SELECT', buffer.text)

text = u'CREATE TABLE "select'
buffer.set_document(Document(text, len(text)))
self.capitalizer(buffer)
self.capitalizer.apply_capitalization(buffer)
self.assertEqual(u'CREATE TABLE "select', buffer.text)

text = u'CREATE TABLE \'select\''
buffer.set_document(Document(text, len(text)))
self.capitalizer(buffer)
self.capitalizer.apply_capitalization(buffer)
self.assertEqual(u'CREATE TABLE \'select\'', buffer.text)

text = u'create table test (x int)'
buffer.set_document(Document(text, len(text)))
self.capitalizer(buffer)
self.capitalizer.apply_capitalization(buffer)
self.assertEqual(u'CREATE TABLE test (x INT)', buffer.text)

text = u'create table test (a boolean, b string, c integer)'
buffer.set_document(Document(text, len(text)))
self.capitalizer(buffer)
self.capitalizer.apply_capitalization(buffer)
self.assertEqual(u'CREATE TABLE test (a BOOLEAN, b STRING, c INTEGER)', buffer.text)

text = u'create table test\n(a boolean, b string, c integer)'
buffer.set_document(Document(text, len(text)))
self.capitalizer(buffer)
self.capitalizer.apply_capitalization(buffer)
self.assertEqual(u'CREATE TABLE test\n(a BOOLEAN, b STRING, c INTEGER)', buffer.text)

text = u'\select dynamic'
buffer.set_document(Document(text, len(text)))
self.capitalizer(buffer)
self.capitalizer.apply_capitalization(buffer)
self.assertEqual(u'\select dynamic', buffer.text)

def test_undo_capitalize(self):
buffer = Buffer()

text = u'Selec'
buffer.set_document(Document(text, len(text)))
self.capitalizer(buffer)
self.capitalizer.apply_capitalization(buffer)
self.assertEqual(u'Selec', buffer.text)

text = buffer.text + 't'
buffer.set_document(Document(text, len(text)))
self.capitalizer(buffer)
self.capitalizer.apply_capitalization(buffer)
self.assertEqual(u'SELECT', buffer.text)

text = buffer.text + 'i'
buffer.set_document(Document(text, len(text)))
self.capitalizer(buffer)
self.capitalizer.apply_capitalization(buffer)
self.assertEqual(u'Selecti', buffer.text)

text = buffer.text + 'on'
buffer.set_document(Document(text, len(text)))
self.capitalizer(buffer)
self.capitalizer.apply_capitalization(buffer)
self.assertEqual(u'Selection', buffer.text)

def test_keyword_replacer(self):
Expand Down
3 changes: 2 additions & 1 deletion src/crate/crash/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
ToggleAutocompleteCommandTest, ToggleAutoCapitalizeCommandTest, ToggleVerboseCommandTest, \
ChecksCommandTest, ShowTablesCommandTest
from .test_sysinfo import SysInfoTest
from .test_repl import SQLCompleterTest, AutoCapitalizeTest
from .test_repl import SQLCompleterTest, AutoCapitalizeTest, CrashBufferTest
from .test_config import ConfigurationTest


Expand Down Expand Up @@ -107,6 +107,7 @@ def test_suite():
suite.addTest(unittest.makeSuite(ShowTablesCommandTest))
suite.addTest(unittest.makeSuite(SQLCompleterTest))
suite.addTest(unittest.makeSuite(AutoCapitalizeTest))
suite.addTest(unittest.makeSuite(CrashBufferTest))
suite.addTest(unittest.makeSuite(ConfigurationTest))
suite.addTest(unittest.makeSuite(TestGetInformationSchemaQuery))

Expand Down

0 comments on commit 48c58df

Please sign in to comment.