Example #1
0
 def test_function_as_command(self):
     '`cli` function as a command'
     def cmd(arg, kwarg=None, kwarg2=False):
         self.assertEquals(arg, 'arg')
         self.assertEquals(kwarg, 'kwarg')
         self.assertEquals(kwarg2, True)
     argv = 'mage.py test arg --kwarg=kwarg --kwarg2'
     manage(dict(test=cmd), argv.split())
Example #2
0
 def test_function_with_convs_as_command(self):
     '`cli` function with converters as a command'
     @argument(0, argument.to_int)
     @argument('kwarg', argument.to_date)
     def cmd(arg, kwarg=None, kwarg2=False):
         self.assertEquals(arg, 1)
         self.assertEquals(kwarg, datetime.date(2010, 6, 9))
         self.assertEquals(kwarg2, True)
     argv = 'mage.py test 1 --kwarg=9/6/2010 --kwarg2'
     manage(dict(test=cmd), argv.split())
Example #3
0
    def test_no_command_provided(self):
        class TestCommand(Cli):
            def command_avocado(self, kwarg=None, kwarg2=False):
                pass
        argv = 'manage.py'

        out = get_io()
        with patch.object(sys, 'stdout', out):
            with self.assertRaises(SystemExit) as exc:
                manage(dict(fruit=TestCommand()), argv.split())
            self.assertEqual(u'Commands:\nfruit\n', out.getvalue())
Example #4
0
 def test_manage(self):
     '`cli` manage'
     assrt = self.assertEquals
     class TestCommand(Cli):
         def command_test(self, arg, kwarg=None, kwarg2=False):
             assrt(arg, 'arg1')
             assrt(kwarg, 'kwarg3')
             assrt(kwarg2, False)
     test_cmd = TestCommand()
     argv = 'mage.py test:test arg1 --kwarg=kwarg3'
     manage(dict(test=test_cmd), argv.split())
Example #5
0
    def test_no_command_provided(self):
        class TestCommand(Cli):
            def command_avocado(self, kwarg=None, kwarg2=False):
                pass

        argv = 'manage.py'

        out = get_io()
        with patch.object(sys, 'stdout', out):
            with self.assertRaises(SystemExit) as exc:
                manage(dict(fruit=TestCommand()), argv.split())
            self.assertEqual(u'Commands:\nfruit\n', out.getvalue())
Example #6
0
 def test_convs(self):
     '`cli` converter'
     assrt = self.assertEquals
     class TestCommand(Cli):
         @argument(1, argument.to_int)
         @argument('kwarg', argument.to_date)
         def command_test(self, arg, kwarg=None, kwarg2=False):
             assrt(arg, 1)
             assrt(kwarg, datetime.date(2010, 6, 9))
             assrt(kwarg2, True)
     test_cmd = TestCommand()
     argv = 'mage.py test:test 1 --kwarg=9/6/2010 --kwarg2'
     manage(dict(test=test_cmd), argv.split())
Example #7
0
    def test_lazy_autocomplete(self):
        @LazyCli
        def cli():
            return MyCli()

        argv = 'manage.py fruit:'
        with patch.dict('os.environ', {'IKTOMI_AUTO_COMPLETE':'1',
                                       'COMP_WORDS':argv.replace(":", " : "),
                                       'COMP_CWORD':'2' }):
            out = StringIO()
            with patch.object(sys, 'stdout', out):
                with self.assertRaises(SystemExit):
                    manage(dict(fruit=cli), argv.split())
            self.assertEqual('run', out.getvalue())
Example #8
0
    def test_command_not_found(self):
        class TestCommand(Cli):
            def command_avocado(self, kwarg=None, kwarg2=False):
                pass
        argv = 'manage.py fruit:orange --kwarg --kwarg2'

        out = get_io()
        with patch.object(sys, 'stdout', out):
            with self.assertRaises(SystemExit) as exc:
                manage(dict(fruit=TestCommand()), argv.split())
            self.assertEqual(exc.exception.code,
                             'ERROR: Command "fruit:orange" not found')
        self.assertIn(u'manage.py', out.getvalue())
        self.assertIn(u'fruit:avocado [kwarg] [kwarg2]', out.getvalue())
