1# Copyright (c) 2018 Foundries.io
2# Copyright (c) 2019 Nordic Semiconductor ASA.
3#
4# SPDX-License-Identifier: Apache-2.0
5
6import argparse
7import platform
8from unittest.mock import patch, call
9
10import pytest
11
12from runners.dediprog import DediProgBinaryRunner
13from conftest import RC_KERNEL_BIN
14
15DPCMD_EXE = 'dpcmd.exe' if platform.system() == 'Windows' else 'dpcmd'
16
17EXPECTED_COMMAND = {
18    (RC_KERNEL_BIN, None):
19    [DPCMD_EXE,
20     '--auto', RC_KERNEL_BIN,
21     '-x', 'ff',
22     '--silent', '--verify'],
23
24
25    (RC_KERNEL_BIN, '0'):
26    [DPCMD_EXE,
27     '--auto', RC_KERNEL_BIN, '--vcc', '0',
28     '-x', 'ff',
29     '--silent', '--verify'],
30
31    (RC_KERNEL_BIN, '1'):
32    [DPCMD_EXE,
33     '--auto', RC_KERNEL_BIN, '--vcc', '1',
34     '-x', 'ff',
35     '--silent', '--verify'],
36}
37
38def require_patch(program):
39    assert program in [DPCMD_EXE]
40
41def id_fn(tc):
42    return 'spi_image={},vcc={}'.format(*tc)
43
44@pytest.mark.parametrize('tc', [
45    (RC_KERNEL_BIN, None),
46    (RC_KERNEL_BIN, '0'),
47    (RC_KERNEL_BIN, '1'),
48], ids=id_fn)
49@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
50@patch('runners.core.ZephyrBinaryRunner.check_call')
51def test_dediprog_init(cc, req, tc, runner_config):
52    '''Test commands using a runner created by constructor.'''
53    spi_image, vcc = tc
54    runner = DediProgBinaryRunner(runner_config, spi_image=spi_image,
55                                  vcc=vcc, retries=0)
56    runner.run('flash')
57    assert cc.call_args_list == [call(EXPECTED_COMMAND[tc])]
58
59@pytest.mark.parametrize('tc', [
60    (RC_KERNEL_BIN, None),
61    (RC_KERNEL_BIN, '0'),
62    (RC_KERNEL_BIN, '1'),
63], ids=id_fn)
64@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
65@patch('runners.core.ZephyrBinaryRunner.check_call')
66def test_dediprog_create(cc, req, tc, runner_config):
67    '''Test commands using a runner created from command line parameters.'''
68    spi_image, vcc = tc
69    args = ['--spi-image', spi_image, '--retries', '0']
70    if vcc:
71        args.extend(['--vcc', vcc])
72    parser = argparse.ArgumentParser(allow_abbrev=False)
73    DediProgBinaryRunner.add_parser(parser)
74    arg_namespace = parser.parse_args(args)
75    runner = DediProgBinaryRunner.create(runner_config, arg_namespace)
76    runner.run('flash')
77    assert cc.call_args_list == [call(EXPECTED_COMMAND[tc])]
78