From 8deb88804743c295f756d405afc58313fa3601fd Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 8 Aug 2024 13:01:21 +0100 Subject: test(remote): helper to run as git remote helper add a test helper function to enable the git remote helper to be run as intended (via git) rather than calling it directly --- test_utils/src/lib.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'test_utils/src/lib.rs') diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs index ff3833d..3270a79 100644 --- a/test_utils/src/lib.rs +++ b/test_utils/src/lib.rs @@ -740,6 +740,18 @@ impl CliTester { } } + pub fn new_git_with_remote_helper_from_dir(dir: &PathBuf, args: I) -> Self + where + I: IntoIterator, + S: AsRef, + { + Self { + rexpect_session: git_with_remote_helper_rexpect_with_from_dir(dir, args, 3000) + .expect("rexpect to spawn new process"), + formatter: ColorfulTheme::default(), + } + } + pub fn restart_with(&mut self, args: I) -> &mut Self where I: IntoIterator, @@ -987,6 +999,55 @@ pub fn remote_helper_rexpect_with_from_dir( ) } +pub fn git_with_remote_helper_rexpect_with_from_dir( + dir: &PathBuf, + args: I, + timeout_ms: u64, +) -> Result +where + I: IntoIterator, + S: AsRef, +{ + let git_exec_dir = dir.parent().unwrap().join("tmpgit-git-exec-path"); + if !git_exec_dir.exists() { + std::fs::create_dir_all(&git_exec_dir)?; + let src = PathBuf::from( + String::from_utf8_lossy( + &std::process::Command::new("git") + .arg("--exec-path") + .output()? + .stdout, + ) + .trim() + .to_string(), + ); + for entry in std::fs::read_dir(src)? { + let src_path = entry?.path(); + std::fs::copy(&src_path, &git_exec_dir.join(src_path.file_name().unwrap()))?; + } + } + std::fs::copy( + assert_cmd::cargo::cargo_bin("git-remote-nostr"), + git_exec_dir.join("git-remote-nostr"), + )?; + + let mut cmd = std::process::Command::new("git"); + cmd.env("GIT_EXEC_PATH", git_exec_dir); + cmd.env("NGITTEST", "TRUE"); + cmd.env("RUST_BACKTRACE", "0"); + cmd.current_dir(dir); + cmd.args(args); + // using branch for PR https://github.com/rust-cli/rexpect/pull/103 to strip ansi escape codes + rexpect::session::spawn_with_options( + cmd, + Options { + timeout_ms: Some(timeout_ms), + strip_ansi_escape_codes: true, + }, + ) + .context("spawning failed") +} + /** copied from client.rs */ async fn get_local_cache_database(git_repo_path: &Path) -> Result { SQLiteDatabase::open(git_repo_path.join(".git/nostr-cache.sqlite")) -- cgit v1.2.3