Example #9
0
    def test_function_as_command(self):
        '`cli` function as a command'
        def cmd(arg, kwarg=None, kwarg2=False, kwarg3=False):
            self.assertEquals(arg, 'arg')
            self.assertEquals(kwarg, 'kwarg')
            self.assertEquals(kwarg2, True)
            self.assertEquals(kwarg3, '')
            sys.stdout.write(u"Completed\n")

        argv = 'manage.py fruit arg --kwarg=kwarg --kwarg2 --kwarg3='
        out = get_io()
        with patch.object(sys, 'stdout', out):
            manage(dict(fruit=cmd), argv.split())
        self.assertEqual(u"Completed\n", out.getvalue())
Example #10
0
 def test_function_with_convs_as_command(self):
     '`cli` function with converters as a command'
     @argument(0, argument.to_int)
     @argument('kwarg', argument.to_date)
     def cmd(arg, kwarg=None, kwarg2=False):
         self.assertEquals(arg, 1)
         self.assertEquals(kwarg, datetime.date(2010, 6, 9))
         self.assertEquals(kwarg2, True)
         sys.stdout.write(u"Completed\n")
     argv = 'manage.py fruit 1 --kwarg=9/6/2010 --kwarg2'
     out = get_io()
     with patch.object(sys, 'stdout', out):
         manage(dict(fruit=cmd), argv.split())
     self.assertEqual(u"Completed\n", out.getvalue())
Example #11
0
    def test_command_not_found(self):
        class TestCommand(Cli):
            def command_avocado(self, kwarg=None, kwarg2=False):
                pass

        argv = 'manage.py fruit:orange --kwarg --kwarg2'

        out = get_io()
        with patch.object(sys, 'stdout', out):
            with self.assertRaises(SystemExit) as exc:
                manage(dict(fruit=TestCommand()), argv.split())
            self.assertEqual(exc.exception.code,
                             'ERROR: Command "fruit:orange" not found')
        self.assertIn(u'manage.py', out.getvalue())
        self.assertIn(u'fruit:avocado [kwarg] [kwarg2]', out.getvalue())
Example #12
0
    def test_function_as_command(self):
        '`cli` function as a command'

        def cmd(arg, kwarg=None, kwarg2=False, kwarg3=False):
            self.assertEquals(arg, 'arg')
            self.assertEquals(kwarg, 'kwarg')
            self.assertEquals(kwarg2, True)
            self.assertEquals(kwarg3, '')
            sys.stdout.write(u"Completed\n")

        argv = 'manage.py fruit arg --kwarg=kwarg --kwarg2 --kwarg3='
        out = get_io()
        with patch.object(sys, 'stdout', out):
            manage(dict(fruit=cmd), argv.split())
        self.assertEqual(u"Completed\n", out.getvalue())
Example #13
0
 def test_converter_int_error(self):
     class TestCommand(Cli):
         @argument('kwarg', argument.to_int)
         def command_avocado(self, kwarg):
             pass
     test_cmd = TestCommand()
     argv = 'manage.py fruit:avocado --kwarg=noint'
     err = get_io()
     with patch.object(sys, 'stderr', err):
         with self.assertRaises(SystemExit) as exc:
             manage(dict(fruit=test_cmd), argv.split())
         self.assertIn(u'One of the arguments for command "avocado" is wrong:',
                        err.getvalue())
         self.assertEqual('Cannot convert \'noint\' to int',
                          exc.exception.code.args[0])
