15
15
See the License for the specific language governing permissions and
16
16
limitations under the License.
17
17
"""
18
-
18
+ import os
19
19
import sys
20
+ import shutil
21
+ import tempfile
20
22
from os .path import join , abspath , dirname
23
+ from contextlib import contextmanager
21
24
import unittest
22
25
23
26
# Be sure that the tools directory is in the search path
27
+
24
28
ROOT = abspath (join (dirname (__file__ ), ".." , ".." , ".." ))
25
29
sys .path .insert (0 , ROOT )
26
30
27
- from tools .targets import TARGETS
31
+ from tools .targets import TARGETS , TARGET_MAP , Target , update_target_data
28
32
from tools .arm_pack_manager import Cache
29
33
34
+
30
35
class TestTargets (unittest .TestCase ):
31
36
32
37
def test_device_name (self ):
@@ -39,5 +44,97 @@ def test_device_name(self):
39
44
"Target %s contains invalid device_name %s" %
40
45
(target .name , target .device_name ))
41
46
47
+ @contextmanager
48
+ def temp_target_file (self , extra_target , json_filename = 'custom_targets.json' ):
49
+ """Create an extra targets temp file in a context manager"""
50
+ tempdir = tempfile .mkdtemp ()
51
+ try :
52
+ targetfile = os .path .join (tempdir , json_filename )
53
+ with open (targetfile , 'w' ) as f :
54
+ f .write (extra_target )
55
+ yield tempdir
56
+ finally :
57
+ # Reset extra targets
58
+ Target .set_targets_json_location ()
59
+ # Delete temp files
60
+ shutil .rmtree (tempdir )
61
+
62
+ def test_add_extra_targets (self ):
63
+ """Search for extra targets json in a source folder"""
64
+ test_target_json = """
65
+ {
66
+ "Test_Target": {
67
+ "inherits": ["Target"]
68
+ }
69
+ }
70
+ """
71
+ with self .temp_target_file (test_target_json ) as source_dir :
72
+ Target .add_extra_targets (source_dir = source_dir )
73
+ update_target_data ()
74
+
75
+ assert 'Test_Target' in TARGET_MAP
76
+ assert TARGET_MAP ['Test_Target' ].core is None , \
77
+ "attributes should be inherited from Target"
78
+
79
+ def test_modify_default_target (self ):
80
+ """Set default targets file, then override base Target definition"""
81
+ initial_target_json = """
82
+ {
83
+ "Target": {
84
+ "core": null,
85
+ "default_toolchain": "ARM",
86
+ "supported_toolchains": null,
87
+ "extra_labels": [],
88
+ "is_disk_virtual": false,
89
+ "macros": [],
90
+ "device_has": [],
91
+ "features": [],
92
+ "detect_code": [],
93
+ "public": false,
94
+ "default_lib": "std",
95
+ "bootloader_supported": false
96
+ },
97
+ "Test_Target": {
98
+ "inherits": ["Target"],
99
+ "core": "Cortex-M4",
100
+ "supported_toolchains": ["ARM"]
101
+ }
102
+ }"""
103
+
104
+ test_target_json = """
105
+ {
106
+ "Target": {
107
+ "core": "Cortex-M0",
108
+ "default_toolchain": "GCC_ARM",
109
+ "supported_toolchains": null,
110
+ "extra_labels": [],
111
+ "is_disk_virtual": false,
112
+ "macros": [],
113
+ "device_has": [],
114
+ "features": [],
115
+ "detect_code": [],
116
+ "public": false,
117
+ "default_lib": "std",
118
+ "bootloader_supported": true
119
+ }
120
+ }
121
+ """
122
+
123
+ with self .temp_target_file (initial_target_json , json_filename = "targets.json" ) as targets_dir :
124
+ Target .set_targets_json_location (os .path .join (targets_dir , "targets.json" ))
125
+ update_target_data ()
126
+ assert TARGET_MAP ["Test_Target" ].core == "Cortex-M4"
127
+ assert TARGET_MAP ["Test_Target" ].default_toolchain == 'ARM'
128
+ assert TARGET_MAP ["Test_Target" ].bootloader_supported == False
129
+
130
+ with self .temp_target_file (test_target_json ) as source_dir :
131
+ Target .add_extra_targets (source_dir = source_dir )
132
+ update_target_data ()
133
+
134
+ assert TARGET_MAP ["Test_Target" ].core == "Cortex-M4"
135
+ assert TARGET_MAP ["Test_Target" ].default_toolchain == 'GCC_ARM'
136
+ assert TARGET_MAP ["Test_Target" ].bootloader_supported == True
137
+
138
+
42
139
if __name__ == '__main__' :
43
140
unittest .main ()
0 commit comments