def test_has_get(self): """ Application.has() and Application.get() should determine and get commands """ application = Application() self.assertTrue(application.has('list'), msg='.has() returns true if a command is registered') self.assertFalse( application.has('afoobar'), msg='.has() returns false if a command is not registered') foo = FooCommand() application.add(foo) self.assertTrue(application.has('afoobar'), msg='.has() returns true if an alias is registered') self.assertEqual(foo, application.get('foo:bar'), msg='.get() returns a command by name') self.assertEqual(foo, application.get('afoobar'), msg='.get() returns a command by alias') application = Application() application.add(foo) # Simulate help application._want_helps = True self.assertTrue( isinstance(application.get('foo:bar'), HelpCommand), msg= '.get() returns the help command if --help is provided as the input' )
def test_render_exception(self): """ Application.render_exception() displays formatted exception. """ application = Application() application.set_auto_exit(False) application.get_terminal_width = self.mock().MagicMock(return_value=120) tester = ApplicationTester(application) tester.run([('command', 'foo')], {'decorated': False}) self.assertEqual( self.open_fixture('application_renderexception1.txt'), tester.get_display() ) tester.run([('command', 'foo')], {'decorated': False, 'verbosity': Output.VERBOSITY_VERBOSE}) self.assertRegex( tester.get_display(), 'Exception trace' ) tester.run([('command', 'list'), ('--foo', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_renderexception2.txt'), tester.get_display() ) application.add(Foo3Command()) tester = ApplicationTester(application) tester.run([('command', 'foo3:bar')], {'decorated': False}) self.assertEqual( self.open_fixture('application_renderexception3.txt'), tester.get_display() ) tester = ApplicationTester(application) tester.run([('command', 'foo3:bar')], {'decorated': True}) self.assertEqual( self.open_fixture('application_renderexception3decorated.txt'), tester.get_display() ) application = Application() application.set_auto_exit(False) application.get_terminal_width = self.mock().MagicMock(return_value=31) tester = ApplicationTester(application) tester.run([('command', 'foo')], {'decorated': False}) self.assertEqual( self.open_fixture('application_renderexception4.txt'), tester.get_display() )
def test_merge_application_definition(self): """ Command.merge_application_definition() merges command and application. """ application1 = Application() application1.get_definition().add_arguments([InputArgument('foo')]) application1.get_definition().add_options([InputOption('bar')]) command = SomeCommand() command.set_application(application1) command.set_definition( InputDefinition([ InputArgument('bar'), InputOption('foo') ]) ) command.merge_application_definition() self.assertTrue(command.get_definition().has_argument('foo')) self.assertTrue(command.get_definition().has_option('foo')) self.assertTrue(command.get_definition().has_argument('bar')) self.assertTrue(command.get_definition().has_option('bar')) # It should not merge the definitions twice command.merge_application_definition() self.assertEqual(3, command.get_definition().get_argument_count())
def test_set_run_custom_default_command(self): """ Application calls the default command. """ application = Application() application.set_auto_exit(False) command = FooCommand() application.add(command) application.set_default_command(command.get_name()) tester = ApplicationTester(application) tester.run([]) self.assertEqual( 'interact called\ncalled\n', tester.get_display() ) application = CustomDefaultCommandApplication() application.set_auto_exit(False) tester = ApplicationTester(application) tester.run([]) self.assertEqual( 'interact called\ncalled\n', tester.get_display() )
def test_find_namespace(self): """ Application.find_namespace() should return a namespace """ application = Application() application.add(FooCommand()) self.assertEqual( 'foo', application.find_namespace('foo'), msg='.find_namespace() returns the given namespace if it exists' ) self.assertEqual( 'foo', application.find_namespace('f'), msg='.find_namespace() finds a namespace given an abbreviation' ) application.add(Foo2Command()) self.assertEqual( 'foo', application.find_namespace('foo'), msg='.find_namespace() returns the given namespace if it exists' )
def test_execute_hello_world_command_no_params(): application = Application() application.add(HelloWorldCommand()) command = application.find('greet') command_tester = CommandTester(command) command_tester.execute() assert "Hello" == command_tester.io.fetch_output().rstrip()
def test_find(self): """ Application.find() should return a command """ application = Application() application.add(FooCommand()) self.assertTrue( isinstance(application.find('foo:bar'), FooCommand), msg='.find() returns a command if its name exists' ) self.assertTrue( isinstance(application.find('h'), HelpCommand), msg='.find() returns a command if its name exists' ) self.assertTrue( isinstance(application.find('f:bar'), FooCommand), msg='.find() returns a command if the abbreviation for the namespace exists' ) self.assertTrue( isinstance(application.find('f:b'), FooCommand), msg='.find() returns a command if the abbreviation for the namespace and the command name exist' ) self.assertTrue( isinstance(application.find('a'), FooCommand), msg='.find() returns a command if the abbreviation exists for an alias' )
def test_find_alternative_commands(self): application = Application() application.add(FooCommand()) application.add(Foo1Command()) application.add(Foo2Command()) command_name = 'Unknown command' try: application.find(command_name) self.fail('.find() raises an Exception if command does not exist') except Exception as e: self.assertEqual('Command "%s" is not defined.' % command_name, str(e)) command_name = 'bar1' try: application.find(command_name) self.fail('.find() raises an Exception if command does not exist') except Exception as e: self.assertRegexpMatches( str(e), 'Command "%s" is not defined.' % command_name) self.assertRegexpMatches( str(e), 'afoobar1', ) self.assertRegexpMatches( str(e), 'foo:bar1', ) self.assertNotRegex(str(e), 'foo:bar$')
def test_combined_decorators(self): """ Combining decorators should register a command with arguments and options """ application = Application() @application.command('decorated_foo', description='Foo command') @application.argument('foo', description='Foo argument', required=True, is_list=True) @application.option('bar', 'b', description='Bar option', value_required=True, is_list=True) def decorated_foo_code(i, o): """Foo Description""" o.writeln('called') self.assertTrue(application.has('decorated_foo')) command = application.get('decorated_foo') self.assertEqual(command._code, decorated_foo_code) self.assertEqual(command.get_description(), 'Foo command') self.assertTrue('decorated_foo_code' in command.get_aliases()) argument = command.get_definition().get_argument('foo') self.assertEqual('Foo argument', argument.get_description()) self.assertTrue(argument.is_required()) self.assertTrue(argument.is_list()) option = command.get_definition().get_option('bar') self.assertEqual('b', option.get_shortcut()) self.assertEqual('Bar option', option.get_description()) self.assertTrue(option.is_value_required()) self.assertTrue(option.is_list())
def test_execute_for_command_alias(self): command = HelpCommand() command.set_application(Application()) tester = CommandTester(command) tester.execute([('command_name', 'li')], {'decorated': False}) self.assertIn('list [options] [--] [<namespace>]', tester.get_display())
def test_help(self): """ Application.get_help() returns the help message of the application """ application = Application() self.assertEqual(self.open_fixture('application_gethelp.txt'), application.get_help())
def test_get_long_version(self): """ Application.get_long_version() returns the long version of the application """ application = Application('foo', 'bar') self.assertEqual('foo <info>bar</info>', application.get_long_version())
def test_find_command_with_missing_namespace(self): """ Application.find() returns a command with missing namespace """ application = Application() application.add(Foo4Command()) self.assertTrue(isinstance(application.find('f::t'), Foo4Command))
def test_execute_namespace_command(): app = Application() app.add(FooBarCommand()) tester = CommandTester(app.find("foo bar")) assert 0 == tester.execute() assert 0 == tester.status_code assert "foo bar called\n" == tester.io.fetch_output()
def test_set_application(self): """ Command.set_application() sets the current application """ application = Application() command = SomeCommand() command.set_application(application) self.assertEqual(application, command.get_application(), msg='.set_application() sets the current application')
def test_get_invalid_command(self): """ Application.get() should raise an exception when command does not exist """ application = Application() self.assertRaisesRegexp(Exception, 'The command "foofoo" does not exist', application.get, 'foofoo')
def test_find_namespace_does_not_fail_on_deep_similar_namespaces(self): applicaton = Application() applicaton.get_namespaces = self.mock().MagicMock(return_value=['foo:sublong', 'bar:sub']) self.assertEqual( 'foo:sublong', applicaton.find_namespace('f:sub') )
def test_render_exception(self): """ Application.render_exception() displays formatted exception. """ application = Application() application.set_auto_exit(False) os.environ['COLUMNS'] = '120' tester = ApplicationTester(application) tester.run([('command', 'foo')], {'decorated': False}) self.assertEqual(self.open_fixture('application_renderexception1.txt'), tester.get_display()) tester.run([('command', 'foo')], { 'decorated': False, 'verbosity': Output.VERBOSITY_VERBOSE }) self.assertRegex(tester.get_display(), 'Exception trace') tester.run([('command', 'list'), ('--foo', True)], {'decorated': False}) self.assertEqual(self.open_fixture('application_renderexception2.txt'), tester.get_display()) application.add(Foo3Command()) tester = ApplicationTester(application) tester.run([('command', 'foo3:bar')], {'decorated': False}) self.assertEqual(self.open_fixture('application_renderexception3.txt'), tester.get_display()) tester = ApplicationTester(application) tester.run([('command', 'foo3:bar')], {'decorated': True}) self.assertEqual( self.open_fixture('application_renderexception3decorated.txt'), tester.get_display()) application = Application() application.set_auto_exit(False) os.environ['COLUMNS'] = '31' tester = ApplicationTester(application) tester.run([('command', 'foo')], {'decorated': False}) self.assertEqual(self.open_fixture('application_renderexception4.txt'), tester.get_display())
def test_get_default_helper_set_returns_default_values(self): application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) helper_set = application.get_helper_set() self.assertTrue(helper_set.has('formatter')) self.assertTrue(helper_set.has('question'))
def test_add_with_dictionary(self): """ Application.add() accepts a dictionary as argument. """ application = Application() foo = application.add(foo_commmand) self.assertTrue(isinstance(foo, Command)) self.assertEqual('foo:bar1', foo.get_name())
def test_find_command_with_ambiguous_namespace_but_unique_name(self): """ Application.find() returns a command with ambiguous namespace """ application = Application() application.add(FooCommand()) application.add(FoobarCommand()) self.assertTrue(isinstance(application.find('f:f'), FoobarCommand))
def find_invalid_namespace(self): """ Application.find_namespace() should raise an error when finding missing namespace """ application = Application() self.assertRaisesRegexp( Exception, 'There are no commands defined in the "bar" namespace\.', application.find_namespace, 'bar')
def test_run_return_exit_code_one_for_exception_code_zero(self): exception = OSError(0, '') application = Application() application.set_auto_exit(False) application.do_run = self.mock().MagicMock(side_effect=exception) exit_code = application.run(ListInput([]), NullOutput()) self.assertEqual(1, exit_code)
def test_set_get_version(self): """ Application version accessors behave properly """ application = Application() application.set_version('bar') self.assertEqual('bar', application.get_version(), msg='.set_version() sets the version of the application')
def find_alternative_commands_with_an_alias(self): foo_command = FooCommand() foo_command.set_aliases(['foo2']) application = Application() application.add(foo_command) result = application.find('foo') self.assertEqual(foo_command, result)
def test_set_get_name(self): """ Application name accessors behave properly """ application = Application() application.set_name('foo') self.assertEqual('foo', application.get_name(), msg='.set_name() sets the name of the application')
def test_as_text(self): command = TestCommand() command.set_application(Application()) tester = CommandTester(command) tester.execute([('command', command.get_name())]) self.assertEqual( self.open_fixture('command_astext.txt'), command.as_text() )
def test_register(self): """ Application.register() registers a new command """ application = Application() command = application.register('foo') self.assertEqual('foo', command.get_name(), msg='.register() registers a new command')
def test_get_helper(self): application = Application() command = SomeCommand() command.set_application(application) formatter_helper = FormatterHelper() self.assertEqual( formatter_helper.get_name(), command.get_helper('formatter').get_name() )
def test_terminal_dimensions(self): application = Application() original_dimensions = application.get_terminal_dimensions(StreamOutput(sys.stdout)) self.assertEqual(2, len(original_dimensions)) width = 80 if original_dimensions[0] == width: width = 100 application.set_terminal_dimensions(width, 80) self.assertEqual((width, 80), application.get_terminal_dimensions(StreamOutput(sys.stdout)))