CVS update: php3/regtests

From: Date: Sun, 23 Aug 1998 02:19:38 +0000
Subject: CVS update: php3/regtests
Groups: php.dev 
Request: Send a blank email to php-dev+get-500@lists.php.net to get a copy of this message
Date: Saturday August 22, 1998 @ 22:19 Author: rocky Update of /repository/php3/regtests In directory asf:/tmp/cvs-serv10175 Modified Files: testphp.pl Log Message: Add way to add comment lines. Lingo: -- Add ability to match patterns in HTTP header. Lingo: --HEAD-- --TEST-- is now --TITLE-- Improve --quiet behavior, is more quiet now And FAILURE is reported more Remove more awk-inherited programming oddities, simplified that part of the code. Index: php3/regtests/testphp.pl diff -c php3/regtests/testphp.pl:1.1 php3/regtests/testphp.pl:1.2 *** php3/regtests/testphp.pl:1.1 Thu Aug 20 00:47:44 1998 --- php3/regtests/testphp.pl Sat Aug 22 22:19:38 1998 *************** *** 1,35 **** ! #!/bin/perl ! $vcid='$Id: testphp.pl,v 1.1 1998/08/20 04:47:44 rocky Exp $'; sub usage { print " ! usage: $program [OPTIONS] server *php-test-program* Used to test php3 programs. A URL is constructed and requested. For example it might be something like myserver.org:${serverport}${prefix}001.phtml. ! The output is compared against the results found in php-test-program. ! The format of php-test-program contains: ! 1. a description of the test following the line --TEST-- ! 2. the name of the php-program to run after a server, port and prefix ! are prepended after the --FILE-- ! 3. The expected output after the line --EXPECT-- ! 4. Either --GET-- or --POST-- to specify the GET or POST method. Here is a complete testfile: ! --TEST-- Trivial 'Hello World' test ... ! --FILE-- basic/001.phtml - --GET-- --EXPECT-- Hello World OPTIONS: help -- display this help version -- print version string and exit diff *prog* -- specify diff program and options for use in comparison. Default is: $diff port *n* -- specify port number to connect to in HTTP server --- 1,71 ---- ! #!/bin/perl -w ! $vcid='$Id: testphp.pl,v 1.2 1998/08/23 02:19:38 rocky Exp $'; sub usage { print " ! usage: $program [OPTIONS] server *php-test-control-file* Used to test php3 programs. A URL is constructed and requested. For example it might be something like myserver.org:${serverport}${prefix}001.phtml. ! The output is compared against the results found in php-control-file. ! The format of php-test-control-file is a sequence of sections: ! a TITLE section, a URL section, an optional POST section, an ! optional HEAD section and an EXPECT section. ! ! The start of each section is indicated in the ! file by surrounding it in dashes. Here is a template: ! ! -- A comment can go after any line starting with: -- ! --TITLE-- ! Description of test ! [line2]... ! --URL-- ! test-program[?args1=value[&arg2=value...] ! [--POST-- ! [post argument line 1] ! [post argument line 2...] ! [--HEAD-- ! pattern,...] ! --EXPECT-- ! [expect output] ! ! ! The TITLE section can contain a number of lines and merely describes the ! name of the test. It is not used in validation, but is manditory. ! It is printed before the test is run. ! ! The URL section gives the name of the test to run, and is manditory. ! The servername, port and an optional prefix are all prepended ! to the URL. ! ! The POST section is optional, but if given, a POST method will be used. ! It is followed by a number of lines, possibly empty which contain ! the POST arguments. ! ! If there is no POST section, only a GET method will be used. ! ! The HEAD section is optional too, but if given the header from the ! HTTP server will be checked againt the perl pattern that is in ! this section. + The EXPECT section contains the desired output to check against. The + section is manditory, although there my be no lines (no output). + Here is a complete testfile: ! --TITLE-- Trivial 'Hello World' test ... ! --URL-- basic/001.phtml --EXPECT-- Hello World OPTIONS: help -- display this help version -- print version string and exit + quiet -- don't print the Description seciton. diff *prog* -- specify diff program and options for use in comparison. Default is: $diff port *n* -- specify port number to connect to in HTTP server *************** *** 42,64 **** init(); process_options(); read_test_info($php_testfile); ! run_test($cmd); ! $rc=compare_results(); exit $rc; sub init { ($program = $0) =~ s,.*/,,; # Who am I today, anyway? ! $php_out = 'php.out'; ! $php_test = 'php.test'; $http_out = 'http.out'; - $expect = 0; - $get = 0; - $post = 0; - $serverport= 80; ! $prefix='/test/'; $diff='diff -c -w'; } # Grok program options -- the bane of programming. --- 78,97 ---- init(); process_options(); read_test_info($php_testfile); ! run_test($serverhost, $serverport, $url); ! $rc=compare_results($http_out, $expect_out, $http_header); exit $rc; sub init { ($program = $0) =~ s,.*/,,; # Who am I today, anyway? ! $expect_out = 'expect.out'; $http_out = 'http.out'; $serverport= 80; ! $prefix=''; $diff='diff -c -w'; + $debug=0; + $quiet=0; } # Grok program options -- the bane of programming. *************** *** 70,75 **** --- 103,109 ---- 'help' => \$help, 'version' => \$show_version, 'debug' => \$debug, + 'quiet' => \$quiet, 'diff=s' => \$diff, 'port=i' => \$serverport, 'prefix=s' => \$prefix, *************** *** 82,182 **** } $serverhost =$ARGV[0]; $php_testfile=$ARGV[1]; - $single_thread = $debug != 0; - } # Reads the PHP test file spec, and creates the expected output file ! # to compare results against. sub read_test_info { ! my($file) = @_; ! my($expect, $post, $testcase)=(0,0,0); # 1 if in EXPECT, POST, TEST sect. ! my($Expect); # Strings for these sections ! undef($Post); - # get rid of temp files left laying around - unlink $php_out, $php_test; ! open(PHP_OUT, ">$php_out") ! || die "Cannot create file $php_out for writing: $!"; ! open(PHP, "<$file") ! || die "Can't open $file for reading: $!"; ! ! while (<PHP>) { ! # See if we've hit a new section header. ! if (/--TEST--/) { ! $file = 0; ! $expect = 0; ! $post = 0; ! $testcase = 1; ! next; ! } elsif (/--FILE--/) { ! $file = 1; ! $expect = 0; ! $post = 0; ! $testcase = 0; ! next; ! } elsif (/--POST--/) { ! $file = 0; ! $expect = 0; ! $Post = ''; ! $post = 1; ! $testcase = 0; ! next; ! } elsif (/--GET--/) { ! $get = 1; ! $post = 0; ! $file = 0; ! $expect = 0; ! $testcase = 0; ! next; ! } elsif (/--EXPECT--/) { ! $expect = 1; ! $Expect=''; ! $post = 0; ! $file = 0; ! $testcase = 0; ! next; ! } ! # Above we handled switching between sections. We get here only ! # if in the middle of some section. Process that section. ! if ($expect) { ! $Expect .= $_ if !/^\s+$/; ! } elsif ($file) { ! chomp; ! $url = "${prefix}$_"; ! } elsif ($testcase) { ! $desc .= $_; ! } elsif ($post) { ! chomp; ! $Post .= $_; ! } } # Now do some basic error checking of the test file. ! if ($get && $Post) { ! print STDERR "We don't support both GET and POSTs--ignoring GET\n"; ! } elsif (! $get && !defined($Post)) { ! myerr("Need exactly one GET or POST", 10); ! } elsif (!$url) { ! myerr("FILE section not found", 20); } elsif (!defined($Expect)) { ! myerr("Didn't see --EXPECT--", 30); } ! print PHP_OUT $Expect ; ! close(PHP_OUT); ! close(PHP); ! } # Connects to HTTP server which should have the PHP3 test program # and fetches the output ignoring the header. sub run_test { ! my($cmd) = @_; - print $desc; - use IO::Socket; $remote = IO::Socket::INET->new (Proto => "tcp", PeerAddr => $serverhost, --- 116,217 ---- } $serverhost =$ARGV[0]; $php_testfile=$ARGV[1]; } # Reads the PHP test file spec, and creates the expected output file ! # to compare results against. Save the output in string $Expect too. sub read_test_info { ! my($test_control_file) = @_; ! my($desc); ! # Name of section we are processing: TITLE, EXPECT, POST, URL, etc. ! my($in_section)=''; ! undef($Post); # String part of the POST section ! undef($Expect); # String part of the EXPECT section ! undef($Head_pat); # String part of the HEAD section ! # get rid of temp files left laying around ! unlink $expect_out, $http_out; ! open(EXPECT_OUT, ">$expect_out") ! || die "Cannot create file $expect_out for writing: $!"; ! open(CONTROL_FILE, "<$test_control_file") ! || die "Can't open $test_control_file for reading: $!"; ! ! my($lineno); ! for ($lineno=1; <CONTROL_FILE>; $lineno++) { ! # See if we've hit a new section header. ! if (/^--TITLE--/) { ! $in_section='title'; ! next; ! } elsif (/^--URL--/) { ! $in_section='url'; ! next; ! } elsif (/^--POST--/) { ! $in_section='post'; ! $Post = ''; ! next; ! } elsif (/^--HEAD--/) { ! $in_section='head'; ! next; ! } elsif (/^--EXPECT--/) { ! $in_section='expect'; ! $Expect=''; ! next; ! } elsif (/^--/) { ! # Comment. ! next; ! } ! ! # Above we handled switching between sections. We get here only ! # if in the middle of some section. Process the line of that section. ! if ($in_section eq 'expect') { ! $Expect .= $_; ! } elsif ($in_section eq 'url') { ! chomp; ! $url = "${prefix}$_"; ! } elsif ($in_section eq 'title') { ! $desc .= $_; ! } elsif ($in_section eq 'post') { ! chomp; ! $Post .= $_; ! } elsif ($in_section eq 'head') { ! chomp; ! $Head_pat .= $_; ! } elsif ($in_section eq '') { ! print STDERR "$program: line $lineno not in any section--ignored.\n" ! if !$quiet; ! } else { ! myerr("$program internal error in value \$in_section; is $in_section", ! 11); ! } } + + if ($quiet) { + printf "%s: ", $test_control_file; + } else { + $desc = "No description supplied.\n" if !defined($desc); + printf "%s\n%s:\n%s", '-' x 50, $test_control_file, $desc + } + # Now do some basic error checking of the test file. ! if (!$url) { ! myerr("URL section not found", 20); } elsif (!defined($Expect)) { ! myerr("Didn't see --EXPECT--", 30); } ! print EXPECT_OUT $Expect ; ! close(EXPECT_OUT); ! close(CONTROL_FILE); ! } # Connects to HTTP server which should have the PHP3 test program # and fetches the output ignoring the header. sub run_test { ! my($serverhost, $serverport, $url) = @_; use IO::Socket; $remote = IO::Socket::INET->new (Proto => "tcp", PeerAddr => $serverhost, *************** *** 198,214 **** } else { print $remote "GET $url HTTP/1.0\n\n"; } - while (defined($_=<PHP>)) { - print $remote $_; - } - close(PHP); open(HTTP_OUT, ">$http_out") || die "Cannot create file $http_out for writing: $!"; ! # Skip after the HTTP header. It should be after \n\n or \r\n\r\n ! # The below is a not quite perfect... ! while ( defined($line=<$remote>) && ($line !~ /^\s*$/) ) { ; } while ( defined($line=<$remote>) ) { next if $line =~ /^\s*$/; --- 233,248 ---- } else { print $remote "GET $url HTTP/1.0\n\n"; } open(HTTP_OUT, ">$http_out") || die "Cannot create file $http_out for writing: $!"; ! # Skip until after the HTTP header. It should be after \n\n or \r\n\r\n ! # If we have a http header pattern, save the header. ! # The below may not be quite perfect in finding the end of header. ! while ( defined($line=<$remote>) && ($line !~ /^\s*$/) ) { ! $http_header .= $line if defined($Head_pat); ! } while ( defined($line=<$remote>) ) { next if $line =~ /^\s*$/; *************** *** 220,228 **** } ! sub compare_results { ! my(@output)=$diff $http_out $php_out 2>&1; my($ret) = $? >> 8; if ( $ret ) { --- 254,274 ---- } ! # ! # Compare what we got back from PHP on the server with ! # what we're supposed to see. ! # Right now this uses Unix diff, perhaps we'll expand to remove ! # this program dependency and add pattern matching. sub compare_results { ! my($http_file, $expect_file, $http_header)=@_; ! ! if (defined($Head_pat)) { ! if (!compare_with_pattern($http_header, $Head_pat)) { ! print "Header match failed:\n$Head_pat\n---\n$http_header"; ! return 1; ! } ! } ! my(@output)=$diff $http_file $expect_file 2>&1; my($ret) = $? >> 8; if ( $ret ) { *************** *** 231,237 **** } else { print " passed\n"; } ! exit $ret; } # Print version and exit. --- 277,283 ---- } else { print " passed\n"; } ! return $ret; } # Print version and exit. *************** *** 242,247 **** sub myerr { my($msg, $rc) = @_; ! print STDERR "$msg\n"; exit $rc; } --- 288,301 ---- sub myerr { my($msg, $rc) = @_; ! print STDERR "$msg\n" if !$quiet; ! print " FAILED\n"; exit $rc; + } + + # Compare output string with a perl Pattern and return 1 if it matches, + # or 0 if not. + sub compare_with_pattern { + my($http_output, $expect_pat) = @_; + return $http_output =~ /$expect_pat/; } -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#500) next »