Upgrade yt-dlp from version stable@2024.10.07 to stable@2024.12.13
[sunny256-utils.git] / Lib / std / t / sh.t
blobbba32754a2f5b24d30fb61090e5cb91dab5eadca
1 #!/usr/bin/env perl
3 #==============================================================================
4 # sh.t
5 # File ID: c22bbf3c-ae3e-11e6-8a6c-279c2a0468a3
7 # Test suite for sh(1).
9 # Character set: UTF-8
10 # ©opyleft 2016– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of file for
12 # legal stuff.
13 #==============================================================================
15 use strict;
16 use warnings;
18 BEGIN {
19 use Test::More qw{no_plan};
20 # use_ok() goes here
23 use Getopt::Long;
25 local $| = 1;
27 our $CMDB = "sh";
28 our $CMD = "../$CMDB";
30 our %Opt = (
32 'all' => 0,
33 'help' => 0,
34 'quiet' => 0,
35 'todo' => 0,
36 'verbose' => 0,
37 'version' => 0,
41 our $progname = $0;
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.0.0';
45 my %descriptions = ();
47 Getopt::Long::Configure('bundling');
48 GetOptions(
50 'all|a' => \$Opt{'all'},
51 'help|h' => \$Opt{'help'},
52 'quiet|q+' => \$Opt{'quiet'},
53 'todo|t' => \$Opt{'todo'},
54 'verbose|v+' => \$Opt{'verbose'},
55 'version' => \$Opt{'version'},
57 ) || die("$progname: Option error. Use -h for help.\n");
59 $Opt{'verbose'} -= $Opt{'quiet'};
60 $Opt{'help'} && usage(0);
61 if ($Opt{'version'}) {
62 print_version();
63 exit(0);
66 exit(main());
68 sub main {
69 my $Retval = 0;
71 diag(sprintf('========== Executing %s v%s ==========',
72 $progname, $VERSION));
74 if ($Opt{'todo'} && !$Opt{'all'}) {
75 goto todo_section;
78 test_standard_options();
80 todo_section:
83 if ($Opt{'all'} || $Opt{'todo'}) {
84 diag('Running TODO tests...');
85 TODO: {
86 local $TODO = '';
87 # Insert TODO tests here.
91 diag('Testing finished.');
93 return $Retval;
96 sub test_standard_options {
97 diag('Testing -h (--help) option...');
98 likecmd("$CMD -h",
99 '/ Show this help/i',
100 '/^$/',
102 'Option -h prints help screen');
103 likecmd("$CMD --help",
104 '/ Show this help/i',
105 '/^$/',
107 'Option --help prints help screen');
109 diag('Testing -v (--verbose) option...');
110 likecmd("$CMD -h -v",
111 '/^\n\S+ \d+\.\d+\.\d+/s',
112 '/^$/',
114 'Option -h with -v returns version number and help screen');
116 diag('Testing --version option...');
117 likecmd("$CMD -h --verbose",
118 '/^\n\S+ \d+\.\d+\.\d+/s',
119 '/^$/',
121 'Option -h with --verbose returns version number and help screen');
122 likecmd("$CMD --help --verbose",
123 '/^\n\S+ \d+\.\d+\.\d+/s',
124 '/^$/',
126 'Option --help with --verbose returns version number and help screen');
127 likecmd("$CMD -v --help",
128 '/^\n\S+ \d+\.\d+\.\d+/s',
129 '/^$/',
131 'Option -v with --help returns version number and help screen');
133 diag('Testing --version option...');
134 likecmd("$CMD --version",
135 '/^\S+ \d+\.\d+\.\d+/',
136 '/^$/',
138 'Option --version returns version number');
139 likecmd("$CMD --version --help",
140 '/^\S+ \d+\.\d+\.\d+/',
141 '/^$/',
143 'Option --version --help returns version number');
144 diag("Unknown options and arguments");
145 testcmd("$CMD -U",
147 "STDfilenameDTS: -U: Unknown option\n",
149 'Unknown short option');
150 testcmd("$CMD --unknown",
152 "STDfilenameDTS: --unknown: Unknown option\n",
154 'Unknown long option');
155 testcmd("$CMD blabla",
159 'Accepts non-option argument');
160 testcmd("$CMD -- -U",
164 'Unknown options after -- are ignored');
165 testcmd("$CMD blabla -U",
169 'Unknown options after first non-option are ignored');
170 return;
173 sub testcmd {
174 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
175 defined($descriptions{$Desc}) &&
176 BAIL_OUT("testcmd(): '$Desc' description is used twice");
177 $descriptions{$Desc} = 1;
178 my $stderr_cmd = '';
179 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
180 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
181 my $TMP_STDERR = "$CMDB-stderr.tmp";
182 my $retval = 1;
184 if (defined($Exp_stderr)) {
185 $stderr_cmd = " 2>$TMP_STDERR";
187 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
188 my $ret_val = $?;
189 if (defined($Exp_stderr)) {
190 $retval &= is(file_data($TMP_STDERR),
191 $Exp_stderr, "$Txt (stderr)");
192 unlink($TMP_STDERR);
193 } else {
194 diag("Warning: stderr not defined for '$Txt'");
196 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
198 return $retval;
201 sub likecmd {
202 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
203 defined($descriptions{$Desc}) &&
204 BAIL_OUT("likecmd(): '$Desc' description is used twice");
205 $descriptions{$Desc} = 1;
206 my $stderr_cmd = '';
207 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
208 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
209 my $TMP_STDERR = "$CMDB-stderr.tmp";
210 my $retval = 1;
212 if (defined($Exp_stderr)) {
213 $stderr_cmd = " 2>$TMP_STDERR";
215 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
216 my $ret_val = $?;
217 if (defined($Exp_stderr)) {
218 $retval &= like(file_data($TMP_STDERR),
219 $Exp_stderr, "$Txt (stderr)");
220 unlink($TMP_STDERR);
221 } else {
222 diag("Warning: stderr not defined for '$Txt'");
224 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
226 return $retval;
229 sub file_data {
230 # Return file content as a string
231 my $File = shift;
232 my $Txt;
234 open(my $fp, '<', $File) or return undef;
235 local $/ = undef;
236 $Txt = <$fp>;
237 close($fp);
238 return $Txt;
241 sub create_file {
242 # Create new file and fill it with data
243 my ($file, $text) = @_;
244 my $retval = 0;
246 open(my $fp, ">$file") or return 0;
247 print($fp $text);
248 close($fp);
249 $retval = is(file_data($file), $text,
250 "$file was successfully created");
252 return $retval; # 0 if error, 1 if ok
255 sub print_version {
256 # Print program version
257 print("$progname $VERSION\n");
258 return;
261 sub usage {
262 # Send the help message to stdout
263 my $Retval = shift;
265 if ($Opt{'verbose'}) {
266 print("\n");
267 print_version();
269 print(<<"END");
271 Usage: $progname [options]
273 Contains tests for the $CMDB(1) program.
275 Options:
277 -a, --all
278 Run all tests, also TODOs.
279 -h, --help
280 Show this help.
281 -q, --quiet
282 Be more quiet. Can be repeated to increase silence.
283 -t, --todo
284 Run only the TODO tests.
285 -v, --verbose
286 Increase level of verbosity. Can be repeated.
287 --version
288 Print version information.
291 exit($Retval);
294 sub msg {
295 # Print a status message to stderr based on verbosity level
296 my ($verbose_level, $Txt) = @_;
298 $verbose_level > $Opt{'verbose'} && return;
299 print(STDERR "$progname: $Txt\n");
300 return;
303 __END__
305 # This program is free software; you can redistribute it and/or modify it under
306 # the terms of the GNU General Public License as published by the Free Software
307 # Foundation; either version 2 of the License, or (at your option) any later
308 # version.
310 # This program is distributed in the hope that it will be useful, but WITHOUT
311 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
312 # FOR A PARTICULAR PURPOSE.
313 # See the GNU General Public License for more details.
315 # You should have received a copy of the GNU General Public License along with
316 # this program.
317 # If not, see L<http://www.gnu.org/licenses/>.
319 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :