use super::*;
use anyhow::Result;
use clap::Parser;
use std::io::Write;
use std::path::{Path, PathBuf};
#[derive(Parser, Debug)]
struct TestCli {}
type TestRepl = Repl<TestCli>;
#[test]
fn test_history_path_parsing() -> Result<()> {
let no_path: Option<PathBuf> = TestRepl::get_history_file_path(None);
assert_eq!(None, no_path);
let just_a_filename = TestRepl::get_history_file_path(Some("a_test_file.txt".to_string()));
let mut home_dir = dirs::home_dir().unwrap();
home_dir.push("a_test_file.txt");
assert_eq!(home_dir, just_a_filename.unwrap());
let mut tempfile = tempfile::NamedTempFile::new()?;
let real_file: String = tempfile.path().to_path_buf().to_str().unwrap().to_string();
tempfile.write_all("some_test_data".as_bytes())?;
info!("The tempfile is {}", real_file);
let relative_path_to_real_file = TestRepl::get_history_file_path(Some(real_file.clone()));
tempfile.close()?;
assert_eq!(
Path::new(&real_file).to_path_buf(),
relative_path_to_real_file.unwrap()
);
let mut tempdir = tempfile::tempdir()?.into_path();
let directory_plus_default_filename =
TestRepl::get_history_file_path(Some(tempdir.to_str().unwrap().to_string()));
tempdir.push(super::DEFAULT_HISTORY_FILE_NAME);
assert_eq!(tempdir, directory_plus_default_filename.unwrap());
let bad_path = "/some/fake/path.txt".to_string();
let no_file = TestRepl::get_history_file_path(Some(bad_path));
assert_eq!(None, no_file);
Ok(())
}