Example #14
0
    def test_autocomplete(self):
        class TestCommand(Cli):
            def command_avocado(self, kwarg):
                pass

        argv = 'manage.py'
        test_cmd = TestCommand()
        with patch.dict('os.environ', {
                'IKTOMI_AUTO_COMPLETE': '1',
                'COMP_WORDS': argv,
                'COMP_CWORD': '1'
        }):
            out = get_io()
            with patch.object(sys, 'stdout', out):
                with self.assertRaises(SystemExit):
                    manage(dict(fruit=test_cmd), argv.split())
            self.assertEqual(u'fruit fruit:', out.getvalue())

        argv = 'manage.py fr'
        test_cmd = TestCommand()
        with patch.dict('os.environ', {
                'IKTOMI_AUTO_COMPLETE': '1',
                'COMP_WORDS': argv,
                'COMP_CWORD': '1'
        }):
            out = get_io()
            with patch.object(sys, 'stdout', out):
                with self.assertRaises(SystemExit):
                    manage(dict(fruit=test_cmd), argv.split())
            self.assertEqual(u'fruit fruit:', out.getvalue())

        argv = 'manage.py fruit:'
        test_cmd = TestCommand()
        with patch.dict(
                'os.environ', {
                    'IKTOMI_AUTO_COMPLETE': '1',
                    'COMP_WORDS': argv.replace(":", " : "),
                    'COMP_CWORD': '2'
                }):
            out = get_io()
            with patch.object(sys, 'stdout', out):
                with self.assertRaises(SystemExit):
                    manage(dict(fruit=test_cmd), argv.split())
            self.assertEqual(u'avocado', out.getvalue())
        argv = 'manage.py fruit:av'
        test_cmd = TestCommand()

        with patch.dict(
                'os.environ', {
                    'IKTOMI_AUTO_COMPLETE': '1',
                    'COMP_WORDS': argv.replace(":", " : "),
                    'COMP_CWORD': '3'
                }):
            out = get_io()
            with patch.object(sys, 'stdout', out):
                with self.assertRaises(SystemExit):
                    manage(dict(fruit=test_cmd), argv.split())
            self.assertEqual(u'avocado', out.getvalue())
Example #15
0
    def test_incorrect_call(self):
        u'`cli` incorrect call'

        class TestCommand(Cli):
            def command_avocado(self, arg, kwarg=None, kwarg2=False):
                pass
        argv = 'manage.py fruit'
        out = get_io()
        with patch.object(sys, 'stdout', out):
            with self.assertRaises(SystemExit) as exc:
                manage(dict(fruit=TestCommand()), argv.split())
            self.assertEqual(exc.exception.code,
                             'ERROR: "fruit" command digest requires command name')
        self.assertIn(u"fruit:avocado", out.getvalue())
        self.assertIn(u"manage.py", out.getvalue())
        self.assertNotIn(u"./manage.py", out.getvalue())
Example #16
0
    def test_function_with_convs_as_command(self):
        '`cli` function with converters as a command'

        @argument(0, argument.to_int)
        @argument('kwarg', argument.to_date)
        def cmd(arg, kwarg=None, kwarg2=False):
            self.assertEquals(arg, 1)
            self.assertEquals(kwarg, datetime.date(2010, 6, 9))
            self.assertEquals(kwarg2, True)
            sys.stdout.write(u"Completed\n")

        argv = 'manage.py fruit 1 --kwarg=9/6/2010 --kwarg2'
        out = get_io()
        with patch.object(sys, 'stdout', out):
            manage(dict(fruit=cmd), argv.split())
        self.assertEqual(u"Completed\n", out.getvalue())
Example #17
0
    def test_manage(self):
        '`cli` manage'
        assrt = self.assertEquals
        class TestCommand(Cli):
            def command_avocado(self, arg, kwarg=None, kwarg2=False):
                assrt(arg, 'arg1')
                assrt(kwarg, 'kwarg3')
                assrt(kwarg2, False)
                sys.stdout.write(u"Completed\n")

        test_cmd = TestCommand()
        argv = 'manage.py fruit:avocado arg1 --kwarg=kwarg3'
        out = get_io()
        with patch.object(sys, 'stdout', out):
            manage(dict(fruit=test_cmd), argv.split())
        self.assertEqual(u"Completed\n", out.getvalue())
Example #18
0
    def test_argument_required(self):
        class TestCommand(Cli):
            @argument('kwarg', argument.to_int, required=True)
            def command_avocado(self, kwarg=1, kwarg2=None):
                pass
        argv = 'manage.py fruit:avocado --kwarg2=1'
        test_cmd = TestCommand()

        err = get_io()
        with patch.object(sys, 'stderr', err):
            with self.assertRaises(SystemExit) as exc:
                manage(dict(fruit=test_cmd), argv.split())
            self.assertIn(u'One of the arguments for command "avocado" is wrong:',
                          err.getvalue())
            self.assertIn('Keyword argument "kwarg" is required',
                          exc.exception.code.args[0])
