[nginx-tests] Tests: removed usage of "cat" and "grep" in tests.

Maxim Dounin mdounin at mdounin.ru
Mon Aug 12 03:37:27 UTC 2024


details:   http://freenginx.org/hg/nginx-tests/rev/d329b05e20fa
branches:  
changeset: 1995:d329b05e20fa
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Fri Aug 09 05:18:51 2024 +0300
description:
Tests: removed usage of "cat" and "grep" in tests.

Both "cat" and "grep" might not be available on Windows.  Instead,
$t->read_file() is used, as it is already used in other places.  To
simplify checking logs as previously done with "grep", $t->grep_file()
helper function is introduced.

diffstat:

 grpc_request_buffering.t |   2 +-
 h3_ssl_session_reuse.t   |   2 +-
 lib/Test/Nginx.pm        |  16 ++++++++++++----
 memcached_fake.t         |   2 +-
 post_action.t            |   2 +-
 proxy_cache_vary.t       |   2 +-
 proxy_keepalive.t        |   2 +-
 ssl.t                    |   2 +-
 ssl_ocsp.t               |   2 +-
 ssl_session_reuse.t      |   2 +-
 ssl_stapling.t           |   2 +-
 stream_resolver.t        |   2 +-
 12 files changed, 23 insertions(+), 15 deletions(-)

diffs (168 lines):

