2 # -------+---------+---------+-------- + --------+---------+---------+---------+
3 # Copyright (c) 2005 - Garance Alistair Drosehn <gad@FreeBSD.org>.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # -------+---------+---------+-------- + --------+---------+---------+---------+
28 # -------+---------+---------+-------- + --------+---------+---------+---------+
29 # This script was written to provide a battery of regression-tests for some
30 # changes I am making to the `env' command. I wrote a new script for this
31 # for several reasons. 1) I needed to test all kinds of special-character
32 # combinations, and I wanted to be able to type those in exactly as they would
33 # would be in real-life situations. 2) I wanted to set environment variables
34 # before executing a test, 3) I had many different details to test, so I wanted
35 # to write up dozens of tests, without needing to create a hundred separate
36 # little tiny files, 4) I wanted to test *failure* conditions, where I expected
37 # the test would fail but I wanted to be sure that it failed the way I intended
39 # This script was written for the special "shebang-line" testing that I
40 # wanted for my changes to `env', but I expect it could be turned into a
41 # general-purpose test-suite with a little more work.
42 # Garance/June 12/2005
43 # -------+---------+---------+-------- + --------+---------+---------+---------+
46 # -------+---------+---------+-------- + --------+---------+---------+---------+
48 attr_writer :cmdvalue, :shebang_args, :user_args
51 def ExpectedResult.add_gblenv(avar, avalue)
52 @@gbl_envs[avar] = avalue
58 @clear_envs = Hash.new
62 @expect_err = Array.new
63 @expect_out = Array.new
68 def add_expecterr(aline)
72 def add_expectout(aline)
77 @script_lines += aline
78 @script_lines += "\n" if aline[-1] != "\n"
81 def add_clearenv(avar)
82 @clear_envs[avar] = true
85 def add_setenv(avar, avalue)
86 @new_envs[avar] = avalue
89 def add_symlink(srcf, newf)
90 @symlinks << Array.[](srcf, newf)
93 def check_out(name, fname, expect_arr)
97 rdata = File.open(fname)
98 rdata.each_line { |rline|
101 if idx > expect_arr.length - 1
102 if extra_lines == 0 and $verbose >= 1
103 printf "-- Extra line(s) on %s:\n", name
105 printf "-- [%d] > %s\n", idx, rline if $verbose >= 1
107 elsif rline != expect_arr[idx]
108 if all_matched and $verbose >= 1
109 printf "-- Mismatched line(s) on %s:\n", name
111 printf "-- [%d] < %s\n", idx, expect_arr[idx] if $verbose >= 2
112 printf "-- > %s\n", rline if $verbose >= 1
115 printf "-- %s[%d] = %s\n", name, idx, rline if $verbose >= 5
120 printf "-- %d extra line(s) found on %s\n", extra_lines,
121 name if $verbose == 0
125 printf "-- Mismatched line(s) found on %s\n",
126 name if $verbose == 0
133 @symlinks.each { |fnames|
135 printf "-- Creating: symlink %s %s\n", fnames[0], fnames[1]
137 symres = File.symlink(fnames[0], fnames[1])
138 return false if symres == nil
139 return false unless File.symlink?(fnames[1])
145 @symlinks.each { |fnames|
147 printf "-- Removing: %s (symlink)\n", fnames[1]
149 if File.symlink?(fnames[1])
150 if File.delete(fnames[1]) != 1
151 $stderr.printf "Warning: problem removing symlink '%s'\n",
155 $stderr.printf "Warning: Symlink '%s' does not exist?!?\n",
163 @stderr = $scriptfile + ".stderr"
164 @stdout = $scriptfile + ".stdout"
165 File.delete(@stderr) if File.exists?(@stderr)
166 File.delete(@stdout) if File.exists?(@stdout)
169 @redirs = " <" + @stdin
170 @redirs += " >" + @stdout
171 @redirs += " 2>" + @stderr
176 @new_envs.each_key { |evar|
177 if @old_envs.has_key?(evar)
178 ENV[evar] = @old_envs[evar]
186 @@gbl_envs.each_pair { |evar, eval|
189 @new_envs.each_pair { |evar, eval|
190 if ENV.has_key?(evar)
191 @old_envs[evar] = ENV[evar]
198 tscript = File.open($scriptfile, "w")
199 tscript.printf "#!%s", $testpgm
200 tscript.printf " %s", @shebang_args if @shebang_args != ""
202 tscript.printf "%s", @script_lines if @script_lines != ""
204 File.chmod(0755, $scriptfile)
206 usercmd = $scriptfile
207 usercmd += " " + @user_args if @user_args != nil
211 return 0 unless create_links
212 printf "- Executing: %s\n", usercmd if $verbose >= 1
213 printf "----- with: %s\n", @redirs if $verbose >= 6
214 sys_ok = system(usercmd + @redirs)
218 @sav_cmdvalue = $?.exitstatus
225 if @sav_cmdvalue != @cmdvalue
226 printf "-- Expecting cmdvalue of %d, but $? == %d\n", @cmdvalue,
230 sys_ok = false unless check_out("stdout", @stdout, @expect_out)
231 sys_ok = false unless check_out("stderr", @stderr, @expect_err)
237 # -------+---------+---------+-------- + --------+---------+---------+---------+
238 # Processing of the command-line options given to the regress-sb.rb script.
241 def CommandOptions.parse(command_args)
243 command_args.each { |userarg|
245 when /^--rgdata=(\S+)$/
246 parse_ok = false unless set_rgdatafile($1)
247 when /^--testpgm=(\S+)$/
248 parse_ok = false unless set_testpgm($1)
249 $cmdopt_testpgm = $testpgm
250 when "--stop-on-error", "--stop_on_error"
251 $stop_on_error = true
253 $stderr.printf "Error: Invalid long option: %s\n", userarg
256 userarg = userarg[1...userarg.length]
257 userarg.each_byte { |byte|
263 $stderr.printf "Error: Invalid short option: -%s\n", char
268 $stderr.printf "Error: Invalid request: %s\n", userarg
272 if $rgdatafile == nil
273 rgmatch = Dir.glob("regress*.rgdata")
274 if rgmatch.length == 1
275 $rgdatafile = rgmatch[0]
276 printf "Assuming --rgdata=%s\n", $rgdatafile
278 $stderr.printf "Error: The --rgdata file was not specified\n"
285 def CommandOptions.set_rgdatafile(fname)
286 if not File.exists?(fname)
287 $stderr.printf "Error: Rgdata file '%s' does not exist\n", fname
289 elsif not File.readable?(fname)
290 $stderr.printf "Error: Rgdata file '%s' is not readable\n", fname
293 $rgdatafile = File.expand_path(fname)
297 def CommandOptions.set_testpgm(fname)
298 if not File.exists?(fname)
299 $stderr.printf "Error: Testpgm file '%s' does not exist\n", fname
301 elsif not File.executable?(fname)
302 $stderr.printf "Error: Testpgm file '%s' is not executable\n", fname
305 $testpgm = File.expand_path(fname)
310 # -------+---------+---------+-------- + --------+---------+---------+---------+
311 # Processing of the test-specific options specifed in each [test]/[run]
312 # section of the regression-data file. This will set values in the
313 # global $testdata object.
318 def RGTestOptions.init_rgtopts
319 @@rgtest_opts = Hash.new
320 @@rgtest_opts["$?"] = true
321 @@rgtest_opts["clearenv"] = true
322 @@rgtest_opts["sb_args"] = true
323 @@rgtest_opts["script"] = true
324 @@rgtest_opts["setenv"] = true
325 @@rgtest_opts["stderr"] = true
326 @@rgtest_opts["stdout"] = true
327 @@rgtest_opts["symlink"] = true
328 @@rgtest_opts["user_args"] = true
331 def RGTestOptions.parse(optname, optval)
332 init_rgtopts unless @@rgtest_opts
334 if not @@rgtest_opts.has_key?(optname)
335 $stderr.printf "Error: Invalid test-option in rgdata file: %s\n",
340 # Support a few very specific substitutions in values specified
341 # for test data. Format of all recognized values should be:
343 # which is hopefully distinctive-enough that they will never
344 # conflict with any naturally-occuring string. Also note that
345 # we only match the specific values that we recognize, and not
346 # "just anything" that matches the general pattern. There are
347 # no blanks in the recognized values, but I use an x-tended
348 # regexp and then add blanks to make it more readable.
349 optval.gsub!(/\[%- testpgm\.basename -%\]/x, File.basename($testpgm))
350 optval.gsub!(/\[%- script\.pathname -%\]/x, $scriptfile)
352 invalid_value = false
356 $testdata.cmdvalue = optval.to_i
361 if optval =~ /^\s*([A-Za-z]\w*)\s*$/
362 $testdata.add_clearenv($1)
367 $testdata.shebang_args = optval
369 $testdata.add_script(optval)
371 if optval =~ /^\s*([A-Za-z]\w*)=(.*)$/
372 $testdata.add_setenv($1, $2)
377 $testdata.add_expecterr(optval)
379 $testdata.add_expectout(optval)
381 if optval =~ /^\s*(\S+)\s+(\S+)\s*$/
384 if not File.exists?(srcfile)
385 $stderr.printf "Error: source file '%s' does not exist.\n",
388 elsif File.exists?(newfile)
389 $stderr.printf "Error: new file '%s' already exists.\n",
393 $testdata.add_symlink(srcfile, newfile)
399 $testdata.user_args = optval
401 $stderr.printf "InternalError: Invalid test-option in rgdata file: %s\n",
407 $stderr.printf "Error: Invalid value(s) for %s: %s\n",
415 # -------+---------+---------+-------- + --------+---------+---------+---------+
416 # Here's where the "main" routine begins...
419 $cmdopt_testpgm = nil
422 $scriptfile = "/tmp/env-regress"
423 $stop_on_error = false
426 exit 1 unless CommandOptions.parse(ARGV)
433 regress_data = File.open($rgdatafile)
434 regress_data.each_line { |dline|
436 when /^\s*#/, /^\s*$/
437 # Just a comment line, ignore it.
438 when /^\s*gblenv=\s*(.+)$/
440 $stderr.printf "Error: Cannot define a global-value in the middle of a test (#5d)\n", test_lineno
441 errline = regress_data.lineno
445 if tempval !~ /^([A-Za-z]\w*)=(.*)$/
446 $stderr.printf "Error: Invalid value for 'gblenv=' request: %s\n",
448 errline = regress_data.lineno
451 ExpectedResult.add_gblenv($1, $2)
452 when /^testpgm=\s*(\S+)\s*/
453 # Set the location of the program to be tested, if it wasn't set
454 # on the command-line processing.
455 if $cmdopt_testpgm == nil
456 if not CommandOptions.set_testpgm($1)
457 errline = regress_data.lineno
463 $stderr.printf "Error: Request to define a [test], but we are still defining\n"
464 $stderr.printf " the [test] at line #%s\n", test_lineno
465 errline = regress_data.lineno
468 test_lineno = regress_data.lineno
469 max_test = test_lineno
470 printf "- Defining test at line #%s\n", test_lineno if $verbose >= 6
471 $testdata = ExpectedResult.new
473 # User wants us to ignore the remainder of the rgdata file...
477 $stderr.printf "Error: Request to [run] a test, but no test is presently defined\n"
478 errline = regress_data.lineno
481 printf "- Running test at line #%s\n", test_lineno if $verbose >= 1
482 run_result = $testdata.run_test
484 printf "[Test #%3d: ", test_count
488 printf "Failed! (line %4d)]\n", test_lineno
489 break if $stop_on_error
491 # Test ran as expected
495 # Internal error of some sort
496 printf "InternalError! (line %4d)]\n", test_lineno
497 errline = regress_data.lineno
502 when /^(\s*)([^\s:]+)\s*:(.+)$/
507 $stderr.printf "Error: No test is presently being defined\n"
508 errline = regress_data.lineno
511 # All the real work happens in RGTestOptions.parse
512 if not RGTestOptions.parse(test_lhs, test_rhs)
513 errline = regress_data.lineno
516 if blankpfx.length == 0
517 $stderr.printf "Note: You should at least one blank before:%s\n",
519 $stderr.printf " at line %d of rgdata file %s\n",
520 regress_data.lineno, $rgdatafile
524 $stderr.printf "Error: Invalid line: %s\n", dline.chomp
525 errline = regress_data.lineno
531 $stderr.printf " at line %d of rgdata file %s\n", errline, $rgdatafile
534 if testok_count != test_count
535 printf "%d of %d tests were successful.\n", testok_count, test_count
539 printf "All %d tests were successful!\n", testok_count