Skip to content

Commit 98662dd

Browse files
committed
Modify tests to use both ConfigBuilder and Config
1 parent 82d23c7 commit 98662dd

30 files changed

+1456
-163
lines changed

src/builder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use std::str::FromStr;
22
use std::{collections::HashMap, iter::IntoIterator};
33

4-
use crate::{
5-
config::Config, error, error::ConfigError, path::Expression, source::Source, value::Value,
6-
};
4+
use crate::{config::Config, error, path::Expression, source::Source, value::Value};
75

86
/// A configuration builder
97
///
@@ -142,7 +140,7 @@ impl ConfigBuilder {
142140
fn build_internal(
143141
defaults: HashMap<Expression, Value>,
144142
overrides: HashMap<Expression, Value>,
145-
sources: &Vec<Box<dyn Source + Send + Sync>>,
143+
sources: &[Box<dyn Source + Send + Sync>],
146144
) -> error::Result<Config> {
147145
let mut cache: Value = HashMap::<String, Value>::new().into();
148146

src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ impl Config {
5656
T: Source + Send + Sync,
5757
{
5858
self.sources.push(Box::new(source));
59+
60+
#[allow(deprecated)]
5961
self.refresh()
6062
}
6163

@@ -67,6 +69,8 @@ impl Config {
6769
T: Source + Send + Sync,
6870
{
6971
self.sources.push(Box::new(source));
72+
73+
#[allow(deprecated)]
7074
self.refresh()?;
7175
Ok(self)
7276
}
@@ -107,6 +111,8 @@ impl Config {
107111
T: Into<Value>,
108112
{
109113
self.defaults.insert(key.parse()?, value.into());
114+
115+
#[allow(deprecated)]
110116
self.refresh()
111117
}
112118

@@ -124,6 +130,8 @@ impl Config {
124130
T: Into<Value>,
125131
{
126132
self.overrides.insert(key.parse()?, value.into());
133+
134+
#[allow(deprecated)]
127135
self.refresh()
128136
}
129137

src/ser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ impl ConfigSerializer {
2727
)))
2828
}
2929
};
30+
31+
#[allow(deprecated)]
3032
self.output.set(&key, value.into())?;
3133
Ok(())
3234
}

src/source.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ impl Source for Vec<Box<dyn Source + Send + Sync>> {
5555
}
5656
}
5757

58+
impl Source for [Box<dyn Source + Send + Sync>] {
59+
fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> {
60+
Box::new(self.to_owned())
61+
}
62+
63+
fn collect(&self) -> Result<HashMap<String, Value>> {
64+
let mut cache: Value = HashMap::<String, Value>::new().into();
65+
66+
for source in self {
67+
source.collect_to(&mut cache)?;
68+
}
69+
70+
if let ValueKind::Table(table) = cache.kind {
71+
Ok(table)
72+
} else {
73+
unreachable!();
74+
}
75+
}
76+
}
77+
5878
impl<T> Source for Vec<T>
5979
where
6080
T: Source + Sync + Send,

tests/datetime.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,53 @@ use chrono::{DateTime, TimeZone, Utc};
1414
use config::*;
1515

