1#!/usr/bin/perl
2# Basic tests for "fmt".
3
4# Copyright (C) 2001-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19use strict;
20
21(my $program_name = $0) =~ s|.*/||;
22my $normalize_strerror = "s/': .*/'/";
23
24my @Tests =
25    (
26     ['8-bit-pfx', qw (-p 'ç'),
27      {IN=> "ça\nçb\n"},
28      {OUT=>"ça b\n"}],
29     ['wide-1', '-w 32768',
30      {ERR => "fmt: invalid width: '32768'\n"}, {EXIT => 1},
31      {ERR_SUBST => $normalize_strerror}],
32     ['wide-2', '-w 2147483647',
33      {ERR => "fmt: invalid width: '2147483647'\n"}, {EXIT => 1},
34      {ERR_SUBST => $normalize_strerror}],
35     ['bad-suffix', '-72x',	{IN=> ''},
36      {ERR => "fmt: invalid width: '72x'\n"}, {EXIT => 1}],
37     ['no-file', 'no-such-file',
38      {ERR => "fmt: cannot open 'no-such-file' for reading:"
39       . " No such file or directory\n"}, {EXIT => 1}],
40     ['obs-1', '-c -72',
41      {ERR => "fmt: invalid option -- 7; -WIDTH is recognized only when it"
42       . " is the first\noption; use -w N instead\n"
43       . "Try 'fmt --help' for more information.\n" }, {EXIT => 1}],
44
45     # With --prefix=P, do not remove leading space on lines without the prefix.
46     ['pfx-1', qw (-p '>'),
47      {IN=>  " 1\n  2\n\t3\n\t\t4\n> quoted\n> text\n"},
48      {OUT=> " 1\n  2\n\t3\n\t\t4\n> quoted text\n"}],
49
50     # Don't remove prefix from a prefix-only line.
51     ['pfx-only', qw (-p '>'),
52      {IN=>  ">\n"},
53      {OUT=> ">\n"}],
54
55     # With a multi-byte prefix, say, "foo", don't empty a line that
56     # starts with a strict prefix (e.g. "fo") of that prefix.
57     # With fmt from coreutils-6.7, it would mistakenly output an empty line.
58     ['pfx-of-pfx', qw (-p 'foo'),
59      {IN=>  "fo\n"},
60      {OUT=> "fo\n"}],
61);
62
63my $save_temps = $ENV{DEBUG};
64my $verbose = $ENV{VERBOSE};
65my $prog = 'fmt';
66my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
67exit $fail;
68