14
14
15
15
use nativelink_config:: serde_utils:: {
16
16
convert_data_size_with_shellexpand, convert_duration_with_shellexpand,
17
+ convert_numeric_with_shellexpand, convert_optional_numeric_with_shellexpand,
18
+ convert_optional_string_with_shellexpand, convert_string_with_shellexpand,
19
+ convert_vec_string_with_shellexpand,
17
20
} ;
18
21
use pretty_assertions:: assert_eq;
19
22
use serde:: Deserialize ;
20
23
24
+ const INVALID_ENV_VAR_ERROR : & str =
25
+ "error looking key 'INVALID_ENV_VAR' up: environment variable not found" ;
26
+ const MAP_TYPE_ERROR : & str = "invalid type: map, expected a string containing json data" ;
27
+
28
+ const DURATION_HUMAN_READABLE : & str = r#"{"duration": "1m 10s"}"# ;
29
+ const DURATION_USIZE : & str = r#"{"duration": 10}"# ;
30
+ const DATA_SIZE_KIB : & str = r#"{"data_size": "1KiB"}"# ;
31
+ const DATA_SIZE_USIZE : & str = r#"{"data_size": 10}"# ;
32
+ const STRING_ENV_VAR : & str = r#"{"expanded_string": "$INVALID_ENV_VAR"}"# ;
33
+ const EMPTY_STRING_ENV_VAR : & str = r#"{"expanded_string": ""}"# ;
34
+ const NULL_STRING_ENV_VAR : & str = r#"{"expanded_string": null}"# ;
35
+ const VEC_STRING_EMPTY : & str = r#"{"expanded_strings": []}"# ;
36
+
21
37
#[ derive( Deserialize ) ]
22
38
struct DurationEntity {
23
39
#[ serde( default , deserialize_with = "convert_duration_with_shellexpand" ) ]
@@ -30,38 +46,206 @@ struct DataSizeEntity {
30
46
data_size : usize ,
31
47
}
32
48
49
+ #[ derive( Deserialize ) ]
50
+ struct NumericEntity {
51
+ #[ serde( default , deserialize_with = "convert_numeric_with_shellexpand" ) ]
52
+ value : usize ,
53
+ }
54
+
55
+ #[ derive( Deserialize ) ]
56
+ struct OptionalNumericEntity {
57
+ #[ serde(
58
+ default ,
59
+ deserialize_with = "convert_optional_numeric_with_shellexpand"
60
+ ) ]
61
+ optional_value : Option < usize > ,
62
+ }
63
+
64
+ #[ derive( Deserialize ) ]
65
+ struct VecStringEntity {
66
+ #[ serde( deserialize_with = "convert_vec_string_with_shellexpand" ) ]
67
+ expanded_strings : Vec < String > ,
68
+ }
69
+
70
+ #[ derive( Deserialize ) ]
71
+ struct OptionalStringEntity {
72
+ #[ serde( deserialize_with = "convert_optional_string_with_shellexpand" ) ]
73
+ expanded_string : Option < String > ,
74
+ }
75
+
76
+ #[ derive( Deserialize ) ]
77
+ struct StringEntity {
78
+ #[ serde( deserialize_with = "convert_string_with_shellexpand" ) ]
79
+ expanded_string : String ,
80
+ }
81
+
33
82
#[ test]
34
83
fn test_duration_human_readable_deserialize ( ) {
35
- let example = r#"
36
- {"duration": "1m 10s"}
37
- "# ;
84
+ let example = DURATION_HUMAN_READABLE ;
38
85
let deserialized: DurationEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
39
86
assert_eq ! ( deserialized. duration, 70 ) ;
40
87
}
41
88
42
89
#[ test]
43
90
fn test_duration_usize_deserialize ( ) {
44
- let example = r#"
45
- {"duration": 10}
46
- "# ;
91
+ let example = DURATION_USIZE ;
47
92
let deserialized: DurationEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
48
93
assert_eq ! ( deserialized. duration, 10 ) ;
49
94
}
50
95
96
+ #[ test]
97
+ fn test_duration_invalid_string ( ) {
98
+ let example = r#"{"duration": {size:10, in:8}}"# ;
99
+ let result: Result < DurationEntity , _ > = serde_json5:: from_str ( example) ;
100
+ assert ! ( result. is_err( ) ) ;
101
+ if let Err ( e) = result {
102
+ assert_eq ! ( e. to_string( ) , MAP_TYPE_ERROR ) ;
103
+ }
104
+ }
105
+
51
106
#[ test]
52
107
fn test_data_size_unit_deserialize ( ) {
53
- let example = r#"
54
- {"data_size": "1KiB"}
55
- "# ;
108
+ let example = DATA_SIZE_KIB ;
56
109
let deserialized: DataSizeEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
57
110
assert_eq ! ( deserialized. data_size, 1024 ) ;
58
111
}
59
112
60
113
#[ test]
61
114
fn test_data_size_usize_deserialize ( ) {
62
- let example = r#"
63
- {"data_size": 10}
64
- "# ;
115
+ let example = DATA_SIZE_USIZE ;
65
116
let deserialized: DataSizeEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
66
117
assert_eq ! ( deserialized. data_size, 10 ) ;
67
118
}
119
+
120
+ #[ test]
121
+ fn test_data_size_invalid_string ( ) {
122
+ let example = r#"{"data_size": {size:10, in:8}}"# ;
123
+ let result: Result < DataSizeEntity , _ > = serde_json5:: from_str ( example) ;
124
+ assert ! ( result. is_err( ) ) ;
125
+ if let Err ( e) = result {
126
+ assert_eq ! ( e. to_string( ) , MAP_TYPE_ERROR ) ;
127
+ }
128
+ }
129
+
130
+ #[ test]
131
+ fn test_numeric_with_shellexpand_integer ( ) {
132
+ let example = r#"{ "value": 42 }"# ;
133
+ let deserialized: NumericEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
134
+ assert_eq ! ( deserialized. value, 42 ) ;
135
+ }
136
+
137
+ #[ test]
138
+ fn test_numeric_with_shellexpand_string ( ) {
139
+ let example = r#"{ "value": "100" }"# ;
140
+ let deserialized: NumericEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
141
+ assert_eq ! ( deserialized. value, 100 ) ;
142
+ }
143
+
144
+ #[ test]
145
+ fn test_numeric_with_shellexpand_invalid_string ( ) {
146
+ let example = r#"{ "value": {size:10, in:8} }"# ;
147
+ let result: Result < NumericEntity , _ > = serde_json5:: from_str ( example) ;
148
+ assert ! ( result. is_err( ) ) ;
149
+ if let Err ( e) = result {
150
+ assert_eq ! ( e. to_string( ) , MAP_TYPE_ERROR ) ;
151
+ }
152
+ }
153
+
154
+ #[ test]
155
+ fn test_optional_numeric_with_shellexpand_integer ( ) {
156
+ let example = r#"{ "optional_value": 42 }"# ;
157
+ let deserialized: OptionalNumericEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
158
+ assert_eq ! ( deserialized. optional_value, Some ( 42 ) ) ;
159
+ }
160
+
161
+ #[ test]
162
+ fn test_optional_numeric_with_shellexpand_string ( ) {
163
+ let example = r#"{ "optional_value": "100" }"# ;
164
+ let deserialized: OptionalNumericEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
165
+ assert_eq ! ( deserialized. optional_value, Some ( 100 ) ) ;
166
+ }
167
+
168
+ #[ test]
169
+ fn test_optional_numeric_with_shellexpand_empty_string ( ) {
170
+ let example = r#"{"optional_value": ""}"# ;
171
+ let deserialized: OptionalNumericEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
172
+ assert_eq ! ( deserialized. optional_value, None ) ;
173
+ }
174
+
175
+ #[ test]
176
+ fn test_optional_numeric_with_shellexpand_invalid_string ( ) {
177
+ let example = r#"{ "optional_value": {size:10, in:8} }"# ;
178
+ let result: Result < OptionalNumericEntity , _ > = serde_json5:: from_str ( example) ;
179
+ assert ! ( result. is_err( ) ) ;
180
+ if let Err ( e) = result {
181
+ assert_eq ! ( e. to_string( ) , MAP_TYPE_ERROR ) ;
182
+ }
183
+ }
184
+
185
+ #[ test]
186
+ fn test_convert_string_with_shellexpand_invalid ( ) {
187
+ let example = STRING_ENV_VAR ;
188
+ let result: Result < StringEntity , _ > = serde_json5:: from_str ( example) ;
189
+ assert ! ( result. is_err( ) ) ;
190
+ if let Err ( e) = result {
191
+ assert_eq ! ( e. to_string( ) , INVALID_ENV_VAR_ERROR ) ;
192
+ }
193
+ }
194
+
195
+ #[ test]
196
+ fn test_convert_string_with_shellexpand_empty ( ) {
197
+ let example = EMPTY_STRING_ENV_VAR ;
198
+ let deserialized: StringEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
199
+ assert_eq ! ( deserialized. expanded_string, "" ) ;
200
+ }
201
+
202
+ #[ test]
203
+ fn test_convert_vec_string_with_shellexpand_empty ( ) {
204
+ let example = VEC_STRING_EMPTY ;
205
+ let deserialized: VecStringEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
206
+ assert_eq ! ( deserialized. expanded_strings, Vec :: <String >:: new( ) ) ;
207
+ }
208
+
209
+ #[ test]
210
+ fn test_convert_vec_string_with_shellexpand_invalid ( ) {
211
+ let example = r#"{"expanded_strings": ["$INVALID_ENV_VAR", "$ANOTHER_MISSING_VAR"]}"# ;
212
+ let result: Result < VecStringEntity , _ > = serde_json5:: from_str ( example) ;
213
+ assert ! ( result. is_err( ) ) ;
214
+ if let Err ( e) = result {
215
+ assert_eq ! ( e. to_string( ) , INVALID_ENV_VAR_ERROR ) ;
216
+ }
217
+ }
218
+
219
+ #[ test]
220
+ fn test_convert_vec_string_with_shellexpand_invalid_alternate ( ) {
221
+ let example = r#"{"expanded_strings": ["config.json", "$INVALID_ENV_VAR"]}"# ;
222
+ let result: Result < VecStringEntity , _ > = serde_json5:: from_str ( example) ;
223
+ assert ! ( result. is_err( ) ) ;
224
+ if let Err ( e) = result {
225
+ assert_eq ! ( e. to_string( ) , INVALID_ENV_VAR_ERROR ) ;
226
+ }
227
+ }
228
+
229
+ #[ test]
230
+ fn test_convert_optional_string_with_shellexpand_none ( ) {
231
+ let example = NULL_STRING_ENV_VAR ;
232
+ let deserialized: OptionalStringEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
233
+ assert_eq ! ( deserialized. expanded_string, None ) ;
234
+ }
235
+
236
+ #[ test]
237
+ fn test_convert_optional_string_with_shellexpand_empty ( ) {
238
+ let example = EMPTY_STRING_ENV_VAR ;
239
+ let deserialized: OptionalStringEntity = serde_json5:: from_str ( example) . unwrap ( ) ;
240
+ assert_eq ! ( deserialized. expanded_string, Some ( String :: new( ) ) ) ;
241
+ }
242
+
243
+ #[ test]
244
+ fn test_convert_optional_string_with_shellexpand_invalid ( ) {
245
+ let example = STRING_ENV_VAR ;
246
+ let result: Result < OptionalStringEntity , _ > = serde_json5:: from_str ( example) ;
247
+ assert ! ( result. is_err( ) ) ;
248
+ if let Err ( e) = result {
249
+ assert_eq ! ( e. to_string( ) , INVALID_ENV_VAR_ERROR ) ;
250
+ }
251
+ }
0 commit comments