Skip to content

Commit 746a7b1

Browse files
SaHHiiLLcakebaker
andauthored
tsort: returns error when input is dir - same as GNU tsort (#5860)
* fix: return error when input is dir * test: when tsort is given a dir * fix: do not need to mention tsort in error message * test: using concrete directory name * tsort: fix formatting in test --------- Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
1 parent 63ef7e4 commit 746a7b1

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/uu/tsort/src/tsort.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
3232
stdin_buf = stdin();
3333
&mut stdin_buf as &mut dyn Read
3434
} else {
35-
file_buf = File::open(Path::new(&input)).map_err_context(|| input.to_string())?;
35+
let path = Path::new(&input);
36+
if path.is_dir() {
37+
return Err(USimpleError::new(
38+
1,
39+
format!("{}: read error: Is a directory", input),
40+
));
41+
}
42+
file_buf = File::open(path).map_err_context(|| input.to_string())?;
3643
&mut file_buf as &mut dyn Read
3744
});
3845

tests/by-util/test_tsort.rs

+9
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,12 @@ fn test_multiple_arguments() {
6464
.fails()
6565
.stderr_contains("unexpected argument 'invalid_file' found");
6666
}
67+
68+
#[test]
69+
fn test_error_on_dir() {
70+
let (at, mut ucmd) = at_and_ucmd!();
71+
at.mkdir("tsort_test_dir");
72+
ucmd.arg("tsort_test_dir")
73+
.fails()
74+
.stderr_contains("tsort: tsort_test_dir: read error: Is a directory");
75+
}

0 commit comments

Comments
 (0)