From ae60045f3f0cf35c4f893c02f22d3c93b396b37f Mon Sep 17 00:00:00 2001 From: Joining7943 <111500881+Joining7943@users.noreply.github.com> Date: Thu, 20 Apr 2023 21:12:24 +0200 Subject: [PATCH] tail: Fix printed header for stdin should be the same on all platforms --- src/uu/tail/src/paths.rs | 43 ++++++++++++++++++++++++++------------ tests/by-util/test_tail.rs | 6 +----- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/uu/tail/src/paths.rs b/src/uu/tail/src/paths.rs index d813ea942..cce3270a8 100644 --- a/src/uu/tail/src/paths.rs +++ b/src/uu/tail/src/paths.rs @@ -10,7 +10,10 @@ use std::ffi::OsStr; use std::fs::{File, Metadata}; use std::io::{Seek, SeekFrom}; #[cfg(unix)] -use std::os::unix::fs::{FileTypeExt, MetadataExt}; +use std::os::unix::{ + fs::{FileTypeExt, MetadataExt}, + prelude::OsStrExt, +}; use std::path::{Path, PathBuf}; use uucore::error::UResult; @@ -20,6 +23,30 @@ pub enum InputKind { Stdin, } +#[cfg(unix)] +impl From<&OsStr> for InputKind { + fn from(value: &OsStr) -> Self { + const DASH: [u8; 1] = [b'-']; + + if value.as_bytes() == DASH { + Self::Stdin + } else { + Self::File(PathBuf::from(value)) + } + } +} + +#[cfg(not(unix))] +impl From<&OsStr> for InputKind { + fn from(value: &OsStr) -> Self { + if value == OsStr::new(text::DASH) { + Self::Stdin + } else { + Self::File(PathBuf::from(value)) + } + } +} + #[derive(Debug, Clone)] pub struct Input { kind: InputKind, @@ -29,21 +56,11 @@ pub struct Input { impl Input { pub fn from>(string: T) -> Self { let string = string.as_ref(); - let kind = if string == OsStr::new(text::DASH) { - InputKind::Stdin - } else { - InputKind::File(PathBuf::from(string)) - }; + let kind = string.into(); let display_name = match kind { InputKind::File(_) => string.to_string_lossy().to_string(), - InputKind::Stdin => { - if cfg!(unix) { - text::STDIN_HEADER.to_string() - } else { - string.to_string_lossy().to_string() - } - } + InputKind::Stdin => text::STDIN_HEADER.to_string(), }; Self { kind, display_name } diff --git a/tests/by-util/test_tail.rs b/tests/by-util/test_tail.rs index f3e55e434..2391a5f7a 100644 --- a/tests/by-util/test_tail.rs +++ b/tests/by-util/test_tail.rs @@ -145,12 +145,8 @@ fn test_stdin_redirect_offset() { } #[test] -#[cfg(all(not(target_vendor = "apple"), not(target_os = "windows")))] // FIXME: for currently not working platforms +#[cfg(all(not(target_vendor = "apple")))] // FIXME: for currently not working platforms fn test_stdin_redirect_offset2() { - // FIXME: windows: Failed because of difference in printed header. See below. - // actual : ==> - <== - // expected: ==> standard input <== - // like test_stdin_redirect_offset but with multiple files let ts = TestScenario::new(util_name!());