Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TOML serialize for package default_cfg #1914

Merged
merged 1 commit into from Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 39 additions & 2 deletions components/core/src/package/install.rs
Expand Up @@ -23,6 +23,7 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;

use toml;
use toml::Value;

use super::{Identifiable, PackageIdent, Target, PackageTarget};
use super::metadata::{Bind, MetaFile};
Expand Down Expand Up @@ -223,14 +224,21 @@ impl PackageInstall {
}

/// Read and return the decoded contents of the packages default configuration.
pub fn default_cfg(&self) -> Option<toml::value::Table> {
pub fn default_cfg(&self) -> Option<toml::value::Value> {
match File::open(self.installed_path.join(DEFAULT_CFG_FILE)) {
Ok(mut file) => {
let mut raw = String::new();
if file.read_to_string(&mut raw).is_err() {
return None;
};

match raw.parse::<Value>() {
Ok(v) => Some(v),
Err(e) => {
debug!("Failed to parse toml, error: {:?}", e);
None
}
}
toml::from_str(&raw).ok()
}
Err(_) => None,
}
Expand Down Expand Up @@ -537,3 +545,32 @@ impl fmt::Display for PackageInstall {
write!(f, "{}", self.ident)
}
}

#[cfg(test)]
mod test {
use std::str::FromStr;
use std::path::PathBuf;
use toml;
use super::super::PackageIdent;
use super::PackageInstall;
use super::super::test_support::*;

#[test]
fn can_serialize_default_config() {
let package_ident = PackageIdent::from_str("just/nothing").unwrap();
let fixture_path = fixture_path("test_package");
let package_install = PackageInstall {
ident: package_ident,
fs_root_path: PathBuf::from(""),
package_root_path: PathBuf::from(""),
installed_path: fixture_path,
};

let cfg = package_install.default_cfg().unwrap();

match toml::ser::to_string(&cfg) {
Ok(_) => (),
Err(e) => assert!(false, format!("{:?}", e)),
}
}
}
11 changes: 11 additions & 0 deletions components/core/src/package/mod.rs
Expand Up @@ -24,3 +24,14 @@ pub use self::ident::{Identifiable, PackageIdent};
pub use self::install::PackageInstall;
pub use self::plan::Plan;
pub use self::target::{Target, PackageTarget};

#[cfg(test)]
pub mod test_support {
use std::path::PathBuf;

pub fn fixture_path(name: &str) -> PathBuf {
let path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("fixtures").join(name);
path
}
}
40 changes: 40 additions & 0 deletions components/core/tests/fixtures/test_package/default.toml
@@ -0,0 +1,40 @@
port = 6379
bind = []
protected-mode = "yes"
timeout = 0
tcp-keepalive = 0
loglevel = "notice"
logfile = "\"\""
databases = 16
stop-writes-on-bgsave-error = "yes"
rbcompression = "yes"
rbchecksum = "yes"
dbfilename = "dump.rdb"
slave-serve-stale-data = "yes"
slave-read-only = "yes"
repl-diskless-sync = "no"
repl-diskless-synx-delay = 5
repl-ping-slave-period = 10
repl-timeout = 60
repl-disable-tcp-nodelay = "no"
repl-backlog-size = "1mb"
repl-backlog-ttl = "3600"
slave-priority = "100"
min-slaves-to-write = false
min-slaves-max-lag = false
requirepass = ""
appendonly = "no"
appendfsync = "everysec"
no-appendfsync-on-rewrite = "no"

[[save]]
sec = 900
keys = 1

[[save]]
sec = 300
keys = 10

[[save]]
sec = 60
keys = 10000