coreutils
coreutils copied to clipboard
seq: needs to handle several types of invalid -f arguments
Here are some test cases that we would need to satisfy in order to match the behavior of GNU seq when given various invalid format string templates:
#[test]
fn test_format_ends_in_percent() {
new_ucmd!()
.args(&["-f", "%", "1"])
.fails()
.stderr_only("seq: format '%' ends in %\n");
}
#[test]
fn test_format_too_many_directives() {
new_ucmd!()
.args(&["-f", "%g%", "1"])
.fails()
.stderr_only("seq: format '%g%' has too many % directives\n");
}
#[test]
fn test_format_too_few_directives() {
new_ucmd!()
.args(&["-f", "\"\"", "1"])
.fails()
.stderr_only("seq: format '' has no % directive\n");
}
#[test]
fn test_format_unknown_directive() {
new_ucmd!()
.args(&["-f", "%0%", "1"])
.fails()
.stderr_only("seq: format ‘%0%’ has unknown %% directive\n");
}
Most of these test cases appear in the GNU test suite in the file tests/misc/seq.pl.
Notice that these errors differ from those in printf:
$ printf % 1
bash: printf: `%': missing format character
$ seq -f % 1
seq: format ‘%’ ends in %
$ printf %g% 1
bash: printf: `%': missing format character
1$ seq -f %g% 1
seq: format ‘%g%’ has too many % directives
$ printf "" 1
$ seq -f "" 1
seq: format ‘’ has no % directive
$ printf "%0%" 1
bash: printf: `%': invalid format character
$ seq -f "%0%" 1
seq: format ‘%0%’ has unknown %% directive
Perhaps there is a way to refactor the code in uucore::memo::Memo (which implements the behavior of printf) so that it can report enough information that seq can implement its own error handling. For example, perhaps Memo could provide a way of counting the number of % directives were found, so that if no directives were found, seq could report "too many % directives".