示例#1
0
    def test_cli_help(self):
        outp = s_output.OutPutStr()
        with s_cli.Cli(outp=outp) as cli:
            cli.runCmdLine('help')

        self.true(
            str(outp).find('Quit the current command line interpreter.') != -1)
示例#2
0
 def test_cli_cmd_loop_eof(self):
     outp = self.getTestOutp()
     cmdg = CmdGenerator(['help'], on_end=EOFError)
     with mock.patch('synapse.lib.cli.get_input', cmdg) as p:
         with s_cli.Cli(None, outp) as cli:
             cli.runCmdLoop()
             self.eq(cli.isfini, True)
     self.false(outp.expect('o/', throw=False))
示例#3
0
    def test_cli_cmd_loop_quit(self):
        outp = self.getTestOutp()
        cmdg = CmdGenerator(['help', 'quit'])

        with mock.patch('synapse.lib.cli.get_input', cmdg) as p:
            with s_cli.Cli(None, outp) as cli:
                cli.runCmdLoop()
                self.eq(cli.isfini, True)
        self.true(outp.expect('o/'))
示例#4
0
    def test_cli_cmdmeths(self):
        hehe = Hehe()
        cli = s_cli.Cli()

        cli.addCmdMeths(hehe)

        self.assertEqual(cli.getCmdBrief('hehe'), 'The hehe cmd.')

        cli.fini()
示例#5
0
    def test_cli_print(self):

        cli = s_cli.Cli()
        wait = self.getTestWait(cli, 1, 'cli:print')

        cli.vprint('hi there!')

        wait.wait()

        self.assertEqual(wait.events[0][1].get('msg'), 'hi there!')
示例#6
0
    def test_cli_opts_parse_kwlist(self):

        with s_cli.Cli(None) as cli:

            quit = cli.getCmdByName('quit')

            quit._cmd_syntax = (('bar', {'type': 'kwlist'}), )

            opts = quit.getCmdOpts('quit hehe=haha')
            self.eq(opts.get('bar'), [('hehe', 'haha')])
示例#7
0
 async def test_cli_fini_disconnect(self):
     evt = threading.Event()
     outp = self.getTestOutp()
     async with self.getTestDmon('dmonboot') as dmon:
         async with await self.getTestProxy(dmon, 'echo00') as prox:
             cli = s_cli.Cli(prox, outp)
             cli.onfini(evt.set)
         self.true(evt.wait(2))
         self.true(cli.isfini)
         self.true(outp.expect('connection closed...'))
示例#8
0
    def test_cli_cmdret(self):
        def woot(cli, line):
            return 20

        cli = s_cli.Cli()
        cli.addCmdFunc(woot)

        self.assertEqual(cli.runCmdLine('woot hehe'), 20)

        cli.fini()
示例#9
0
    def test_cli_cmdret(self):
        class WootCmd(s_cli.Cmd):
            _cmd_name = 'woot'

            def runCmdOpts(self, opts):
                return 20

        with s_cli.Cli(None) as cli:
            cli.addCmdClass(WootCmd)
            self.eq(cli.runCmdLine('woot'), 20)
示例#10
0
 def test_cli_cmd_loop_bad_input(self):
     outp = self.getTestOutp()
     cmdg = CmdGenerator([1234], on_end=EOFError)
     with mock.patch('synapse.lib.cli.get_input', cmdg) as p:
         with s_cli.Cli(None, outp) as cli:
             cli.runCmdLoop()
             self.eq(cli.isfini, True)
     self.true(
         outp.expect(
             "AttributeError: 'int' object has no attribute 'strip'",
             throw=False))
示例#11
0
 def test_cli_get_set(self):
     outp = self.getTestOutp()
     with s_cli.Cli(None, outp=outp, hehe='haha') as cli:
         self.eq(cli.get('hehe'), 'haha')
         self.none(cli.get('foo'))
         cli.set('foo', 'bar')
         self.eq(cli.get('foo'), 'bar')
         cli.runCmdLine('locs')
         self.true(outp.expect('hehe'))
         self.true(outp.expect('haha'))
         self.true(outp.expect('foo'))
         self.true(outp.expect('bar'))
