Skip to content
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
9 changes: 5 additions & 4 deletions src/uu/expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,12 @@ fn expand_shortcuts(args: Vec<OsString>) -> Vec<OsString> {
let mut processed_args = Vec::with_capacity(args.len());

for arg in args {
if let Some(arg) = arg.to_str()
&& arg.starts_with('-')
&& arg[1..].chars().all(is_digit_or_comma)
if let Some(shortcut) = arg
.to_str()
.and_then(|arg| arg.strip_prefix('-'))
.filter(|shortcut| !shortcut.is_empty() && shortcut.chars().all(is_digit_or_comma))
{
arg[1..]
shortcut
.split(',')
.filter(|s| !s.is_empty())
.for_each(|s| processed_args.push(OsString::from(format!("--tabs={s}"))));
Expand Down
7 changes: 5 additions & 2 deletions src/uu/unexpand/src/unexpand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,11 @@ fn expand_shortcuts(args: Vec<OsString>) -> Vec<OsString> {

for arg in args {
if let Some(arg) = arg.to_str() {
if arg.starts_with('-') && arg[1..].chars().all(is_digit_or_comma) {
arg[1..]
if let Some(shortcut) = arg
.strip_prefix('-')
.filter(|shortcut| !shortcut.is_empty() && shortcut.chars().all(is_digit_or_comma))
{
shortcut
.split(',')
.filter(|s| !s.is_empty())
.for_each(|s| processed_args.push(OsString::from(format!("--tabs={s}"))));
Expand Down
13 changes: 12 additions & 1 deletion tests/by-util/test_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@

use rstest::rstest;
use uucore::display::Quotable;
use uutests::new_ucmd;
use uutests::{at_and_ucmd, new_ucmd};

#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
}

#[test]
fn test_bare_dash_reads_stdin_in_file_list() {
let (at, mut ucmd) = at_and_ucmd!();
at.write("file", "file\n");

ucmd.args(&["-", "file"])
.pipe_in("stdin\n")
.succeeds()
.stdout_is("stdin\nfile\n");
}

#[test]
fn test_with_tab() {
new_ucmd!()
Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_unexpand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@ fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
}

#[test]
fn test_bare_dash_reads_stdin_in_file_list() {
let (at, mut ucmd) = at_and_ucmd!();
at.write("file", "file\n");

ucmd.args(&["-", "file"])
.pipe_in("stdin\n")
.succeeds()
.stdout_is("stdin\nfile\n");
}

#[test]
fn test_bare_dash_does_not_force_first_only() {
new_ucmd!()
.args(&["-t", "3", "-"])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could you please also add --first-only -? it was failing with "cannot be used multiple times" in the issue

.pipe_in("a b\n")
.succeeds()
.stdout_is("a\tb\n");
}

#[test]
fn test_first_only_with_bare_stdin_operand() {
new_ucmd!()
.args(&["--first-only", "-"])
.pipe_in(" A B\n")
.succeeds()
.stdout_is("\tA B\n");
}

#[rstest]
#[case::unexpand_init_0(&["-t4"], " 1\n 2\n 3\n 4\n", " 1\n 2\n 3\n\t4\n")]
#[case::unexpand_init_1(&["-t4"], " 5\n 6\n 7\n 8\n", "\t 5\n\t 6\n\t 7\n\t\t8\n")]
Expand Down
Loading