command history: start adjusting tests

This commit is contained in:
Maximilian Hils 2019-11-26 02:43:09 +01:00
parent 06ef7350f5
commit 819d5e6317
2 changed files with 32 additions and 83 deletions

View File

@ -1,81 +1,30 @@
import os import os
import pytest
from mitmproxy import options
from mitmproxy.addons import command_history from mitmproxy.addons import command_history
from mitmproxy.test import taddons from mitmproxy.test import taddons
@pytest.fixture(autouse=True)
def tctx(tmpdir):
# This runs before each test
dir_name = tmpdir.mkdir('mitmproxy').dirname
confdir = dir_name
opts = options.Options()
opts.set(*[f"confdir={confdir}"])
tctx = taddons.context(options=opts)
ch = command_history.CommandHistory()
tctx.master.addons.add(ch)
ch.configure([])
yield tctx
# This runs after each test
ch.cleanup()
class TestCommandHistory: class TestCommandHistory:
def test_existing_command_history(self, tctx): def test_load_from_file(self, tmpdir):
commands = ['cmd1', 'cmd2', 'cmd3'] commands = ['cmd1', 'cmd2', 'cmd3']
confdir = tctx.options.confdir with open(tmpdir.join('command_history'), 'w') as f:
f = open(os.path.join(confdir, 'command_history'), 'w') f.write("\n".join(commands))
f.write("\n".join(commands))
f.close()
ch = command_history.CommandHistory()
with taddons.context(ch) as tctx:
tctx.options.confdir = str(tmpdir)
assert ch.history == commands
def test_add_command(self):
history = command_history.CommandHistory() history = command_history.CommandHistory()
history.configure([])
saved_commands = [cmd for cmd in history.saved_commands]
assert saved_commands == ['cmd1', 'cmd2', 'cmd3']
history.cleanup()
def test_add_command(self, tctx):
history = command_history.CommandHistory(3)
history.configure([])
history.add_command('cmd1') history.add_command('cmd1')
history.add_command('cmd2') history.add_command('cmd2')
saved_commands = [cmd for cmd in history.saved_commands] assert history.history == ['cmd1', 'cmd2']
assert saved_commands == ['cmd1', 'cmd2']
history.add_command('') history.add_command('')
saved_commands = [cmd for cmd in history.saved_commands] assert history.history == ['cmd1', 'cmd2']
assert saved_commands == ['cmd1', 'cmd2']
# The history size is only 3. So, we forget the first
# one command, when adding fourth command
history.add_command('cmd3')
history.add_command('cmd4')
saved_commands = [cmd for cmd in history.saved_commands]
assert saved_commands == ['cmd2', 'cmd3', 'cmd4']
history.add_command('')
saved_commands = [cmd for cmd in history.saved_commands]
assert saved_commands == ['cmd2', 'cmd3', 'cmd4']
# Commands with the same text are not repeated in the history one by one
history.add_command('cmd3')
saved_commands = [cmd for cmd in history.saved_commands]
assert saved_commands == ['cmd2', 'cmd4', 'cmd3']
history.add_command('cmd2')
saved_commands = [cmd for cmd in history.saved_commands]
assert saved_commands == ['cmd4', 'cmd3', 'cmd2']
history.cleanup()
def test_get_next_and_prev(self, tctx): def test_get_next_and_prev(self, tctx):
history = command_history.CommandHistory(5) history = command_history.CommandHistory(5)
@ -161,7 +110,7 @@ class TestCommandHistory:
history.add_command('cmd2') history.add_command('cmd2')
history.clear_history() history.clear_history()
saved_commands = [cmd for cmd in history.saved_commands] saved_commands = [cmd for cmd in history.history]
assert saved_commands == [] assert saved_commands == []
assert history.get_next() == '' assert history.get_next() == ''
@ -215,57 +164,57 @@ class TestCommandHistory:
for i in instances: for i in instances:
i.configure([]) i.configure([])
saved_commands = [cmd for cmd in i.saved_commands] saved_commands = [cmd for cmd in i.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].saved_commands] saved_commands = [cmd for cmd in instances[0].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].saved_commands] saved_commands = [cmd for cmd in instances[1].history]
assert saved_commands == [] assert saved_commands == []
saved_commands = [cmd for cmd in instances[2].saved_commands] saved_commands = [cmd for cmd in instances[2].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].saved_commands] saved_commands = [cmd for cmd in instances[1].history]
assert saved_commands == ['cmd1', 'cmd2'] assert saved_commands == ['cmd1', 'cmd2']
saved_commands = [cmd for cmd in instances[0].saved_commands] saved_commands = [cmd for cmd in instances[0].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].saved_commands] saved_commands = [cmd for cmd in instances[2].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].saved_commands] saved_commands = [cmd for cmd in instances[2].history]
assert saved_commands == ['cmd1', 'cmd2', 'cmd3'] assert saved_commands == ['cmd1', 'cmd2', 'cmd3']
instances[0].add_command('cmd4') instances[0].add_command('cmd4')
saved_commands = [cmd for cmd in instances[0].saved_commands] saved_commands = [cmd for cmd in instances[0].history]
assert saved_commands == ['cmd1', 'cmd2', 'cmd3', 'cmd4'] assert saved_commands == ['cmd1', 'cmd2', 'cmd3', 'cmd4']
instances.append(command_history.CommandHistory(10)) instances.append(command_history.CommandHistory(10))
instances[3].configure([]) instances[3].configure([])
saved_commands = [cmd for cmd in instances[3].saved_commands] saved_commands = [cmd for cmd in instances[3].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)
saved_commands = [cmd for cmd in instances[0].saved_commands] saved_commands = [cmd for cmd in instances[0].history]
assert saved_commands == ['cmd1', 'cmd2'] assert saved_commands == ['cmd1', 'cmd2']
instances[0].add_command('new_cmd') instances[0].add_command('new_cmd')
saved_commands = [cmd for cmd in instances[0].saved_commands] saved_commands = [cmd for cmd in instances[0].history]
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.pop(0) instances.pop(0)
@ -285,7 +234,7 @@ class TestCommandHistory:
for i in instances: for i in instances:
i.configure([]) i.configure([])
i.clear_history() i.clear_history()
saved_commands = [cmd for cmd in i.saved_commands] saved_commands = [cmd for cmd in i.history]
assert saved_commands == [] assert saved_commands == []
instances[0].add_command('cmd1') instances[0].add_command('cmd1')
@ -294,7 +243,7 @@ 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].saved_commands] saved_commands = [cmd for cmd in instances[1].history]
assert saved_commands == ['cmd1', 'cmd2', 'cmd3', 'cmd4', 'cmd5'] assert saved_commands == ['cmd1', 'cmd2', 'cmd3', 'cmd4', 'cmd5']
instances.pop() instances.pop()

