Exemplo n.º 1
0
    def test_load_config(self):
        meta = Meta(
            to_unicode(
                os.path.join(os.path.abspath(os.path.curdir), 'tests',
                             'resources')))

        with self.withAssertOutput(
                'Reading file: %s\n' % self._testfile('minimum.yml'),
                '') as (out, err):
            self.assertEqual(
                Setting(config_path=self._testfile('minimum.yml'),
                        encoding='utf-8',
                        stdout=out,
                        stderr=err).load_config().root_menu,
                Menu('', [], meta))
        path = os.path.join(os.path.abspath(os.path.curdir), 'tests',
                            'resources', 'flat.yml')
        with self.withAssertOutput('Reading file: %s\n' % path,
                                   '') as (out, err):
            self.assertEqual(
                Setting(config_path=self._testfile('flat.yml'),
                        encoding='utf-8',
                        stdout=out,
                        stderr=err).load_config().root_menu,
                Menu('Main Menu', [
                    Command('Menu 1', [CommandLine('echo 1', meta)]),
                    Command('Menu 2', [CommandLine('echo 2', meta)]),
                    Command('Menu 3', [CommandLine('echo 3', meta)]),
                    Command('Menu 4', [CommandLine('echo 4', meta)]),
                    Command('Menu 5', [CommandLine('echo 5', meta)]),
                    Command('Menu 6', [CommandLine('echo 6', meta)]),
                ], meta))