1616
fn make() -> Config {
17-
Config::default()
18-
.merge(File::from_str(
17+
let mut builder = Config::builder();
18+
19+
builder
20+
.add_source(File::from_str(
1921
r#"
2022
{
2123
"json_datetime": "2017-05-10T02:14:53Z"
2224
}
2325
"#,
2426
FileFormat::Json,
2527
))
26-
.unwrap()
27-
.merge(File::from_str(
28+
.add_source(File::from_str(
2829
r#"
2930
yaml_datetime: 2017-06-12T10:58:30Z
3031
"#,
3132
FileFormat::Yaml,
3233
))
33-
.unwrap()
34-
.merge(File::from_str(
34+
.add_source(File::from_str(
3535
r#"
3636
toml_datetime = 2017-05-11T14:55:15Z
3737
"#,
3838
FileFormat::Toml,
3939
))
40-
.unwrap()
41-
.merge(File::from_str(
40+
.add_source(File::from_str(
4241
r#"
4342
{
4443
"hjson_datetime": "2017-05-10T02:14:53Z"
4544
}
4645
"#,
4746
FileFormat::Hjson,
4847
))
49-
.unwrap()
50-
.merge(File::from_str(
48+
.add_source(File::from_str(
5149
r#"
5250
ini_datetime = 2017-05-10T02:14:53Z
5351
"#,
5452
FileFormat::Ini,
5553
))
56-
.unwrap()
57-
.merge(File::from_str(
54+
.add_source(File::from_str(
5855
r#"
5956
(
6057
ron_datetime: "2021-04-19T11:33:02Z"
6158
)
6259
"#,
6360
FileFormat::Ron,
64-
))
65-
.unwrap()
66-
.clone()
61+
));
62+
63+
builder.build().unwrap()
6764
}
6865

6966
#[test]

tests/errors.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@ use std::path::PathBuf;
1010
use config::*;
1111

1212
fn make() -> Config {
13-
let mut c = Config::default();
14-
c.merge(File::new("tests/Settings", FileFormat::Toml))
15-
.unwrap();
16-
17-
c
13+
let mut c = Config::builder();
14+
c.add_source(File::new("tests/Settings", FileFormat::Toml));
15+
c.build().unwrap()
1816
}
1917

2018
#[test]
2119
fn test_error_parse() {
22-
let mut c = Config::default();
23-
let res = c.merge(File::new("tests/Settings-invalid", FileFormat::Toml));
20+
let mut c = Config::builder();
21+
c.add_source(File::new("tests/Settings-invalid", FileFormat::Toml));
22+
let res = c.build();
2423

2524
let path: PathBuf = ["tests", "Settings-invalid.toml"].iter().collect();
2625

@@ -121,9 +120,9 @@ inner:
121120
test: ABC
122121
"#;
123122

124-
let mut cfg = Config::default();
125-
cfg.merge(File::from_str(CFG, FileFormat::Yaml)).unwrap();
126-
let e = cfg.try_into::<Outer>().unwrap_err();
123+
let mut cfg = Config::builder();
124+
cfg.add_source(File::from_str(CFG, FileFormat::Yaml));
125+
let e = cfg.build().unwrap().try_into::<Outer>().unwrap_err();
127126
if let ConfigError::Type {
128127
key: Some(path), ..
129128
} = e

tests/file.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ use config::*;
66

77
#[test]
88
fn test_file_not_required() {
9-
let mut c = Config::default();
10-
let res = c.merge(File::new("tests/NoSettings", FileFormat::Yaml).required(false));
9+
let mut c = Config::builder();
10+
c.add_source(File::new("tests/NoSettings", FileFormat::Yaml).required(false));
11+
let res = c.build();
1112

1213
assert!(res.is_ok());
1314
}
1415

1516
#[test]
1617
fn test_file_required_not_found() {
17-
let mut c = Config::default();
18-
let res = c.merge(File::new("tests/NoSettings", FileFormat::Yaml));
18+
let mut c = Config::builder();
19+
c.add_source(File::new("tests/NoSettings", FileFormat::Yaml));
20+
let res = c.build();
1921

2022
assert!(res.is_err());
2123
assert_eq!(
@@ -26,18 +28,20 @@ fn test_file_required_not_found() {
2628

2729
#[test]
2830
fn test_file_auto() {
29-
let mut c = Config::default();
30-
c.merge(File::with_name("tests/Settings-production"))
31-
.unwrap();
31+
let mut builder = Config::builder();
32+
builder.add_source(File::with_name("tests/Settings-production"));
33+
34+
let c = builder.build().unwrap();
3235

3336
assert_eq!(c.get("debug").ok(), Some(false));
3437
assert_eq!(c.get("production").ok(), Some(true));
3538
}
3639

3740
#[test]
3841
fn test_file_auto_not_found() {
39-
let mut c = Config::default();
40-
let res = c.merge(File::with_name("tests/NoSettings"));
42+
let mut c = Config::builder();
43+
c.add_source(File::with_name("tests/NoSettings"));
44+
let res = c.build();
4145

4246
assert!(res.is_err());
4347
assert_eq!(
@@ -48,8 +52,10 @@ fn test_file_auto_not_found() {
4852

4953
#[test]
5054
fn test_file_ext() {
51-
let mut c = Config::default();
52-
c.merge(File::with_name("tests/Settings.json")).unwrap();
55+
let mut builder = Config::builder();
56+
builder.add_source(File::with_name("tests/Settings.json"));
57+
58+
let c = builder.build().unwrap();
5359

5460
assert_eq!(c.get("debug").ok(), Some(true));
5561
assert_eq!(c.get("production").ok(), Some(false));

tests/file_hjson.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ struct Settings {
3535
}
3636

3737
fn make() -> Config {
38-
let mut c = Config::default();
39-
c.merge(File::new("tests/Settings", FileFormat::Hjson))
40-
.unwrap();
41-
42-
c
38+
let mut c = Config::builder();
39+
c.add_source(File::new("tests/Settings", FileFormat::Hjson));
40+
c.build().unwrap()
4341
}
4442

4543
#[test]
@@ -68,8 +66,9 @@ fn test_file() {
6866

6967
#[test]
7068
fn test_error_parse() {
71-
let mut c = Config::default();
72-
let res = c.merge(File::new("tests/Settings-invalid", FileFormat::Hjson));
69+
let mut c = Config::builder();
70+
c.add_source(File::new("tests/Settings-invalid", FileFormat::Hjson));
71+
let res = c.build();
7372

7473
let path: PathBuf = ["tests", "Settings-invalid.hjson"].iter().collect();
7574

tests/file_ini.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ struct Settings {
2828
}
2929

3030
fn make() -> Config {
31-
let mut c = Config::default();
32-
c.merge(File::new("tests/Settings", FileFormat::Ini))
33-
.unwrap();
34-
c
31+
let mut c = Config::builder();
32+
c.add_source(File::new("tests/Settings", FileFormat::Ini));
33+
c.build().unwrap()
3534
}
3635

3736
#[test]
@@ -56,8 +55,9 @@ fn test_file() {
5655

5756
#[test]
5857
fn test_error_parse() {
59-
let mut c = Config::default();
60-
let res = c.merge(File::new("tests/Settings-invalid", FileFormat::Ini));
58+
let mut c = Config::builder();
59+
c.add_source(File::new("tests/Settings-invalid", FileFormat::Ini));
60+
let res = c.build();
6161

6262
let path: PathBuf = ["tests", "Settings-invalid.ini"].iter().collect();
6363

tests/file_json.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ struct Settings {
3535
}
3636

3737
fn make() -> Config {
38-
let mut c = Config::default();
39-
c.merge(File::new("tests/Settings", FileFormat::Json))
40-
.unwrap();
41-
42-
c
38+
let mut c = Config::builder();
39+
c.add_source(File::new("tests/Settings", FileFormat::Json));
40+
c.build().unwrap()
4341
}
4442

4543
#[test]
@@ -68,8 +66,9 @@ fn test_file() {
6866

6967
#[test]
7068
fn test_error_parse() {
71-
let mut c = Config::default();
72-
let res = c.merge(File::new("tests/Settings-invalid", FileFormat::Json));
69+
let mut c = Config::builder();
70+
c.add_source(File::new("tests/Settings-invalid", FileFormat::Json));
71+
let res = c.build();
7372

7473
let path_with_extension: PathBuf = ["tests", "Settings-invalid.json"].iter().collect();
7574

@@ -85,17 +84,17 @@ fn test_error_parse() {
8584

8685
#[test]
8786
fn test_json_vec() {
88-
let c = Config::default()
89-
.merge(File::from_str(
90-
r#"
87+
let mut builder = Config::builder();
88+
builder.add_source(File::from_str(
89+
r#"
9190
{
9291
"WASTE": ["example_dir1", "example_dir2"]
9392
}
9493
"#,
95-
FileFormat::Json,
96-
))
97-
.unwrap()
98-
.clone();
94+
FileFormat::Json,
95+
));
96+
97+
let c = builder.build().unwrap();
9998

10099
let v = c.get_array("WASTE").unwrap();
101100
let mut vi = v.into_iter();

0 commit comments

Comments
 (0)