def setUp(self, mock_print): self.saws = Saws(refresh_resources=False) self.completer = self.create_completer() self.completer.resources._set_resources_path( 'data/RESOURCES_SAMPLE.txt') self.completer.refresh_resources_and_options() self.completer_event = self.create_completer_event() mock_print.assert_called_with('Loaded resources from cache')
class ToolbarTest(unittest.TestCase): @mock.patch('saws.resources.print') def setUp(self, mock_print): self.saws = Saws() self.toolbar = Toolbar(self.saws.get_color, self.saws.get_fuzzy_match, self.saws.get_shortcut_match) mock_print.assert_called_with('Loaded resources from cache') def test_toolbar_on(self): expected = [(Token.Toolbar.On, ' [F2] Color: ON '), (Token.Toolbar.On, ' [F3] Fuzzy: ON '), (Token.Toolbar.On, ' [F4] Shortcuts: ON '), (Token.Toolbar, ' [F5] Refresh '), (Token.Toolbar, ' [F9] Docs '), (Token.Toolbar, ' [F10] Exit ')] assert expected == self.toolbar.handler(None) def test_toolbar_off(self): self.saws.set_color(False) self.saws.set_fuzzy_match(False) self.saws.set_shortcut_match(False) expected = [(Token.Toolbar.Off, ' [F2] Color: OFF '), (Token.Toolbar.Off, ' [F3] Fuzzy: OFF '), (Token.Toolbar.Off, ' [F4] Shortcuts: OFF '), (Token.Toolbar, ' [F5] Refresh '), (Token.Toolbar, ' [F9] Docs '), (Token.Toolbar, ' [F10] Exit ')] assert expected == self.toolbar.handler(None)
class ToolbarTest(unittest.TestCase): @mock.patch('saws.resources.print') def setUp(self, mock_print): self.saws = Saws() self.toolbar = Toolbar(self.saws.get_color, self.saws.get_fuzzy_match, self.saws.get_shortcut_match) mock_print.assert_called_with('Loaded resources from cache') def test_toolbar_on(self): expected = [ (Token.Toolbar.On, ' [F2] Color: ON '), (Token.Toolbar.On, ' [F3] Fuzzy: ON '), (Token.Toolbar.On, ' [F4] Shortcuts: ON '), (Token.Toolbar, ' [F5] Refresh '), (Token.Toolbar, ' [F9] Docs '), (Token.Toolbar, ' [F10] Exit ')] assert expected == self.toolbar.handler(None) def test_toolbar_off(self): self.saws.set_color(False) self.saws.set_fuzzy_match(False) self.saws.set_shortcut_match(False) expected = [ (Token.Toolbar.Off, ' [F2] Color: OFF '), (Token.Toolbar.Off, ' [F3] Fuzzy: OFF '), (Token.Toolbar.Off, ' [F4] Shortcuts: OFF '), (Token.Toolbar, ' [F5] Refresh '), (Token.Toolbar, ' [F9] Docs '), (Token.Toolbar, ' [F10] Exit ')] assert expected == self.toolbar.handler(None)
class ToolbarTest(unittest.TestCase): def setUp(self): self.saws = Saws(refresh_resources=False) self.toolbar = Toolbar(self.saws.get_color, self.saws.get_fuzzy_match, self.saws.get_shortcut_match) def test_toolbar_on(self): expected = [ (Token.Toolbar.On, ' [F2] Color: ON '), (Token.Toolbar.On, ' [F3] Fuzzy: ON '), (Token.Toolbar.On, ' [F4] Shortcuts: ON '), (Token.Toolbar, ' [F5] Refresh '), (Token.Toolbar, ' [F9] Docs '), (Token.Toolbar, ' [F10] Exit ')] assert expected == self.toolbar.handler(None) def test_toolbar_off(self): self.saws.set_color(False) self.saws.set_fuzzy_match(False) self.saws.set_shortcut_match(False) expected = [ (Token.Toolbar.Off, ' [F2] Color: OFF '), (Token.Toolbar.Off, ' [F3] Fuzzy: OFF '), (Token.Toolbar.Off, ' [F4] Shortcuts: OFF '), (Token.Toolbar, ' [F5] Refresh '), (Token.Toolbar, ' [F9] Docs '), (Token.Toolbar, ' [F10] Exit ')] assert expected == self.toolbar.handler(None)
class KeysTest(unittest.TestCase): def setUp(self): self.saws = Saws(refresh_resources=False) self.registry = self.saws.key_manager.manager.registry self.processor = self.saws.aws_cli.input_processor self.DOCS_HOME_URL = 'http://docs.aws.amazon.com/cli/latest/reference/index.html' def test_F2(self): orig_color = self.saws.get_color() self.processor.feed_key(KeyPress(Keys.F2, '')) assert orig_color != self.saws.get_color() def test_F3(self): orig_fuzzy = self.saws.get_fuzzy_match() self.processor.feed_key(KeyPress(Keys.F3, '')) assert orig_fuzzy != self.saws.get_fuzzy_match() def test_F4(self): orig_shortcut = self.saws.get_shortcut_match() self.processor.feed_key(KeyPress(Keys.F4, '')) assert orig_shortcut != self.saws.get_shortcut_match() @mock.patch('saws.saws.webbrowser') def test_F9(self, mock_webbrowser): self.processor.feed_key(KeyPress(Keys.F9, '')) mock_webbrowser.open.assert_called_with(self.DOCS_HOME_URL) def test_F10(self): with self.assertRaises(EOFError): self.processor.feed_key(KeyPress(Keys.F10, '')) '''@mock.patch('saws.resources.print')
class KeysTest(unittest.TestCase): def setUp(self): self.saws = Saws(refresh_resources=False) self.registry = self.saws.key_manager.manager.registry self.processor = self.saws.aws_cli.input_processor self.DOCS_HOME_URL = \ 'http://docs.aws.amazon.com/cli/latest/reference/index.html' def feed_key(self, key): self.processor.feed(KeyPress(key, u'')) self.processor.process_keys() def test_F2(self): orig_color = self.saws.get_color() self.feed_key(Keys.F2) assert orig_color != self.saws.get_color() def test_F3(self): orig_fuzzy = self.saws.get_fuzzy_match() self.feed_key(Keys.F3) assert orig_fuzzy != self.saws.get_fuzzy_match() def test_F4(self): orig_shortcut = self.saws.get_shortcut_match() self.feed_key(Keys.F4) assert orig_shortcut != self.saws.get_shortcut_match() @mock.patch('saws.saws.webbrowser') def test_F9(self, mock_webbrowser): self.feed_key(Keys.F9) mock_webbrowser.open.assert_called_with(self.DOCS_HOME_URL) def test_F10(self): with self.assertRaises(EOFError): self.feed_key(Keys.F10) @mock.patch('saws.resources.print') def test_f5(self, mock_print): self.feed_key(Keys.F5) mock_print.assert_called_with('Done refreshing')
def setUp(self): self.file_name = os.path.expanduser('~') + '/' + '.saws.log' self.saws = Saws(refresh_resources=False) self.DOCS_HOME_URL = 'http://docs.aws.amazon.com/cli/latest/reference/index.html'
def setUp(self): self.saws = Saws(refresh_resources=False) self.registry = self.saws.key_manager.manager.registry self.processor = self.saws.aws_cli.input_processor self.DOCS_HOME_URL = \ 'http://docs.aws.amazon.com/cli/latest/reference/index.html'
def setUp(self): self.file_name = os.path.expanduser('~') + '/' + '.saws.log' # creating object with default args self.saws = Saws(refresh_resources=True) self.DOCS_HOME_URL = 'http://docs.aws.amazon.com/cli/latest/reference/index.html'
def setUp(self, mock_print): self.saws = Saws() self.completer = self.create_completer() self.completer_event = self.create_completer_event() mock_print.assert_called_with('Loaded resources from cache')
class SawsTest(unittest.TestCase): @mock.patch('saws.resources.print') def setUp(self, mock_print): self.file_name = os.path.expanduser('~') + '/' + '.saws.log' self.saws = Saws() mock_print.assert_called_with('Loaded resources from cache') self.DOCS_HOME_URL = 'http://docs.aws.amazon.com/cli/latest/reference/index.html' @mock.patch('saws.saws.click') def test_log_exception(self, mock_click): exception_message = 'test_log_exception' e = Exception(exception_message) try: raise e except Exception: # Traceback needs to have an active exception as described in: # http://bugs.python.org/issue23003 self.saws.log_exception(e, traceback, echo=True) mock_click.secho.assert_called_with(str(e), fg='red') assert os.path.isfile(self.file_name) with open(self.file_name, 'r') as fp: for line in fp: pass assert exception_message in line def test_set_get_color(self): self.saws.set_color(True) assert self.saws.get_color() self.saws.set_color(False) assert not self.saws.get_color() def test_get_set_fuzzy_match(self): self.saws.set_fuzzy_match(True) assert self.saws.get_fuzzy_match() self.saws.set_fuzzy_match(False) assert not self.saws.get_fuzzy_match() def test_get_set_shortcut_match(self): self.saws.set_shortcut_match(True) assert self.saws.get_shortcut_match() self.saws.set_shortcut_match(False) assert not self.saws.get_shortcut_match() @mock.patch('saws.saws.webbrowser') def test_handle_docs(self, mock_webbrowser): EC2_URL = 'http://docs.aws.amazon.com/cli/latest/reference/ec2/index.html' EC2_DESC_INSTANCES_URL = 'http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html' assert not self.saws.handle_docs('') assert not self.saws.handle_docs('foo bar') assert self.saws.handle_docs('', from_fkey=True) mock_webbrowser.open.assert_called_with(self.DOCS_HOME_URL) assert self.saws.handle_docs('baz', from_fkey=True) mock_webbrowser.open.assert_called_with(self.DOCS_HOME_URL) assert self.saws.handle_docs('aws ec2', from_fkey=True) mock_webbrowser.open.assert_called_with(EC2_URL) assert self.saws.handle_docs('aws ec2 docs', from_fkey=False) mock_webbrowser.open.assert_called_with(EC2_URL) assert self.saws.handle_docs('aws ec2 describe-instances', from_fkey=True) mock_webbrowser.open.assert_called_with(EC2_DESC_INSTANCES_URL) assert self.saws.handle_docs('aws ec2 describe-instances docs', from_fkey=False) mock_webbrowser.open.assert_called_with(EC2_DESC_INSTANCES_URL) @mock.patch('saws.saws.os') def test_handle_cd(self, mock_os): assert not self.saws.handle_cd('aws') assert self.saws.handle_cd('cd ') assert self.saws.handle_cd('cd foo') mock_os.chdir.assert_called_with('foo') def test_colorize_output(self): self.saws.set_color(False) assert self.saws.colorize_output(AwsCommands.AWS_COMMAND) == \ AwsCommands.AWS_COMMAND self.saws.set_color(True) assert self.saws.colorize_output(AwsCommands.AWS_CONFIGURE) == \ AwsCommands.AWS_CONFIGURE assert self.saws.colorize_output(AwsCommands.AWS_HELP) == \ AwsCommands.AWS_HELP EC2_LS_CMD = 'aws ec2 ls' assert self.saws.colorize_output(EC2_LS_CMD) == \ EC2_LS_CMD + self.saws.PYGMENTS_CMD @mock.patch('saws.saws.subprocess') @mock.patch('saws.saws.webbrowser') def test_process_command_docs(self, mock_webbrowser, mock_subprocess): self.saws.process_command('aws docs') mock_webbrowser.open.assert_called_with(self.DOCS_HOME_URL) mock_subprocess.call.assert_not_called() @mock.patch('saws.saws.subprocess') def test_process_command_cd(self, mock_subprocess): self.saws.process_command('cd .') mock_subprocess.call.assert_not_called() @mock.patch('saws.saws.subprocess') def test_process_command(self, mock_subprocess): self.saws.set_color(False) INVAL_CMD = 'foo' self.saws.process_command(INVAL_CMD) mock_subprocess.call.assert_called_with(INVAL_CMD, shell=True) self.saws.process_command(AwsCommands.AWS_COMMAND) mock_subprocess.call.assert_called_with(AwsCommands.AWS_COMMAND, shell=True) self.saws.set_color(True) colorized_command = AwsCommands.AWS_COMMAND + self.saws.PYGMENTS_CMD self.saws.process_command(AwsCommands.AWS_COMMAND) mock_subprocess.call.assert_called_with(colorized_command, shell=True)
def create_options(self): self.saws = Saws(refresh_resources=False) self.options = self.saws.completer.options
def setUp(self, mock_print): self.saws = Saws() self.toolbar = Toolbar(self.saws.get_color, self.saws.get_fuzzy_match, self.saws.get_shortcut_match) mock_print.assert_called_with('Loaded resources from cache')
def create_resources(self, mock_print): self.saws = Saws() self.resources = self.saws.completer.resources self.resources.RESOURCE_FILE = self.RESOURCES_SAMPLE mock_print.assert_called_with('Loaded resources from cache')
def create_resources(self): self.saws = Saws(refresh_resources=False) self.resources = self.saws.completer.resources self.resources._set_resources_path('data/RESOURCES_SAMPLE.txt')
def setUp(self): self.saws = Saws(refresh_resources=False) self.toolbar = Toolbar(self.saws.get_color, self.saws.get_fuzzy_match, self.saws.get_shortcut_match)
class ToolbarTest(unittest.TestCase): def setUp(self): self.saws = Saws(refresh_resources=False) self.toolbar = Toolbar(self.saws.get_color, self.saws.get_fuzzy_match, self.saws.get_shortcut_match) def test_toolbar_on(self): self.saws.set_color(True) self.saws.set_fuzzy_match(True) self.saws.set_shortcut_match(True) expected = [ (Token.Toolbar.On, ' [F2] Color: ON '), (Token.Toolbar.On, ' [F3] Fuzzy: ON '), (Token.Toolbar.On, ' [F4] Shortcuts: ON '), (Token.Toolbar, ' [F5] Refresh '), (Token.Toolbar, ' [F9] Docs '), (Token.Toolbar, ' [F10] Exit ')] assert expected == self.toolbar.handler(None) def test_toolbar_off(self): self.saws.set_color(False) self.saws.set_fuzzy_match(False) self.saws.set_shortcut_match(False) expected = [ (Token.Toolbar.Off, ' [F2] Color: OFF '), (Token.Toolbar.Off, ' [F3] Fuzzy: OFF '), (Token.Toolbar.Off, ' [F4] Shortcuts: OFF '), (Token.Toolbar, ' [F5] Refresh '), (Token.Toolbar, ' [F9] Docs '), (Token.Toolbar, ' [F10] Exit ')] assert expected == self.toolbar.handler(None)
class SawsTest(unittest.TestCase): def setUp(self): self.file_name = os.path.expanduser('~') + '/' + '.saws.log' self.saws = Saws(refresh_resources=False) self.DOCS_HOME_URL = \ 'http://docs.aws.amazon.com/cli/latest/reference/index.html' @mock.patch('saws.saws.click') def test_log_exception(self, mock_click): exception_message = 'test_log_exception' e = Exception(exception_message) try: raise e except Exception: # Traceback needs to have an active exception as described in: # http://bugs.python.org/issue23003 self.saws.log_exception(e, traceback, echo=True) mock_click.secho.assert_called_with(str(e), fg='red') assert os.path.isfile(self.file_name) with open(self.file_name, 'r') as fp: for line in fp: pass assert exception_message in line def test_set_get_color(self): self.saws.set_color(True) assert self.saws.get_color() self.saws.set_color(False) assert not self.saws.get_color() def test_get_set_fuzzy_match(self): self.saws.set_fuzzy_match(True) assert self.saws.get_fuzzy_match() self.saws.set_fuzzy_match(False) assert not self.saws.get_fuzzy_match() def test_get_set_shortcut_match(self): self.saws.set_shortcut_match(True) assert self.saws.get_shortcut_match() self.saws.set_shortcut_match(False) assert not self.saws.get_shortcut_match() @mock.patch('saws.saws.webbrowser') def test_handle_docs(self, mock_webbrowser): EC2_URL = \ 'http://docs.aws.amazon.com/cli/latest/reference/ec2/index.html' EC2_DESC_INSTANCES_URL = \ 'http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html' # NOQA assert not self.saws.handle_docs('') assert not self.saws.handle_docs('foo bar') assert self.saws.handle_docs('', from_fkey=True) mock_webbrowser.open.assert_called_with(self.DOCS_HOME_URL) assert self.saws.handle_docs('baz', from_fkey=True) mock_webbrowser.open.assert_called_with(self.DOCS_HOME_URL) assert self.saws.handle_docs('aws ec2', from_fkey=True) mock_webbrowser.open.assert_called_with(EC2_URL) assert self.saws.handle_docs('aws ec2 docs', from_fkey=False) mock_webbrowser.open.assert_called_with(EC2_URL) assert self.saws.handle_docs('aws ec2 describe-instances', from_fkey=True) mock_webbrowser.open.assert_called_with(EC2_DESC_INSTANCES_URL) assert self.saws.handle_docs('aws ec2 describe-instances docs', from_fkey=False) mock_webbrowser.open.assert_called_with(EC2_DESC_INSTANCES_URL) @mock.patch('saws.saws.os') def test_handle_cd(self, mock_os): assert not self.saws._handle_cd('aws') assert self.saws._handle_cd('cd ') assert self.saws._handle_cd('cd foo') mock_os.chdir.assert_called_with('foo') def test_colorize_output(self): self.saws.set_color(False) assert self.saws._colorize_output(AwsCommands.AWS_COMMAND) == \ AwsCommands.AWS_COMMAND self.saws.set_color(True) assert self.saws._colorize_output(AwsCommands.AWS_CONFIGURE) == \ AwsCommands.AWS_CONFIGURE assert self.saws._colorize_output(AwsCommands.AWS_HELP) == \ AwsCommands.AWS_HELP EC2_LS_CMD = 'aws ec2 ls' assert self.saws._colorize_output(EC2_LS_CMD) == \ EC2_LS_CMD + self.saws.PYGMENTS_CMD @mock.patch('saws.saws.subprocess') @mock.patch('saws.saws.webbrowser') def test_process_command_docs(self, mock_webbrowser, mock_subprocess): self.saws._process_command('aws docs') mock_webbrowser.open.assert_called_with(self.DOCS_HOME_URL) mock_subprocess.call.assert_not_called() @mock.patch('saws.saws.subprocess') def test_process_command_cd(self, mock_subprocess): self.saws._process_command('cd .') mock_subprocess.call.assert_not_called() @mock.patch('saws.saws.subprocess') def test_process_command(self, mock_subprocess): self.saws.set_color(False) INVAL_CMD = 'foo' self.saws._process_command(INVAL_CMD) mock_subprocess.call.assert_called_with(INVAL_CMD, shell=True) self.saws._process_command(AwsCommands.AWS_COMMAND) mock_subprocess.call.assert_called_with(AwsCommands.AWS_COMMAND, shell=True) self.saws.set_color(True) colorized_command = AwsCommands.AWS_COMMAND + self.saws.PYGMENTS_CMD self.saws._process_command(AwsCommands.AWS_COMMAND) mock_subprocess.call.assert_called_with(colorized_command, shell=True) def test_handle_keyboard_interrupt(self): e = KeyboardInterrupt('') # TODO: Mock calls to renderer.clear and input_processor.feed self.saws._handle_keyboard_interrupt(e, platform='Darwin') with self.assertRaises(KeyboardInterrupt): self.saws._handle_keyboard_interrupt(e, platform='Windows')
def setUp(self): self.file_name = os.path.expanduser('~') + '/' + '.saws.log' self.saws = Saws(refresh_resources=False) self.DOCS_HOME_URL = \ 'http://docs.aws.amazon.com/cli/latest/reference/index.html'
def setUp(self, mock_print): self.saws = Saws() mock_print.assert_called_with('Loaded resources from cache') self.registry = self.saws.key_manager.manager.registry self.processor = self.saws.aws_cli.input_processor self.DOCS_HOME_URL = 'http://docs.aws.amazon.com/cli/latest/reference/index.html'
def setUp(self, mock_print): self.file_name = os.path.expanduser('~') + '/' + '.saws.log' self.saws = Saws() mock_print.assert_called_with('Loaded resources from cache') self.DOCS_HOME_URL = 'http://docs.aws.amazon.com/cli/latest/reference/index.html'