Exemplo n.º 2
0
    def test_loop(self):
        self.maxDiff = None

        root_menu = Menu('Main menu', [
            Command('Menu a', [CommandLine('echo executing a', Meta())]),
            Command('Menu b',
                    [CommandLine('echo executing b && exit 130', Meta())]),
            Menu('Sub Menu 1', [
                Command('Menu 1', [CommandLine('echo executing 1', Meta())]),
                Command('Menu 2', [CommandLine('echo executing 2', Meta())]),
                Command('Menu 3', [CommandLine('echo executing 3', Meta())]),
                Command('Menu 4', [CommandLine('echo executing 4', Meta())]),
                Command('Menu 5', [CommandLine('echo executing 5', Meta())]),
                Command('Menu 6', [CommandLine('echo executing 6', Meta())]),
                Command('Menu 7', [CommandLine('echo executing 7', Meta())]),
                Command('Menu 8', [CommandLine('echo executing 8', Meta())]),
                Command('Menu 9', [CommandLine('echo executing 9', Meta())]),
                Command('Menu 10', [CommandLine('echo executing 10', Meta())]),
            ], Meta())
        ], Meta())

        _in = FakeInput(''.join([
            '1n', '1N', '1\n', '1yx', '2Yx', '3n', '1yx', 'p', '9yx', '0', '-0'
        ]))

        # We use a temporary file due to capture the output of subprocess#call.
        path = os.path.join('tests', 'resources', 'expect',
                            'terminal_test_loop.txt')
        with self.withAssertOutputFile(path) as out:
            t = Terminal(root_menu,
                         'host',
                         'user',
                         self.get_exec(encoding='utf-8',
                                       stdout=out,
                                       stderr=out),
                         handler=TerminalHandler(stdin=_in,
                                                 stdout=out,
                                                 stderr=out,
                                                 keep_input_clean=False),
                         _input=_in,
                         _output=out,
                         encoding='utf-8',
                         lang='en_US',
                         width=80,
                         timing=False)
            t.loop()

        self.assertEqual(t.executor.logger.buffer, [
            (6, '[INFO] Command started: echo executing a'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo executing b && exit 130'),
            (6, '[INFO] Command ended with return code: 130'),
            (6, '[INFO] Command started: echo executing 10'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo executing 9'),
            (6, '[INFO] Command ended with return code: 0'),
        ])
Exemplo n.º 3
0
    def test_wait_input_menu(self):
        self.maxDiff = None

        _in = FakeInput('a\n9\n0\n')

        expected = '\n'.join([
            'Host: host                                                            User: user',
            '================================================================================',
            '  ',
            '--------------------------------------------------------------------------------',
            '------+-------------------------------------------------------------------------',
            '  [0] | Quit',
            '================================================================================',
            'Press menu number (0-0): ',
        ]) * 3
        with self.withAssertOutput(expected, '') as (out, err):
            t = Terminal(Menu('', [], Meta()),
                         'host',
                         'user',
                         self.get_exec(encoding='utf-8',
                                       stdout=out,
                                       stderr=err),
                         handler=TerminalHandler(stdin=_in,
                                                 stdout=out,
                                                 stderr=err,
                                                 keep_input_clean=False,
                                                 getch_enabled=False),
                         _input=_in,
                         _output=out,
                         encoding='utf-8',
                         lang='en_US',
                         width=80,
                         timing=False)
            t.loop()
Exemplo n.º 4
0
    def test_print_source_ja(self):
        self.maxDiff = None

        root_meta = Meta('/path/to/work', {'ENV1': '値1', 'ENV2': '値2'})
        sub_meta = Meta('/tmp2', {'ENV1': '値1', 'ENV2': '値2'})
        special_meta = Meta('/path/to/work2', {
            'ENV1': '値9',
            'ENV2': '値2',
            'ENV3': '値3'
        })

        root_menu = Menu('メイン メニュー', [
            Command('メニュー 1', [CommandLine('コマンド 1', root_meta)]),
            Command('メニュー 2', [CommandLine('コマンド 2', root_meta)]),
            Command('メニュー 3', [
                CommandLine('コマンド 3', special_meta),
                CommandLine('command 4', root_meta)
            ]),
            Menu('サブ メニュー',
                 [Command('メニュー s1', [CommandLine('コマンド 5', sub_meta)])],
                 sub_meta),
            Command('メニュー 9', [CommandLine('コマンド 9', root_meta)]),
        ], root_meta)

        _in = FakeInput('s\nx\n0\n')
        path = os.path.join('tests', 'resources', 'expect',
                            'terminal_test_print_source_ja.txt')
        with self.withAssertOutputFile(path, {'': ''}) as out:
            t = Terminal(root_menu,
                         'host',
                         'user',
                         self.get_exec(encoding='utf-8',
                                       stdout=out,
                                       stderr=out),
                         handler=TerminalHandler(stdin=_in,
                                                 stdout=out,
                                                 stderr=out,
                                                 keep_input_clean=False,
                                                 getch_enabled=False),
                         _input=_in,
                         _output=out,
                         encoding='utf-8',
                         lang='ja_JP',
                         width=80,
                         timing=False)
            t.loop()
Exemplo n.º 5
0
    def test_print_source(self):
        self.maxDiff = None

        root_meta = Meta('/path/to/work', {'ENV1': 'VAL1', 'ENV2': 'VAL2'})
        sub_meta = Meta('/tmp2', {'ENV1': 'VAL1', 'ENV2': 'VAL2'})
        special_meta = Meta('/path/to/work2', {
            'ENV1': 'VAL9',
            'ENV2': 'VAL2',
            'ENV3': 'VAL3'
        })

        root_menu = Menu('Main menu', [
            Command('label 1', [CommandLine('command 1', root_meta)]),
            Command('label 2', [CommandLine('command 2', root_meta)]),
            Command('label 3', [
                CommandLine('command 3', special_meta),
                CommandLine('command 4', root_meta)
            ]),
            Menu('sub menu',
                 [Command('label s1', [CommandLine('command 5', sub_meta)])],
                 sub_meta),
            Command('label 9', [CommandLine('command 9', root_meta)]),
        ], root_meta)

        _in = FakeInput('s\nx\n0\n')
        path = os.path.join('tests', 'resources', 'expect',
                            'terminal_test_print_source.txt')
        with self.withAssertOutputFile(path, {'': ''}) as out:
            t = Terminal(root_menu,
                         'host',
                         'user',
                         self.get_exec(encoding='utf-8',
                                       stdout=out,
                                       stderr=out),
                         handler=TerminalHandler(stdin=_in,
                                                 stdout=out,
                                                 stderr=out,
                                                 keep_input_clean=False,
                                                 getch_enabled=False),
                         _input=_in,
                         _output=out,
                         encoding='utf-8',
                         lang='en_US',
                         width=80,
                         timing=False)
            t.loop()
Exemplo n.º 6
0
    def parse(data, meta, loader, encoding='utf-8', depth=0):
        """
        :param data:
        :param meta:
        :param loader:
        :param encoding:
        :param depth: indicator for the nesting level
        :return:
        """
        from easy_menu.entity import Menu, Command, KEYWORD_META, KEYWORD_INCLUDE, KEYWORD_EVAL

        # avoid for inclusion loops and stack overflow
        assert depth < 50, 'Nesting level too deep.'

        # if the data has meta key, it should be a menu.
        if KEYWORD_META in data:
            return Menu.parse(data, meta, loader, encoding, depth)

        # parse eval cache setting
        eval_expire = None
        if KEYWORD_EVAL in data:
            if 'cache' in data:
                eval_expire = data['cache']
                del data['cache']

        assert len(
            data
        ) == 1, 'Item should have only one element, not %s.' % len(data)

        title, content = get_single_item(data)

        if title == KEYWORD_INCLUDE:
            assert isinstance(content, six.string_types), \
                '"include" section must have string content, not %s.' % type(content).__name__
            return Menu.parse(loader.load(False, content), meta, loader,
                              encoding, depth)
        elif title == KEYWORD_EVAL:
            assert isinstance(content, six.string_types), \
                '"eval" section must have string content, not %s.' % type(content).__name__
            return Menu.parse(loader.load(True, content, eval_expire), meta,
                              loader, encoding, depth)
        elif Item._is_command_like(content):
            return Command.parse(data, meta, loader, encoding, depth)
        else:
            return Menu.parse(data, meta, loader, encoding, depth)
Exemplo n.º 7
0
    def parse(data, meta, loader, encoding='utf-8', depth=0):
        """
        :param data:
        :param meta:
        :param loader:
        :param encoding:
        :param depth: indicator for the nesting level
        :return:
        """
        from easy_menu.entity import Menu, Command, KEYWORD_META, KEYWORD_INCLUDE, KEYWORD_EVAL

        # avoid for inclusion loops and stack overflow
        assert depth < 50, 'Nesting level too deep.'

        # if the data has meta key, it should be a menu.
        if KEYWORD_META in data:
            return Menu.parse(data, meta, loader, encoding, depth)

        # parse eval cache setting
        eval_expire = None
        if KEYWORD_EVAL in data:
            if 'cache' in data:
                eval_expire = data['cache']
                del data['cache']

        assert len(data) == 1, 'Item should have only one element, not %s.' % len(data)

        title, content = get_single_item(data)

        if title == KEYWORD_INCLUDE:
            assert isinstance(content, six.string_types), \
                '"include" section must have string content, not %s.' % type(content).__name__
            return Menu.parse(loader.load(False, content), meta, loader, encoding, depth)
        elif title == KEYWORD_EVAL:
            assert isinstance(content, six.string_types), \
                '"eval" section must have string content, not %s.' % type(content).__name__
            return Menu.parse(loader.load(True, content, eval_expire), meta, loader, encoding, depth)
        elif Item._is_command_like(content):
            return Command.parse(data, meta, loader, encoding, depth)
        else:
            return Menu.parse(data, meta, loader, encoding, depth)
Exemplo n.º 8
0
 def test_load_config_dynamic(self):
     meta = Meta(
         to_unicode(
             os.path.join(os.path.abspath(os.path.curdir), 'tests',
                          'resources')))
     path = os.path.join(os.path.abspath(os.path.curdir), 'tests',
                         'resources', 'with_dynamic.yml')
     expect = '\n'.join([
         'Reading file: %s' % path,
         """Executing: echo '{"Sub Menu": [{"Menu 2": "echo 2"}, {"Menu 3": "echo 3"}]}'\n"""
     ])
     with self.withAssertOutput(expect, '') as (out, err):
         self.assertEqual(
             Setting(config_path=self._testfile('with_dynamic.yml'),
                     encoding='utf-8',
                     stdout=out,
                     stderr=err).load_config().root_menu,
             Menu('Main Menu', [
                 Command('Menu 1', [CommandLine('echo 1', meta)]),
                 Menu('Sub Menu', [
                     Command('Menu 2', [CommandLine('echo 2', meta)]),
                     Command('Menu 3', [CommandLine('echo 3', meta)]),
                 ], meta)
             ], meta))
Exemplo n.º 9
0
    def load_config(self):
        """
        Load the configuration file or url.

        If it contains 'include' sections, load them recursively.
        :return: updated Setting instance
        """
        if self.config_path is None:
            raise SettingError('Not found configuration file.')

        loader = Loader(self.work_dir, self.cache_dir, self.encoding, self.stdout, self.clear_cache)
        data = loader.load(False, self.config_path)
        try:
            root_menu = Menu.parse(data, Meta(self.work_dir), loader, self.encoding, 0)
        except (AssertionError, ValueError, TypeError) as e:
            raise ConfigError(self.config_path, e)
        return self.copy(root_menu=root_menu)
Exemplo n.º 10
0
    def test_loop_sjis(self):
        self.maxDiff = None

        root_menu = Menu(
            'メインメニュー',
            [Command('メニュー 1', [CommandLine("echo 'あいうえお'", Meta())])], Meta())

        _in = FakeInput(''.join(['1yx', '0']))

        # We use a temporary file due to capture the output of subprocess#call.
        path = os.path.join('tests', 'resources', 'expect',
                            'terminal_test_loop_sjis.txt')
        with self.withAssertOutputFile(path,
                                       expect_file_encoding='sjis',
                                       output_encoding='sjis') as out:
            t = Terminal(root_menu,
                         'ホスト',
                         'ユーザ',
                         self.get_exec(encoding='sjis', stdout=out,
                                       stderr=out),
                         handler=TerminalHandler(stdin=_in,
                                                 stdout=out,
                                                 stderr=out,
                                                 keep_input_clean=False),
                         _input=_in,
                         _output=out,
                         encoding='sjis',
                         lang='ja_JP',
                         width=80,
                         timing=False)
            t.loop()
            # out.seek(0)
            # print(out.read())

        self.assertEqual(t.executor.logger.buffer, [
            (6, "[INFO] Command started: echo 'あいうえお'"),
            (6, "[INFO] Command ended with return code: 0"),
        ])
Exemplo n.º 11
0
    def test_execute_command_duplicate(self):
        self.maxDiff = None
        sleep_cmd = 'python -c "import time;time.sleep(1)"' if os.name == 'nt' else 'sleep 1'
        _in1 = FakeInput('\n'.join(['y', '\r']))
        _in2 = FakeInput('\n'.join(['y', '\r']))

        root_menu = Menu(
            'Main Menu',
            [Command('Menu 1', [CommandLine(sleep_cmd, Meta(lock=True))])])
        expected_en = '\n'.join([
            'Host: host                                                            User: user',
            '================================================================================',
            '  Confirmation',
            '--------------------------------------------------------------------------------',
            '  Would execute: Menu 1',
            '================================================================================',
            'Do you really want to execute? (y/n) [n]: '
            'Host: host                                                            User: user',
            '================================================================================',
            '  Duplicate check',
            '--------------------------------------------------------------------------------',
            '  Already running: Menu 1',
            '================================================================================',
            'Do you really want to continue? (y/n) [n]: ',
        ])

        with self.withAssertOutput(expected_en, '') as (out, err):
            t = Terminal(root_menu,
                         'host',
                         'user',
                         MockExecutor(0, True),
                         TerminalHandler(stdin=_in1,
                                         stdout=out,
                                         stderr=err,
                                         keep_input_clean=False,
                                         getch_enabled=False),
                         _input=_in1,
                         _output=out,
                         encoding='utf-8',
                         lang='en_US',
                         width=80,
                         timing=False)
            t.execute_command(
                Command('Menu 1', [CommandLine(sleep_cmd, Meta(lock=True))]))

        expected_ja = '\n'.join([
            'ホスト名: host                                                  実行ユーザ: user',
            '================================================================================',
            '  実行確認',
            '--------------------------------------------------------------------------------',
            '  Menu 1 を行います。',
            '================================================================================',
            'よろしいですか? (y/n) [n]: '
            'ホスト名: host                                                  実行ユーザ: user',
            '================================================================================',
            '  多重実行チェック',
            '--------------------------------------------------------------------------------',
            '  Menu 1 は既に実行中です。',
            '================================================================================',
            '本当によろしいですか? (y/n) [n]: ',
        ])

        with self.withAssertOutput(expected_ja, '') as (out, err):
            t = Terminal(root_menu,
                         'host',
                         'user',
                         MockExecutor(0, True),
                         TerminalHandler(stdin=_in2,
                                         stdout=out,
                                         stderr=err,
                                         keep_input_clean=False,
                                         getch_enabled=False),
                         _input=_in2,
                         _output=out,
                         encoding='utf-8',
                         lang='ja_JP',
                         width=80,
                         timing=False)
            t.execute_command(
                Command('Menu 1', [CommandLine(sleep_cmd, Meta(lock=True))]))
Exemplo n.º 12
0
    def test_loop_multiple_commands(self):
        self.maxDiff = None

        root_menu = Menu('Main Menu', [
            Menu('Sub Menu 1', [
                Command('Menu 1', [
                    CommandLine('echo 1', Meta()),
                    CommandLine('echo 2', Meta()),
                ])
            ], Meta()),
            Menu('Sub Menu 2', [
                Menu('Sub Menu 3', [
                    Command('Menu 3', [CommandLine('echo 3', Meta())]),
                    Command('Menu 4', [CommandLine('echo 4', Meta())]),
                ], Meta()),
                Command('Menu 5', [CommandLine('echo 5', Meta())])
            ], Meta()),
            Command('Menu 6', [
                CommandLine('echo 6', Meta()),
                CommandLine('echo 7', Meta()),
                CommandLine('false', Meta()),
                CommandLine('echo 8', Meta()),
            ])
        ], Meta())

        _in = FakeInput(''.join(
            ['1', '.1yx', '0', '21.1yx', '0.0', '3yx', '0']))

        # We use a temporary file due to capture the output of subprocess#call.
        with self.withAssertOutputFile(
                'tests/resources/expect/terminal_test_loop_multiple_commands.txt'
        ) as out:
            t = Terminal(root_menu,
                         'host',
                         'user',
                         self.get_exec(encoding='utf-8',
                                       stdout=out,
                                       stderr=out),
                         handler=TerminalHandler(stdin=_in,
                                                 stdout=out,
                                                 stderr=out,
                                                 keep_input_clean=False),
                         _input=_in,
                         _output=out,
                         encoding='utf-8',
                         lang='en_US',
                         width=80,
                         timing=False)
            t.loop()

        self.assertEqual(t.executor.logger.buffer, [
            (6, '[INFO] Command started: echo 1'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo 2'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo 3'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo 6'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo 7'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: false'),
            (6, '[INFO] Command ended with return code: 1'),
        ])
Exemplo n.º 13
0
    def test_get_page_ja(self):
        self.maxDiff = None

        t = Terminal({'': []},
                     'ホスト',
                     'ユーザ',
                     self.get_exec(),
                     handler=self.handler,
                     encoding='utf-8',
                     lang='ja_JP',
                     width=80)
        self.assertEqual(
            t.get_page(['タイトル'], [], 0, 1), '\n'.join([
                'ホスト名: ホスト                                              実行ユーザ: ユーザ',
                '================================================================================',
                '  タイトル',
                '--------------------------------------------------------------------------------',
                '------+-------------------------------------------------------------------------',
                '  [0] | 終了',
                '================================================================================',
                '番号を入力してください (0-0): '
            ]))

        self.assertEqual(
            t.get_page(['メインメニュー', 'タイトル'], [
                Command('メニュー a', [CommandLine('コマンド a', Meta())]),
                Command('メニュー b', [CommandLine('コマンド b', Meta())]),
                Command('メニュー c', [CommandLine('コマンド c', Meta())]),
            ], 0, 1), '\n'.join([
                'ホスト名: ホスト                                              実行ユーザ: ユーザ',
                '================================================================================',
                '  メインメニュー > タイトル',
                '--------------------------------------------------------------------------------',
                '  [1] | メニュー a', '  [2] | メニュー b', '  [3] | メニュー c',
                '------+-------------------------------------------------------------------------',
                '  [0] | メインメニュー に戻る',
                '================================================================================',
                '番号を入力してください (0-3): '
            ]))

        self.assertEqual(
            t.get_page([
                'メインメニュー',
                'タイトル1',
                'タイトル2',
                'タイトル3',
                'タイトル4',
                'タイトル5',
                'タイトル6',
                'タイトル7',
                'タイトル8',
            ], [
                Command('メニュー a', [CommandLine('コマンド a', Meta())]),
                Command('メニュー b', [CommandLine('コマンド b', Meta())]),
                Command('メニュー c', [CommandLine('コマンド c', Meta())]),
            ], 0, 1), '\n'.join([
                'ホスト名: ホスト                                              実行ユーザ: ユーザ',
                '================================================================================',
                '  ~ル2 > タイトル3 > タイトル4 > タイトル5 > タイトル6 > タイトル7 > タイトル8',
                '--------------------------------------------------------------------------------',
                '  [1] | メニュー a', '  [2] | メニュー b', '  [3] | メニュー c',
                '------+-------------------------------------------------------------------------',
                '  [0] | タイトル7 に戻る',
                '================================================================================',
                '番号を入力してください (0-3): '
            ]))

        self.assertEqual(
            t.get_page(['タイトル'], [
                Command('メニュー a', [CommandLine('コマンド a', Meta())]),
                Command('メニュー b', [CommandLine('コマンド b', Meta())]),
                Command('メニュー c', [CommandLine('コマンド c', Meta())]),
                Command('メニュー d', [CommandLine('コマンド d', Meta())]),
                Command('メニュー e', [CommandLine('コマンド e', Meta())]),
                Command('メニュー f', [CommandLine('コマンド f', Meta())]),
                Command('メニュー g', [CommandLine('コマンド g', Meta())]),
                Command('メニュー h', [CommandLine('コマンド h', Meta())]),
                Command('メニュー i', [CommandLine('コマンド i', Meta())]),
            ], 0, 100), '\n'.join([
                'ホスト名: ホスト                                              実行ユーザ: ユーザ',
                '================================================================================',
                '  タイトル',
                '--------------------------------------------------------------------------------',
                '                                  Page 1 / 100                            [N] =>',
                '--------------------------------------------------------------------------------',
                '  [1] | メニュー a', '  [2] | メニュー b', '  [3] | メニュー c',
                '  [4] | メニュー d', '  [5] | メニュー e', '  [6] | メニュー f',
                '  [7] | メニュー g', '  [8] | メニュー h', '  [9] | メニュー i',
                '------+-------------------------------------------------------------------------',
                '  [0] | 終了',
                '================================================================================',
                '番号を入力してください (0-9): '
            ]))

        self.assertEqual(
            t.get_page(['タイトル'], [
                Command('メニュー a', [CommandLine('コマンド a', Meta())]),
                Command('メニュー b', [CommandLine('コマンド b', Meta())]),
                Command('メニュー c', [CommandLine('コマンド c', Meta())]),
                Command('メニュー d', [CommandLine('コマンド d', Meta())]),
                Command('メニュー e', [CommandLine('コマンド e', Meta())]),
                Command('メニュー f', [CommandLine('コマンド f', Meta())]),
                Command('メニュー g', [CommandLine('コマンド g', Meta())]),
                Command('メニュー h', [CommandLine('コマンド h', Meta())]),
                Command('メニュー i', [CommandLine('コマンド i', Meta())]),
            ], 8, 100), '\n'.join([
                'ホスト名: ホスト                                              実行ユーザ: ユーザ',
                '================================================================================',
                '  タイトル',
                '--------------------------------------------------------------------------------',
                '<= [P]                            Page 9 / 100                            [N] =>',
                '--------------------------------------------------------------------------------',
                '  [1] | メニュー a', '  [2] | メニュー b', '  [3] | メニュー c',
                '  [4] | メニュー d', '  [5] | メニュー e', '  [6] | メニュー f',
                '  [7] | メニュー g', '  [8] | メニュー h', '  [9] | メニュー i',
                '------+-------------------------------------------------------------------------',
                '  [0] | 終了',
                '================================================================================',
                '番号を入力してください (0-9): '
            ]))

        self.assertEqual(
            t.get_page(['タイトル'], [
                Command('メニュー a', [CommandLine('コマンド a', Meta())]),
            ], 99, 100), '\n'.join([
                'ホスト名: ホスト                                              実行ユーザ: ユーザ',
                '================================================================================',
                '  タイトル',
                '--------------------------------------------------------------------------------',
                '<= [P]                           Page 100 / 100                                 ',
                '--------------------------------------------------------------------------------',
                '  [1] | メニュー a',
                '------+-------------------------------------------------------------------------',
                '  [0] | 終了',
                '================================================================================',
                '番号を入力してください (0-1): '
            ]))

        # sub menu
        self.assertEqual(
            t.get_page(['メインメニュー', 'タイトル'], [
                Menu('メニュー a',
                     [Command('メニュー b', [CommandLine('コマンド b', Meta())])],
                     Meta()),
                Menu('メニュー c', [
                    Command('メニュー c', [CommandLine('コマンド c', Meta())]),
                    Command('メニュー d', [CommandLine('コマンド d', Meta())]),
                ], Meta()),
            ], 0, 1), '\n'.join([
                'ホスト名: ホスト                                              実行ユーザ: ユーザ',
                '================================================================================',
                '  メインメニュー > タイトル',
                '--------------------------------------------------------------------------------',
                '  [1] | メニュー a へ', '  [2] | メニュー c へ',
                '------+-------------------------------------------------------------------------',
                '  [0] | メインメニュー に戻る',
                '================================================================================',
                '番号を入力してください (0-2): '
            ]))

        # multiple command lines
        self.assertEqual(
            t.get_page(['メインメニュー', 'タイトル'], [
                Command('メニュー a', [CommandLine('コマンド a', Meta())]),
                Command('メニュー b', [
                    CommandLine('コマンド b', Meta()),
                    CommandLine('コマンド b', Meta()),
                ]),
                Menu('メニュー c', [
                    Command('メニュー d', [
                        CommandLine('コマンド d', Meta()),
                        CommandLine('コマンド d', Meta()),
                    ])
                ], Meta()),
            ], 0, 1), '\n'.join([
                'ホスト名: ホスト                                              実行ユーザ: ユーザ',
                '================================================================================',
                '  メインメニュー > タイトル',
                '--------------------------------------------------------------------------------',
                '  [1] | メニュー a', '  [2] | メニュー b', '  [3] | メニュー c へ',
                '------+-------------------------------------------------------------------------',
                '  [0] | メインメニュー に戻る',
                '================================================================================',
                '番号を入力してください (0-3): '
            ]))
Exemplo n.º 14
0
    def test_get_page(self):
        self.maxDiff = None

        t = Terminal({'': []},
                     'host',
                     'user',
                     self.get_exec(),
                     handler=self.handler,
                     encoding='utf-8',
                     lang='C',
                     width=80)
        self.assertEqual(
            t.get_page(['title'], [], 0, 1), '\n'.join([
                'Host: host                                                            User: user',
                '================================================================================',
                '  title',
                '--------------------------------------------------------------------------------',
                '------+-------------------------------------------------------------------------',
                '  [0] | Quit',
                '================================================================================',
                'Press menu number (0-0): '
            ]))

        self.assertEqual(
            t.get_page(['Main Menu', 'title'], [
                Command('menu a', [CommandLine('command a', Meta())]),
                Command('menu b', [CommandLine('command b', Meta())]),
                Command('menu c', [CommandLine('command c', Meta())]),
            ], 0, 1), '\n'.join([
                'Host: host                                                            User: user',
                '================================================================================',
                '  Main Menu > title',
                '--------------------------------------------------------------------------------',
                '  [1] | menu a', '  [2] | menu b', '  [3] | menu c',
                '------+-------------------------------------------------------------------------',
                '  [0] | Return to Main Menu',
                '================================================================================',
                'Press menu number (0-3): '
            ]))

        self.assertEqual(
            t.get_page(['title'], [
                Command('menu a', [CommandLine('command a', Meta())]),
                Command('menu b', [CommandLine('command b', Meta())]),
                Command('menu c', [CommandLine('command c', Meta())]),
                Command('menu d', [CommandLine('command d', Meta())]),
                Command('menu e', [CommandLine('command e', Meta())]),
                Command('menu f', [CommandLine('command f', Meta())]),
                Command('menu g', [CommandLine('command g', Meta())]),
                Command('menu h', [CommandLine('command h', Meta())]),
                Command('menu i', [CommandLine('command i', Meta())]),
            ], 0, 100), '\n'.join([
                'Host: host                                                            User: user',
                '================================================================================',
                '  title',
                '--------------------------------------------------------------------------------',
                '                                  Page 1 / 100                            [N] =>',
                '--------------------------------------------------------------------------------',
                '  [1] | menu a', '  [2] | menu b', '  [3] | menu c',
                '  [4] | menu d', '  [5] | menu e', '  [6] | menu f',
                '  [7] | menu g', '  [8] | menu h', '  [9] | menu i',
                '------+-------------------------------------------------------------------------',
                '  [0] | Quit',
                '================================================================================',
                'Press menu number (0-9): '
            ]))

        self.assertEqual(
            t.get_page(['title'], [
                Command('menu a', [CommandLine('command a', Meta())]),
                Command('menu b', [CommandLine('command b', Meta())]),
                Command('menu c', [CommandLine('command c', Meta())]),
                Command('menu d', [CommandLine('command d', Meta())]),
                Command('menu e', [CommandLine('command e', Meta())]),
                Command('menu f', [CommandLine('command f', Meta())]),
                Command('menu g', [CommandLine('command g', Meta())]),
                Command('menu h', [CommandLine('command h', Meta())]),
                Command('menu i', [CommandLine('command i', Meta())]),
            ], 8, 100), '\n'.join([
                'Host: host                                                            User: user',
                '================================================================================',
                '  title',
                '--------------------------------------------------------------------------------',
                '<= [P]                            Page 9 / 100                            [N] =>',
                '--------------------------------------------------------------------------------',
                '  [1] | menu a', '  [2] | menu b', '  [3] | menu c',
                '  [4] | menu d', '  [5] | menu e', '  [6] | menu f',
                '  [7] | menu g', '  [8] | menu h', '  [9] | menu i',
                '------+-------------------------------------------------------------------------',
                '  [0] | Quit',
                '================================================================================',
                'Press menu number (0-9): '
            ]))

        self.assertEqual(
            t.get_page(['title'], [
                Command('menu a', [CommandLine('command a', Meta())]),
            ], 99, 100), '\n'.join([
                'Host: host                                                            User: user',
                '================================================================================',
                '  title',
                '--------------------------------------------------------------------------------',
                '<= [P]                           Page 100 / 100                                 ',
                '--------------------------------------------------------------------------------',
                '  [1] | menu a',
                '------+-------------------------------------------------------------------------',
                '  [0] | Quit',
                '================================================================================',
                'Press menu number (0-1): '
            ]))

        # sub menu
        self.assertEqual(
            t.get_page(['Main Menu', 'title'], [
                Menu('menu a',
                     [Command('menu b', [CommandLine('command b', Meta())])],
                     Meta()),
                Menu('menu c', [
                    Command('menu c', [CommandLine('command c', Meta())]),
                    Command('menu d', [CommandLine('command d', Meta())]),
                ], Meta()),
            ], 0, 1), '\n'.join([
                'Host: host                                                            User: user',
                '================================================================================',
                '  Main Menu > title',
                '--------------------------------------------------------------------------------',
                '  [1] | Go to menu a', '  [2] | Go to menu c',
                '------+-------------------------------------------------------------------------',
                '  [0] | Return to Main Menu',
                '================================================================================',
                'Press menu number (0-2): '
            ]))

        # multiple command lines
        self.assertEqual(
            t.get_page(['Main Menu', 'title'], [
                Command('menu a', [CommandLine('command a', Meta())]),
                Command('menu b', [
                    CommandLine('command b', Meta()),
                    CommandLine('command b', Meta()),
                ]),
                Menu('menu c', [
                    Command('menu d', [
                        CommandLine('command d', Meta()),
                        CommandLine('command d', Meta()),
                    ])
                ], Meta()),
            ], 0, 1), '\n'.join([
                'Host: host                                                            User: user',
                '================================================================================',
                '  Main Menu > title',
                '--------------------------------------------------------------------------------',
                '  [1] | menu a', '  [2] | menu b', '  [3] | Go to menu c',
                '------+-------------------------------------------------------------------------',
                '  [0] | Return to Main Menu',
                '================================================================================',
                'Press menu number (0-3): '
            ]))
Exemplo n.º 15
0
    def test_load_config_dynamic_cache(self):
        self.maxDiff = None

        meta = Meta(
            to_unicode(
                os.path.join(os.path.abspath(os.path.curdir), 'tests',
                             'resources')))
        path = os.path.join(os.path.abspath(os.path.curdir), 'tests',
                            'resources', 'with_dynamic_cache.yml')

        cache_dir = os.path.join(os.path.curdir, 'tests', 'work')
        cache_path = os.path.join(cache_dir, '57',
                                  '8f2d1550c1fad02f46409db2b538c9')

        def clear_files():
            if os.path.exists(cache_path):
                os.remove(cache_path)
            if os.path.exists(os.path.dirname(cache_path)):
                os.removedirs(os.path.dirname(cache_path))
            if os.path.exists(cache_dir):
                os.removedirs(cache_dir)

        expect = '\n'.join([
            'Reading file: %s' % path,
            """Executing: echo '{"Sub Menu": [{"Menu 2": "echo 2"}, {"Menu 3": "echo 3"}]}'""",
            'Writing eval cache: %s' % cache_path,
            'Reading file: %s' % path,
            'Reading eval cache: %s' % cache_path, ''
        ])

        clear_files()

        try:
            with self.withAssertOutput(expect, '') as (out, err):
                self.assertEqual(
                    Setting(
                        config_path=self._testfile('with_dynamic_cache.yml'),
                        encoding='utf-8',
                        stdout=out,
                        stderr=err,
                        cache_dir=cache_dir).load_config().root_menu,
                    Menu('Main Menu', [
                        Command('Menu 1', [CommandLine('echo 1', meta)]),
                        Menu('Sub Menu', [
                            Command('Menu 2', [CommandLine('echo 2', meta)]),
                            Command('Menu 3', [CommandLine('echo 3', meta)]),
                        ], meta)
                    ], meta))
                # read twice to use cache
                self.assertEqual(
                    Setting(
                        config_path=self._testfile('with_dynamic_cache.yml'),
                        encoding='utf-8',
                        stdout=out,
                        stderr=err,
                        cache_dir=cache_dir).load_config().root_menu,
                    Menu('Main Menu', [
                        Command('Menu 1', [CommandLine('echo 1', meta)]),
                        Menu('Sub Menu', [
                            Command('Menu 2', [CommandLine('echo 2', meta)]),
                            Command('Menu 3', [CommandLine('echo 3', meta)]),
                        ], meta)
                    ], meta))
        finally:
            clear_files()