From d4b754a8b1ebbccb0a67df0e966e5b670850e60f Mon Sep 17 00:00:00 2001 From: Wang Xiaozhe Date: Tue, 18 May 2010 18:35:05 +0800 Subject: [PATCH] added basic tests and reindex script --- t/{sanity.t => 0-sanity.t} | 0 t/basic.t | 36 +++++++++++++++++++++ util/reindex | 64 ++++++++++++++++++++++++++++++++++++++ util/run_test.sh | 3 ++ 4 files changed, 103 insertions(+) rename t/{sanity.t => 0-sanity.t} (100%) create mode 100644 t/basic.t create mode 100755 util/reindex diff --git a/t/sanity.t b/t/0-sanity.t similarity index 100% rename from t/sanity.t rename to t/0-sanity.t diff --git a/t/basic.t b/t/basic.t new file mode 100644 index 00000000..ccf00250 --- /dev/null +++ b/t/basic.t @@ -0,0 +1,36 @@ +# vi:ft=perl +use lib 'lib'; +use Test::Nginx::Socket; + +repeat_each(2); + +plan tests => blocks() * repeat_each() * 2; + +run_tests(); + +__DATA__ + +=== TEST 1: internal only +--- config + location /lua { + set_by_lua $res "function fib(n) if n > 2 then return fib(n-1)+fib(n-2) else return 1 end end return fib(10)"; + echo $res; + } +--- request +GET /lua +--- response_body +55 + + + +=== TEST 2: internal script with argument +--- config + location /lua { + set_by_lua $res "return ngx.arg[1]+ngx.arg[2]" $arg_a $arg_b; + echo $res; + } +--- request +GET /lua?a=1&b=2 +--- response_body +3 + diff --git a/util/reindex b/util/reindex new file mode 100755 index 00000000..bd54c9df --- /dev/null +++ b/util/reindex @@ -0,0 +1,64 @@ +#!/usr/bin/perl +#: reindex.pl +#: reindex .t files for Test::Base based test files +#: Copyright (c) 2006 Agent Zhang +#: 2006-04-27 2006-05-09 + +use strict; +use warnings; + +#use File::Copy; +use Getopt::Std; + +my %opts; +getopts('hb:', \%opts); +if ($opts{h} or ! @ARGV) { + die "Usage: reindex [-b 0] t/*.t\n"; +} + +my $init = $opts{b}; +$init = 1 if not defined $init; + +my @files = map glob, @ARGV; +for my $file (@files) { + next if -d $file or $file !~ /\.t_?$/; + reindex($file); +} + +sub reindex { + my $file = $_[0]; + open my $in, $file or + die "Can't open $file for reading: $!"; + my @lines; + my $counter = $init; + my $changed; + while (<$in>) { + s/\r$//; + my $num; + s/ ^ === \s+ TEST \s+ (\d+)/$num=$1; "=== TEST " . $counter++/xie; + next if !defined $num; + if ($num != $counter-1) { + $changed++; + } + } continue { + push @lines, $_; + } + close $in; + my $text = join '', @lines; + $text =~ s/(?x) \n+ === \s+ TEST/\n\n\n\n=== TEST/ixsg; + $text =~ s/__(DATA|END)__\n+=== TEST/__${1}__\n\n=== TEST/; + #$text =~ s/\n+$/\n\n/s; + if (! $changed and $text eq join '', @lines) { + warn "reindex: $file:\tskipped.\n"; + return; + } + #File::Copy::copy( $file, "$file.bak" ); + open my $out, "> $file" or + die "Can't open $file for writing: $!"; + binmode $out; + print $out $text; + close $out; + + warn "reindex: $file:\tdone.\n"; +} + diff --git a/util/run_test.sh b/util/run_test.sh index 3346375a..8f3179bb 100755 --- a/util/run_test.sh +++ b/util/run_test.sh @@ -1,6 +1,9 @@ #!/bin/bash script_dir=$(dirname $0) root=$(readlink -f $script_dir/..) +cd $root +git submodule update --init +$script_dir/reindex $root/t/*.t export PATH=$root/work/sbin:$PATH killall nginx prove -I$root/t/test-nginx/lib $root/t/*.t