示例#12
0
    def test_cli_opts_parse_valu(self):

        with s_cli.Cli(None) as cli:

            quit = cli.getCmdByName('quit')

            quit._cmd_syntax = (('--bar', {'type': 'valu'}), )

            opts = quit.getCmdOpts('quit --bar woah')
            self.eq(opts.get('bar'), 'woah')

            self.raises(BadSyntaxError, quit.getCmdOpts,
                        'quit --bar woah this is too much text')
示例#13
0
    def test_cli_cmdfunc(self):

        data = {}

        def woot(xcli, line):
            data['line'] = line

        cli = s_cli.Cli()
        cli.addCmdFunc(woot)

        cli.runCmdLine('woot haha')

        self.assertEqual(data.get('line'), 'woot haha')
示例#14
0
 def test_cli_cmd_loop(self):
     outp = self.getTestOutp()
     cmdg = s_t_utils.CmdGenerator(
         ['help', 'locs', '', '    ', 'throwzero', 'throwkeyboard', 'quit'])
     with mock.patch('synapse.lib.cli.get_input', cmdg):
         with s_cli.Cli(None, outp) as cli:
             cli.addCmdClass(TstThrowCmd)
             cli.addCmdClass(TstThrowKeyboard)
             cli.runCmdLoop()
             self.true(outp.expect('o/'))
             self.true(outp.expect('{}'))
             self.true(outp.expect('ZeroDivisionError'))
             self.true(outp.expect('Cmd cancelled'))
             self.true(cli.isfini)
示例#15
0
    def test_cli_brief(self):
        def woot(xcli, line):
            '''
            This is a brief.
            And this is some description.
            '''
            pass

        cli = s_cli.Cli()
        cli.addCmdFunc(woot)

        self.assertEqual(cli.getCmdBrief('woot'), 'This is a brief.')

        cli.fini()
示例#16
0
    def test_cli_opts_flag(self):
        with s_cli.Cli(None) as cli:

            quit = cli.getCmdByName('quit')

            quit._cmd_syntax = (
                ('--bar', {}),
                ('haha', {
                    'type': 'valu'
                }),
            )

            opts = quit.getCmdOpts('quit --bar hoho')

            self.eq(opts.get('bar'), True)
            self.eq(opts.get('haha'), 'hoho')
示例#17
0
    def test_cli_opts_glob(self):

        with s_cli.Cli() as cli:

            quit = cli.getCmdByName('quit')

            quit._cmd_syntax = (
                ('--bar', {}),
                ('haha', {
                    'type': 'glob'
                }),
            )

            opts = quit.getCmdOpts('quit --bar hoho lulz')

            self.eq(opts.get('bar'), True)
            self.eq(opts.get('haha'), 'hoho lulz')
示例#18
0
    def test_cli_opts_list(self):

        with s_cli.Cli(None) as cli:

            quit = cli.getCmdByName('quit')

            quit._cmd_syntax = (
                ('--bar', {}),
                ('haha', {
                    'type': 'list'
                }),
            )

            opts = quit.getCmdOpts('quit --bar hoho haha "hehe hehe"')

            self.eq(opts.get('bar'), True)
            self.eq(tuple(opts.get('haha')), ('hoho', 'haha', 'hehe hehe'))
示例#19
0
    def test_cli_opts_parse_enums(self):

        with s_cli.Cli(None) as cli:

            quit = cli.getCmdByName('quit')

            quit._cmd_syntax = (('--bar', {
                'type': 'enum',
                'enum:vals': ('foo', 'baz')
            }), )

            opts = quit.getCmdOpts('quit --bar foo')
            self.eq(opts.get('bar'), 'foo')
            opts = quit.getCmdOpts('quit --bar baz')
            self.eq(opts.get('bar'), 'baz')
            self.raises(BadSyntaxError, quit.getCmdOpts, 'quit --bar')
            self.raises(BadSyntaxError, quit.getCmdOpts, 'quit --bar bar')
