Skip to content

Commit f4a0bb3

Browse files
authored
more: replace nix::unistd with std::fs (#12268)
1 parent eca185b commit f4a0bb3

2 files changed

Lines changed: 23 additions & 22 deletions

File tree

tests/by-util/test_more.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
#[cfg(unix)]
7-
use nix::unistd::{read, write};
86
#[cfg(unix)]
97
use std::fs::File;
108
#[cfg(unix)]
119
use std::fs::{Permissions, set_permissions};
10+
#[cfg(unix)]
11+
use std::io::{Read as _, Write as _};
1212
#[cfg(target_os = "linux")]
1313
use std::os::unix::ffi::OsStrExt;
1414
#[cfg(unix)]
@@ -23,8 +23,8 @@ fn run_more_with_pty(
2323
args: &[&str],
2424
file: &str,
2525
content: &str,
26-
) -> (uutests::util::UChild, std::os::fd::OwnedFd, String) {
27-
let (path, controller, _replica) = pty_path();
26+
) -> (uutests::util::UChild, File, String) {
27+
let (path, mut controller, _replica) = pty_path();
2828
let (at, mut ucmd) = at_and_ucmd!();
2929
at.write(file, content);
3030

@@ -37,15 +37,15 @@ fn run_more_with_pty(
3737

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

4343
(child, controller, output_str)
4444
}
4545

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

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

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

109109
// cleanup
110-
write(&controller, b"q").unwrap();
110+
controller.write_all(b"q").unwrap();
111111
child.delay(50);
112112
}
113113

@@ -217,39 +217,40 @@ fn test_more_non_utf8_paths() {
217217
#[test]
218218
#[cfg(unix)]
219219
fn test_basic_display() {
220-
let (child, controller, output) = run_more_with_pty(&[], "test.txt", "line1\nline2\nline3\n");
220+
let (child, mut controller, output) =
221+
run_more_with_pty(&[], "test.txt", "line1\nline2\nline3\n");
221222
assert!(output.contains("line1"));
222-
quit_more(&controller, child);
223+
quit_more(&mut controller, child);
223224
}
224225

225226
#[test]
226227
#[cfg(unix)]
227228
fn test_squeeze_blank_lines() {
228-
let (child, controller, output) =
229+
let (child, mut controller, output) =
229230
run_more_with_pty(&["-s"], "test.txt", "line1\n\n\n\nline2\n");
230231
assert!(output.contains("line1"));
231-
quit_more(&controller, child);
232+
quit_more(&mut controller, child);
232233
}
233234

234235
#[test]
235236
#[cfg(unix)]
236237
fn test_pattern_search() {
237-
let (child, controller, output) = run_more_with_pty(
238+
let (child, mut controller, output) = run_more_with_pty(
238239
&["-P", "target"],
239240
"test.txt",
240241
"foo\nbar\nbaz\ntarget\nend\n",
241242
);
242243
assert!(output.contains("target"));
243244
assert!(!output.contains("foo"));
244-
quit_more(&controller, child);
245+
quit_more(&mut controller, child);
245246
}
246247

247248
#[test]
248249
#[cfg(unix)]
249250
fn test_from_line_option() {
250-
let (child, controller, output) =
251+
let (child, mut controller, output) =
251252
run_more_with_pty(&["-F", "2"], "test.txt", "line1\nline2\nline3\nline4\n");
252253
assert!(output.contains("line2"));
253254
assert!(!output.contains("line1"));
254-
quit_more(&controller, child);
255+
quit_more(&mut controller, child);
255256
}

tests/uutests/src/lib/util.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,20 +2941,20 @@ pub fn whoami() -> String {
29412941

29422942
/// Create a PTY (pseudo-terminal) for testing utilities that require a TTY.
29432943
///
2944-
/// Returns a tuple of (path, controller_fd, replica_fd) where:
2944+
/// Returns a tuple of (path, controller, replica) where:
29452945
/// - path: The filesystem path to the PTY replica device
2946-
/// - controller_fd: The controller file descriptor
2947-
/// - replica_fd: The replica file descriptor
2946+
/// - controller: The controller file
2947+
/// - replica: The replica file
29482948
#[cfg(unix)]
2949-
pub fn pty_path() -> (String, OwnedFd, OwnedFd) {
2949+
pub fn pty_path() -> (String, File, File) {
29502950
use nix::pty::openpty;
29512951
use nix::unistd::ttyname;
29522952
let pty = openpty(None, None).expect("Failed to create PTY");
29532953
let path = ttyname(&pty.slave)
29542954
.expect("Failed to get PTY path")
29552955
.to_string_lossy()
29562956
.to_string();
2957-
(path, pty.master, pty.slave)
2957+
(path, pty.master.into(), pty.slave.into())
29582958
}
29592959

29602960
/// Add prefix 'g' for `util_name` if not on linux

0 commit comments

Comments
 (0)