Skip to content

Commit

Permalink
test: Add some tests of core CLI behavior
Browse files Browse the repository at this point in the history
Signed-off-by: bin liu <[email protected]>
[split from larger commit; new commit message]
Signed-off-by: Greg Price <[email protected]>
  • Loading branch information
liubin authored and gnprice committed Dec 4, 2022
1 parent 9ebd6e3 commit da7b78c
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
51 changes: 51 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ serde = "1.0"
serde_json = "1.0"
structopt = "0.3"
toml_edit = "0.15"

[dev-dependencies]
tempfile = "3.3.0"
71 changes: 70 additions & 1 deletion test/test.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,87 @@
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());
let stderr = str::from_utf8(proc.stderr.as_slice()).unwrap();
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"?
Expand Down

0 comments on commit da7b78c

Please sign in to comment.