Skip to content

Commit 29adc24

Browse files
authored
Adding TTY helper for unix to be able to create tests for stty and more (#9348)
* Adding TTY helper for unix to be able to create tests for stty and more * removing missing flag on github actions and spellcheck ignore
1 parent 17919f2 commit 29adc24

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

tests/by-util/test_stty.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,52 @@
22
//
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
5-
// spell-checker:ignore parenb parmrk ixany iuclc onlcr ofdel icanon noflsh econl igpar ispeed ospeed
5+
// spell-checker:ignore parenb parmrk ixany iuclc onlcr icanon noflsh econl igpar ispeed ospeed
66

77
use uutests::new_ucmd;
8+
use uutests::util::pty_path;
89

910
#[test]
1011
fn test_invalid_arg() {
1112
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
1213
}
1314

1415
#[test]
15-
#[ignore = "Fails because cargo test does not run in a tty"]
16-
fn runs() {
17-
new_ucmd!().succeeds();
16+
#[cfg(unix)]
17+
fn test_basic() {
18+
let (path, _controller, _replica) = pty_path();
19+
new_ucmd!()
20+
.args(&["--file", &path])
21+
.succeeds()
22+
.stdout_contains("speed");
1823
}
1924

2025
#[test]
21-
#[ignore = "Fails because cargo test does not run in a tty"]
22-
fn print_all() {
23-
let res = new_ucmd!().args(&["--all"]).succeeds();
24-
25-
// Random selection of flags to check for
26-
for flag in [
27-
"parenb", "parmrk", "ixany", "onlcr", "ofdel", "icanon", "noflsh",
28-
] {
29-
res.stdout_contains(flag);
26+
#[cfg(unix)]
27+
fn test_all_flag() {
28+
let (path, _controller, _replica) = pty_path();
29+
let result = new_ucmd!().args(&["--all", "--file", &path]).succeeds();
30+
31+
for flag in ["parenb", "parmrk", "ixany", "onlcr", "icanon", "noflsh"] {
32+
result.stdout_contains(flag);
3033
}
3134
}
3235

3336
#[test]
34-
#[ignore = "Fails because cargo test does not run in a tty"]
35-
fn sane_settings() {
36-
new_ucmd!().args(&["intr", "^A"]).succeeds();
37-
new_ucmd!().succeeds().stdout_contains("intr = ^A");
37+
#[cfg(unix)]
38+
fn test_sane() {
39+
let (path, _controller, _replica) = pty_path();
40+
41+
new_ucmd!()
42+
.args(&["--file", &path, "intr", "^A"])
43+
.succeeds();
44+
new_ucmd!()
45+
.args(&["--file", &path])
46+
.succeeds()
47+
.stdout_contains("intr = ^A");
48+
new_ucmd!().args(&["--file", &path, "sane"]).succeeds();
3849
new_ucmd!()
39-
.args(&["sane"])
50+
.args(&["--file", &path])
4051
.succeeds()
4152
.stdout_str_check(|s| !s.contains("intr = ^A"));
4253
}

tests/uutests/src/lib/util.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// file that was distributed with this source code.
55
//spell-checker: ignore (linux) rlimit prlimit coreutil ggroups uchild uncaptured scmd SHLVL canonicalized openpty
66
//spell-checker: ignore (linux) winsize xpixel ypixel setrlimit FSIZE SIGBUS SIGSEGV sigbus tmpfs mksocket
7+
//spell-checker: ignore (ToDO) ttyname
78

89
#![allow(dead_code)]
910
#![allow(
@@ -2886,6 +2887,24 @@ pub fn whoami() -> String {
28862887
})
28872888
}
28882889

2890+
/// Create a PTY (pseudo-terminal) for testing utilities that require a TTY.
2891+
///
2892+
/// Returns a tuple of (path, controller_fd, replica_fd) where:
2893+
/// - path: The filesystem path to the PTY replica device
2894+
/// - controller_fd: The controller file descriptor
2895+
/// - replica_fd: The replica file descriptor
2896+
#[cfg(unix)]
2897+
pub fn pty_path() -> (String, OwnedFd, OwnedFd) {
2898+
use nix::pty::openpty;
2899+
use nix::unistd::ttyname;
2900+
let pty = openpty(None, None).expect("Failed to create PTY");
2901+
let path = ttyname(&pty.slave)
2902+
.expect("Failed to get PTY path")
2903+
.to_string_lossy()
2904+
.to_string();
2905+
(path, pty.master, pty.slave)
2906+
}
2907+
28892908
/// Add prefix 'g' for `util_name` if not on linux
28902909
#[cfg(unix)]
28912910
pub fn host_name_for(util_name: &str) -> Cow<'_, str> {

0 commit comments

Comments
 (0)