diff --git a/grpc_request_buffering.t b/grpc_request_buffering.t
--- a/grpc_request_buffering.t
+++ b/grpc_request_buffering.t
@@ -105,7 +105,7 @@ is(eval(join '+', map { $_->{length} } g
 is(eval(join '+', map { $_->{length} } grep { $_->{type} eq "DATA" } @$frames),
 	465, 'preserve_output - last body bytes');
 
-like(`grep -F '[crit]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no crits');
+is($t->grep_file('error.log', '[crit]'), '', 'no crit');
 
 ###############################################################################
 
diff --git a/h3_ssl_session_reuse.t b/h3_ssl_session_reuse.t
--- a/h3_ssl_session_reuse.t
+++ b/h3_ssl_session_reuse.t
@@ -151,7 +151,7 @@ is(test_reuse(8949), 0, 'cache off not r
 
 $t->stop();
 
-like(`grep -F '[crit]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no crit');
+is($t->grep_file('error.log', '[crit]'), '', 'no crit');
 
 ###############################################################################
 
diff --git a/lib/Test/Nginx.pm b/lib/Test/Nginx.pm
--- a/lib/Test/Nginx.pm
+++ b/lib/Test/Nginx.pm
@@ -84,14 +84,12 @@ sub DESTROY {
 	}
 
 	if (Test::More->builder->expected_tests) {
-		my $errors = $self->read_file('error.log');
-		$errors = join "\n", $errors =~ /.+Sanitizer.+/gm;
+		my $errors = $self->grep_file('error.log', 'Sanitizer');
 		Test::More::is($errors, '', 'no sanitizer errors');
 	}
 
 	if (Test::More->builder->expected_tests && $ENV{TEST_NGINX_VALGRIND}) {
-		my $errors = $self->read_file('valgrind.log');
-		$errors = join "\n", $errors =~ /^==\d+== .+/gm;
+		my $errors = $self->grep_file('valgrind.log', /^==\d+== .+/m);
 		Test::More::is($errors, '', 'no valgrind errors');
 	}
 
@@ -633,6 +631,16 @@ sub read_file($) {
 	return $content;
 }
 
+sub grep_file($$) {
+	my ($self, $name, $regex) = @_;
+
+	my $lines = $self->read_file($name);
+
+	$regex = qr/.*\Q$regex\E.*/m if ref($regex) eq '';
+
+	return join "\n", $lines =~ /$regex/g;
+}
+
 sub write_file($$) {
 	my ($self, $name, $content) = @_;
 
diff --git a/memcached_fake.t b/memcached_fake.t
--- a/memcached_fake.t
+++ b/memcached_fake.t
@@ -69,7 +69,7 @@ like(http_get('/'), qr/SEE-THIS/, 'memca
 
 like(http_get('/ssi.html'), qr/SEE-THIS/, 'memcached ssi var');
 
-like(`grep -F '[error]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no errors');
+is($t->grep_file('error.log', '[error]'), '', 'no errors');
 
 ###############################################################################
 
diff --git a/post_action.t b/post_action.t
--- a/post_action.t
+++ b/post_action.t
@@ -76,6 +76,6 @@ unlike(http_get('/remote'), qr/HIDDEN/m,
 
 $t->stop();
 
-like(`cat ${\($t->testdir())}/access.log`, qr/post/, 'post action in logs');
+like($t->read_file('access.log'), qr/post/, 'post action in logs');
 
 ###############################################################################
diff --git a/proxy_cache_vary.t b/proxy_cache_vary.t
--- a/proxy_cache_vary.t
+++ b/proxy_cache_vary.t
@@ -293,7 +293,7 @@ like(get1('/cold?vary=x,y&xtra=1', 'x:2'
 
 $t->stop();
 
-like(`grep -F '[crit]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no crit');
+is($t->grep_file('error.log', '[crit]'), '', 'no crit');
 
 ###############################################################################
 
diff --git a/proxy_keepalive.t b/proxy_keepalive.t
--- a/proxy_keepalive.t
+++ b/proxy_keepalive.t
@@ -208,7 +208,7 @@ like(http_get('/inmemory/closed2'), qr/2
 
 # check for errors, shouldn't be any
 
-like(`grep -F '[error]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no errors');
+is($t->grep_file('error.log', '[error]'), '', 'no errors');
 
 ###############################################################################
 
diff --git a/ssl.t b/ssl.t
--- a/ssl.t
+++ b/ssl.t
@@ -275,7 +275,7 @@ is(get_ssl_shutdown(8085), 1, 'ssl shutd
 like($t->read_file('ssl.log'), qr/^(TLS|SSL)v(\d|\.)+$/m,
 	'log ssl variable on lingering close');
 
-like(`grep -F '[crit]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no crit');
+is($t->grep_file('error.log', '[crit]'), '', 'no crit');
 
 ###############################################################################
 
diff --git a/ssl_ocsp.t b/ssl_ocsp.t
--- a/ssl_ocsp.t
+++ b/ssl_ocsp.t
@@ -409,7 +409,7 @@ like(get('root', port => 8447), qr/200 O
 
 # check for errors
 
-like(`grep -F '[crit]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no crit');
+is($t->grep_file('error.log', '[crit]'), '', 'no crit');
 
 ###############################################################################
 
diff --git a/ssl_session_reuse.t b/ssl_session_reuse.t
--- a/ssl_session_reuse.t
+++ b/ssl_session_reuse.t
@@ -195,7 +195,7 @@ is(test_reuse(8449), 0, 'cache off not r
 
 $t->stop();
 
-like(`grep -F '[crit]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no crit');
+is($t->grep_file('error.log', '[crit]'), '', 'no crit');
 
 ###############################################################################
 
diff --git a/ssl_stapling.t b/ssl_stapling.t
--- a/ssl_stapling.t
+++ b/ssl_stapling.t
@@ -301,7 +301,7 @@ local $TODO = 'broken TLSv1.3 sigalgs in
 	&& !Net::SSLeay::constant("LIBRESSL_VERSION_NUMBER")
 	&& test_tls13();
 
-like(`grep -F '[crit]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no crit');
+is($t->grep_file('error.log', '[crit]'), '', 'no crit');
 
 }
 
diff --git a/stream_resolver.t b/stream_resolver.t
--- a/stream_resolver.t
+++ b/stream_resolver.t
@@ -107,7 +107,7 @@ ok(stream('127.0.0.1:' . port(8086))->re
 SKIP: {
 skip "relies on error log contents", 2 unless $ENV{TEST_NGINX_UNSAFE};
 
-my $log = `grep -F '[error]' ${\($t->testdir())}/error.log`;
+my $log = $t->grep_file('error.log', '[error]');
 like($log, qr/no port in upstream "a.example.com"/, 'log - no port');
 like($log, qr/nx.example.com could not be resolved/, 'log - not found');
 


More information about the nginx-devel mailing list