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