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

comm: generate an error if the input is a directory #6853

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/uu/comm/src/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// spell-checker:ignore (ToDO) delim mkdelim

use std::cmp::Ordering;
use std::fs::File;
use std::fs::{metadata, File};
use std::io::{self, stdin, BufRead, BufReader, Stdin};
use std::path::Path;
use uucore::error::{FromIo, UResult, USimpleError};
Expand Down Expand Up @@ -130,7 +130,11 @@ fn open_file(name: &str, line_ending: LineEnding) -> io::Result<LineReader> {
if name == "-" {
Ok(LineReader::new(Input::Stdin(stdin()), line_ending))
} else {
let f = File::open(Path::new(name))?;
let path = Path::new(name);
if metadata(path)?.is_dir() {
return Err(io::Error::new(io::ErrorKind::Other, "Is a directory"));
}
let f = File::open(path)?;
Comment on lines +133 to +137
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the path variable necessary? Unless I'm missing something I think you can pass name directly to metadata and File::open.

Ok(LineReader::new(
Input::FileIn(BufReader::new(f)),
line_ending,
Expand Down
33 changes: 33 additions & 0 deletions tests/by-util/test_comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,36 @@ fn test_no_such_file() {
.fails()
.stderr_only("comm: bogus_file_1: No such file or directory\n");
}

#[test]
fn test_is_dir() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

scene
.ucmd()
.args(&[".", "."])
.fails()
.stderr_only("comm: .: Is a directory\n");
at.mkdir("dir");

Comment on lines +306 to +307
Copy link
Contributor

Choose a reason for hiding this comment

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

A detail:

Suggested change
at.mkdir("dir");
at.mkdir("dir");

scene
.ucmd()
.args(&["dir", "."])
.fails()
.stderr_only("comm: dir: Is a directory\n");

at.touch("file");
scene
.ucmd()
.args(&[".", "file"])
.fails()
.stderr_only("comm: .: Is a directory\n");

at.touch("file");
scene
.ucmd()
.args(&["file", "."])
.fails()
.stderr_only("comm: .: Is a directory\n");
}
Loading