Example #19
0
    def test_converter_int_error(self):
        class TestCommand(Cli):
            @argument('kwarg', argument.to_int)
            def command_avocado(self, kwarg):
                pass

        test_cmd = TestCommand()
        argv = 'manage.py fruit:avocado --kwarg=noint'
        err = get_io()
        with patch.object(sys, 'stderr', err):
            with self.assertRaises(SystemExit) as exc:
                manage(dict(fruit=test_cmd), argv.split())
            self.assertIn(
                u'One of the arguments for command "avocado" is wrong:',
                err.getvalue())
            self.assertEqual('Cannot convert \'noint\' to int',
                             exc.exception.code.args[0])
Example #20
0
    def test_argument_call_error(self):
        class TestCommand(Cli):
            @argument(2, argument.to_int)
            def command_avocado(self, arg, kwarg=None):
                pass
        argv = 'manage.py fruit:avocado --kwarg=test 1'
        test_cmd = TestCommand()

        err = get_io()
        with patch.object(sys, 'stderr', err):
            with self.assertRaises(SystemExit) as exc:
                manage(dict(fruit=test_cmd), argv.split())
            self.assertIn(u'One of the arguments for command "avocado" is wrong:',
                           err.getvalue())
            self.assertEqual('Total positional args = 2, but you apply converter '+\
                             'for 2 argument (indexing starts from 0)',
                             exc.exception.code.args[0])
Example #21
0
    def test_manage(self):
        '`cli` manage'
        assrt = self.assertEquals

        class TestCommand(Cli):
            def command_avocado(self, arg, kwarg=None, kwarg2=False):
                assrt(arg, 'arg1')
                assrt(kwarg, 'kwarg3')
                assrt(kwarg2, False)
                sys.stdout.write(u"Completed\n")

        test_cmd = TestCommand()
        argv = 'manage.py fruit:avocado arg1 --kwarg=kwarg3'
        out = get_io()
        with patch.object(sys, 'stdout', out):
            manage(dict(fruit=test_cmd), argv.split())
        self.assertEqual(u"Completed\n", out.getvalue())
Example #22
0
 def test_incorrect_call(self):
     '`cli` incorrect call'
     assrt = self.assertEquals
     class TestCommand(Cli):
         def command_test(self, arg, kwarg=None, kwarg2=False):
             pass
     argv = 'mage.py test'
     self.assertRaises(SystemExit, lambda: manage(dict(test=TestCommand()), argv.split()))
Example #23
0
    def test_convs(self):
        '`cli` converter'
        assrt = self.assertEquals
        class TestCommand(Cli):
            @argument(1, argument.to_int)
            @argument('kwarg', argument.to_date)
            def command_avocado(self, arg, kwarg=None, kwarg2=False):
                assrt(arg, 1)
                assrt(kwarg, datetime.date(2010, 6, 9))
                assrt(kwarg2, True)
                sys.stdout.write(u"Completed\n")

        test_cmd = TestCommand()
        argv = 'manage.py fruit:avocado 1 --kwarg=9/6/2010 --kwarg2'
        out = get_io()
        with patch.object(sys, 'stdout', out):
            manage(dict(fruit=test_cmd), argv.split())
        self.assertEqual(u"Completed\n", out.getvalue())
Example #24
0
    def test_incorrect_call(self):
        u'`cli` incorrect call'

        class TestCommand(Cli):
            def command_avocado(self, arg, kwarg=None, kwarg2=False):
                pass

        argv = 'manage.py fruit'
        out = get_io()
        with patch.object(sys, 'stdout', out):
            with self.assertRaises(SystemExit) as exc:
                manage(dict(fruit=TestCommand()), argv.split())
            self.assertEqual(
                exc.exception.code,
                'ERROR: "fruit" command digest requires command name')
        self.assertIn(u"fruit:avocado", out.getvalue())
        self.assertIn(u"manage.py", out.getvalue())
        self.assertNotIn(u"./manage.py", out.getvalue())
