From 150b287fe8ab268a6ef3fded5aa0e4ce3aa6cd2d Mon Sep 17 00:00:00 2001 From: Biplab Mochan Gartia <45629823+biplab5464@users.noreply.github.com> Date: Wed, 24 Jan 2024 20:31:26 +0530 Subject: [PATCH] unexpand: allow multiple files & show error message if a directory is specified * unexpand: should allow multiple files #5852 and unexpand: show error message if a directory is specified #5845 * test file added for #5845 #5852 * test case test_multiple_files improve * cakebaker suggestion for a better code #5845 #5852 --------- Co-authored-by: biplab5464 --- src/uu/unexpand/src/unexpand.rs | 18 +++++++++++++----- tests/by-util/test_unexpand.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/uu/unexpand/src/unexpand.rs b/src/uu/unexpand/src/unexpand.rs index 66d9a0187..7336376eb 100644 --- a/src/uu/unexpand/src/unexpand.rs +++ b/src/uu/unexpand/src/unexpand.rs @@ -11,11 +11,12 @@ use std::fmt; use std::fs::File; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Stdout, Write}; use std::num::IntErrorKind; +use std::path::Path; use std::str::from_utf8; use unicode_width::UnicodeWidthChar; use uucore::display::Quotable; use uucore::error::{FromIo, UError, UResult, USimpleError}; -use uucore::{crash_if_err, format_usage, help_about, help_usage}; +use uucore::{crash_if_err, format_usage, help_about, help_usage, show}; const USAGE: &str = help_usage!("unexpand.md"); const ABOUT: &str = help_about!("unexpand.md"); @@ -105,8 +106,8 @@ impl Options { && !matches.get_flag(options::FIRST_ONLY); let uflag = !matches.get_flag(options::NO_UTF8); - let files = match matches.get_one::(options::FILE) { - Some(v) => vec![v.to_string()], + let files = match matches.get_many::(options::FILE) { + Some(v) => v.cloned().collect(), None => vec!["-".to_owned()], }; @@ -211,7 +212,13 @@ pub fn uu_app() -> Command { fn open(path: &str) -> UResult>> { let file_buf; - if path == "-" { + let filename = Path::new(path); + if filename.is_dir() { + Err(Box::new(USimpleError { + code: 1, + message: format!("{}: Is a directory", filename.display()), + })) + } else if path == "-" { Ok(BufReader::new(Box::new(stdin()) as Box)) } else { file_buf = File::open(path).map_err_context(|| path.to_string())?; @@ -401,7 +408,8 @@ fn unexpand(options: &Options) -> UResult<()> { let mut fh = match open(file) { Ok(reader) => reader, Err(err) => { - return Err(USimpleError::new(1, err.to_string())); + show!(err); + continue; } }; diff --git a/tests/by-util/test_unexpand.rs b/tests/by-util/test_unexpand.rs index ddbe3343e..c1310be01 100644 --- a/tests/by-util/test_unexpand.rs +++ b/tests/by-util/test_unexpand.rs @@ -2,6 +2,7 @@ // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. +// spell-checker:ignore contenta use crate::common::util::TestScenario; #[test] @@ -235,3 +236,34 @@ fn test_tabs_shortcut_with_too_large_size() { new_ucmd!().arg(arg).fails().stderr_contains(expected_error); } + +#[test] +fn test_is_directory() { + let (at, mut ucmd) = at_and_ucmd!(); + let dir_name = "dir"; + at.mkdir(dir_name); + + ucmd.arg(dir_name) + .fails() + .stderr_contains(format!("unexpand: {}: Is a directory", dir_name)); +} + +#[test] +fn test_multiple_files() { + let (at, mut ucmd) = at_and_ucmd!(); + + at.write("file", "content"); + at.write("file1", "a b"); + + ucmd.args(&["file", "file1"]) + .succeeds() + .stdout_is("contenta b"); +} + +#[test] +fn test_one_nonexisting_file() { + new_ucmd!() + .arg("asdf.txt") + .fails() + .stderr_contains("asdf.txt: No such file or directory"); +}