Lines
90.24 %
Functions
33.33 %
Branches
100 %
use std::fs;
use std::io::{Error, ErrorKind};
use std::process::Command;
/// Regression test: cdup should exit gracefully (not panic) when the current
/// working directory no longer exists. This can happen when switching git
/// branches while inside a directory that only exists on the previous branch.
#[cfg(unix)]
#[test]
fn cd_in_nonexistent_directory_does_not_panic() {
use std::os::unix::process::CommandExt;
// GIVEN a temporary directory with a subdirectory
let tmp = tempfile::tempdir().expect("failed to create temp dir");
let dir = tmp.path().join("ephemeral");
fs::create_dir(&dir).expect("failed to create dir");
let bin = env!("CARGO_BIN_EXE_cdup");
// WHEN we run cdup cd 1 in the subdirectory and remove the subdirectory
// after the fork but before exec using pre_exec.
let dir_clone = dir.clone();
let output = unsafe {
Command::new(bin)
.current_dir(&dir)
.arg("cd")
.arg("1")
.pre_exec(move || {
fs::remove_dir(&dir_clone).map_err(|e| Error::new(ErrorKind::Other, e))
})
.output()
}
.expect("failed to execute cdup");
// THEN we do not panic, even though the child process starts with a
// nonexistent cwd.
assert!(!output.status.success(), "should exit with error");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(!stderr.contains("panicked"), "cdup panicked: {}", stderr);
assert!(
stderr.contains("Could not get current working directory"),
"unexpected error message: {}",
stderr
);
fn hint_in_nonexistent_directory_does_not_panic() {
// WHEN we run cdup hint in the subdirectory and remove the subdirectory
.arg("hint")