BA
/
BaBoRo_test2
Backup 1
mbed-os/tools/test/targets/target_test.py@0:02dd72d1d465, 2018-04-24 (annotated)
- Committer:
- borlanic
- Date:
- Tue Apr 24 11:45:18 2018 +0000
- Revision:
- 0:02dd72d1d465
BaBoRo_test2 - backup 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:02dd72d1d465 | 1 | #!/usr/bin/env python |
borlanic | 0:02dd72d1d465 | 2 | """ |
borlanic | 0:02dd72d1d465 | 3 | mbed |
borlanic | 0:02dd72d1d465 | 4 | Copyright (c) 2017-2017 ARM Limited |
borlanic | 0:02dd72d1d465 | 5 | |
borlanic | 0:02dd72d1d465 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:02dd72d1d465 | 7 | you may not use this file except in compliance with the License. |
borlanic | 0:02dd72d1d465 | 8 | You may obtain a copy of the License at |
borlanic | 0:02dd72d1d465 | 9 | |
borlanic | 0:02dd72d1d465 | 10 | http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:02dd72d1d465 | 11 | |
borlanic | 0:02dd72d1d465 | 12 | Unless required by applicable law or agreed to in writing, software |
borlanic | 0:02dd72d1d465 | 13 | distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:02dd72d1d465 | 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:02dd72d1d465 | 15 | See the License for the specific language governing permissions and |
borlanic | 0:02dd72d1d465 | 16 | limitations under the License. |
borlanic | 0:02dd72d1d465 | 17 | """ |
borlanic | 0:02dd72d1d465 | 18 | import os |
borlanic | 0:02dd72d1d465 | 19 | import sys |
borlanic | 0:02dd72d1d465 | 20 | import shutil |
borlanic | 0:02dd72d1d465 | 21 | import tempfile |
borlanic | 0:02dd72d1d465 | 22 | from os.path import join, abspath, dirname |
borlanic | 0:02dd72d1d465 | 23 | from contextlib import contextmanager |
borlanic | 0:02dd72d1d465 | 24 | import pytest |
borlanic | 0:02dd72d1d465 | 25 | |
borlanic | 0:02dd72d1d465 | 26 | from tools.targets import TARGETS, TARGET_MAP, Target, update_target_data |
borlanic | 0:02dd72d1d465 | 27 | from tools.arm_pack_manager import Cache |
borlanic | 0:02dd72d1d465 | 28 | |
borlanic | 0:02dd72d1d465 | 29 | |
borlanic | 0:02dd72d1d465 | 30 | def test_device_name(): |
borlanic | 0:02dd72d1d465 | 31 | """Assert device name is in a pack""" |
borlanic | 0:02dd72d1d465 | 32 | cache = Cache(True, True) |
borlanic | 0:02dd72d1d465 | 33 | named_targets = (target for target in TARGETS if |
borlanic | 0:02dd72d1d465 | 34 | hasattr(target, "device_name")) |
borlanic | 0:02dd72d1d465 | 35 | for target in named_targets: |
borlanic | 0:02dd72d1d465 | 36 | assert target.device_name in cache.index,\ |
borlanic | 0:02dd72d1d465 | 37 | ("Target %s contains invalid device_name %s" % |
borlanic | 0:02dd72d1d465 | 38 | (target.name, target.device_name)) |
borlanic | 0:02dd72d1d465 | 39 | |
borlanic | 0:02dd72d1d465 | 40 | @contextmanager |
borlanic | 0:02dd72d1d465 | 41 | def temp_target_file(extra_target, json_filename='custom_targets.json'): |
borlanic | 0:02dd72d1d465 | 42 | """Create an extra targets temp file in a context manager |
borlanic | 0:02dd72d1d465 | 43 | |
borlanic | 0:02dd72d1d465 | 44 | :param extra_target: the contents of the extra targets temp file |
borlanic | 0:02dd72d1d465 | 45 | """ |
borlanic | 0:02dd72d1d465 | 46 | tempdir = tempfile.mkdtemp() |
borlanic | 0:02dd72d1d465 | 47 | try: |
borlanic | 0:02dd72d1d465 | 48 | targetfile = os.path.join(tempdir, json_filename) |
borlanic | 0:02dd72d1d465 | 49 | with open(targetfile, 'w') as f: |
borlanic | 0:02dd72d1d465 | 50 | f.write(extra_target) |
borlanic | 0:02dd72d1d465 | 51 | yield tempdir |
borlanic | 0:02dd72d1d465 | 52 | finally: |
borlanic | 0:02dd72d1d465 | 53 | # Reset extra targets |
borlanic | 0:02dd72d1d465 | 54 | Target.set_targets_json_location() |
borlanic | 0:02dd72d1d465 | 55 | # Delete temp files |
borlanic | 0:02dd72d1d465 | 56 | shutil.rmtree(tempdir) |
borlanic | 0:02dd72d1d465 | 57 | |
borlanic | 0:02dd72d1d465 | 58 | def test_add_extra_targets(): |
borlanic | 0:02dd72d1d465 | 59 | """Search for extra targets json in a source folder""" |
borlanic | 0:02dd72d1d465 | 60 | test_target_json = """ |
borlanic | 0:02dd72d1d465 | 61 | { |
borlanic | 0:02dd72d1d465 | 62 | "Test_Target": { |
borlanic | 0:02dd72d1d465 | 63 | "inherits": ["Target"] |
borlanic | 0:02dd72d1d465 | 64 | } |
borlanic | 0:02dd72d1d465 | 65 | } |
borlanic | 0:02dd72d1d465 | 66 | """ |
borlanic | 0:02dd72d1d465 | 67 | with temp_target_file(test_target_json) as source_dir: |
borlanic | 0:02dd72d1d465 | 68 | Target.add_extra_targets(source_dir=source_dir) |
borlanic | 0:02dd72d1d465 | 69 | update_target_data() |
borlanic | 0:02dd72d1d465 | 70 | |
borlanic | 0:02dd72d1d465 | 71 | assert 'Test_Target' in TARGET_MAP |
borlanic | 0:02dd72d1d465 | 72 | assert TARGET_MAP['Test_Target'].core is None, \ |
borlanic | 0:02dd72d1d465 | 73 | "attributes should be inherited from Target" |
borlanic | 0:02dd72d1d465 | 74 | |
borlanic | 0:02dd72d1d465 | 75 | def test_modify_existing_target(): |
borlanic | 0:02dd72d1d465 | 76 | """Set default targets file, then override base Target definition""" |
borlanic | 0:02dd72d1d465 | 77 | initial_target_json = """ |
borlanic | 0:02dd72d1d465 | 78 | { |
borlanic | 0:02dd72d1d465 | 79 | "Target": { |
borlanic | 0:02dd72d1d465 | 80 | "core": null, |
borlanic | 0:02dd72d1d465 | 81 | "default_toolchain": "ARM", |
borlanic | 0:02dd72d1d465 | 82 | "supported_toolchains": null, |
borlanic | 0:02dd72d1d465 | 83 | "extra_labels": [], |
borlanic | 0:02dd72d1d465 | 84 | "is_disk_virtual": false, |
borlanic | 0:02dd72d1d465 | 85 | "macros": [], |
borlanic | 0:02dd72d1d465 | 86 | "device_has": [], |
borlanic | 0:02dd72d1d465 | 87 | "features": [], |
borlanic | 0:02dd72d1d465 | 88 | "detect_code": [], |
borlanic | 0:02dd72d1d465 | 89 | "public": false, |
borlanic | 0:02dd72d1d465 | 90 | "default_lib": "std", |
borlanic | 0:02dd72d1d465 | 91 | "bootloader_supported": false |
borlanic | 0:02dd72d1d465 | 92 | }, |
borlanic | 0:02dd72d1d465 | 93 | "Test_Target": { |
borlanic | 0:02dd72d1d465 | 94 | "inherits": ["Target"], |
borlanic | 0:02dd72d1d465 | 95 | "core": "Cortex-M4", |
borlanic | 0:02dd72d1d465 | 96 | "supported_toolchains": ["ARM"] |
borlanic | 0:02dd72d1d465 | 97 | } |
borlanic | 0:02dd72d1d465 | 98 | }""" |
borlanic | 0:02dd72d1d465 | 99 | |
borlanic | 0:02dd72d1d465 | 100 | test_target_json = """ |
borlanic | 0:02dd72d1d465 | 101 | { |
borlanic | 0:02dd72d1d465 | 102 | "Target": { |
borlanic | 0:02dd72d1d465 | 103 | "core": "Cortex-M0", |
borlanic | 0:02dd72d1d465 | 104 | "default_toolchain": "GCC_ARM", |
borlanic | 0:02dd72d1d465 | 105 | "supported_toolchains": null, |
borlanic | 0:02dd72d1d465 | 106 | "extra_labels": [], |
borlanic | 0:02dd72d1d465 | 107 | "is_disk_virtual": false, |
borlanic | 0:02dd72d1d465 | 108 | "macros": [], |
borlanic | 0:02dd72d1d465 | 109 | "device_has": [], |
borlanic | 0:02dd72d1d465 | 110 | "features": [], |
borlanic | 0:02dd72d1d465 | 111 | "detect_code": [], |
borlanic | 0:02dd72d1d465 | 112 | "public": false, |
borlanic | 0:02dd72d1d465 | 113 | "default_lib": "std", |
borlanic | 0:02dd72d1d465 | 114 | "bootloader_supported": true |
borlanic | 0:02dd72d1d465 | 115 | } |
borlanic | 0:02dd72d1d465 | 116 | } |
borlanic | 0:02dd72d1d465 | 117 | """ |
borlanic | 0:02dd72d1d465 | 118 | |
borlanic | 0:02dd72d1d465 | 119 | with temp_target_file(initial_target_json, json_filename="targets.json") as targets_dir: |
borlanic | 0:02dd72d1d465 | 120 | Target.set_targets_json_location(os.path.join(targets_dir, "targets.json")) |
borlanic | 0:02dd72d1d465 | 121 | update_target_data() |
borlanic | 0:02dd72d1d465 | 122 | assert TARGET_MAP["Test_Target"].core == "Cortex-M4" |
borlanic | 0:02dd72d1d465 | 123 | assert TARGET_MAP["Test_Target"].default_toolchain == 'ARM' |
borlanic | 0:02dd72d1d465 | 124 | assert TARGET_MAP["Test_Target"].bootloader_supported == False |
borlanic | 0:02dd72d1d465 | 125 | |
borlanic | 0:02dd72d1d465 | 126 | with temp_target_file(test_target_json) as source_dir: |
borlanic | 0:02dd72d1d465 | 127 | Target.add_extra_targets(source_dir=source_dir) |
borlanic | 0:02dd72d1d465 | 128 | update_target_data() |
borlanic | 0:02dd72d1d465 | 129 | |
borlanic | 0:02dd72d1d465 | 130 | assert TARGET_MAP["Test_Target"].core == "Cortex-M4" |
borlanic | 0:02dd72d1d465 | 131 | # The existing target should not be modified by custom targets |
borlanic | 0:02dd72d1d465 | 132 | assert TARGET_MAP["Test_Target"].default_toolchain != 'GCC_ARM' |
borlanic | 0:02dd72d1d465 | 133 | assert TARGET_MAP["Test_Target"].bootloader_supported != True |