1
1
#![ cfg( feature = "yaml" ) ]
2
2
3
3
use std:: collections:: HashMap ;
4
- use std:: path:: PathBuf ;
5
4
6
5
use chrono:: { DateTime , TimeZone , Utc } ;
7
6
use float_cmp:: ApproxEqUlps ;
@@ -33,7 +32,28 @@ fn test_file() {
33
32
}
34
33
35
34
let c = Config :: builder ( )
36
- . add_source ( File :: new ( "tests/Settings" , FileFormat :: Yaml ) )
35
+ . add_source ( File :: from_str (
36
+ r#"
37
+ debug: true
38
+ production: false
39
+ arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
40
+ place:
41
+ name: Torre di Pisa
42
+ longitude: 43.7224985
43
+ latitude: 10.3970522
44
+ favorite: false
45
+ reviews: 3866
46
+ rating: 4.5
47
+ creator:
48
+ name: John Smith
49
+ username: jsmith
50
+ email: jsmith@localhost
51
+ # For override tests
52
+ FOO: FOO should be overridden
53
+ bar: I am bar
54
+ "# ,
55
+ FileFormat :: Yaml ,
56
+ ) )
37
57
. build ( )
38
58
. unwrap ( ) ;
39
59
@@ -75,18 +95,19 @@ fn test_file() {
75
95
#[ cfg( unix) ]
76
96
fn test_error_parse ( ) {
77
97
let res = Config :: builder ( )
78
- . add_source ( File :: new ( "tests/Settings-invalid" , FileFormat :: Yaml ) )
98
+ . add_source ( File :: from_str (
99
+ r#"
100
+ ok: true
101
+ error false
102
+ "# ,
103
+ FileFormat :: Yaml ,
104
+ ) )
79
105
. build ( ) ;
80
106
81
- let path_with_extension: PathBuf = [ "tests" , "Settings-invalid.yaml" ] . iter ( ) . collect ( ) ;
82
-
83
107
assert ! ( res. is_err( ) ) ;
84
108
assert_eq ! (
85
109
res. unwrap_err( ) . to_string( ) ,
86
- format!(
87
- "simple key expect ':' at byte 21 line 3 column 1 in {}" ,
88
- path_with_extension. display( )
89
- )
110
+ "simple key expect ':' at byte 22 line 4 column 1" ,
90
111
) ;
91
112
}
92
113
@@ -104,7 +125,19 @@ fn test_yaml_parsing_key() {
104
125
}
105
126
106
127
let config = Config :: builder ( )
107
- . add_source ( File :: new ( "tests/test-keys.yaml" , FileFormat :: Yaml ) )
128
+ . add_source ( File :: from_str (
129
+ r#"
130
+ inner_int:
131
+ "1":
132
+ member: "Test Int 1"
133
+ 2:
134
+ member: "Test Int 2"
135
+ inner_string:
136
+ str_key:
137
+ member: "Test String"
138
+ "# ,
139
+ FileFormat :: Yaml ,
140
+ ) )
108
141
. build ( )
109
142
. unwrap ( )
110
143
. try_deserialize :: < Outer > ( )
@@ -134,7 +167,28 @@ fn test_override_uppercase_value_for_struct() {
134
167
std:: env:: set_var ( "APP_FOO" , "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE" ) ;
135
168
136
169
let cfg = Config :: builder ( )
137
- . add_source ( File :: new ( "tests/Settings.yaml" , FileFormat :: Yaml ) )
170
+ . add_source ( File :: from_str (
171
+ r#"
172
+ debug: true
173
+ production: false
174
+ arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
175
+ place:
176
+ name: Torre di Pisa
177
+ longitude: 43.7224985
178
+ latitude: 10.3970522
179
+ favorite: false
180
+ reviews: 3866
181
+ rating: 4.5
182
+ creator:
183
+ name: John Smith
184
+ username: jsmith
185
+ email: jsmith@localhost
186
+ # For override tests
187
+ FOO: FOO should be overridden
188
+ bar: I am bar
189
+ "# ,
190
+ FileFormat :: Yaml ,
191
+ ) )
138
192
. add_source ( config:: Environment :: with_prefix ( "APP" ) . separator ( "_" ) )
139
193
. build ( )
140
194
. unwrap ( ) ;
@@ -176,7 +230,28 @@ fn test_override_lowercase_value_for_struct() {
176
230
std:: env:: set_var ( "config_bar" , "I have been overridden_with_lower_case" ) ;
177
231
178
232
let cfg = Config :: builder ( )
179
- . add_source ( File :: new ( "tests/Settings.yaml" , FileFormat :: Yaml ) )
233
+ . add_source ( File :: from_str (
234
+ r#"
235
+ debug: true
236
+ production: false
237
+ arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
238
+ place:
239
+ name: Torre di Pisa
240
+ longitude: 43.7224985
241
+ latitude: 10.3970522
242
+ favorite: false
243
+ reviews: 3866
244
+ rating: 4.5
245
+ creator:
246
+ name: John Smith
247
+ username: jsmith
248
+ email: jsmith@localhost
249
+ # For override tests
250
+ FOO: FOO should be overridden
251
+ bar: I am bar
252
+ "# ,
253
+ FileFormat :: Yaml ,
254
+ ) )
180
255
. add_source ( config:: Environment :: with_prefix ( "config" ) . separator ( "_" ) )
181
256
. build ( )
182
257
. unwrap ( ) ;
@@ -199,7 +274,12 @@ fn test_override_uppercase_value_for_enums() {
199
274
std:: env:: set_var ( "APPS_BAR" , "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE" ) ;
200
275
201
276
let cfg = Config :: builder ( )
202
- . add_source ( File :: new ( "tests/Settings-enum-test.yaml" , FileFormat :: Yaml ) )
277
+ . add_source ( File :: from_str (
278
+ r#"
279
+ bar: bar is a lowercase param
280
+ "# ,
281
+ FileFormat :: Yaml ,
282
+ ) )
203
283
. add_source ( config:: Environment :: with_prefix ( "APPS" ) . separator ( "_" ) )
204
284
. build ( )
205
285
. unwrap ( ) ;
@@ -221,7 +301,12 @@ fn test_override_lowercase_value_for_enums() {
221
301
std:: env:: set_var ( "test_bar" , "I have been overridden_with_lower_case" ) ;
222
302
223
303
let cfg = Config :: builder ( )
224
- . add_source ( File :: new ( "tests/Settings-enum-test.yaml" , FileFormat :: Yaml ) )
304
+ . add_source ( File :: from_str (
305
+ r#"
306
+ bar: bar is a lowercase param
307
+ "# ,
308
+ FileFormat :: Yaml ,
309
+ ) )
225
310
. add_source ( config:: Environment :: with_prefix ( "test" ) . separator ( "_" ) )
226
311
. build ( )
227
312
. unwrap ( ) ;
@@ -240,7 +325,7 @@ fn yaml() {
240
325
. add_source ( File :: from_str (
241
326
r#"
242
327
yaml_datetime: 2017-06-12T10:58:30Z
243
- "#,
328
+ "# ,
244
329
FileFormat :: Yaml ,
245
330
) )
246
331
. build ( )
0 commit comments