3
3
# SPDX-License-Identifier: Apache-2.0
4
4
#
5
5
"""Tests for `mbed_tools.targets.target_attributes`."""
6
- import pathlib
7
- import tempfile
8
6
from unittest import TestCase , mock
9
7
10
8
from mbed_tools .targets ._internal .exceptions import TargetsJsonConfigurationError
11
9
from mbed_tools .targets ._internal .target_attributes import (
12
- ParsingTargetsJSONError ,
13
10
TargetNotFoundError ,
14
11
get_target_attributes ,
15
- _read_json_file ,
16
12
_extract_target_attributes ,
17
13
_extract_core_labels ,
18
14
_apply_config_overrides ,
@@ -55,64 +51,26 @@ def test_target_private(self):
55
51
_extract_target_attributes (all_targets_data , "Target_1" ),
56
52
57
53
58
- class TestReadTargetsJSON (TestCase ):
59
- def test_valid_path (self ):
60
- contents = """{
61
- "Target_Name": {
62
- "attribute_1": []
63
- }
64
- }"""
65
- with tempfile .TemporaryDirectory () as directory :
66
- json_file = pathlib .Path (directory , "targets.json" )
67
- json_file .write_text (contents )
68
- result = _read_json_file (json_file )
69
-
70
- self .assertEqual (type (result ), dict )
71
-
72
- def test_invalid_path (self ):
73
- json_file = pathlib .Path ("i_dont_exist" )
74
-
75
- with self .assertRaises (FileNotFoundError ):
76
- _read_json_file (json_file )
77
-
78
- def test_malformed_json (self ):
79
- contents = """{
80
- "Target_Name": {
81
- []
82
- }
83
- }"""
84
- with tempfile .TemporaryDirectory () as directory :
85
- json_file = pathlib .Path (directory , "targets.json" )
86
- json_file .write_text (contents )
87
-
88
- with self .assertRaises (ParsingTargetsJSONError ):
89
- _read_json_file (json_file )
90
-
91
-
92
54
class TestGetTargetAttributes (TestCase ):
93
- @mock .patch ("mbed_tools.targets._internal.target_attributes._read_json_file" )
94
55
@mock .patch ("mbed_tools.targets._internal.target_attributes._extract_target_attributes" )
95
56
@mock .patch ("mbed_tools.targets._internal.target_attributes.get_labels_for_target" )
96
57
@mock .patch ("mbed_tools.targets._internal.target_attributes._extract_core_labels" )
97
- def test_gets_attributes_for_target (
98
- self , extract_core_labels , get_labels_for_target , extract_target_attributes , read_json_file
99
- ):
100
- targets_json_path = pathlib .Path ("mbed-os/targets/targets.json" )
58
+ def test_gets_attributes_for_target (self , extract_core_labels , get_labels_for_target , extract_target_attributes ):
59
+ targets_json_data = {"attrs" : "vals" }
101
60
target_name = "My_Target"
102
61
build_attributes = {"attribute" : "value" }
103
62
extract_target_attributes .return_value = build_attributes
104
63
105
- result = get_target_attributes (targets_json_path , target_name )
64
+ result = get_target_attributes (targets_json_data , target_name )
106
65
107
- read_json_file .assert_called_once_with (targets_json_path )
108
- extract_target_attributes .assert_called_once_with (read_json_file .return_value , target_name )
109
- get_labels_for_target .assert_called_once_with (read_json_file .return_value , target_name )
66
+ extract_target_attributes .assert_called_once_with (targets_json_data , target_name )
67
+ get_labels_for_target .assert_called_once_with (targets_json_data , target_name )
110
68
extract_core_labels .assert_called_once_with (build_attributes .get ("core" , None ))
111
69
self .assertEqual (result , extract_target_attributes .return_value )
112
70
113
71
114
72
class TestExtractCoreLabels (TestCase ):
115
- @mock .patch ("mbed_tools.targets._internal.target_attributes._read_json_file " )
73
+ @mock .patch ("mbed_tools.targets._internal.target_attributes.decode_json_file " )
116
74
def test_extract_core (self , read_json_file ):
117
75
core_labels = ["FOO" , "BAR" ]
118
76
metadata = {"CORE_LABELS" : {"core_name" : core_labels }}
@@ -127,7 +85,7 @@ def test_no_core(self):
127
85
result = _extract_core_labels (None )
128
86
self .assertEqual (result , set ())
129
87
130
- @mock .patch ("mbed_tools.targets._internal.target_attributes._read_json_file " )
88
+ @mock .patch ("mbed_tools.targets._internal.target_attributes.decode_json_file " )
131
89
def test_no_labels (self , read_json_file ):
132
90
metadata = {"CORE_LABELS" : {"not_the_same_core" : []}}
133
91
read_json_file .return_value = metadata
0 commit comments