File: test_cmdline.py

package info (click to toggle)
luma.core 2.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,040 kB
  • sloc: python: 6,186; makefile: 204
file content (470 lines) | stat: -rw-r--r-- 14,080 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017-2021 Richard Hull and contributors
# See LICENSE.rst for details.

"""
Tests for the :py:mod:`luma.core.cmdline` module.
"""

import pytest
import errno
from unittest.mock import patch, Mock

from luma.core import cmdline, error
from luma.core.interface.serial import __all__ as serial_iface_types
from luma.core.interface.parallel import __all__ as parallel_iface_types

from helpers import (get_reference_file, i2c_error, rpi_gpio_missing,
    spidev_missing, skip_unsupported_platform)


test_config_file = get_reference_file('config-test.txt')


class test_spi_opts(object):
    spi_port = 0
    spi_mode = 0
    spi_device = 0
    spi_bus_speed = 8000000
    spi_transfer_size = 4096
    gpio_data_command = 24
    gpio_reset = 25
    gpio_backlight = 18
    gpio_reset_hold_time = 0
    gpio_reset_release_time = 0

    interface = 'spi'
    framebuffer = 'diff_to_previous'
    num_segments = 25
    debug = False
    framebuffer_device = None


def test_get_interface_types():
    """
    Enumerate interface types.
    """
    assert cmdline.get_interface_types() == serial_iface_types + parallel_iface_types


def test_ensure_cmdline_opt_contains_all_interfaces():
    """
    Checks that the cmdline make_interface factory contains initializers for all interface classes
    """
    class opts:
        pass

    factory = cmdline.make_interface(opts)
    for interface in cmdline.get_interface_types():
        assert hasattr(factory, interface)


def test_get_display_types():
    """
    Enumerate display types.
    """
    assert list(cmdline.get_display_types().keys()) == \
        cmdline.get_supported_libraries()


def test_get_choices_unknown_module():
    """
    :py:func:`luma.core.cmdline.get_choices` returns an empty list when
    trying to inspect an unknown module.
    """
    result = cmdline.get_choices('foo')
    assert result == []


def test_get_library_version():
    """
    :py:func:`luma.core.cmdline.get_library_version` returns the version number
    for the specified library name.
    """
    lib_name = 'hotscreenz'
    lib_version = '0.1.2'

    # set version nr for fake luma library
    luma_fake_lib = Mock()
    luma_fake_lib.__version__ = lib_version

    # version is found
    with patch.dict('sys.modules', {'luma.' + lib_name: luma_fake_lib}):
        assert cmdline.get_library_version(lib_name) == lib_version

    # no version for module without __version__ attribute
    lib_name = 'no_version'
    luma_without_version_lib = Mock()
    with patch.dict('sys.modules', {'luma.' + lib_name: luma_without_version_lib}):
        assert cmdline.get_library_version(lib_name) is None

    # no version for non-existing module
    assert cmdline.get_library_version('foo') is None


def test_get_library_for_display_type():
    """
    :py:func:`luma.core.cmdline.get_library_for_display_type` returns the
    the library name for a particular display.
    """
    display_type = 'coolscreen'
    lib_name = 'screens'

    with patch('luma.core.cmdline.get_display_types') as mocka:
        mocka.return_value = {
            lib_name: [display_type, 'bar'],
            'emulator': ['x', 'y']
        }
        assert cmdline.get_library_for_display_type(display_type) == lib_name


def test_load_config_file_parse():
    """
    :py:func:`luma.core.cmdline.load_config` parses a text file and returns a
    list of arguments.
    """
    result = cmdline.load_config(test_config_file)
    assert result == [
        '--display=capture',
        '--width=800',
        '--height=8600',
        '--spi-bus-speed=16000000'
    ]


def test_create_parser():
    """
    :py:func:`luma.core.cmdline.create_parser` returns an argument parser
    instance.
    """
    with patch.dict('sys.modules', **{
            'luma.emulator': Mock(),
            'luma.emulator.render': Mock(),
        }):
        with patch('luma.core.cmdline.get_display_types') as mocka:
            mocka.return_value = {
                'foo': ['a', 'b'],
                'bar': ['c', 'd'],
                'emulator': ['e', 'f']
            }
            parser = cmdline.create_parser(description='test')
            args = parser.parse_args(['-f', test_config_file])
            assert args.config == test_config_file


def test_make_interface_noop():
    """
    :py:func:`luma.core.cmdline.make_interface.noop` returns an ``noop` instance.
    """
    class opts:
        interface = 'noop'

    factory = cmdline.make_interface(opts)
    assert 'luma.core.interface.serial.noop' in repr(factory.noop())


