|
1 | 1 | use anyhow::Result; |
2 | 2 | use git2::{Repository, Signature, Time}; |
3 | 3 | use std::fs; |
| 4 | +use std::io::Write; |
4 | 5 | use std::path::{Path, PathBuf}; |
5 | | -use std::process::{Command, Output}; |
| 6 | +use std::process::{Command, Output, Stdio}; |
6 | 7 | use std::sync::atomic::{AtomicU64, Ordering}; |
7 | 8 | use std::time::{SystemTime, UNIX_EPOCH}; |
8 | 9 |
|
@@ -132,6 +133,37 @@ fn run_command(command: &mut Command) -> Result<Output> { |
132 | 133 | Ok(command.output()?) |
133 | 134 | } |
134 | 135 |
|
| 136 | +fn shell_quote(value: &str) -> String { |
| 137 | + format!("'{}'", value.replace('\'', "'\\''")) |
| 138 | +} |
| 139 | + |
| 140 | +fn run_in_pseudo_tty(home: &TempHome, args: &[&str], input: &[u8]) -> Result<Output> { |
| 141 | + let command = std::iter::once(shell_quote(env!("CARGO_BIN_EXE_gitlogue"))) |
| 142 | + .chain(args.iter().map(|arg| shell_quote(arg))) |
| 143 | + .collect::<Vec<_>>() |
| 144 | + .join(" "); |
| 145 | + let mut child = Command::new("script") |
| 146 | + .args(["-qefc", &command, "/dev/null"]) |
| 147 | + .env("HOME", &home.path) |
| 148 | + .env("USERPROFILE", &home.path) |
| 149 | + .env_remove("HOMEDRIVE") |
| 150 | + .env_remove("HOMEPATH") |
| 151 | + .env("TERM", "xterm-256color") |
| 152 | + .env("COLUMNS", "100") |
| 153 | + .env("LINES", "40") |
| 154 | + .stdin(Stdio::piped()) |
| 155 | + .stdout(Stdio::piped()) |
| 156 | + .stderr(Stdio::piped()) |
| 157 | + .spawn()?; |
| 158 | + child |
| 159 | + .stdin |
| 160 | + .as_mut() |
| 161 | + .expect("script stdin should be piped") |
| 162 | + .write_all(input)?; |
| 163 | + let _ = child.stdin.take(); |
| 164 | + Ok(child.wait_with_output()?) |
| 165 | +} |
| 166 | + |
135 | 167 | fn stdout(output: &Output) -> String { |
136 | 168 | String::from_utf8(output.stdout.clone()).unwrap() |
137 | 169 | } |
@@ -226,3 +258,37 @@ fn default_playback_fails_only_after_ui_startup_without_tty() -> Result<()> { |
226 | 258 |
|
227 | 259 | Ok(()) |
228 | 260 | } |
| 261 | + |
| 262 | +#[test] |
| 263 | +fn default_playback_quits_cleanly_in_pseudo_tty() -> Result<()> { |
| 264 | + let home = TempHome::new()?; |
| 265 | + let repo = TestRepo::new()?; |
| 266 | + repo.commit_file("src/main.rs", "fn main() {}\n", "initial", 1)?; |
| 267 | + |
| 268 | + let output = run_in_pseudo_tty(&home, &["--path", repo_path(&repo).to_str().unwrap()], b"q")?; |
| 269 | + |
| 270 | + assert!(output.status.success()); |
| 271 | + assert_eq!(stderr(&output), ""); |
| 272 | + |
| 273 | + Ok(()) |
| 274 | +} |
| 275 | + |
| 276 | +#[test] |
| 277 | +fn diff_subcommand_with_staged_changes_quits_cleanly_in_pseudo_tty() -> Result<()> { |
| 278 | + let home = TempHome::new()?; |
| 279 | + let repo = TestRepo::new()?; |
| 280 | + repo.commit_file("src/lib.rs", "fn clean() {}\n", "initial", 1)?; |
| 281 | + repo.write_file("src/lib.rs", "fn staged() {}\n")?; |
| 282 | + repo.stage_file("src/lib.rs")?; |
| 283 | + |
| 284 | + let output = run_in_pseudo_tty( |
| 285 | + &home, |
| 286 | + &["--path", repo_path(&repo).to_str().unwrap(), "diff"], |
| 287 | + b"q", |
| 288 | + )?; |
| 289 | + |
| 290 | + assert!(output.status.success()); |
| 291 | + assert_eq!(stderr(&output), ""); |
| 292 | + |
| 293 | + Ok(()) |
| 294 | +} |
0 commit comments