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_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("dialog")) self.assertTrue(helper_set.has("progress"))
def test_adding_single_helper_set_overwrites_default_values(self): application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) application.set_helper_set(HelperSet([FormatterHelper()])) helper_set = application.get_helper_set() self.assertTrue(helper_set.has('formatter')) self.assertFalse(helper_set.has('question'))
def test_adding_single_helper_set_overwrites_default_values(self): application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) application.set_helper_set(HelperSet({"formatter": FormatterHelper()})) helper_set = application.get_helper_set() self.assertTrue(helper_set.has("formatter")) self.assertFalse(helper_set.has("dialog")) self.assertFalse(helper_set.has("progress"))
def test_silent_help(self): """ Silent help should return nothing """ application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) tester = ApplicationTester(application) tester.run([("-h", True), ("-q", True)], {"decorated": False}) self.assertEqual("", tester.get_display())
def test_silent_help(self): """ Silent help should return nothing """ application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) tester = ApplicationTester(application) tester.run([('-h', True), ('-q', True)], {'decorated': False}) self.assertEqual('', tester.get_display())
def test_get_default_input_definition_returns_default_values(self): application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) definition = application.get_definition() self.assertTrue(definition.has_argument('command')) self.assertTrue(definition.has_option('help')) self.assertTrue(definition.has_option('quiet')) self.assertTrue(definition.has_option('verbose')) self.assertTrue(definition.has_option('version')) self.assertTrue(definition.has_option('ansi')) self.assertTrue(definition.has_option('no-ansi')) self.assertTrue(definition.has_option('no-interaction'))
def test_set_catch_exceptions(self): application = Application() application.set_auto_exit(False) application.get_terminal_width = self.mock().MagicMock(return_value=120) tester = ApplicationTester(application) application.set_catch_exceptions(True) tester.run([("command", "foo")], {"decorated": False}) self.assertEqual(self.open_fixture("application_renderexception1.txt"), tester.get_display()) application.set_catch_exceptions(False) try: tester.run([("command", "foo")], {"decorated": False}) self.fail(".set_catch_exceptions() sets the catch exception flag") except Exception as e: self.assertEqual('Command "foo" is not defined.', str(e))
def test_get_default_input_definition_returns_default_values(self): application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) definition = application.get_definition() self.assertTrue(definition.has_argument("command")) self.assertTrue(definition.has_option("help")) self.assertTrue(definition.has_option("quiet")) self.assertTrue(definition.has_option("verbose")) self.assertTrue(definition.has_option("version")) self.assertTrue(definition.has_option("ansi")) self.assertTrue(definition.has_option("no-ansi")) self.assertTrue(definition.has_option("no-interaction"))
def test_set_catch_exceptions(self): application = Application() application.set_auto_exit(False) os.environ['COLUMNS'] = '120' tester = ApplicationTester(application) application.set_catch_exceptions(True) tester.run([('command', 'foo')], {'decorated': False}) self.assertEqual(self.open_fixture('application_renderexception1.txt'), tester.get_display()) application.set_catch_exceptions(False) try: tester.run([('command', 'foo')], {'decorated': False}) self.fail('.set_catch_exceptions() sets the catch exception flag') except Exception as e: self.assertEqual('Command "foo" is not defined.', str(e))
def test_adding_already_set_definition_element_data(self): data = [[InputArgument('command', InputArgument.REQUIRED)], [InputOption('quiet', '', InputOption.VALUE_NONE)], [InputOption('query', 'q', InputOption.VALUE_NONE)]] for d in data: application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) application.register('foo')\ .set_definition(d)\ .set_code(lambda in_, out_: None) input_ = ListInput([('command', 'foo')]) output_ = NullOutput() self.assertRaises(Exception, application.run, input_, output_)
def test_adding_already_set_definition_element_data(self): data = [ [InputArgument("command", InputArgument.REQUIRED)], [InputOption("quiet", "", InputOption.VALUE_NONE)], [InputOption("query", "q", InputOption.VALUE_NONE)], ] for d in data: application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) application.register("foo").set_definition(d).set_code(lambda in_, out_: None) input_ = ListInput([("command", "foo")]) output_ = NullOutput() self.assertRaises(Exception, application.run, input_, output_)
def test_set_catch_exceptions(self): application = Application() application.set_auto_exit(False) application.get_terminal_width = self.mock().MagicMock(return_value=120) tester = ApplicationTester(application) application.set_catch_exceptions(True) tester.run([('command', 'foo')], {'decorated': False}) self.assertEqual( self.open_fixture('application_renderexception1.txt'), tester.get_display() ) application.set_catch_exceptions(False) try: tester.run([('command', 'foo')], {'decorated': False}) self.fail('.set_catch_exceptions() sets the catch exception flag') except Exception as e: self.assertEqual('Command "foo" is not defined.', str(e))
def test_setting_input_definition_overwrites_default_values(self): application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) application.set_definition( InputDefinition([InputOption("--custom", "-c", InputOption.VALUE_NONE, "Set the custom input definition.")]) ) definition = application.get_definition() self.assertFalse(definition.has_argument("command")) self.assertFalse(definition.has_option("help")) self.assertFalse(definition.has_option("quiet")) self.assertFalse(definition.has_option("verbose")) self.assertFalse(definition.has_option("version")) self.assertFalse(definition.has_option("ansi")) self.assertFalse(definition.has_option("no-ansi")) self.assertFalse(definition.has_option("no-interaction")) self.assertTrue(definition.has_option("custom"))
def test_setting_input_definition_overwrites_default_values(self): application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) application.set_definition(InputDefinition([ InputOption('--custom', '-c', InputOption.VALUE_NONE, 'Set the custom input definition.') ])) definition = application.get_definition() self.assertFalse(definition.has_argument('command')) self.assertFalse(definition.has_option('help')) self.assertFalse(definition.has_option('quiet')) self.assertFalse(definition.has_option('verbose')) self.assertFalse(definition.has_option('version')) self.assertFalse(definition.has_option('ansi')) self.assertFalse(definition.has_option('no-ansi')) self.assertFalse(definition.has_option('no-interaction')) self.assertTrue(definition.has_option('custom'))
def test_run(self): application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) command = Foo1Command() application.add(command) sys.argv = ["cli.py", "foo:bar1"] application.run() self.assertEqual("ArgvInput", command.input.__class__.__name__) self.assertEqual("ConsoleOutput", command.output.__class__.__name__) application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) self.ensure_static_command_help(application) tester = ApplicationTester(application) tester.run([], {"decorated": False}) self.assertEqual(self.open_fixture("application_run1.txt"), tester.get_display()) tester.run([("--help", True)], {"decorated": False}) self.assertEqual(self.open_fixture("application_run2.txt"), tester.get_display()) tester.run([("-h", True)], {"decorated": False}) self.assertEqual(self.open_fixture("application_run2.txt"), tester.get_display()) tester.run([("command", "list"), ("--help", True)], {"decorated": False}) self.assertEqual(self.open_fixture("application_run3.txt"), tester.get_display()) tester.run([("command", "list"), ("-h", True)], {"decorated": False}) self.assertEqual(self.open_fixture("application_run3.txt"), tester.get_display()) tester.run([("--ansi", True)]) self.assertTrue(tester.get_output().is_decorated()) tester.run([("--no-ansi", True)]) self.assertFalse(tester.get_output().is_decorated()) tester.run([("--version", True)], {"decorated": False}) self.assertEqual(self.open_fixture("application_run4.txt"), tester.get_display()) tester.run([("-V", True)], {"decorated": False}) self.assertEqual(self.open_fixture("application_run4.txt"), tester.get_display()) tester.run([("command", "list"), ("--quiet", True)]) self.assertEqual("", tester.get_display()) tester.run([("command", "list"), ("-q", True)]) self.assertEqual("", tester.get_display()) tester.run([("command", "list"), ("--verbose", True)]) self.assertEqual(Output.VERBOSITY_VERBOSE, tester.get_output().get_verbosity()) tester.run([("command", "list"), ("-v", True)]) self.assertEqual(Output.VERBOSITY_VERBOSE, tester.get_output().get_verbosity()) application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) application.add(FooCommand()) tester = ApplicationTester(application) tester.run([("command", "foo:bar"), ("--no-interaction", True)], {"decorated": False}) self.assertEqual("called\n", tester.get_display()) tester.run([("command", "foo:bar"), ("-n", True)], {"decorated": False}) self.assertEqual("called\n", tester.get_display())
def test_run(self): application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) command = Foo1Command() application.add(command) sys.argv = ['cli.py', 'foo:bar1'] application.run() self.assertEqual( 'ArgvInput', command.input.__class__.__name__ ) self.assertEqual( 'ConsoleOutput', command.output.__class__.__name__ ) application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) self.ensure_static_command_help(application) tester = ApplicationTester(application) tester.run([], {'decorated': False}) self.assertEqual( self.open_fixture('application_run1.txt'), tester.get_display() ) tester.run([('--help', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_run2.txt'), tester.get_display() ) tester.run([('-h', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_run2.txt'), tester.get_display() ) tester.run([('command', 'list'), ('--help', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_run3.txt'), tester.get_display() ) tester.run([('command', 'list'), ('-h', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_run3.txt'), tester.get_display() ) tester.run([('--ansi', True)]) self.assertTrue(tester.get_output().is_decorated()) tester.run([('--no-ansi', True)]) self.assertFalse(tester.get_output().is_decorated()) tester.run([('--version', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_run4.txt'), tester.get_display() ) tester.run([('-V', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_run4.txt'), tester.get_display() ) tester.run([('command', 'list'), ('--quiet', True)]) self.assertEqual( '', tester.get_display() ) tester.run([('command', 'list'), ('-q', True)]) self.assertEqual( '', tester.get_display() ) tester.run([('command', 'list'), ('--verbose', True)]) self.assertEqual( Output.VERBOSITY_VERBOSE, tester.get_output().get_verbosity() ) tester.run([('command', 'list'), ('-v', True)]) self.assertEqual( Output.VERBOSITY_VERBOSE, tester.get_output().get_verbosity() ) application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) application.add(FooCommand()) tester = ApplicationTester(application) tester.run([('command', 'foo:bar'), ('--no-interaction', True)], {'decorated': False}) self.assertEqual( 'called\n', tester.get_display() ) tester.run([('command', 'foo:bar'), ('-n', True)], {'decorated': False}) self.assertEqual( 'called\n', tester.get_display() )