Example #1
0
    def test_line_wrapping(self, print_mock, termsize_mock, _, __, ___, ____):
        """Test how the `output` method handles wrapping lines that are too long"""
        output = Output()

        # We only need a column value for the terminal size
        termsize_tuple = namedtuple('termsize_tuple', 'columns')
        termsize_mock.return_value = termsize_tuple(13)

        output._results = [  # pylint: disable=protected-access
            'short',  # no truncation - too short
            'looooooong',  # truncation - too long
            'adjusted',  # no truncation - exactly the right width
            '\x1b[0;31mshort\x1b[0m',  # no truncation - too short
            '\x1b[0;31mlooooooong\x1b[0m'  # truncation - too long, long word truncated
        ]
        output.output()

        print_mock.assert_called_with("""\
W    short
O    \x1b[0m...
O    adjusted
O    \x1b[0;31mshort\x1b[0m
O    \x1b[0;31m\x1b[0m...\x1b[0m\
""")
        # Check that `print` has been called only once.
        self.assertTrue(print_mock.assert_called_once)
Example #2
0
    def test_format_to_json(self, print_mock, _, __):
        """Test how the `output` method handles JSON preferred formatting of entries"""
        output = Output(format_to_json=True)
        # We can't set the `name` attribute of a mock on its creation,
        # so this is a little bit messy...
        mocked_entries = [
            Mock(value='test'),
            Mock(value=0xDEAD)
        ]
        mocked_entries[0].name = 'test'
        mocked_entries[1].name = 'name'

        output._entries = mocked_entries  # pylint: disable=protected-access
        output.output()

        # Check that `print` output is properly formatted as JSON, with expected results.
        output_json_data = json.loads(print_mock.call_args[0][0])['data']
        self.assertEqual(
            output_json_data['test'],
            'test'
        )
        self.assertEqual(
            output_json_data['name'],
            0xDEAD
        )
Example #3
0
    def test_centered_output(self, _):
        """Test how the `output` method handle centering operations"""
        output = Output()

        # Let's manually set the distribution for the test case...
        output.distribution = Distributions.DEBIAN

        # # ODD ENTRIES NUMBER # #
        output.results = ['1', '2', '3']
        # Let's run the algorithm !
        output.output()
        self.assertListEqual(output.results, [
            '', '', '', '', '', '', '', '1', '2', '3', '', '', '', '', '', '',
            '', ''
        ])

        # # EVEN ENTRIES NUMBER # #
        output.results = ['1', '2', '3', '4']
        output.output()
        self.assertListEqual(output.results, [
            '', '', '', '', '', '', '', '1', '2', '3', '4', '', '', '', '', '',
            '', ''
        ])

        # # FULL ENTRIES # #
        output.results = [
            '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
            '13', '14', '15', '16', '17', '18'
        ]
        output.output()
        self.assertListEqual(output.results, [
            '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
            '13', '14', '15', '16', '17', '18'
        ])

        # # NO ENTRY # #
        output.results = []
        output.output()
        self.assertListEqual(output.results, [
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
            '',
        ])
Example #4
0
def main():
    """Simple entry point"""
    args = args_parsing()

    # `Processes` is a singleton, let's populate the internal list here.
    Processes()

    # `Configuration` is a singleton, let's populate the internal object here.
    configuration = Configuration(config_path=args.config_path)

    # From configuration, gather the entries user-enabled.
    enabled_entries = [
        (entry.value, entry.name) for entry in Entries
        if configuration.get('entries', {}).get(entry.name, True)
    ]

    output = Output(preferred_distribution=args.distribution,
                    format_to_json=args.json)

    # We will map this function onto our enabled entries to instantiate them.
    def _entry_instantiator(entry_tuple):
        return entry_tuple[0](name=entry_tuple[1])

    # Let's use a context manager stack to manage conditional use of `TheadPoolExecutor`.
    with ExitStack() as cm_stack:
        if not configuration.get('parallel_loading'):
            mapper = map
        else:
            # Instantiate a threads pool to load our enabled entries in parallel.
            # We use threads (and not processes) since most work done by our entries is IO-bound.
            # `max_workers` is manually computed to mimic Python 3.8+ behaviour, but for our needs.
            #   See <https://github.com/python/cpython/pull/13618>.
            executor = cm_stack.enter_context(
                ThreadPoolExecutor(max_workers=min(
                    len(enabled_entries) or 1, (os.cpu_count() or 1) + 4)))
            mapper = executor.map

        for entry_instance in mapper(_entry_instantiator, enabled_entries):
            output.add_entry(entry_instance)

    output.output()

    # Has the screenshot flag been specified ?
    if args.screenshot is not None:
        # If so, but still _falsy_, pass `None` as no output file has been specified by the user.
        take_screenshot((args.screenshot or None))
Example #5
0
def main():
    """Simple entry point"""
    parser = argparse.ArgumentParser(prog='archey')
    parser.add_argument('-v',
                        '--version',
                        action='version',
                        version=__version__)
    parser.parse_args()

    # `Processes` is a singleton, let's populate the internal list here.
    Processes()

    # `Configuration` is a singleton, let's populate the internal object here.
    configuration = Configuration()

    output = Output()
    for entry in Entries:
        if configuration.get('entries', {}).get(entry.name, True):
            output.append(entry.name, entry.value().value)

    output.output()
Example #6
0
    def test_centered_output(self, print_mock, _, __, ___):
        """Test how the `output` method handles centering operations"""
        output = Output()

        with self.subTest(
                'Odd number of entries (entries smaller than logo).'):
            output._results = [  # pylint: disable=protected-access
                '1', '2', '3'
            ]
            output.output()
            self.assertListEqual(
                output._results,  # pylint: disable=protected-access
                [
                    '', '', '', '', '', '', '', '1', '2', '3', '', '', '', '',
                    '', '', '', ''
                ])

        with self.subTest('Odd number of entries (entries bigger than logo).'):
            output._results = [  # pylint: disable=protected-access
                '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
                '13', '14', '15', '16', '17', '18', '19', '20', '21'
            ]
            output.output()
            print_mock.assert_called_with("""\
FAKE_COLOR    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
FAKE_COLOR    20
FAKE_COLOR    21\x1b[0m\
""")

        output = Output()

        with self.subTest(
                'Even number of entries (entries smaller than logo).'):
            output._results = [  # pylint: disable=protected-access
                '1', '2', '3', '4'
            ]
            output.output()
            self.assertListEqual(
                output._results,  # pylint: disable=protected-access
                [
                    '', '', '', '', '', '', '', '1', '2', '3', '4', '', '', '',
                    '', '', '', ''
                ])

        with self.subTest(
                'Even number of entries (entries bigger than logo).'):
            output._results = [  # pylint: disable=protected-access
                '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
                '13', '14', '15', '16', '17', '18', '19', '20', '21', '22'
            ]
            output.output()
            print_mock.assert_called_with("""\
FAKE_COLOR    1
FAKE_COLOR    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
FAKE_COLOR    21
FAKE_COLOR    22\x1b[0m\
""")

        output = Output()

        with self.subTest('Full entries.'):
            output._results = [  # pylint: disable=protected-access
                '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
                '13', '14', '15', '16', '17', '18'
            ]
            output.output()
            self.assertListEqual(
                output._results,  # pylint: disable=protected-access
                [
                    '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11',
                    '12', '13', '14', '15', '16', '17', '18'
                ])

        output = Output()

        with self.subTest('No entry.'):
            output._results = []  # pylint: disable=protected-access
            output.output()
            self.assertListEqual(
                output._results,  # pylint: disable=protected-access
                [
                    '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
                    '', '', ''
                ])