Fixed small bugs on command_history and tests

This commit is contained in:
Henrique 2019-11-27 09:21:30 -05:00
parent 863d2fbcb2
commit 8eb173b44e
2 changed files with 160 additions and 144 deletions

View File

@ -33,11 +33,12 @@ class CommandHistory:
if "command_history" in updated or "confdir" in updated: if "command_history" in updated or "confdir" in updated:
if ctx.options.command_history and self.history_file.is_file(): if ctx.options.command_history and self.history_file.is_file():
self.history = self.history_file.read_text().splitlines() self.history = self.history_file.read_text().splitlines()
self.set_filter('')
def done(self): def done(self):
if ctx.options.command_history and len(self.history) > self.VACUUM_SIZE: if ctx.options.command_history and len(self.history) > self.VACUUM_SIZE:
# vacuum history so that it doesn't grow indefinitely. # vacuum history so that it doesn't grow indefinitely.
history_str = "\n".join(self.history[-self.VACUUM_SIZE/2:]) + "\n" history_str = "\n".join(self.history[-self.VACUUM_SIZE / 2:]) + "\n"
self.history_file.write_text(history_str) self.history_file.write_text(history_str)
@command.command("commands.history.add") @command.command("commands.history.add")
@ -49,6 +50,9 @@ class CommandHistory:
if ctx.options.command_history: if ctx.options.command_history:
with self.history_file.open("a") as f: with self.history_file.open("a") as f:
f.write(f"{command}\n") f.write(f"{command}\n")
f.close()
self.set_filter('')
@command.command("commands.history.get") @command.command("commands.history.get")
def get_history(self) -> typing.Sequence[str]: def get_history(self) -> typing.Sequence[str]:
@ -57,8 +61,10 @@ class CommandHistory:
@command.command("commands.history.clear") @command.command("commands.history.clear")
def clear_history(self): def clear_history(self):
self.history_file.unlink() if self.history_file.exists():
self.history_file.unlink()
self.history = [] self.history = []
self.set_filter('')
# Functionality to provide a filtered list that can be iterated through. # Functionality to provide a filtered list that can be iterated through.

View File

