Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions tests/by-util/test_more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#[cfg(unix)]
use nix::unistd::{read, write};
#[cfg(unix)]
use std::fs::File;
#[cfg(unix)]
use std::fs::{Permissions, set_permissions};
#[cfg(unix)]
use std::io::{Read as _, Write as _};
#[cfg(target_os = "linux")]
use std::os::unix::ffi::OsStrExt;
#[cfg(unix)]
Expand All @@ -23,8 +23,8 @@ fn run_more_with_pty(
args: &[&str],
file: &str,
content: &str,
) -> (uutests::util::UChild, std::os::fd::OwnedFd, String) {
let (path, controller, _replica) = pty_path();
) -> (uutests::util::UChild, File, String) {
let (path, mut controller, _replica) = pty_path();
let (at, mut ucmd) = at_and_ucmd!();
at.write(file, content);

Expand All @@ -37,15 +37,15 @@ fn run_more_with_pty(

child.delay(200);
let mut output = vec![0u8; 1024];
let n = read(&controller, &mut output).unwrap();
let n = controller.read(&mut output).unwrap();
let output_str = String::from_utf8_lossy(&output[..n]).to_string();

(child, controller, output_str)
}

#[cfg(unix)]
fn quit_more(controller: &std::os::fd::OwnedFd, mut child: uutests::util::UChild) {
write(controller, b"q").unwrap();
fn quit_more(controller: &mut File, mut child: uutests::util::UChild) {
controller.write_all(b"q").unwrap();
child.delay(50);
}

Expand Down Expand Up @@ -88,7 +88,7 @@ fn test_valid_arg() {
#[cfg(unix)]
fn test_alive(args: &[&str]) {
let (at, mut ucmd) = at_and_ucmd!();
let (path, controller, _replica) = pty_path();
let (path, mut controller, _replica) = pty_path();

let content = "test content";
let file = "test_file";
Expand All @@ -107,7 +107,7 @@ fn test_alive(args: &[&str]) {
assert!(child.is_alive(), "Command should still be alive");

// cleanup
write(&controller, b"q").unwrap();
controller.write_all(b"q").unwrap();
child.delay(50);
}

Expand Down Expand Up @@ -217,39 +217,40 @@ fn test_more_non_utf8_paths() {
#[test]
#[cfg(unix)]
fn test_basic_display() {
let (child, controller, output) = run_more_with_pty(&[], "test.txt", "line1\nline2\nline3\n");
let (child, mut controller, output) =
run_more_with_pty(&[], "test.txt", "line1\nline2\nline3\n");
assert!(output.contains("line1"));
quit_more(&controller, child);
quit_more(&mut controller, child);
}

#[test]
#[cfg(unix)]
fn test_squeeze_blank_lines() {
let (child, controller, output) =
let (child, mut controller, output) =
run_more_with_pty(&["-s"], "test.txt", "line1\n\n\n\nline2\n");
assert!(output.contains("line1"));
quit_more(&controller, child);
quit_more(&mut controller, child);
}

#[test]
#[cfg(unix)]
fn test_pattern_search() {
let (child, controller, output) = run_more_with_pty(
let (child, mut controller, output) = run_more_with_pty(
&["-P", "target"],
"test.txt",
"foo\nbar\nbaz\ntarget\nend\n",
);
assert!(output.contains("target"));
assert!(!output.contains("foo"));
quit_more(&controller, child);
quit_more(&mut controller, child);
}

#[test]
#[cfg(unix)]
fn test_from_line_option() {
let (child, controller, output) =
let (child, mut controller, output) =
run_more_with_pty(&["-F", "2"], "test.txt", "line1\nline2\nline3\nline4\n");
assert!(output.contains("line2"));
assert!(!output.contains("line1"));
quit_more(&controller, child);
quit_more(&mut controller, child);
}
10 changes: 5 additions & 5 deletions tests/uutests/src/lib/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2941,20 +2941,20 @@ pub fn whoami() -> String {

/// Create a PTY (pseudo-terminal) for testing utilities that require a TTY.
///
/// Returns a tuple of (path, controller_fd, replica_fd) where:
/// Returns a tuple of (path, controller, replica) where:
/// - path: The filesystem path to the PTY replica device
/// - controller_fd: The controller file descriptor
/// - replica_fd: The replica file descriptor
/// - controller: The controller file
/// - replica: The replica file
#[cfg(unix)]
pub fn pty_path() -> (String, OwnedFd, OwnedFd) {
pub fn pty_path() -> (String, File, File) {
use nix::pty::openpty;
use nix::unistd::ttyname;
let pty = openpty(None, None).expect("Failed to create PTY");
let path = ttyname(&pty.slave)
.expect("Failed to get PTY path")
.to_string_lossy()
.to_string();
(path, pty.master, pty.slave)
(path, pty.master.into(), pty.slave.into())
}

/// Add prefix 'g' for `util_name` if not on linux
Expand Down
Loading