Example #25
0
    def test_argument_required(self):
        class TestCommand(Cli):
            @argument('kwarg', argument.to_int, required=True)
            def command_avocado(self, kwarg=1, kwarg2=None):
                pass

        argv = 'manage.py fruit:avocado --kwarg2=1'
        test_cmd = TestCommand()

        err = get_io()
        with patch.object(sys, 'stderr', err):
            with self.assertRaises(SystemExit) as exc:
                manage(dict(fruit=test_cmd), argv.split())
            self.assertIn(
                u'One of the arguments for command "avocado" is wrong:',
                err.getvalue())
            self.assertIn('Keyword argument "kwarg" is required',
                          exc.exception.code.args[0])
Example #26
0
    def test_convs(self):
        '`cli` converter'
        assrt = self.assertEquals

        class TestCommand(Cli):
            @argument(1, argument.to_int)
            @argument('kwarg', argument.to_date)
            def command_avocado(self, arg, kwarg=None, kwarg2=False):
                assrt(arg, 1)
                assrt(kwarg, datetime.date(2010, 6, 9))
                assrt(kwarg2, True)
                sys.stdout.write(u"Completed\n")

        test_cmd = TestCommand()
        argv = 'manage.py fruit:avocado 1 --kwarg=9/6/2010 --kwarg2'
        out = get_io()
        with patch.object(sys, 'stdout', out):
            manage(dict(fruit=test_cmd), argv.split())
        self.assertEqual(u"Completed\n", out.getvalue())
Example #27
0
    def test_argument_call_error(self):
        class TestCommand(Cli):
            @argument(2, argument.to_int)
            def command_avocado(self, arg, kwarg=None):
                pass

        argv = 'manage.py fruit:avocado --kwarg=test 1'
        test_cmd = TestCommand()

        err = get_io()
        with patch.object(sys, 'stderr', err):
            with self.assertRaises(SystemExit) as exc:
                manage(dict(fruit=test_cmd), argv.split())
            self.assertIn(
                u'One of the arguments for command "avocado" is wrong:',
                err.getvalue())
            self.assertEqual('Total positional args = 2, but you apply converter '+\
                             'for 2 argument (indexing starts from 0)',
                             exc.exception.code.args[0])
Example #28
0
    def test_autocomplete(self):
        class TestCommand(Cli):
            def command_avocado(self, kwarg):
                pass

        argv = 'manage.py'
        test_cmd = TestCommand()
        with patch.dict('os.environ', {'IKTOMI_AUTO_COMPLETE':'1',
                                       'COMP_WORDS':argv,
                                       'COMP_CWORD':'1' }):
            out = get_io()
            with patch.object(sys, 'stdout', out):
                with self.assertRaises(SystemExit):
                    manage(dict(fruit=test_cmd), argv.split())
            self.assertEqual(u'fruit fruit:', out.getvalue())

        argv = 'manage.py fr'
        test_cmd = TestCommand()
        with patch.dict('os.environ', {'IKTOMI_AUTO_COMPLETE':'1',
                                       'COMP_WORDS':argv,
                                       'COMP_CWORD':'1' }):
            out = get_io()
            with patch.object(sys, 'stdout', out):
                with self.assertRaises(SystemExit):
                    manage(dict(fruit=test_cmd), argv.split())
            self.assertEqual(u'fruit fruit:', out.getvalue())

        argv = 'manage.py fruit:'
        test_cmd = TestCommand()
        with patch.dict('os.environ', {'IKTOMI_AUTO_COMPLETE':'1',
                                       'COMP_WORDS':argv.replace(":", " : "),
                                       'COMP_CWORD':'2' }):
            out = get_io()
            with patch.object(sys, 'stdout', out):
                with self.assertRaises(SystemExit):
                    manage(dict(fruit=test_cmd), argv.split())
            self.assertEqual(u'avocado', out.getvalue())
        argv = 'manage.py fruit:av'
        test_cmd = TestCommand()

        with patch.dict('os.environ', {'IKTOMI_AUTO_COMPLETE':'1',
                                       'COMP_WORDS':argv.replace(":", " : "),
                                       'COMP_CWORD':'3' }):
            out = get_io()
            with patch.object(sys, 'stdout', out):
                with self.assertRaises(SystemExit):
                    manage(dict(fruit=test_cmd), argv.split())
            self.assertEqual(u'avocado', out.getvalue())