Skip to content

Commit 6bd55a1

Browse files
committed
Add basic unit tests for custom_targets.json handling
1 parent 49645b4 commit 6bd55a1

File tree

1 file changed

+99
-2
lines changed

1 file changed

+99
-2
lines changed

tools/test/targets/target_test.py

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,23 @@
1515
See the License for the specific language governing permissions and
1616
limitations under the License.
1717
"""
18-
18+
import os
1919
import sys
20+
import shutil
21+
import tempfile
2022
from os.path import join, abspath, dirname
23+
from contextlib import contextmanager
2124
import unittest
2225

2326
# Be sure that the tools directory is in the search path
27+
2428
ROOT = abspath(join(dirname(__file__), "..", "..", ".."))
2529
sys.path.insert(0, ROOT)
2630

27-
from tools.targets import TARGETS
31+
from tools.targets import TARGETS, TARGET_MAP, Target, update_target_data
2832
from tools.arm_pack_manager import Cache
2933

34+
3035
class TestTargets(unittest.TestCase):
3136

3237
def test_device_name(self):
@@ -39,5 +44,97 @@ def test_device_name(self):
3944
"Target %s contains invalid device_name %s" %
4045
(target.name, target.device_name))
4146

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+
42139
if __name__ == '__main__':
43140
unittest.main()

0 commit comments

Comments
 (0)