@ -26,200 +26,210 @@ class TestCommandHistory:
history.add_command('') history.add_command('')
assert history.history == ['cmd1', 'cmd2'] assert history.history == ['cmd1', 'cmd2']
def test_get_next_and_prev(self, tctx): def test_get_next_and_prev(self, tmpdir):
history = command_history.CommandHistory(5) ch = command_history.CommandHistory()
history.configure([])
history.add_command('cmd1') with taddons.context(ch) as tctx:
tctx.options.confdir = str(tmpdir)
assert history.get_next() == '' ch.add_command('cmd1')
assert history.get_next() == ''
assert history.get_prev() == 'cmd1'
assert history.get_prev() == 'cmd1'
assert history.get_prev() == 'cmd1'
assert history.get_next() == ''
assert history.get_next() == ''
history.add_command('cmd2') assert ch.get_next() == ''
assert ch.get_next() == ''
assert ch.get_prev() == 'cmd1'
assert ch.get_prev() == 'cmd1'
assert ch.get_prev() == 'cmd1'
assert ch.get_next() == ''
assert ch.get_next() == ''
assert history.get_next() == '' ch.add_command('cmd2')
assert history.get_next() == ''
assert history.get_prev() == 'cmd2'
assert history.get_prev() == 'cmd1'
assert history.get_prev() == 'cmd1'
assert history.get_next() == 'cmd2'
assert history.get_next() == ''
assert history.get_next() == ''
history.add_command('cmd3') assert ch.get_next() == ''
assert ch.get_next() == ''
assert ch.get_prev() == 'cmd2'
assert ch.get_prev() == 'cmd1'
assert ch.get_prev() == 'cmd1'
assert ch.get_next() == 'cmd2'
assert ch.get_next() == ''
assert ch.get_next() == ''
assert history.get_next() == '' ch.add_command('cmd3')
assert history.get_next() == ''
assert history.get_prev() == 'cmd3'
assert history.get_prev() == 'cmd2'
assert history.get_prev() == 'cmd1'
assert history.get_prev() == 'cmd1'
assert history.get_next() == 'cmd2'
assert history.get_next() == 'cmd3'
assert history.get_next() == ''
assert history.get_next() == ''
assert history.get_prev() == 'cmd3'
assert history.get_prev() == 'cmd2'
history.add_command('cmd4') assert ch.get_next() == ''
assert ch.get_next() == ''
assert ch.get_prev() == 'cmd3'
assert ch.get_prev() == 'cmd2'
assert ch.get_prev() == 'cmd1'
assert ch.get_prev() == 'cmd1'
assert ch.get_next() == 'cmd2'
assert ch.get_next() == 'cmd3'
assert ch.get_next() == ''
assert ch.get_next() == ''
assert ch.get_prev() == 'cmd3'
assert ch.get_prev() == 'cmd2'
assert history.get_prev() == 'cmd4' ch.add_command('cmd4')
assert history.get_prev() == 'cmd3'
assert history.get_prev() == 'cmd2'
assert history.get_prev() == 'cmd1'
assert history.get_prev() == 'cmd1'
assert history.get_next() == 'cmd2'
assert history.get_next() == 'cmd3'
assert history.get_next() == 'cmd4'
assert history.get_next() == ''
assert history.get_next() == ''
history.add_command('cmd5') assert ch.get_prev() == 'cmd4'
history.add_command('cmd6') assert ch.get_prev() == 'cmd3'
assert ch.get_prev() == 'cmd2'
assert ch.get_prev() == 'cmd1'
assert ch.get_prev() == 'cmd1'
assert ch.get_next() == 'cmd2'
assert ch.get_next() == 'cmd3'
assert ch.get_next() == 'cmd4'
assert ch.get_next() == ''
assert ch.get_next() == ''
assert history.get_next() == '' ch.add_command('cmd5')
assert history.get_prev() == 'cmd6' ch.add_command('cmd6')
assert history.get_prev() == 'cmd5'
assert history.get_prev() == 'cmd4'
assert history.get_next() == 'cmd5'
assert history.get_prev() == 'cmd4'
assert history.get_prev() == 'cmd3'
assert history.get_prev() == 'cmd2'
assert history.get_next() == 'cmd3'
assert history.get_prev() == 'cmd2'
assert history.get_prev() == 'cmd2'
assert history.get_next() == 'cmd3'
assert history.get_next() == 'cmd4'
assert history.get_next() == 'cmd5'
assert history.get_next() == 'cmd6'
assert history.get_next() == ''
assert history.get_next() == ''
history.cleanup() assert ch.get_next() == ''
assert ch.get_prev() == 'cmd6'
assert ch.get_prev() == 'cmd5'
assert ch.get_prev() == 'cmd4'
assert ch.get_next() == 'cmd5'
assert ch.get_prev() == 'cmd4'
assert ch.get_prev() == 'cmd3'
assert ch.get_prev() == 'cmd2'
assert ch.get_next() == 'cmd3'
assert ch.get_prev() == 'cmd2'
assert ch.get_prev() == 'cmd1'
assert ch.get_prev() == 'cmd1'
assert ch.get_prev() == 'cmd1'
assert ch.get_next() == 'cmd2'
assert ch.get_next() == 'cmd3'
assert ch.get_next() == 'cmd4'
assert ch.get_next() == 'cmd5'
assert ch.get_next() == 'cmd6'
assert ch.get_next() == ''
assert ch.get_next() == ''
def test_clear(self, tctx): ch.clear_history()
history = command_history.CommandHistory(3)
history.configure([])
history.add_command('cmd1') def test_clear(self, tmpdir):
history.add_command('cmd2') ch = command_history.CommandHistory()
history.clear_history()
saved_commands = [cmd for cmd in history.history] with taddons.context(ch) as tctx:
assert saved_commands == [] tctx.options.confdir = str(tmpdir)
ch.add_command('cmd1')
ch.add_command('cmd2')
ch.clear_history()
assert history.get_next() == '' saved_commands = ch.get_history()
assert history.get_next() == '' assert saved_commands == []
assert history.get_prev() == ''
assert history.get_prev() == ''
history.cleanup() assert ch.get_next() == ''
assert ch.get_next() == ''
assert ch.get_prev() == ''
assert ch.get_prev() == ''
def test_filter(self, tctx): ch.clear_history()
history = command_history.CommandHistory(3)
history.configure([])
history.add_command('cmd1') def test_filter(self, tmpdir):
history.add_command('cmd2') ch = command_history.CommandHistory()
history.add_command('abc')
history.set_filter('c')
assert history.get_next() == '' with taddons.context(ch) as tctx:
assert history.get_next() == '' tctx.options.confdir = str(tmpdir)
assert history.get_prev() == 'c'
assert history.get_prev() == 'cmd2'
assert history.get_prev() == 'cmd1'
assert history.get_prev() == 'cmd1'
assert history.get_next() == 'cmd2'
assert history.get_next() == 'c'
assert history.get_next() == ''
assert history.get_next() == ''
history.set_filter('') ch.add_command('cmd1')
ch.add_command('cmd2')
ch.add_command('abc')
ch.set_filter('c')
assert history.get_next() == '' assert ch.get_next() == 'c'
assert history.get_next() == '' assert ch.get_next() == 'c'
assert history.get_prev() == 'abc' assert ch.get_prev() == 'cmd2'
assert history.get_prev() == 'cmd2' assert ch.get_prev() == 'cmd1'
assert history.get_prev() == 'cmd1' assert ch.get_prev() == 'cmd1'
assert history.get_prev() == 'cmd1' assert ch.get_next() == 'cmd2'
assert history.get_next() == 'cmd2' assert ch.get_next() == 'c'
assert history.get_next() == 'abc' assert ch.get_next() == 'c'
assert history.get_next() == ''
assert history.get_next() == ''
history.cleanup() ch.set_filter('')
assert ch.get_next() == ''
assert ch.get_next() == ''
assert ch.get_prev() == 'abc'
assert ch.get_prev() == 'cmd2'
assert ch.get_prev() == 'cmd1'
assert ch.get_prev() == 'cmd1'
assert ch.get_next() == 'cmd2'
assert ch.get_next() == 'abc'
assert ch.get_next() == ''
assert ch.get_next() == ''
ch.clear_history()
def test_multiple_instances(self, tmpdir):
ch = command_history.CommandHistory()
with taddons.context(ch) as tctx:
tctx.options.confdir = str(tmpdir)
def test_multiple_instances(self, tctx):
instances = [ instances = [
command_history.CommandHistory(10), command_history.CommandHistory(),
command_history.CommandHistory(10), command_history.CommandHistory(),
command_history.CommandHistory(10) command_history.CommandHistory()
] ]
for i in instances: for i in instances:
i.configure([]) i.configure('command_history')
saved_commands = [cmd for cmd in i.history] saved_commands = i.get_history()
assert saved_commands == [] assert saved_commands == []
instances[0].add_command('cmd1') instances[0].add_command('cmd1')
saved_commands = [cmd for cmd in instances[0].history] saved_commands = instances[0].get_history()
assert saved_commands == ['cmd1'] assert saved_commands == ['cmd1']
# These instances haven't yet added a new command, so they haven't # These instances haven't yet added a new command, so they haven't
# yet reloaded their commands from the command file. # yet reloaded their commands from the command file.
# This is expected, because if the user is filtering a command on # This is expected, because if the user is filtering a command on
# another window, we don't want to interfere with that # another window, we don't want to interfere with that
saved_commands = [cmd for cmd in instances[1].history] saved_commands = instances[1].get_history()
assert saved_commands == [] assert saved_commands == []
saved_commands = [cmd for cmd in instances[2].history] saved_commands = instances[2].get_history()
assert saved_commands == [] assert saved_commands == []
# Since the second instanced added a new command, its list of # Since the second instanced added a new command, its list of
# saved commands has been updated to have the commands from the # saved commands has been updated to have the commands from the
# first instance + its own commands # first instance + its own commands
instances[1].add_command('cmd2') instances[1].add_command('cmd2')
saved_commands = [cmd for cmd in instances[1].history] saved_commands = instances[1].get_history()
assert saved_commands == ['cmd1', 'cmd2'] assert saved_commands == ['cmd2']
saved_commands = [cmd for cmd in instances[0].history] saved_commands = instances[0].get_history()
assert saved_commands == ['cmd1'] assert saved_commands == ['cmd1']
# Third instance is still empty as it has not yet ran any command # Third instance is still empty as it has not yet ran any command
saved_commands = [cmd for cmd in instances[2].history] saved_commands = instances[2].get_history()
assert saved_commands == [] assert saved_commands == []
instances[2].add_command('cmd3') instances[2].add_command('cmd3')
saved_commands = [cmd for cmd in instances[2].history] saved_commands = instances[2].get_history()
assert saved_commands == ['cmd1', 'cmd2', 'cmd3'] assert saved_commands == ['cmd3']
instances[0].add_command('cmd4') instances[0].add_command('cmd4')
saved_commands = [cmd for cmd in instances[0].history] saved_commands = instances[0].get_history()
assert saved_commands == ['cmd1', 'cmd2', 'cmd3', 'cmd4'] assert saved_commands == ['cmd1', 'cmd4']
instances.append(command_history.CommandHistory(10)) instances.append(command_history.CommandHistory())
instances[3].configure([]) instances[3].configure('command_history')
saved_commands = [cmd for cmd in instances[3].history] saved_commands = instances[3].get_history()
assert saved_commands == ['cmd1', 'cmd2', 'cmd3', 'cmd4'] assert saved_commands == ['cmd1', 'cmd2', 'cmd3', 'cmd4']
instances[0].add_command('cmd_before_close') instances[0].add_command('cmd_before_close')
instances.pop(0) instances.pop(0).done()
saved_commands = [cmd for cmd in instances[0].history] saved_commands = instances[0].get_history()
assert saved_commands == ['cmd1', 'cmd2'] assert saved_commands == ['cmd2']
instances[0].add_command('new_cmd') instances[0].add_command('new_cmd')
saved_commands = [cmd for cmd in instances[0].history] saved_commands = instances[0].get_history()
assert saved_commands == ['cmd1', 'cmd2', 'cmd3', 'cmd4', 'cmd_before_close', 'new_cmd'] assert saved_commands == ['cmd2', 'new_cmd']
instances.pop(0) instances.pop(0).done()
instances.pop(0) instances.pop(0).done()
instances.pop(0) instances.pop(0).done()
_path = os.path.join(tctx.options.confdir, 'command_history') _path = os.path.join(tctx.options.confdir, 'command_history')
lines = open(_path, 'r').readlines() lines = open(_path, 'r').readlines()
@ -227,14 +237,14 @@ class TestCommandHistory:
assert saved_commands == ['cmd1', 'cmd2', 'cmd3', 'cmd4', 'cmd_before_close', 'new_cmd'] assert saved_commands == ['cmd1', 'cmd2', 'cmd3', 'cmd4', 'cmd_before_close', 'new_cmd']
instances = [ instances = [
command_history.CommandHistory(10), command_history.CommandHistory(),
command_history.CommandHistory(10) command_history.CommandHistory()
] ]
for i in instances: for i in instances:
i.configure([]) i.configure('command_history')
i.clear_history() i.clear_history()
saved_commands = [cmd for cmd in i.history] saved_commands = i.get_history()
assert saved_commands == [] assert saved_commands == []
instances[0].add_command('cmd1') instances[0].add_command('cmd1')
@ -243,11 +253,11 @@ class TestCommandHistory:
instances[1].add_command('cmd4') instances[1].add_command('cmd4')
instances[1].add_command('cmd5') instances[1].add_command('cmd5')
saved_commands = [cmd for cmd in instances[1].history] saved_commands = instances[1].get_history()
assert saved_commands == ['cmd1', 'cmd2', 'cmd3', 'cmd4', 'cmd5'] assert saved_commands == ['cmd3', 'cmd4', 'cmd5']
instances.pop() instances.pop().done()
instances.pop() instances.pop().done()
_path = os.path.join(tctx.options.confdir, 'command_history') _path = os.path.join(tctx.options.confdir, 'command_history')
lines = open(_path, 'r').readlines() lines = open(_path, 'r').readlines()