Skip to content

Commit 0ef490b

Browse files
committed
test(yaml): Reduce the scope of fixtures
1 parent 81ee8de commit 0ef490b

File tree

1 file changed

+100
-15
lines changed

1 file changed

+100
-15
lines changed

tests/testsuite/file_yaml.rs

Lines changed: 100 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![cfg(feature = "yaml")]
22

33
use std::collections::HashMap;
4-
use std::path::PathBuf;
54

65
use chrono::{DateTime, TimeZone, Utc};
76
use float_cmp::ApproxEqUlps;
@@ -33,7 +32,28 @@ fn test_file() {
3332
}
3433

3534
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+
))
3757
.build()
3858
.unwrap();
3959

@@ -75,18 +95,19 @@ fn test_file() {
7595
#[cfg(unix)]
7696
fn test_error_parse() {
7797
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+
))
79105
.build();
80106

81-
let path_with_extension: PathBuf = ["tests", "Settings-invalid.yaml"].iter().collect();
82-
83107
assert!(res.is_err());
84108
assert_eq!(
85109
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",
90111
);
91112
}
92113

@@ -104,7 +125,19 @@ fn test_yaml_parsing_key() {
104125
}
105126

106127
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+
))
108141
.build()
109142
.unwrap()
110143
.try_deserialize::<Outer>()
@@ -134,7 +167,28 @@ fn test_override_uppercase_value_for_struct() {
134167
std::env::set_var("APP_FOO", "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE");
135168

136169
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+
))
138192
.add_source(config::Environment::with_prefix("APP").separator("_"))
139193
.build()
140194
.unwrap();
@@ -176,7 +230,28 @@ fn test_override_lowercase_value_for_struct() {
176230
std::env::set_var("config_bar", "I have been overridden_with_lower_case");
177231

178232
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+
))
180255
.add_source(config::Environment::with_prefix("config").separator("_"))
181256
.build()
182257
.unwrap();
@@ -199,7 +274,12 @@ fn test_override_uppercase_value_for_enums() {
199274
std::env::set_var("APPS_BAR", "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE");
200275

201276
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+
))
203283
.add_source(config::Environment::with_prefix("APPS").separator("_"))
204284
.build()
205285
.unwrap();
@@ -221,7 +301,12 @@ fn test_override_lowercase_value_for_enums() {
221301
std::env::set_var("test_bar", "I have been overridden_with_lower_case");
222302

223303
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+
))
225310
.add_source(config::Environment::with_prefix("test").separator("_"))
226311
.build()
227312
.unwrap();
@@ -240,7 +325,7 @@ fn yaml() {
240325
.add_source(File::from_str(
241326
r#"
242327
yaml_datetime: 2017-06-12T10:58:30Z
243-
"#,
328+
"#,
244329
FileFormat::Yaml,
245330
))
246331
.build()

0 commit comments

Comments
 (0)