View File

@ -114,8 +114,8 @@ class TestCommandEdit:
def test_up_and_down(self, tctx): def test_up_and_down(self, tctx):
edit = commander.CommandEdit(tctx.master, '') edit = commander.CommandEdit(tctx.master, '')
tctx.master.commands.execute('command_history.clear') tctx.master.commands.execute('commands.history.clear')
tctx.master.commands.execute('command_history.add "cmd1"') tctx.master.commands.execute('commands.history.add "cmd1"')
edit.keypress(1, 'up') edit.keypress(1, 'up')
assert edit.get_edit_text() == 'cmd1' assert edit.get_edit_text() == 'cmd1'
@ -131,9 +131,9 @@ class TestCommandEdit:
edit = commander.CommandEdit(tctx.master, '') edit = commander.CommandEdit(tctx.master, '')
tctx.master.commands.execute('command_history.clear') tctx.master.commands.execute('commands.history.clear')
tctx.master.commands.execute('command_history.add "cmd1"') tctx.master.commands.execute('commands.history.add "cmd1"')
tctx.master.commands.execute('command_history.add "cmd2"') tctx.master.commands.execute('commands.history.add "cmd2"')
edit.keypress(1, 'up') edit.keypress(1, 'up')
assert edit.get_edit_text() == 'cmd2' assert edit.get_edit_text() == 'cmd2'
@ -168,7 +168,7 @@ class TestCommandEdit:
assert edit.get_edit_text() == 'abc' assert edit.get_edit_text() == 'abc'
edit = commander.CommandEdit(tctx.master, '') edit = commander.CommandEdit(tctx.master, '')
tctx.master.commands.execute('command_history.add "cmd3"') tctx.master.commands.execute('commands.history.add "cmd3"')
edit.keypress(1, 'z') edit.keypress(1, 'z')
edit.keypress(1, 'up') edit.keypress(1, 'up')