def test_with_command_with_empty_help(self, executable_factory, readout): """ Handle the case where help returns empty string """ executable_factory('nd-my-command', make_help_command('')) with readout() as message: help.main(['nd']) assert 'my-command <help not found>' in message
def test_with_namespace(self, executable_factory, readout): """ Running help on a namespace shows help for each command in the namespace """ executable_factory('nd-my-namespace~my-command', make_help_command('my help message')) with readout() as message: help.main(['nd', 'my-namespace']) assert 'my-namespace my-command my help message' in message
def test_with_failing_command(self, executable_factory, readout): """ Handle the case where a command returns non-zero exit status """ executable_factory('nd-my-command', '#!/bin/bash\nexit 1') with readout() as message: help.main(['nd']) assert 'my-command <help not found>' in message
def test_dot_command_fails_triggers_system_exit(self, executable_factory): """ Handle dot command failing with exit code not zero by exiting system """ executable_factory('nd-.my-check', '#!/bin/bash\nexit 1') with pytest.raises(SystemExit) as exc_info: self.command.run_dot_commands([], '', []) assert "Dot command 'nd-.my-check' failed." in str(exc_info.value)
def test_parses_nonargparse_generated_help(self, executable_factory, readout): """ Print help for all commands parses descriptions from generic commands' --help """ executable_factory('nd-my-command', make_help_command('my help message')) with readout() as message: help.main(['nd']) assert 'my-command my help message' in message
def test_when_command_name_is_a_prefix_of_another_command( self, executable_factory): """ Handle executing nd command in path """ executable_factory('nd-my-command') executable_factory('nd-my-command-two') assert split('my-command') == ([], 'my-command', [])
def test_calls_commands_with_help_flag(self, executable_factory, readout, run_as_child): """ Print help for all commands parses descriptions from generic commands' --help """ executable_factory('nd-my-command', '#!/bin/echo') with readout() as message: run_as_child(help.main, ['nd', 'my-command']) assert '--help' in message
def test_slow_help_is_skipped(self, executable_factory, readout): """ Skip printing help info for a slow help """ executable_factory('nd-my-command', '#!/usr/bin/env bash\nsleep 10\necho my help message') with readout() as message: with mock.patch.object(help, 'HELP_TIMEOUT', 0): help.main(['nd']) assert 'my-command <help not found>' in message
def test_command_help_not_found(self, executable_factory): """ Handle being given a command or namespace for help not in path """ executable_factory('nd-my-namespace~my-command') with pytest.raises(SystemExit) as exc_info: help.main(['nd', 'my-namespace', 'missing', 'help']) assert "Command or namespace 'missing' not found in 'my-namespace'" in str(exc_info.value)
def test_excluded_command(self, executable_factory, monkeypatch): """ Help returns an error when applied to excluded commands. """ monkeypatch.setenv('BUCKLE_HELP_OPTS_ND', '-X nd-my-excluded-command') executable_factory('nd-my-excluded-command', '#!/bin/echo\nFAIL') with pytest.raises(SystemExit) as exc_info: help.main(['nd', 'my-excluded-command']) assert "Command 'nd-my-excluded-command' is excluded from help" in str(exc_info.value)
def test_with_no_args(self, executable_factory, readout): """ Running help without a command or namespace prints help of commands on path """ executable_factory('nd-my-namespace~my-command', make_help_command('my help message')) with readout() as message: help.main(['nd']) assert 'Showing help for all' in message assert 'my-namespace my-command my help message' in message
def test_with_command_cannot_be_run(self, executable_factory, run_as_child): """ Handle the case where a command cannot be run """ executable_factory('nd-my-command', '') with pytest.raises(run_as_child.ChildError) as exc_info: run_as_child(help.main, ['nd', 'my-command']) assert 'SystemExit' in str(exc_info.value) assert "Command 'nd-my-command' could not be run" in str(exc_info.value)
def test_excluded_command(self, executable_factory, monkeypatch, readout): """ Help returns an error when applied to excluded commands. """ monkeypatch.setenv('BUCKLE_HELP_OPTS_ND', '-X nd-my-excluded-command') executable_factory('nd-my-included-command') executable_factory('nd-my-excluded-command', '#!/bin/echo\nFAIL') with readout() as message: help.main(['nd']) assert 'my-included-command' in message assert 'my-excluded-command' not in message
def test_parses_argparse_generated_help(self, executable_factory, readout): """ Print help for all commands parses argparse's description from its generated help """ executable_factory('nd-my-command', make_help_command("""\ usage: ... my help message"""), dedent=False) with readout() as message: help.main(['nd']) assert 'my-command my help message' in message
def test_namespace_and_command_and_arguments_get_split( self, executable_factory): """ Handle splitting a command on the path with namespaces and arguments""" executable_factory('nd-my-namespace~my-subnamespace~my-command', '') assert (['my-namespace', 'my-subnamespace'], 'my-command', []) == \ split('my-namespace', 'my-subnamespace', 'my-command') assert (['my-namespace', 'my-subnamespace'], 'my-command', ['arg1', 'arg2']) == \ split('my-namespace', 'my-subnamespace', 'my-command', 'arg1', 'arg2')
def test_child_dot_commands_do_not_run(self, executable_factory, readout): """ Dot commands in child namespaces do not get called """ executable_factory('nd-.my-check', '#!/bin/bash\necho parent dot command output') executable_factory('nd-my-namespace~.my-check', '#!/bin/bash\necho child dot command output') with readout() as output: self.command.run_dot_commands([], '', []) assert output == 'parent dot command output\n'
def test_missing_commands(self, executable_factory): """ Handle being given a missing command with or without namespaces """ executable_factory('nd-my-namespace~my-command') with pytest.raises(SystemExit) as exc_info: self.split('nd', 'missing') assert "Command 'missing' not found" in str(exc_info.value) with pytest.raises(SystemExit) as exc_info: self.split('nd', 'my-namespace', 'missing') assert "Command 'missing' not found in 'my-namespace" in str( exc_info.value)
def test_commands(self, executable_factory): """ Handle being given a command with or without namespaces """ executable_factory('nd-my-command') executable_factory('nd-my-namespace~my-command') assert ('nd', [], 'my-command', []) == self.split('nd', 'my-command') assert ('nd', ['my-namespace'], 'my-command', []) == self.split('nd', 'my-namespace', 'my-command') assert ('nd', ['my-namespace'], 'my-command', ['arg']) == self.split('nd', 'my-namespace', 'my-command', 'arg')
def test_runs_with_passed_command_and_args(self, executable_factory, readout): """ Dot Commands are called with the called command and its args """ executable_factory( 'nd-my-namespace~.my-check', """\ #!/bin/bash echo $1 echo $2 echo $3""") with readout() as output: self.command.run_dot_commands(['my-namespace'], 'my-command', ['arg1', 'arg2']) assert output == 'my-namespace my-command\narg1\narg2\n'
def test_dot_commands_disabled_option(self, disable_clock_check, disable_update, executable_factory, readout, run_as_child): """ Ignore dot commands before the given command if option is set in toolbelt """ executable_factory('nd-.my-check', '#!/bin/bash\necho my dot command output') executable_factory('nd-my-command', '#!/bin/bash\necho my command output') with readout() as output: run_as_child(base.main, ['buckle', '--skip-dot-commands', 'my-command']) assert output == 'my command output\n'
def test_help(self, executable_factory): """ Handle being given a command or namespaces for help """ executable_factory('buckle-help') executable_factory('nd-my-command') executable_factory('nd-my-namespace~my-command') executable_factory('nd-my-other-namespace~help') assert ('buckle', [], 'help', []) == self.split('nd') assert ('buckle', [], 'help', ['my-command']) == self.split('nd', 'help', 'my-command') assert ('buckle', [], 'help', ['missing']) == self.split('nd', 'help', 'missing') assert ('buckle', [], 'help', ['my-namespace']) == self.split('nd', 'help', 'my-namespace') assert ('buckle', [], 'help', ['my-namespace', 'missing']) == self.split('nd', 'help', 'my-namespace', 'missing') assert ('buckle', [], 'help', ['my-namespace', 'missing']) == self.split('nd', 'my-namespace', 'help', 'missing') assert ('nd', ['my-other-namespace'], 'help', []) == self.split('nd', 'my-other-namespace', 'help') assert ('nd', ['my-other-namespace'], 'help', ['arg']) == self.split('nd', 'my-other-namespace', 'help', 'arg') with pytest.raises(SystemExit) as exc_info: self.split('nd', 'my-namespace', 'missing', 'help') assert "Command or namespace 'missing' not found in 'my-namespace" in str( exc_info.value)
def test_without_columns_envvar(self, executable_factory, monkeypatch, readout): """ Running help on a namespace shows help for each command in the namespace """ monkeypatch.delenv('COLUMNS') with mock.patch.object(os, 'popen') as popen: popen.return_value.read.return_value = '30 40' executable_factory('nd-my-command', make_help_command('my really really really long help message')) with readout() as message: help.main(['nd']) assert '...' in message popen.assert_called_with('stty size', 'r')
def test_output_is_sorted_alphabetically_by_namespace(self, executable_factory, readout): """ Help returns an error when applied to excluded commands. """ executable_factory('nd-my-a-command') executable_factory('nd-my-z-command') executable_factory('nd-my-a-namespace~my-command') executable_factory('nd-my-namespace~my-subnamespace~my-command') executable_factory('nd-my-z-namespace~my-command') with readout() as output: help.main(['nd']) message = str(output) assert (message.index('my-a-command') < message.index('my-z-command') < message.index('my-a-namespace my-command') < message.index('my-namespace my-subnamespace my-command') < message.index('my-z-namespace my-command'))
def test_dot_commands_run_only_once(self, executable_factory, monkeypatch, readout): """ Handle the dot command appearing multiple times on path by running it only once """ tmp_path = executable_factory( 'nd-.my-check', '#!/bin/bash\necho my dot command output') monkeypatch.setenv('PATH', tmp_path, prepend=':') with readout() as output: self.command.run_dot_commands([], '', []) assert output == 'my dot command output\n'
def test_raises_missing_command_or_namespace(self, executable_factory): """ Handle the list of arguments not containing a command that is in the path """ executable_factory('nd-my-namespace~my-command') with pytest.raises(path.CommandNotFound) as e: split('my-command') assert e.value.path == ('my-command', ) with pytest.raises(path.CommandOrNamespaceNotFound) as e: split('missing', 'some-arg') assert e.value.path == ('missing', ) with pytest.raises(path.CommandNotFound) as e: split('my-namespace', 'missing') assert e.value.path == ('my-namespace', 'missing') with pytest.raises(path.CommandOrNamespaceNotFound) as e: split('my-namespace', 'missing', 'some-arg') assert e.value.path == ('my-namespace', 'missing') with pytest.raises(path.CommandOrNamespaceNotFound) as e: split('missing-namespace', 'missing-command') assert e.value.path == ('missing-namespace', )
def test_excludes_completion_commands(self, executable_factory, readout): """ Doesn't report autocompletion scripts """ executable_factory('nd-my-command') executable_factory('nd-my-command.completion') executable_factory('nd-my-command.completion.sh') with readout() as message: help.main(['nd']) assert 'my-command' in message assert 'my-command.completion' not in message
def test_dot_commands_run_alphabetically(self, executable_factory, readout): """ All dot commands for the given namespace should run in alphabetical order """ executable_factory('nd-.my-checkC', '#!/bin/bash\necho dot command C output') executable_factory('nd-.my-checkA', '#!/bin/bash\necho dot command A output') executable_factory('nd-.my-checkB', '#!/bin/bash\necho dot command B output') with readout() as output: self.command.run_dot_commands([], '', []) assert output == 'dot command A output\ndot command B output\ndot command C output\n'
def test_dot_commands_run_in_order_of_namespace(self, executable_factory, readout): """ Dot commands in parent namespaces run before its children """ executable_factory('nd-.my-check', '#!/bin/bash\necho parent dot command output') executable_factory('nd-my-namespace~.my-check', '#!/bin/bash\necho child dot command output') executable_factory('nd-my-namespace~subnamespace~.my-check', '#!/bin/bash\necho grandchild dot command output') with readout() as output: self.command.run_dot_commands(['my-namespace', 'subnamespace'], '', []) assert output == ( 'parent dot command output\nchild dot command output\n' 'grandchild dot command output\n')
def test_returns_sorted_list(self, executable_factory): executable_factory('nd-c') executable_factory('nd-a') executable_factory('nd-b') result = autocomplete.get_executables_starting_with('nd-') assert result == ['nd-a', 'nd-b', 'nd-c']
def test_finds_commands_in_path(self, executable_factory): executable_factory('nd-my-test-command') result = autocomplete.get_executables_starting_with(prefix='nd-my-test-command') assert result == ['nd-my-test-command']