Skip to content

Commit 07668ea

Browse files
committed
test(cli): add PTY-based integration tests for normal and staged-diff playback shutdown
1 parent 782e28f commit 07668ea

1 file changed

Lines changed: 67 additions & 1 deletion

File tree

‎tests/cli_non_interactive.rs‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use anyhow::Result;
22
use git2::{Repository, Signature, Time};
33
use std::fs;
4+
use std::io::Write;
45
use std::path::{Path, PathBuf};
5-
use std::process::{Command, Output};
6+
use std::process::{Command, Output, Stdio};
67
use std::sync::atomic::{AtomicU64, Ordering};
78
use std::time::{SystemTime, UNIX_EPOCH};
89

@@ -132,6 +133,37 @@ fn run_command(command: &mut Command) -> Result<Output> {
132133
Ok(command.output()?)
133134
}
134135

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+
135167
fn stdout(output: &Output) -> String {
136168
String::from_utf8(output.stdout.clone()).unwrap()
137169
}
@@ -226,3 +258,37 @@ fn default_playback_fails_only_after_ui_startup_without_tty() -> Result<()> {
226258

227259
Ok(())
228260
}
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

Comments
 (0)