Skip to content

Commit 6755956

Browse files
sylvestrecakebaker
andauthored
cut.pl: adjust to our messages as they are better (#6921)
* cut.pl: adjust to our messages as they are better but we still have some differences on this test * cut: add some missing line return when needed * cut: add failing tests covered by cut.pl * Remove dup test * cut: add spell-checker:ignore line to test --------- Co-authored-by: Daniel Hofstetter <[email protected]>
1 parent 3886249 commit 6755956

File tree

4 files changed

+113
-3
lines changed

4 files changed

+113
-3
lines changed

.vscode/cspell.dictionaries/jargon.wordlist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ retval
157157
subdir
158158
val
159159
vals
160+
inval
161+
nofield
160162

161163
# * clippy
162164
uninlined

src/uu/cut/src/cut.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ fn cut_fields_explicit_out_delim<R: Read, M: Matcher>(
131131

132132
if delim_search.peek().is_none() {
133133
if !only_delimited {
134+
// Always write the entire line, even if it doesn't end with `newline_char`
134135
out.write_all(line)?;
135-
if line[line.len() - 1] != newline_char {
136+
if line.is_empty() || line[line.len() - 1] != newline_char {
136137
out.write_all(&[newline_char])?;
137138
}
138139
}
@@ -213,8 +214,12 @@ fn cut_fields_implicit_out_delim<R: Read, M: Matcher>(
213214
let mut print_delim = false;
214215

215216
if delim_search.peek().is_none() {
216-
if !only_delimited && line[line.len() - 1] == newline_char {
217+
if !only_delimited {
218+
// Always write the entire line, even if it doesn't end with `newline_char`
217219
out.write_all(line)?;
220+
if line.is_empty() || line[line.len() - 1] != newline_char {
221+
out.write_all(&[newline_char])?;
222+
}
218223
}
219224

220225
return Ok(true);

tests/by-util/test_cut.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
//
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
5+
6+
// spell-checker:ignore defg
7+
58
use crate::common::util::TestScenario;
69

710
static INPUT: &str = "lists.txt";
@@ -288,7 +291,7 @@ fn test_newline_delimited() {
288291
.args(&["-f", "1", "-d", "\n"])
289292
.pipe_in("a:1\nb:")
290293
.succeeds()
291-
.stdout_only_bytes("a:1\n");
294+
.stdout_only_bytes("a:1\nb:\n");
292295
}
293296

294297
#[test]
@@ -329,3 +332,31 @@ fn test_8bit_non_utf8_delimiter() {
329332
.succeeds()
330333
.stdout_check(|out| out == "b_c\n".as_bytes());
331334
}
335+
336+
#[test]
337+
fn test_newline_preservation_with_f1_option() {
338+
let (at, mut ucmd) = at_and_ucmd!();
339+
at.write("1", "a\nb");
340+
let expected = "a\nb\n";
341+
ucmd.args(&["-f1-", "1"]).succeeds().stdout_is(expected);
342+
}
343+
344+
#[ignore = "Not yet implemented"]
345+
#[test]
346+
fn test_output_delimiter_with_character_ranges() {
347+
new_ucmd!()
348+
.args(&["-c2-3,4-", "--output-delim=:"])
349+
.pipe_in("abcdefg\n")
350+
.succeeds()
351+
.stdout_only("bc:defg\n");
352+
}
353+
354+
#[ignore = "Not yet implemented"]
355+
#[test]
356+
fn test_output_delimiter_with_adjacent_ranges() {
357+
new_ucmd!()
358+
.args(&["-b1-2,3-4", "--output-d=:"])
359+
.pipe_in("abcd\n")
360+
.succeeds()
361+
.stdout_only("ab:cd\n");
362+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
diff --git a/tests/cut/cut.pl b/tests/cut/cut.pl
2+
index 1670db02e..ed633792a 100755
3+
--- a/tests/cut/cut.pl
4+
+++ b/tests/cut/cut.pl
5+
@@ -29,13 +29,15 @@ my $mb_locale = $ENV{LOCALE_FR_UTF8};
6+
7+
my $prog = 'cut';
8+
my $try = "Try '$prog --help' for more information.\n";
9+
-my $from_field1 = "$prog: fields are numbered from 1\n$try";
10+
-my $from_pos1 = "$prog: byte/character positions are numbered from 1\n$try";
11+
-my $inval_fld = "$prog: invalid field range\n$try";
12+
-my $inval_pos = "$prog: invalid byte or character range\n$try";
13+
-my $no_endpoint = "$prog: invalid range with no endpoint: -\n$try";
14+
-my $nofield = "$prog: an input delimiter may be specified only when " .
15+
- "operating on fields\n$try";
16+
+my $from_field1 = "$prog: range '' was invalid: failed to parse range\n";
17+
+my $from_field_0 = "$prog: range '0' was invalid: fields and positions are numbered from 1\n";
18+
+my $from_field_0_dash = "$prog: range '0-' was invalid: fields and positions are numbered from 1\n";
19+
+my $from_field_0_2 = "$prog: range '0-2' was invalid: fields and positions are numbered from 1\n";
20+
+my $from_pos1 = "$prog: range '' was invalid: failed to parse range\n";
21+
+my $inval_fld = "$prog: range '--' was invalid: failed to parse range\n";
22+
+my $inval_pos = "$prog: range '--' was invalid: failed to parse range\n";
23+
+my $no_endpoint = "$prog: range '-' was invalid: invalid range with no endpoint\n";
24+
+my $nofield = "$prog: invalid input: The '--delimiter' ('-d') option only usable if printing a sequence of fields\n";
25+
26+
my @Tests =
27+
(
28+
@@ -44,16 +46,16 @@ my @Tests =
29+
30+
# This failed (as it should) even before coreutils-6.9.90,
31+
# but cut from 6.9.90 produces a more useful diagnostic.
32+
- ['zero-1', '-b0', {ERR=>$from_pos1}, {EXIT => 1} ],
33+
+ ['zero-1', '-b0', {ERR=>$from_field_0}, {EXIT => 1} ],
34+
35+
# Up to coreutils-6.9, specifying a range of 0-2 was not an error.
36+
# It was treated just like "-2".
37+
- ['zero-2', '-f0-2', {ERR=>$from_field1}, {EXIT => 1} ],
38+
+ ['zero-2', '-f0-2', {ERR=>$from_field_0_2}, {EXIT => 1} ],
39+
40+
# Up to coreutils-8.20, specifying a range of 0- was not an error.
41+
- ['zero-3b', '-b0-', {ERR=>$from_pos1}, {EXIT => 1} ],
42+
- ['zero-3c', '-c0-', {ERR=>$from_pos1}, {EXIT => 1} ],
43+
- ['zero-3f', '-f0-', {ERR=>$from_field1}, {EXIT => 1} ],
44+
+ ['zero-3b', '-b0-', {ERR=>$from_field_0_dash}, {EXIT => 1} ],
45+
+ ['zero-3c', '-c0-', {ERR=>$from_field_0_dash}, {EXIT => 1} ],
46+
+ ['zero-3f', '-f0-', {ERR=>$from_field_0_dash}, {EXIT => 1} ],
47+
48+
['1', '-d:', '-f1,3-', {IN=>"a:b:c\n"}, {OUT=>"a:c\n"}],
49+
['2', '-d:', '-f1,3-', {IN=>"a:b:c\n"}, {OUT=>"a:c\n"}],
50+
@@ -96,11 +98,10 @@ my @Tests =
51+
# Errors
52+
# -s may be used only with -f
53+
['y', qw(-s -b4), {IN=>":\n"}, {OUT=>""}, {EXIT=>1},
54+
- {ERR=>"$prog: suppressing non-delimited lines makes sense\n"
55+
- . "\tonly when operating on fields\n$try"}],
56+
+ {ERR=>"$prog: invalid input: The '--only-delimited' ('-s') option only usable if printing a sequence of fields\n"}],
57+
# You must specify bytes or fields (or chars)
58+
['z', '', {IN=>":\n"}, {OUT=>""}, {EXIT=>1},
59+
- {ERR=>"$prog: you must specify a list of bytes, characters, or fields\n$try"}
60+
+ {ERR=>"$prog: invalid usage: expects one of --fields (-f), --chars (-c) or --bytes (-b)\n"}
61+
],
62+
# Empty field list
63+
['empty-fl', qw(-f ''), {IN=>":\n"}, {OUT=>""}, {EXIT=>1},
64+
@@ -199,7 +200,7 @@ my @Tests =
65+
66+
# None of the following invalid ranges provoked an error up to coreutils-6.9.
67+
['inval1', qw(-f 2-0), {IN=>''}, {OUT=>''}, {EXIT=>1},
68+
- {ERR=>"$prog: invalid decreasing range\n$try"}],
69+
+ {ERR=>"$prog: range '2-0' was invalid: fields and positions are numbered from 1\n"}],
70+
['inval2', qw(-f -), {IN=>''}, {OUT=>''}, {EXIT=>1}, {ERR=>$no_endpoint}],
71+
['inval3', '-f', '4,-', {IN=>''}, {OUT=>''}, {EXIT=>1}, {ERR=>$no_endpoint}],
72+
['inval4', '-f', '1-2,-', {IN=>''}, {OUT=>''}, {EXIT=>1},

0 commit comments

Comments
 (0)