use expectrl::{spawn, Expect, Regex};
use std::io::Write;
#[test]
fn test_source_builtin() {
let bin = std::env::var("CARGO_BIN_EXE_pmsh").unwrap_or_else(|_| {
let manifest = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
format!("{}/target/debug/pmsh", manifest)
});
let mut script_file = tempfile::NamedTempFile::new().expect("failed to create temp file");
let script_path = script_file.path().to_string_lossy().to_string();
writeln!(script_file, "cd /tmp").expect("failed to write to script");
let mut p = spawn(&bin).expect("failed to spawn pmsh");
p.expect(Regex("\\$ ")).expect("did not see prompt");
p.send_line(format!("source {}", script_path))
.expect("failed to send source command");
p.expect(Regex("\\$ "))
.expect("did not see prompt after source");
let tmp_dir = tempfile::TempDir::new().expect("failed to create temp dir");
let tmp_dir_path = tmp_dir.path().to_string_lossy().to_string();
let mut script_file = tempfile::NamedTempFile::new().expect("failed to create temp file");
let script_path = script_file.path().to_string_lossy().to_string();
writeln!(script_file, "cd {}", tmp_dir_path).expect("failed to write to script");
let unique_file = tmp_dir.path().join("unique_marker_file");
std::fs::File::create(&unique_file).expect("failed to create marker file");
p.send_line(format!("source {}", script_path))
.expect("failed to send source command");
p.expect(Regex("\\$ "))
.expect("did not see prompt after source 2");
p.send_line("ls").expect("failed to send ls");
p.expect(Regex("unique_marker_file"))
.expect("did not see marker file");
}