Skip to content

Commit ae60045

Browse files
committed
tail: Fix printed header for stdin should be the same on all platforms
1 parent 08e8b40 commit ae60045

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/uu/tail/src/paths.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use std::ffi::OsStr;
1010
use std::fs::{File, Metadata};
1111
use std::io::{Seek, SeekFrom};
1212
#[cfg(unix)]
13-
use std::os::unix::fs::{FileTypeExt, MetadataExt};
13+
use std::os::unix::{
14+
fs::{FileTypeExt, MetadataExt},
15+
prelude::OsStrExt,
16+
};
1417
use std::path::{Path, PathBuf};
1518
use uucore::error::UResult;
1619

@@ -20,6 +23,30 @@ pub enum InputKind {
2023
Stdin,
2124
}
2225

26+
#[cfg(unix)]
27+
impl From<&OsStr> for InputKind {
28+
fn from(value: &OsStr) -> Self {
29+
const DASH: [u8; 1] = [b'-'];
30+
31+
if value.as_bytes() == DASH {
32+
Self::Stdin
33+
} else {
34+
Self::File(PathBuf::from(value))
35+
}
36+
}
37+
}
38+
39+
#[cfg(not(unix))]
40+
impl From<&OsStr> for InputKind {
41+
fn from(value: &OsStr) -> Self {
42+
if value == OsStr::new(text::DASH) {
43+
Self::Stdin
44+
} else {
45+
Self::File(PathBuf::from(value))
46+
}
47+
}
48+
}
49+
2350
#[derive(Debug, Clone)]
2451
pub struct Input {
2552
kind: InputKind,
@@ -29,21 +56,11 @@ pub struct Input {
2956
impl Input {
3057
pub fn from<T: AsRef<OsStr>>(string: T) -> Self {
3158
let string = string.as_ref();
32-
let kind = if string == OsStr::new(text::DASH) {
33-
InputKind::Stdin
34-
} else {
35-
InputKind::File(PathBuf::from(string))
36-
};
3759

60+
let kind = string.into();
3861
let display_name = match kind {
3962
InputKind::File(_) => string.to_string_lossy().to_string(),
40-
InputKind::Stdin => {
41-
if cfg!(unix) {
42-
text::STDIN_HEADER.to_string()
43-
} else {
44-
string.to_string_lossy().to_string()
45-
}
46-
}
63+
InputKind::Stdin => text::STDIN_HEADER.to_string(),
4764
};
4865

4966
Self { kind, display_name }

tests/by-util/test_tail.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,8 @@ fn test_stdin_redirect_offset() {
145145
}
146146

147147
#[test]
148-
#[cfg(all(not(target_vendor = "apple"), not(target_os = "windows")))] // FIXME: for currently not working platforms
148+
#[cfg(all(not(target_vendor = "apple")))] // FIXME: for currently not working platforms
149149
fn test_stdin_redirect_offset2() {
150-
// FIXME: windows: Failed because of difference in printed header. See below.
151-
// actual : ==> - <==
152-
// expected: ==> standard input <==
153-
154150
// like test_stdin_redirect_offset but with multiple files
155151

156152
let ts = TestScenario::new(util_name!());

0 commit comments

Comments
 (0)