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

fmt: accept repeated arguments #6355

Merged
merged 2 commits into from
May 5, 2024
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
1 change: 1 addition & 0 deletions src/uu/fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ pub fn uu_app() -> Command {
.about(ABOUT)
.override_usage(format_usage(USAGE))
.infer_long_args(true)
.args_override_self(true)
.arg(
Arg::new(options::CROWN_MARGIN)
.short('c')
Expand Down
101 changes: 99 additions & 2 deletions tests/by-util/test_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#[test]
fn test_fmt_quick() {
for param in ["-q", "--quick"] {
for param in ["-q", "--quick", "-qq"] {
new_ucmd!()
.args(&["one-word-per-line.txt", param])
.succeeds()
Expand All @@ -35,6 +35,10 @@
.succeeds()
.stdout_is("this is a\nfile with\none word\nper line\n");
}
new_ucmd!()
.args(&["one-word-per-line.txt", "-w50", "--width", "10"])
.succeeds()
.stdout_is("this is a\nfile with\none word\nper line\n");
}

#[test]
Expand Down Expand Up @@ -66,6 +70,11 @@
.code_is(1)
.stderr_is("fmt: invalid width: '2501': Numerical result out of range\n");
}
// However, as a temporary value it is okay:
new_ucmd!()
.args(&["one-word-per-line.txt", "-w2501", "--width", "10"])
.succeeds()
.stdout_is("this is a\nfile with\none word\nper line\n");
}

#[test]
Expand Down Expand Up @@ -97,7 +106,7 @@
.stderr_contains("fmt: invalid width: '25x'");
}

#[ignore]
#[ignore = "our 'goal' algorithm is very different from GNU; fix this!"]
#[test]
fn test_fmt_goal() {
for param in ["-g", "--goal"] {
Expand All @@ -106,6 +115,10 @@
.succeeds()
.stdout_is("this is a\nfile with one\nword per line\n");
}
new_ucmd!()

Check warning on line 118 in tests/by-util/test_fmt.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_fmt.rs#L118

Added line #L118 was not covered by tests
.args(&["one-word-per-line.txt", "-g40", "-g7"])
.succeeds()
.stdout_is("this is a\nfile with one\nword per line\n");

Check warning on line 121 in tests/by-util/test_fmt.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_fmt.rs#L121

Added line #L121 was not covered by tests
}

#[test]
Expand All @@ -130,6 +143,19 @@
}
}

#[ignore = "our 'goal' algorithm is very different from GNU; fix this!"]
#[test]
fn test_fmt_too_big_goal_sometimes_okay() {
new_ucmd!()

Check warning on line 149 in tests/by-util/test_fmt.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_fmt.rs#L148-L149

Added lines #L148 - L149 were not covered by tests
.args(&["one-word-per-line.txt", "--width=75", "-g76", "-g10"])
.succeeds()
.stdout_is("this is a\nfile with one\nword per line\n");
new_ucmd!()

Check warning on line 153 in tests/by-util/test_fmt.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_fmt.rs#L152-L153

Added lines #L152 - L153 were not covered by tests
.args(&["one-word-per-line.txt", "-g76", "-g10"])
.succeeds()
.stdout_is("this is a\nfile with one\nword per line\n");
}

Check warning on line 157 in tests/by-util/test_fmt.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_fmt.rs#L156-L157

Added lines #L156 - L157 were not covered by tests

#[test]
fn test_fmt_non_existent_file() {
new_ucmd!()
Expand Down Expand Up @@ -159,3 +185,74 @@
.stdout_is("this is a file with one word per line\n");
}
}

#[test]
fn split_does_not_reflow() {
for arg in ["-s", "-ss", "--split-only"] {
new_ucmd!()
.arg("one-word-per-line.txt")
.arg(arg)
.succeeds()
.stdout_is_fixture("one-word-per-line.txt");
}
}

#[test]
fn prefix_minus() {
for prefix_args in [
vec!["-p-"],
vec!["-p", "-"],
vec!["--prefix=-"],
vec!["--prefix", "-"],
vec!["--pref=-"],
vec!["--pref", "-"],
// Test self-overriding:
vec!["--prefix==", "--prefix=-"],
] {
new_ucmd!()
.args(&prefix_args)
.arg("prefixed-one-word-per-line.txt")
.succeeds()
.stdout_is_fixture("prefixed-one-word-per-line_p-.txt");
}
}

#[test]
fn prefix_equal() {
for prefix_args in [
// FIXME: #6353 vec!["-p="],
vec!["-p", "="],
vec!["--prefix=="],
vec!["--prefix", "="],
vec!["--pref=="],
vec!["--pref", "="],
// Test self-overriding:
vec!["--prefix=-", "--prefix=="],
] {
new_ucmd!()
.args(&prefix_args)
.arg("prefixed-one-word-per-line.txt")
.succeeds()
.stdout_is_fixture("prefixed-one-word-per-line_p=.txt");
}
}

#[test]
fn prefix_equal_skip_prefix_equal_two() {
for prefix_args in [
// FIXME: #6353 vec!["--prefix==", "-P=2"],
vec!["--prefix==", "-P", "=2"],
vec!["--prefix==", "--skip-prefix==2"],
vec!["--prefix==", "--skip-prefix", "=2"],
vec!["--prefix==", "--skip-pref==2"],
vec!["--prefix==", "--skip-pref", "=2"],
// Test self-overriding:
vec!["--prefix==", "--skip-pref", "asdf", "-P", "=2"],
] {
new_ucmd!()
.args(&prefix_args)
.arg("prefixed-one-word-per-line.txt")
.succeeds()
.stdout_is_fixture("prefixed-one-word-per-line_p=_P=2.txt");
}
}
19 changes: 19 additions & 0 deletions tests/fixtures/fmt/prefixed-one-word-per-line.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- this
- is
- a
- file
- with
- one
- word
- per
- line

=1this
=1is
=1a
=2file
=2with
=2one
=3word
=3per
=3line
11 changes: 11 additions & 0 deletions tests/fixtures/fmt/prefixed-one-word-per-line_p-.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- this is a file with one word per line

=1this
=1is
=1a
=2file
=2with
=2one
=3word
=3per
=3line
11 changes: 11 additions & 0 deletions tests/fixtures/fmt/prefixed-one-word-per-line_p=.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- this
- is
- a
- file
- with
- one
- word
- per
- line

=1this 1is 1a 2file 2with 2one 3word 3per 3line
15 changes: 15 additions & 0 deletions tests/fixtures/fmt/prefixed-one-word-per-line_p=_P=2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- this
- is
- a
- file
- with
- one
- word
- per
- line

=1this 1is 1a
=2file
=2with
=2one
=3word 3per 3line
Loading