Skip to content

Commit

Permalink
simulate terminal utility (squash)
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Feb 7, 2024
1 parent 6998c1a commit a803ce1
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ Cargo.lock
lib*.a
/docs/_build
*.iml
worktree*
### macOS ###
.DS_Store
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ procfs = { version = "0.16", default-features = false }
rlimit = "0.10.1"

[target.'cfg(unix)'.dev-dependencies]
nix = { workspace = true, features = ["process", "signal", "user"] }
nix = { workspace = true, features = ["process", "signal", "user", "term"] }
rand_pcg = "0.3"
xattr = { workspace = true }

Expand Down
93 changes: 93 additions & 0 deletions tests/by-util/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,96 @@ fn test_fail_change_directory() {
.stderr_move_str();
assert!(out.contains("env: cannot change directory to "));
}

#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_false() {
let scene = TestScenario::new(util_name!());

let out = scene.ucmd().arg("sh").arg("is_atty.sh").succeeds();
assert_eq!(
String::from_utf8_lossy(out.stdout()),
"stdin is not atty\nstdout is not atty\nstderr is not atty\n"
);
assert_eq!(
String::from_utf8_lossy(out.stderr()),
"This is an error message.\n"
);
}

#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_true() {
let scene = TestScenario::new(util_name!());

let out = scene
.ucmd()
.arg("sh")
.arg("is_atty.sh")
.terminal_simulation(true)
.succeeds();
assert_eq!(
String::from_utf8_lossy(out.stdout()),
"stdin is atty\r\nstdout is atty\r\nstderr is atty\r\n"
);
assert_eq!(
String::from_utf8_lossy(out.stderr()),
"This is an error message.\r\n"
);
}

#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_pty_sends_eot_automatically() {
let scene = TestScenario::new(util_name!());

let mut cmd = scene.ucmd();
cmd.timeout(std::time::Duration::from_secs(10));
cmd.args(&["cat", "-"]);
cmd.terminal_simulation(true);
let child = cmd.run_no_wait();
let out = child.wait().unwrap(); // cat would block if there is no eot

assert_eq!(String::from_utf8_lossy(out.stdout()), "\r\n");
assert_eq!(String::from_utf8_lossy(out.stderr()), "");
}

#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_pty_pipes_into_data_and_sends_eot_automatically() {
let scene = TestScenario::new(util_name!());

let message = "Hello stdin forwarding!";

let mut cmd = scene.ucmd();
cmd.args(&["cat", "-"]);
cmd.terminal_simulation(true);
cmd.pipe_in(message);
let child = cmd.run_no_wait();
let out = child.wait().unwrap();

assert_eq!(
String::from_utf8_lossy(out.stdout()),
format!("{}\r\n", message)
);
assert_eq!(String::from_utf8_lossy(out.stderr()), "");
}

#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_pty_write_in_data_and_sends_eot_automatically() {
let scene = TestScenario::new(util_name!());

let mut cmd = scene.ucmd();
cmd.args(&["cat", "-"]);
cmd.terminal_simulation(true);
let mut child = cmd.run_no_wait();
child.write_in("Hello stdin forwarding via write_in!");
let out = child.wait().unwrap();

assert_eq!(
String::from_utf8_lossy(out.stdout()),
"Hello stdin forwarding via write_in!\r\n"
);
assert_eq!(String::from_utf8_lossy(out.stderr()), "");
}
30 changes: 30 additions & 0 deletions tests/by-util/test_nohup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore winsize Openpty openpty xpixel ypixel ptyprocess
use crate::common::util::TestScenario;
use std::thread::sleep;

Expand Down Expand Up @@ -31,3 +32,32 @@ fn test_nohup_multiple_args_and_flags() {
assert!(at.file_exists("file1"));
assert!(at.file_exists("file2"));
}

#[test]
#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "freebsd",
target_vendor = "apple"
))]
fn test_nohup_with_pseudo_terminal_emulation_on_stdin_stdout_stderr_get_replaced() {
let ts = TestScenario::new(util_name!());
let result = ts
.ucmd()
.terminal_simulation(true)
.args(&["sh", "is_atty.sh"])
.succeeds();

assert_eq!(
String::from_utf8_lossy(result.stderr()).trim(),
"nohup: ignoring input and appending output to 'nohup.out'"
);

sleep(std::time::Duration::from_millis(10));

// this proves that nohup was exchanging the stdio file descriptors
assert_eq!(
std::fs::read_to_string(ts.fixtures.plus_as_string("nohup.out")).unwrap(),
"stdin is not atty\nstdout is not atty\nstderr is not atty\n"
);
}
Loading

0 comments on commit a803ce1

Please sign in to comment.