Skip to content
Merged
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
16 changes: 11 additions & 5 deletions src/uu/nl/src/nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use clap::{crate_version, Arg, ArgAction, Command};
use std::fs::File;
use std::io::{stdin, BufRead, BufReader, Read};
use std::path::Path;
use uucore::error::{FromIo, UResult, USimpleError};
use uucore::{format_usage, help_about, help_section, help_usage};
use uucore::error::{set_exit_code, FromIo, UResult, USimpleError};
use uucore::{format_usage, help_about, help_section, help_usage, show_error};

mod helper;

Expand Down Expand Up @@ -205,9 +205,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
nl(&mut buffer, &mut stats, &settings)?;
} else {
let path = Path::new(file);
let reader = File::open(path).map_err_context(|| file.to_string())?;
let mut buffer = BufReader::new(reader);
nl(&mut buffer, &mut stats, &settings)?;

if path.is_dir() {
show_error!("{}: Is a directory", path.display());
set_exit_code(1);
} else {
let reader = File::open(path).map_err_context(|| file.to_string())?;
let mut buffer = BufReader::new(reader);
nl(&mut buffer, &mut stats, &settings)?;
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/by-util/test_nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,3 +634,20 @@ fn test_empty_section_delimiter() {
.stdout_is(" 1\ta\n \n 2\tb\n");
}
}

#[test]
fn test_directory_as_input() {
let (at, mut ucmd) = at_and_ucmd!();
let dir = "dir";
let file = "file";
let content = "aaa";

at.mkdir(dir);
at.write(file, content);

ucmd.arg(dir)
.arg(file)
.fails()
.stderr_is(format!("nl: {dir}: Is a directory\n"))
.stdout_contains(content);
}