def test_make_interface_i2c():
    """
    :py:func:`luma.core.cmdline.make_interface.i2c` returns an I2C instance.
    """
    class opts:
        i2c_port = 200
        i2c_address = 0x710

    path_name = f'/dev/i2c-{opts.i2c_port}'
    fake_open = i2c_error(path_name, errno.ENOENT)
    factory = cmdline.make_interface(opts)

    with patch('os.open', fake_open):
        with pytest.raises(error.DeviceNotFoundError):
            factory.i2c()


def test_make_interface_spi():
    """
    :py:func:`luma.core.cmdline.make_interface.spi` returns an SPI instance.
    """
    try:
        factory = cmdline.make_interface(test_spi_opts)
        assert 'luma.core.interface.serial.spi' in repr(factory.spi())
    except ImportError:
        # non-rpi platform, e.g. macos
        pytest.skip(rpi_gpio_missing)
    except error.UnsupportedPlatform as e:
        # non-rpi platform, e.g. ubuntu 64-bit
        skip_unsupported_platform(e)


def test_make_interface_gpio_cs_spi():
    """
    :py:func:`luma.core.cmdline.make_interface.gpio_cs_spi` returns an gpio_cs_spi instance.
    """
    class opts(test_spi_opts):
        interface = 'gpio_cs_spi'
        spi_cs_high = True
        gpio_chip_select = 4

    try:
        factory = cmdline.make_interface(opts)
        assert 'luma.core.interface.serial.spi' in repr(factory.gpio_cs_spi())
    except ImportError:
        # non-rpi platform, e.g. macos
        pytest.skip(rpi_gpio_missing)
    except error.UnsupportedPlatform as e:
        # non-rpi platform, e.g. ubuntu 64-bit
        skip_unsupported_platform(e)


def test_make_interface_spi_alt_gpio():
    """
    :py:func:`luma.core.cmdline.make_interface.spi` returns an SPI instance
    when using an alternative GPIO implementation.
    """
    class opts(test_spi_opts):
        gpio = 'fake_gpio'

    with patch.dict('sys.modules', **{
            'fake_gpio': Mock(unsafe=True)
        }):
        try:
            factory = cmdline.make_interface(opts)
            assert 'luma.core.interface.serial.spi' in repr(factory.spi())
        except ImportError:
            pytest.skip(spidev_missing)
        except error.DeviceNotFoundError as e:
            # non-rpi platform, e.g. ubuntu 64-bit
            skip_unsupported_platform(e)


def test_make_interface_bitbang():
    """
    :py:func:`luma.core.cmdline.make_interface.bitbang` returns an BitBang instance.
    """
    try:
        factory = cmdline.make_interface(test_spi_opts)
        assert 'luma.core.interface.serial.bitbang' in repr(factory.bitbang())
    except ImportError:
        # non-rpi platform, e.g. macos
        pytest.skip(rpi_gpio_missing)
    except error.UnsupportedPlatform as e:
        # non-rpi platform, e.g. ubuntu 64-bit
        skip_unsupported_platform(e)


def test_make_interface_pcf8574():
    """
    :py:func:`luma.core.cmdline.make_interface.pcf8574` returns an pcf8574 instance.
    """
    class opts:
        i2c_port = 200
        i2c_address = 0x710

    path_name = f'/dev/i2c-{opts.i2c_port}'
    fake_open = i2c_error(path_name, errno.ENOENT)
    factory = cmdline.make_interface(opts)

    with patch('os.open', fake_open):
        with pytest.raises(error.DeviceNotFoundError):
            factory.pcf8574()


def test_make_interface_bitbang_6800():
    """
    :py:func:`luma.core.cmdline.make_interface.bitbang_6800` returns a Bitbang-6800 instance.
    """
    class opts:
        pass

    try:
        factory = cmdline.make_interface(opts)
        assert 'luma.core.interface.parallel.bitbang_6800' in repr(factory.bitbang_6800())
    except ImportError:
        # non-rpi platform, e.g. macos
        pytest.skip(rpi_gpio_missing)
    except error.UnsupportedPlatform as e:
        # non-rpi platform, e.g. ubuntu 64-bit
        skip_unsupported_platform(e)


def test_make_interface_bitbang_6800_alt_gpio():
    """
    :py:func:`luma.core.cmdline.make_interface.bitbang_6800` returns an Bitbang-6800 instance
    when using an alternative GPIO implementation.
    """
    class opts():
        gpio = 'fake_gpio'

    with patch.dict('sys.modules', **{
            'fake_gpio': Mock(unsafe=True)
        }):
        try:
            factory = cmdline.make_interface(opts)
            assert 'luma.core.interface.parallel.bitbang_6800' in repr(factory.bitbang_6800())
        except ImportError:
            pytest.skip(spidev_missing)
        except error.DeviceNotFoundError as e:
            # non-rpi platform, e.g. ubuntu 64-bit
            skip_unsupported_platform(e)


