3 #==============================================================================
5 # File ID: c22bbf3c-ae3e-11e6-8a6c-279c2a0468a3
7 # Test suite for sh(1).
10 # ©opyleft 2016– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of file for
13 #==============================================================================
19 use Test
::More
qw{no_plan
};
28 our $CMD = "../$CMDB";
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.0.0';
45 my %descriptions = ();
47 Getopt
::Long
::Configure
('bundling');
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'}) {
71 diag
(sprintf('========== Executing %s v%s ==========',
72 $progname, $VERSION));
74 if ($Opt{'todo'} && !$Opt{'all'}) {
78 test_standard_options
();
83 if ($Opt{'all'} || $Opt{'todo'}) {
84 diag
('Running TODO tests...');
87 # Insert TODO tests here.
91 diag
('Testing finished.');
96 sub test_standard_options
{
97 diag
('Testing -h (--help) option...');
102 'Option -h prints help screen');
103 likecmd
("$CMD --help",
104 '/ Show this help/i',
107 'Option --help prints help screen');
109 diag
('Testing -v (--verbose) option...');
110 likecmd
("$CMD -h -v",
111 '/^\n\S+ \d+\.\d+\.\d+/s',
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',
121 'Option -h with --verbose returns version number and help screen');
122 likecmd
("$CMD --help --verbose",
123 '/^\n\S+ \d+\.\d+\.\d+/s',
126 'Option --help with --verbose returns version number and help screen');
127 likecmd
("$CMD -v --help",
128 '/^\n\S+ \d+\.\d+\.\d+/s',
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+/',
138 'Option --version returns version number');
139 likecmd
("$CMD --version --help",
140 '/^\S+ \d+\.\d+\.\d+/',
143 'Option --version --help returns version number');
144 diag
("Unknown options and arguments");
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');
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;
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";
184 if (defined($Exp_stderr)) {
185 $stderr_cmd = " 2>$TMP_STDERR";
187 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
189 if (defined($Exp_stderr)) {
190 $retval &= is
(file_data
($TMP_STDERR),
191 $Exp_stderr, "$Txt (stderr)");
194 diag
("Warning: stderr not defined for '$Txt'");
196 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
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;
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";
212 if (defined($Exp_stderr)) {
213 $stderr_cmd = " 2>$TMP_STDERR";
215 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
217 if (defined($Exp_stderr)) {
218 $retval &= like
(file_data
($TMP_STDERR),
219 $Exp_stderr, "$Txt (stderr)");
222 diag
("Warning: stderr not defined for '$Txt'");
224 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
230 # Return file content as a string
234 open(my $fp, '<', $File) or return undef;
242 # Create new file and fill it with data
243 my ($file, $text) = @_;
246 open(my $fp, ">$file") or return 0;
249 $retval = is
(file_data
($file), $text,
250 "$file was successfully created");
252 return $retval; # 0 if error, 1 if ok
256 # Print program version
257 print("$progname $VERSION\n");
262 # Send the help message to stdout
265 if ($Opt{'verbose'}) {
271 Usage: $progname [options]
273 Contains tests for the $CMDB(1) program.
278 Run all tests, also TODOs.
282 Be more quiet. Can be repeated to increase silence.
284 Run only the TODO tests.
286 Increase level of verbosity. Can be repeated.
288 Print version information.
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");
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
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
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 :