diff --git a/Cargo.lock b/Cargo.lock index ded163e..aa9c769 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -141,6 +141,15 @@ dependencies = [ "synstructure", ] +[[package]] +name = "fastrand" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499" +dependencies = [ + "instant", +] + [[package]] name = "gimli" version = "0.26.2" @@ -181,6 +190,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + [[package]] name = "itertools" version = "0.10.5" @@ -298,6 +316,24 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + [[package]] name = "rustc-demangle" version = "0.1.21" @@ -386,6 +422,20 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "tempfile" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" +dependencies = [ + "cfg-if", + "fastrand", + "libc", + "redox_syscall", + "remove_dir_all", + "winapi", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -404,6 +454,7 @@ dependencies = [ "serde", "serde_json", "structopt", + "tempfile", "toml_edit", ] diff --git a/Cargo.toml b/Cargo.toml index 58eabb4..93ac301 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,6 @@ serde = "1.0" serde_json = "1.0" structopt = "0.3" toml_edit = "0.15" + +[dev-dependencies] +tempfile = "3.3.0" diff --git a/test/test.rs b/test/test.rs index f17eb54..1577ec9 100644 --- a/test/test.rs +++ b/test/test.rs @@ -1,11 +1,12 @@ use std::env; use std::ffi::OsString; +use std::fs; use std::path::PathBuf; use std::process; use std::str; #[test] -fn help_if_no_args() { +fn integration_test_help_if_no_args() { // Probably want to factor out much of this when adding more tests. let proc = process::Command::new(get_exec_path()).output().unwrap(); assert!(!proc.status.success()); @@ -13,6 +14,74 @@ fn help_if_no_args() { assert!(stderr.contains("-h, --help")); } +#[test] +fn integration_test_cmd_get() { + let body = r#"[a] +b = "c" +[x] +y = "z""#; + let toml_dir = tempfile::tempdir().expect("failed to create tempdir"); + let toml_file = toml_dir.path().join("test.toml"); + fs::write(&toml_file, body).expect("failed to write tempfile"); + let toml_file = toml_file.as_os_str().to_str().unwrap(); + + let cmd = process::Command::new(get_exec_path()) + .args(["get", toml_file, "x.y"]) + .output() + .unwrap(); + assert!(cmd.status.success()); + let stdout = str::from_utf8(cmd.stdout.as_slice()).unwrap(); + assert_eq!("\"z\"\n", stdout); + + // x.z does not exists + let cmd = process::Command::new(get_exec_path()) + .args(["get", toml_file, "x.z"]) + .output() + .unwrap(); + assert!(!cmd.status.success()); +} + +#[test] +fn integration_test_cmd_set() { + // fn set(path: PathBuf, query: &str, value_str: &str, opts: SetOpts) -> Result<(), Error> { + let body = r#"[a] +b = "c" +[x] +y = "z""#; + let toml_dir = tempfile::tempdir().expect("failed to create tempdir"); + let toml_file = toml_dir.path().join("test.toml"); + fs::write(&toml_file, body).expect("failed to write tempfile"); + let toml_file = toml_file.as_os_str().to_str().unwrap(); + + // x.y exists + let cmd = process::Command::new(get_exec_path()) + .args(["set", toml_file, "x.y", "new"]) + .output() + .unwrap(); + assert!(cmd.status.success()); + let stdout = str::from_utf8(cmd.stdout.as_slice()).unwrap(); + let excepted = r#"[a] +b = "c" +[x] +y = "new" +"#; + assert_eq!(excepted, stdout); + + let cmd = process::Command::new(get_exec_path()) + .args(["set", toml_file, "x.z", "123"]) + .output() + .unwrap(); + assert!(cmd.status.success()); + let stdout = str::from_utf8(cmd.stdout.as_slice()).unwrap(); + let excepted = r#"[a] +b = "c" +[x] +y = "z" +z = "123" +"#; + assert_eq!(excepted, stdout); +} + fn get_exec_path() -> PathBuf { // TODO is there no cleaner way to get this from Cargo? // Also should it really be "debug"?