|
| 1 | +# Copyright (c) 2021, Arm Limited and affiliates. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +import shutil |
| 16 | +import mock |
| 17 | +import os |
| 18 | +import unittest |
| 19 | +from tempfile import mkdtemp |
| 20 | + |
| 21 | +from mbed_os_tools.test.host_tests_runner.mbed_base import Mbed |
| 22 | + |
| 23 | +class TemporaryDirectory(object): |
| 24 | + def __init__(self): |
| 25 | + self.fname = "tempdir" |
| 26 | + |
| 27 | + def __enter__(self): |
| 28 | + self.fname = mkdtemp() |
| 29 | + return self.fname |
| 30 | + |
| 31 | + def __exit__(self, *args, **kwargs): |
| 32 | + shutil.rmtree(self.fname) |
| 33 | + |
| 34 | +@mock.patch("mbed_os_tools.test.host_tests_runner.mbed_base.ht_plugins") |
| 35 | +@mock.patch("mbed_os_tools.test.host_tests_runner.mbed_base.detect") |
| 36 | +class TestMbed(unittest.TestCase): |
| 37 | + def test_skips_discover_mbed_if_non_mbed_copy_method_used( |
| 38 | + self, mock_detect, mock_ht_plugins |
| 39 | + ): |
| 40 | + with TemporaryDirectory() as tmpdir: |
| 41 | + image_path = os.path.join(tmpdir, "test.elf") |
| 42 | + with open(image_path, "w") as f: |
| 43 | + f.write("1234") |
| 44 | + |
| 45 | + options = mock.Mock( |
| 46 | + copy_method="pyocd", |
| 47 | + image_path=image_path, |
| 48 | + disk=None, |
| 49 | + port="port", |
| 50 | + micro="mcu", |
| 51 | + target_id="BK99", |
| 52 | + polling_timeout=5, |
| 53 | + program_cycle_s=None, |
| 54 | + json_test_configuration=None, |
| 55 | + format="blah", |
| 56 | + ) |
| 57 | + |
| 58 | + mbed = Mbed(options) |
| 59 | + mbed.copy_image() |
| 60 | + |
| 61 | + mock_detect.create().list_mbeds.assert_not_called() |
| 62 | + mock_ht_plugins.call_plugin.assert_called_once_with( |
| 63 | + "CopyMethod", |
| 64 | + options.copy_method, |
| 65 | + image_path=options.image_path, |
| 66 | + mcu=options.micro, |
| 67 | + serial=options.port, |
| 68 | + destination_disk=options.disk, |
| 69 | + target_id=options.target_id, |
| 70 | + pooling_timeout=options.polling_timeout, |
| 71 | + format=options.format, |
| 72 | + ) |
| 73 | + |
| 74 | + def test_discovers_mbed_if_mbed_copy_method_used( |
| 75 | + self, mock_detect, mock_ht_plugins |
| 76 | + ): |
| 77 | + with TemporaryDirectory() as tmpdir: |
| 78 | + image_path = os.path.join(tmpdir, "test.elf") |
| 79 | + with open(image_path, "w") as f: |
| 80 | + f.write("1234") |
| 81 | + options = mock.Mock( |
| 82 | + copy_method="shell", |
| 83 | + image_path=image_path, |
| 84 | + disk=None, |
| 85 | + port="port", |
| 86 | + micro="mcu", |
| 87 | + target_id="BK99", |
| 88 | + polling_timeout=5, |
| 89 | + program_cycle_s=None, |
| 90 | + json_test_configuration=None, |
| 91 | + format="blah", |
| 92 | + ) |
| 93 | + |
| 94 | + mbed = Mbed(options) |
| 95 | + mbed.copy_image() |
| 96 | + |
| 97 | + mock_detect.create().list_mbeds.assert_called() |
| 98 | + mock_ht_plugins.call_plugin.assert_called_once_with( |
| 99 | + "CopyMethod", |
| 100 | + options.copy_method, |
| 101 | + image_path=options.image_path, |
| 102 | + mcu=options.micro, |
| 103 | + serial=options.port, |
| 104 | + destination_disk=options.disk, |
| 105 | + target_id=options.target_id, |
| 106 | + pooling_timeout=options.polling_timeout, |
| 107 | + format=options.format, |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + unittest.main() |
0 commit comments