Skip to content

Commit

Permalink
Add tests for loading user configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Krzesimir Nowak <krzesimir@kinvolk.io>
  • Loading branch information
krnowak committed Nov 3, 2017
1 parent 5ff6caf commit ebea3b6
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions components/sup/src/manager/service/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ fn is_toml_value_a_table(key: &str, table: &toml::value::Table) -> bool {

#[cfg(test)]
mod test {
use std::fs;
use std::fs::OpenOptions;

use toml;
use tempdir::TempDir;

Expand Down Expand Up @@ -651,6 +654,113 @@ mod test {
}
}

struct TestPkg {
base_path: PathBuf,
}

impl TestPkg {
fn new(tmp: &TempDir) -> Self {
let pkg = Self { base_path: tmp.path().to_owned() };

fs::create_dir_all(pkg.default_config_dir()).expect(
"create deprecated user config dir",
);
fs::create_dir_all(pkg.recommended_user_config_dir())
.expect("create recommended user config dir");
fs::create_dir_all(pkg.deprecated_user_config_dir())
.expect("create default config dir");
pkg
}
}

impl PackageConfigPaths for TestPkg {
fn name(&self) -> String {
String::from("testing")
}
fn default_config_dir(&self) -> PathBuf {
self.base_path.join("root")
}
fn recommended_user_config_dir(&self) -> PathBuf {
self.base_path.join("user")
}
fn deprecated_user_config_dir(&self) -> PathBuf {
self.base_path.join("svc")
}
}

struct CfgTestData {
// We hold tmp here only to make sure that the temporary
// directory gets deleted at the end of the test.
#[allow(dead_code)]
tmp: TempDir,
pkg: TestPkg,
rucp: PathBuf,
ducp: PathBuf,
}

impl CfgTestData {
fn new() -> Self {
let tmp = TempDir::new("habitat_config_test").expect("create temp dir");
let pkg = TestPkg::new(&tmp);
let rucp = pkg.recommended_user_config_dir().join("user.toml");
let ducp = pkg.deprecated_user_config_dir().join("user.toml");
Self {
tmp: tmp,
pkg: pkg,
rucp: rucp,
ducp: ducp,
}
}
}

fn write_toml<P: AsRef<Path>>(path: &P, text: &str) {
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(path)
.expect("create toml file");
file.write_all(text.as_bytes()).expect(
"write raw toml value",
);
file.flush().expect("flush changes in toml file");
}

fn toml_value_from_str(text: &str) -> toml::Value {
toml::Value::Table(toml_from_str(text))
}

#[test]
fn load_deprecated_user_toml() {
let cfg_data = CfgTestData::new();
let toml = "foo = 42";
write_toml(&cfg_data.ducp, toml);
let cfg = Cfg::new(&cfg_data.pkg, None).expect("create config");

assert_eq!(cfg.user, Some(toml_value_from_str(toml)));
}

#[test]
fn load_recommended_user_toml() {
let cfg_data = CfgTestData::new();
let toml = "foo = 42";
write_toml(&cfg_data.rucp, toml);
let cfg = Cfg::new(&cfg_data.pkg, None).expect("create config");

assert_eq!(cfg.user, Some(toml_value_from_str(toml)));
}

#[test]
fn prefer_recommended_to_deprecated() {
let cfg_data = CfgTestData::new();
let rtoml = "foo = 42";
write_toml(&cfg_data.rucp, rtoml);
write_toml(&cfg_data.ducp, "foo = 13");
let cfg = Cfg::new(&cfg_data.pkg, None).expect("create config");

assert_eq!(cfg.user, Some(toml_value_from_str(rtoml)));
}

#[test]
fn serialize_config() {
let pkg_id = PackageIdent::new("testing", "testing", Some("1.0.0"), Some("20170712000000"));
Expand Down

0 comments on commit ebea3b6

Please sign in to comment.