Skip to content
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

unexpand: should allow multiple files #5852 and unexpand: show error message if a directory is specified #5845 #5864

Merged
merged 4 commits into from Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/uu/unexpand/src/unexpand.rs
Expand Up @@ -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");
Expand Down Expand Up @@ -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::<String>(options::FILE) {
Some(v) => vec![v.to_string()],
let files = match matches.get_many::<String>(options::FILE) {
Some(v) => v.cloned().collect(),
None => vec!["-".to_owned()],
};

Expand Down Expand Up @@ -211,7 +212,13 @@ pub fn uu_app() -> Command {

fn open(path: &str) -> UResult<BufReader<Box<dyn Read + 'static>>> {
let file_buf;
if path == "-" {
let filename = Path::new(path);
if filename.is_dir() {
Err(Box::new(USimpleError {
code: 1,
message: format!("unexpand: {}: Is a directory", filename.display()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USimpleError automatically adds the utils name so you don't have to add it manually:

Suggested change
message: format!("unexpand: {}: Is a directory", filename.display()),
message: format!("{}: Is a directory", filename.display()),

}))
} else if path == "-" {
Ok(BufReader::new(Box::new(stdin()) as Box<dyn Read>))
} else {
file_buf = File::open(path).map_err_context(|| path.to_string())?;
Expand Down Expand Up @@ -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;
}
};

Expand Down
32 changes: 32 additions & 0 deletions tests/by-util/test_unexpand.rs
Expand Up @@ -235,3 +235,35 @@

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");

Check failure on line 259 in tests/by-util/test_unexpand.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: Unknown word (contenta) (file:'tests/by-util/test_unexpand.rs', line:259)
}

#[test]
fn test_one_nonexisting_file() {
let (at, mut ucmd) = at_and_ucmd!();

ucmd.arg("asdf.txt")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The at causes an "unused variable"-warning. Instead of at_and_ucmd!() you can simply use new_cmd!() in this case:

Suggested change
let (at, mut ucmd) = at_and_ucmd!();
ucmd.arg("asdf.txt")
new_cmd!()
.arg("asdf.txt")

.fails()
.stderr_contains("asdf.txt: No such file or directory");
}