use assert_cmd::Command;
use std::fs;
use std::fs::create_dir_all;
use std::path::PathBuf;
#[test]
fn gets_files() {
let temp = assert_fs::TempDir::new().unwrap();
let node_modules_path_str: PathBuf = [temp.to_str().unwrap(), "node_modules"].iter().collect();
let node_modules_file_path_str: PathBuf =
[node_modules_path_str.to_str().unwrap(), "package.json"]
.iter()
.collect();
if let Err(e) = create_dir_all(&node_modules_path_str) {
panic!("Error creating tests node_modules directory: {e}")
}
assert!(&node_modules_path_str.as_path().exists());
let temp_path = temp.to_str().unwrap().to_owned();
let dry_run_cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&["--path".to_string(), temp_path.clone(), "--dry".to_owned()])
.unwrap();
let dry_std_out = std::str::from_utf8(&dry_run_cmd.stdout).unwrap();
assert_eq!(dry_std_out,
format!("The following node_modules will be deleted:\n {temp_path}/node_modules\nArgument dry provided, no directories were deleted.\n"));
assert!(node_modules_path_str.as_path().exists());
let data = "hello";
if let Err(e) = fs::write(&node_modules_file_path_str, data) {
panic!("Error writing to temp file: {e}");
};
let hot_run_cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&["--path".to_string(), temp_path.clone()])
.unwrap();
let hot_run_stdout = std::str::from_utf8(&hot_run_cmd.stdout).unwrap();
assert_eq!(hot_run_stdout,
format!("The following node_modules will be deleted:\n {temp_path}/node_modules\nFreed 5 B bytes\n"));
assert!(!node_modules_file_path_str.exists());
assert!(!node_modules_path_str.exists());
temp.close().unwrap();
}