示例#20
0
    def test_cli_cmd_loop_keyint(self):
        outp = self.getTestOutp()
        cmdg = CmdGenerator(['help'], on_end=KeyboardInterrupt)

        data = {'count': 0}

        def _onGetInput(mesg):
            data['count'] = data['count'] + 1
            if data['count'] > 2:
                cmdg.addCmd('quit')

        with mock.patch('synapse.lib.cli.get_input', cmdg) as p:
            with s_cli.Cli(None, outp) as cli:
                cli.on('cli:getinput', _onGetInput)
                cli.runCmdLoop()
                self.eq(cli.isfini, True)
        self.true(outp.expect('<ctrl-c>'))
示例#21
0
    def test_cli_opts_defval(self):

        with s_cli.Cli(None) as cli:

            quit = cli.getCmdByName('quit')

            quit._cmd_syntax = (
                ('--bar', {
                    'type': 'valu',
                    'defval': 'lol'
                }),
                ('haha', {
                    'type': 'glob'
                }),
            )

            opts = quit.getCmdOpts('quit hoho lulz')
            self.eq(opts.get('bar'), 'lol')
示例#22
0
    def test_cli_opts_parse_list(self):

        with s_cli.Cli(None) as cli:

            quit = cli.getCmdByName('quit')

            quit._cmd_syntax = (('--bar', {'type': 'list'}), )

            # The list must be quoted
            opts = quit.getCmdOpts('quit --bar "1,2,3"')
            self.eq(opts.get('bar'), ['1', '2', '3'])

            # Or encapsulated in a storm list syntax
            opts = quit.getCmdOpts('quit --bar (1, 2, 3)')
            self.eq(opts.get('bar'), [1, 2, 3])

            # A single item is fine
            opts = quit.getCmdOpts('quit --bar woah')
            self.eq(opts.get('bar'), ['woah'])
示例#23
0
文件: cmdr.py 项目: mari0d/synapse
def getItemCmdr(item, outp=None, **opts):
    '''
    Construct and return a cmdr for the given item.

    Example:

        cmdr = getItemCmdr(foo)

    '''
    cmdr = s_cli.Cli(item, outp=outp)

    refl = s_reflect.getItemInfo(item)
    if refl is None:
        return cmdr

    for name in refl.get('inherits', ()):
        for mixi in s_mixins.getSynMixins('cmdr', name):
            cmdr.addCmdClass(mixi)

    return cmdr
示例#24
0
 def test_cli_quit(self):
     cli = s_cli.Cli()
     cli.runCmdLine('quit')
     self.assertTrue(cli.isfini)
示例#25
0
 def test_cli_cmd(self):
     with s_cli.Cli(None) as cli:
         quit = cli.getCmdByName('quit')
         self.nn(quit.getCmdDoc())
         self.nn(quit.getCmdBrief())
示例#26
0
 def test_cli_delcmd(self):
     cli = s_cli.Cli()
     self.assertIsNotNone(cli.getCmdFunc('quit'))
     cli.delCmdFunc('quit')
     self.assertIsNone(cli.getCmdFunc('quit'))
示例#27
0
 def test_cli_notacommand(self):
     outp = self.getTestOutp()
     with s_cli.Cli(None, outp=outp) as cli:
         cli.runCmdLine('notacommand')
     self.true(outp.expect('cmd not found: notacommand'))
示例#28
0
 def test_cli_help(self):
     outp = self.getTestOutp()
     with s_cli.Cli(None, outp=outp) as cli:
         cli.runCmdLine('help')
     self.true(outp.expect('Quit the current command line interpreter.'))
示例#29
0
 def test_cli_quit(self):
     outp = self.getTestOutp()
     with s_cli.Cli(None, outp=outp) as cli:
         cli.runCmdLine('quit')
         self.true(cli.isfini)
示例#30
0
 def test_cli_prompt(self):
     outp = self.getTestOutp()
     with s_cli.Cli(None, outp=outp) as cli:
         self.eq(cli.getCmdPrompt(), 'cli> ')
         cli.cmdprompt = 'hehe> '
         self.eq(cli.getCmdPrompt(), 'hehe> ')