changeset 392:c28ecaef065f

Tests: try_run() introduced. This method replaces multiple ad-hoc solutions to grecefully skip tests if nginx startup fails, e.g., due to no support for a particular directive.
author Maxim Dounin <mdounin@mdounin.ru>
date Fri, 18 Apr 2014 18:36:26 +0400
parents 915ef26ac6eb
children 3c9aeeb09ac8
files access.t debug_connection.t fastcgi_buffering.t gunzip_memcached.t gunzip_static.t http_disable_symlinks.t http_resolver_aaaa.t lib/Test/Nginx.pm proxy_cache_lock.t proxy_cache_revalidate.t proxy_cookie.t xslt_params.t
diffstat 12 files changed, 26 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- a/access.t
+++ b/access.t
@@ -82,14 +82,7 @@ http {
 
 EOF
 
-eval {
-	open OLDERR, ">&", \*STDERR; close STDERR;
-	$t->run();
-	open STDERR, ">&", \*OLDERR;
-};
-plan(skip_all => 'no inet6 and/or unix support') if $@;
-
-$t->plan(12);
+$t->try_run('no inet6 and/or unix support')->plan(12);
 
 ###############################################################################
 
--- a/debug_connection.t
+++ b/debug_connection.t
@@ -54,14 +54,7 @@ http {
 
 EOF
 
-eval {
-	open OLDERR, ">&", \*STDERR; close STDERR;
-	$t->run();
-	open STDERR, ">&", \*OLDERR;
-};
-plan(skip_all => 'no inet6 support') if $@;
-
-$t->plan(5);
+$t->try_run('no inet6 support')->plan(5);
 
 ###############################################################################
 
--- a/fastcgi_buffering.t
+++ b/fastcgi_buffering.t
@@ -62,16 +62,9 @@ EOF
 	'<!--#include virtual="/include$request_uri" set="x" -->' .
 	'set: <!--#echo var="x" -->');
 
-eval {
-	open OLDERR, ">&", \*STDERR; close STDERR;
-	$t->run();
-	open STDERR, ">&", \*OLDERR;
-};
-plan(skip_all => 'no fastcgi_buffering') if $@;
+$t->try_run('no fastcgi_buffering')->plan(2);
 
-$t->run_daemon(\&fastcgi_daemon);
-
-$t->plan(2)->waitforsocket('127.0.0.1:8081');
+$t->run_daemon(\&fastcgi_daemon)->waitforsocket('127.0.0.1:8081');
 
 ###############################################################################
 
--- a/gunzip_memcached.t
+++ b/gunzip_memcached.t
@@ -71,14 +71,7 @@ if ($memhelp =~ /-U/) {
 
 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081', @memopts);
 
-eval {
-	open OLDERR, ">&", \*STDERR; close STDERR;
-	$t->run();
-	open STDERR, ">&", \*OLDERR;
-};
-plan(skip_all => 'no memcached_gzip_flag') if $@;
-
-$t->plan(2);
+$t->try_run('no memcached_gzip_flag')->plan(2);
 
 $t->waitforsocket('127.0.0.1:8081')
 	or die "Can't start memcached";
--- a/gunzip_static.t
+++ b/gunzip_static.t
@@ -74,14 +74,7 @@ IO::Compress::Gzip::gzip(\$in => \$out);
 $t->write_file('t2.gz', $out . $out);
 $t->write_file('t3', 'not compressed');
 
-eval {
-	open OLDERR, ">&", \*STDERR; close STDERR;
-	$t->run();
-	open STDERR, ">&", \*OLDERR;
-};
-plan(skip_all => 'no gzip_static always') if $@;
-
-$t->plan(12);
+$t->try_run('no gzip_static always')->plan(12);
 
 ###############################################################################
 
--- a/http_disable_symlinks.t
+++ b/http_disable_symlinks.t
@@ -154,13 +154,6 @@ http {
 
 EOF
 
-eval {
-	open OLDERR, ">&", \*STDERR; close STDERR;
-	$t->run();
-	open STDERR, ">&", \*OLDERR;
-};
-plan(skip_all => 'no disable_symlinks') if $@;
-
 my $uid = getuid();
 my ($extfile) = grep { -f "$_" && $uid != (stat($_))[4] }
 	('/etc/resolv.conf', '/etc/protocols', '/etc/host.conf');
@@ -168,9 +161,9 @@ my ($extfile) = grep { -f "$_" && $uid !
 plan(skip_all => 'no external file found')
 	if !defined $extfile;
 
-my $d = $t->testdir();
+$t->try_run('no disable_symlinks')->plan(28);
 
-$t->plan(28);
+my $d = $t->testdir();
 
 mkdir("$d/on");
 mkdir("$d/not_owner");
--- a/http_resolver_aaaa.t
+++ b/http_resolver_aaaa.t
@@ -68,12 +68,7 @@ http {
 
 EOF
 
-eval {
-	open OLDERR, ">&", \*STDERR; close STDERR;
-	$t->run();
-	open STDERR, ">&", \*OLDERR;
-};
-plan(skip_all => 'no inet6 support') if $@;
+$t->try_run('no inet6 support')->plan(72);
 
 $t->run_daemon(\&dns_daemon, 8081, $t);
 $t->run_daemon(\&dns_daemon, 8082, $t);
@@ -81,8 +76,6 @@ plan(skip_all => 'no inet6 support') if 
 $t->waitforfile($t->testdir . '/8081');
 $t->waitforfile($t->testdir . '/8082');
 
-$t->plan(72);
-
 ###############################################################################
 
 my (@n, $response);
--- a/lib/Test/Nginx.pm
+++ b/lib/Test/Nginx.pm
@@ -172,6 +172,19 @@ sub has_daemon($) {
 	return $self;
 }
 
+sub try_run($$) {
+	my ($self, $message) = @_;
+
+	eval {
+		open OLDERR, ">&", \*STDERR; close STDERR;
+		$self->run();
+		open STDERR, ">&", \*OLDERR;
+	};
+
+	Test::More::plan(skip_all => $message) if $@;
+	return $self;
+}
+
 sub plan($) {
 	my ($self, $plan) = @_;
 
--- a/proxy_cache_lock.t
+++ b/proxy_cache_lock.t
@@ -70,14 +70,8 @@ EOF
 
 $t->run_daemon(\&http_fake_daemon);
 
-eval {
-	open OLDERR, ">&", \*STDERR; close STDERR;
-	$t->run();
-	open STDERR, ">&", \*OLDERR;
-};
-plan(skip_all => 'no proxy_cache_lock') if $@;
+$t->try_run('no proxy_cache_lock')->plan(19);
 
-$t->plan(19);
 $t->waitforsocket('127.0.0.1:8081');
 
 ###############################################################################
--- a/proxy_cache_revalidate.t
+++ b/proxy_cache_revalidate.t
@@ -67,13 +67,7 @@ EOF
 $t->write_file('t', 'SEE-THIS');
 $t->write_file('t2', 'SEE-THIS');
 
-eval {
-	open OLDERR, ">&", \*STDERR; close STDERR;
-	$t->run();
-	open STDERR, ">&", \*OLDERR;
-};
-plan(skip_all => 'no proxy_cache_revalidate') if $@;
-$t->plan(9);
+$t->try_run('no proxy_cache_revalidate')->plan(9);
 
 ###############################################################################
 
--- a/proxy_cookie.t
+++ b/proxy_cookie.t
@@ -73,14 +73,7 @@ http {
 
 EOF
 
-eval {
-	open OLDERR, ">&", \*STDERR; close STDERR;
-	$t->run();
-	open STDERR, ">&", \*OLDERR;
-};
-plan(skip_all => 'no proxy_cookie') if $@;
-
-$t->plan(8);
+$t->try_run('no proxy_cookie')->plan(8);
 
 ###############################################################################
 
--- a/xslt_params.t
+++ b/xslt_params.t
@@ -86,14 +86,7 @@ EOF
 $t->write_file('x2', '<root>data</root>');
 $t->write_file('x3', '<root>data</root>');
 
-eval {
-	open OLDERR, ">&", \*STDERR; close STDERR;
-	$t->run();
-	open STDERR, ">&", \*OLDERR;
-};
-
-plan(skip_all => 'no xslt_param') if $@;
-$t->plan(3);
+$t->try_run('no xslt_param')->plan(3);
 
 ###############################################################################