def test_create_device():
    """
    :py:func:`luma.core.cmdline.create_device` returns ``None`` for unknown
    displays.
    """
    class args:
        display = 'foo'
    assert cmdline.create_device(args) is None


def test_create_device_oled():
    """
    :py:func:`luma.core.cmdline.create_device` supports OLED displays.
    """
    display_name = 'oled1234'
    display_types = {'oled': [display_name]}

    class args(test_spi_opts):
        display = display_name

    module_mock = Mock()
    module_mock.oled.device.oled1234.return_value = display_name
    with patch.dict('sys.modules', **{
            # mock luma.oled package
            'luma': module_mock,
            'luma.oled': module_mock,
            'luma.oled.device': module_mock
        }):
        try:
            device = cmdline.create_device(args, display_types=display_types)
            assert device == display_name
        except ImportError:
            pytest.skip(rpi_gpio_missing)
        except error.UnsupportedPlatform as e:
            # non-rpi platform
            skip_unsupported_platform(e)


def test_create_device_lcd():
    """
    :py:func:`luma.core.cmdline.create_device` supports LCD displays.
    """
    display_name = 'lcd1234'
    display_types = {'lcd': [display_name]}

    class args(test_spi_opts):
        display = display_name
        gpio = 'fake_gpio'
        backlight_active = 'low'

    module_mock = Mock()
    module_mock.lcd.device.lcd1234.return_value = display_name
    with patch.dict('sys.modules', **{
            # mock spidev and luma.lcd packages
            'fake_gpio': module_mock,
            'spidev': module_mock,
            'luma': module_mock,
            'luma.lcd': module_mock,
            'luma.lcd.aux': module_mock,
            'luma.lcd.device': module_mock
        }):
        device = cmdline.create_device(args, display_types=display_types)
        assert device == display_name


def test_create_device_led_matrix():
    """
    :py:func:`luma.core.cmdline.create_device` supports LED matrix displays.
    """
    display_name = 'matrix1234'
    display_types = {'led_matrix': [display_name]}

    class args(test_spi_opts):
        display = display_name

    module_mock = Mock()
    module_mock.led_matrix.device.matrix1234.return_value = display_name
    with patch.dict('sys.modules', **{
            # mock spidev and luma.led_matrix packages
            'spidev': module_mock,
            'luma': module_mock,
            'luma.led_matrix': module_mock,
            'luma.led_matrix.device': module_mock
        }):
        device = cmdline.create_device(args, display_types=display_types)
        assert device == display_name


def test_create_device_emulator():
    """
    :py:func:`luma.core.cmdline.create_device` supports emulators.
    """
    display_name = 'emulator1234'
    display_types = {'emulator': [display_name]}

    class args(test_spi_opts):
        display = display_name

    module_mock = Mock()
    module_mock.emulator.device.emulator1234.return_value = display_name
    with patch.dict('sys.modules', **{
            # mock spidev and luma.emulator packages
            'spidev': module_mock,
            'luma': module_mock,
            'luma.emulator': module_mock,
            'luma.emulator.device': module_mock
        }):
        device = cmdline.create_device(args, display_types=display_types)
        assert device == display_name


def test_create_device_core():
    """
    :py:func:`luma.core.cmdline.create_device` supports code devices.
    """
    display_name = 'coredevice1234'
    display_types = {'core': [display_name]}

    class args(test_spi_opts):
        display = display_name

    module_mock = Mock()
    module_mock.core.device.coredevice1234.return_value = display_name
    with patch.dict('sys.modules', **{
            # mock luma.core package
            'luma': module_mock,
            'luma.core': module_mock,
            'luma.core.device': module_mock
        }):
        device = cmdline.create_device(args, display_types=display_types)
        assert device == display_name


@patch('pyftdi.spi.SpiController')
def test_make_interface_ftdi_spi(mock_controller):
    """
    :py:func:`luma.core.cmdline.make_interface.ftdi_spi` returns an SPI instance.
    """
    class opts(test_spi_opts):
        ftdi_device = 'ftdi://dummy'
        gpio_data_command = 5
        gpio_reset = 6
        gpio_backlight = 7

    factory = cmdline.make_interface(opts)
    assert 'luma.core.interface.serial.spi' in repr(factory.ftdi_spi())


@patch('pyftdi.i2c.I2cController')
def test_make_interface_ftdi_i2c(mock_controller):
    """
    :py:func:`luma.core.cmdline.make_interface.ftdi_i2c` returns an I2C instance.
    """
    class opts:
        ftdi_device = 'ftdi://dummy'
        i2c_port = 200
        i2c_address = 0x710

    factory = cmdline.make_interface(opts)
    assert 'luma.core.interface.serial.i2c' in repr(factory.ftdi_i2c())