-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Tail macos stdin ran from script fix #7844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cf2331a
3c2e83a
6b5f6dd
ecc8a9a
7bc5f8c
7008e79
a0e4f60
f30d772
e04a0dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,6 +189,29 @@ fn tail_stdin( | |
input: &Input, | ||
observer: &mut Observer, | ||
) -> UResult<()> { | ||
// on macOS, resolve() will always return None for stdin, | ||
// we need to detect if stdin is a directory ourselves. | ||
// fstat-ing certain descriptors under /dev/fd fails with | ||
// bad file descriptor or might not catch directory cases | ||
// e.g. see the differences between running ls -l /dev/stdin /dev/fd/0 | ||
// on macOS and Linux. | ||
#[cfg(target_os = "macos")] | ||
{ | ||
if let Ok(mut stdin_handle) = Handle::stdin() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and add a comment here why macos is special :) |
||
if let Ok(meta) = stdin_handle.as_file_mut().metadata() { | ||
if meta.file_type().is_dir() { | ||
set_exit_code(1); | ||
show_error!( | ||
"cannot open '{}' for reading: {}", | ||
input.display_name, | ||
text::NO_SUCH_FILE | ||
); | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
match input.resolve() { | ||
// fifo | ||
Some(path) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -346,6 +346,55 @@ fn test_stdin_redirect_dir_when_target_os_is_macos() { | |
.stderr_is("tail: cannot open 'standard input' for reading: No such file or directory\n"); | ||
} | ||
|
||
#[test] | ||
#[cfg(unix)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems that it is failing on freebsd:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for pointing this out, subtleties in FreeBSD |
||
fn test_stdin_via_script_redirection_and_pipe() { | ||
// $ touch file.txt | ||
// $ echo line1 > file.txt | ||
// $ echo line2 >> file.txt | ||
// $ chmod +x test.sh | ||
// $ ./test.sh < file.txt | ||
// line1 | ||
// line2 | ||
// $ cat file.txt | ./test.sh | ||
// line1 | ||
// line2 | ||
use std::os::unix::fs::PermissionsExt; | ||
|
||
let scene = TestScenario::new(util_name!()); | ||
let at = &scene.fixtures; | ||
let data = "line1\nline2\n"; | ||
|
||
at.write("file.txt", data); | ||
|
||
let mut script = at.make_file("test.sh"); | ||
writeln!(script, "#!/usr/bin/env sh").unwrap(); | ||
writeln!(script, "tail").unwrap(); | ||
script | ||
.set_permissions(PermissionsExt::from_mode(0o755)) | ||
.unwrap(); | ||
|
||
drop(script); // close the file handle to ensure file is not busy | ||
|
||
// test with redirection | ||
scene | ||
.cmd("sh") | ||
.current_dir(at.plus("")) | ||
.arg("-c") | ||
.arg("./test.sh < file.txt") | ||
.succeeds() | ||
.stdout_only(data); | ||
|
||
// test with pipe | ||
scene | ||
.cmd("sh") | ||
.current_dir(at.plus("")) | ||
.arg("-c") | ||
.arg("cat file.txt | ./test.sh") | ||
.succeeds() | ||
.stdout_only(data); | ||
} | ||
|
||
#[test] | ||
fn test_follow_stdin_descriptor() { | ||
let ts = TestScenario::new(util_name!()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a comment explaining why macos is empty