unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 19/30] test_common: use autodie and read_all where possible
Date: Tue, 17 Oct 2023 23:38:04 +0000	[thread overview]
Message-ID: <20231017233815.1637932-20-e@80x24.org> (raw)
In-Reply-To: <20231017233815.1637932-1-e@80x24.org>

Noise reduction to help my aging eyes.
---
 lib/PublicInbox/TestCommon.pm | 73 ++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 39 deletions(-)

diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm
index 77da822b..3839ab45 100644
--- a/lib/PublicInbox/TestCommon.pm
+++ b/lib/PublicInbox/TestCommon.pm
@@ -16,6 +16,7 @@ our @EXPORT;
 my $lei_loud = $ENV{TEST_LEI_ERR_LOUD};
 my $tail_cmd = $ENV{TAIL};
 our ($lei_opt, $lei_out, $lei_err, $lei_cwdfh);
+use autodie qw(chdir close fcntl open opendir seek unlink);
 
 $_ = File::Spec->rel2abs($_) for (grep(!m!^/!, @INC));
 
@@ -46,11 +47,16 @@ sub require_bsd (;$) {
 
 sub xbail (@) { BAIL_OUT join(' ', map { ref() ? (explain($_)) : ($_) } @_) }
 
+sub read_all ($;$$) {
+	require PublicInbox::Git;
+	PublicInbox::Git::read_all($_[0], $_[1], $_[2])
+}
+
 sub eml_load ($) {
 	my ($path, $cb) = @_;
-	open(my $fh, '<', $path) or die "open $path: $!";
+	open(my $fh, '<', $path);
 	require PublicInbox::Eml;
-	PublicInbox::Eml->new(\(do { local $/; <$fh> }));
+	PublicInbox::Eml->new(\(read_all($fh)));
 }
 
 sub tmpdir (;$) {
@@ -243,9 +249,9 @@ sub _prepare_redirects ($) {
 	for (my $fd = 0; $fd <= $#io_mode; $fd++) {
 		my $fh = $fhref->[$fd] or next;
 		my ($oldfh, $mode) = @{$io_mode[$fd]};
-		open my $orig, $mode, $oldfh or die "$oldfh $mode stash: $!";
+		open(my $orig, $mode, $oldfh);
 		$orig_io->[$fd] = $orig;
-		open $oldfh, $mode, $fh or die "$oldfh $mode redirect: $!";
+		open $oldfh, $mode, $fh;
 	}
 	$orig_io;
 }
@@ -255,7 +261,7 @@ sub _undo_redirects ($) {
 	for (my $fd = 0; $fd <= $#io_mode; $fd++) {
 		my $fh = $orig_io->[$fd] or next;
 		my ($oldfh, $mode) = @{$io_mode[$fd]};
-		open $oldfh, $mode, $fh or die "$$oldfh $mode redirect: $!";
+		open $oldfh, $mode, $fh;
 	}
 }
 
@@ -281,8 +287,8 @@ sub key2sub ($) {
 	my ($key) = @_;
 	$cached_scripts{$key} //= do {
 		my $f = key2script($key);
-		open my $fh, '<', $f or die "open $f: $!";
-		my $str = do { local $/; <$fh> };
+		open my $fh, '<', $f;
+		my $str = read_all($fh);
 		my $pkg = (split(m!/!, $f))[-1];
 		$pkg =~ s/([a-z])([a-z0-9]+)(\.t)?\z/\U$1\E$2/;
 		$pkg .= "_T" if $3;
@@ -329,7 +335,7 @@ sub _run_sub ($$$) {
 sub no_coredump (@) {
 	my @dirs = @_;
 	my $cwdfh;
-	if (@dirs) { opendir($cwdfh, '.') or die "opendir(.): $!" }
+	opendir($cwdfh, '.') if @dirs;
 	my @found;
 	for (@dirs, '.') {
 		chdir $_;
@@ -375,7 +381,7 @@ sub run_script ($;$$) {
 			next if $fd > 0;
 			$fh->autoflush(1);
 			print $fh $$redir or die "print: $!";
-			seek($fh, 0, SEEK_SET) or die "seek: $!";
+			seek($fh, 0, SEEK_SET);
 		} elsif ($ref eq 'GLOB') {
 			$spawn_opt->{$fd} = $fhref->[$fd] = $redir;
 		} elsif ($ref) {
@@ -406,15 +412,13 @@ sub run_script ($;$$) {
 		my $orig_io = _prepare_redirects($fhref);
 		my $cwdfh = $lei_cwdfh;
 		if (my $d = $opt->{'-C'}) {
-			unless ($cwdfh) {
-				opendir $cwdfh, '.' or die "opendir .: $!";
-			}
-			chdir $d or die "chdir $d: $!";
+			$cwdfh or opendir $cwdfh, '.';
+			chdir $d;
 		}
 		_run_sub($sub, $key, \@argv);
 		# n.b. all our uses of PublicInbox::DS should be fine
 		# with this and we can't Reset here.
-		die "fchdir(restore): $!" if $cwdfh && !chdir($cwdfh);
+		chdir($cwdfh) if $cwdfh;
 		_undo_redirects($orig_io);
 		select STDOUT;
 		umask($umask);
@@ -425,10 +429,8 @@ sub run_script ($;$$) {
 	for my $fd (1..2) {
 		my $fh = $fhref->[$fd] or next;
 		next unless -f $fh;
-		seek($fh, 0, SEEK_SET) or die "seek: $!";
-		my $redir = $opt->{$fd};
-		local $/;
-		$$redir = <$fh>;
+		seek($fh, 0, SEEK_SET);
+		${$opt->{$fd}} = read_all($fh);
 	}
 	no_coredump($opt->{-C} ? ($opt->{-C}) : ());
 	$? == 0;
@@ -458,7 +460,7 @@ sub wait_for_tail {
 		$ino[0] =~ s!/fd/!/fdinfo/!;
 		my @info;
 		do {
-			if (open my $fh, '<', $ino[0]) {
+			if (CORE::open(my $fh, '<', $ino[0])) {
 				local $/ = "\n";
 				@info = grep(/^inotify wd:/, <$fh>);
 			}
@@ -500,7 +502,6 @@ sub tail_f (@) {
 	$tail_cmd or return; # "tail -F" or "tail -f"
 	my $opt = (ref($f[-1]) eq 'HASH') ? pop(@f) : {};
 	my $clofork = $opt->{-CLOFORK} // [];
-	use autodie qw(fcntl open);
 	my @cfmap = map {
 		my $fl = fcntl($_, F_GETFD, 0);
 		fcntl($_, F_SETFD, $fl | FD_CLOEXEC) unless $fl & FD_CLOEXEC;
@@ -551,9 +552,7 @@ sub start_script {
 					\&PublicInbox::DS::sig_setmask, $oset);
 	my $pid = PublicInbox::DS::do_fork();
 	if ($pid == 0) {
-		for (@{delete($opt->{-CLOFORK}) // []}) {
-			close($_) or die "close $!";
-		}
+		close($_) for (@{delete($opt->{-CLOFORK}) // []});
 		# pretend to be systemd (cf. sd_listen_fds(3))
 		# 3 == SD_LISTEN_FDS_START
 		my $fd;
@@ -561,7 +560,7 @@ sub start_script {
 			my $io = $opt->{$fd} // next;
 			my $old = fileno($io);
 			if ($old == $fd) {
-				fcntl($io, F_SETFD, 0) // die "F_SETFD: $!";
+				fcntl($io, F_SETFD, 0);
 			} else {
 				dup2($old, $fd) // die "dup2($old, $fd): $!";
 			}
@@ -572,7 +571,7 @@ sub start_script {
 			$ENV{LISTEN_PID} = $$;
 			$ENV{LISTEN_FDS} = $fds;
 		}
-		if ($opt->{-C}) { chdir($opt->{-C}) or die "chdir: $!" }
+		if ($opt->{-C}) { chdir($opt->{-C}) }
 		$0 = join(' ', @$cmd);
 		local @SIG{keys %SIG} = map { undef } values %SIG;
 		local $SIG{FPE} = 'IGNORE'; # Perl default
@@ -657,7 +656,6 @@ sub need_scm_rights () {
 
 # returns a pipe with FD_CLOEXEC disabled on the write-end
 sub quit_waiter_pipe () {
-	use autodie qw(fcntl pipe);
 	pipe(my $r, my $w);
 	fcntl($w, F_SETFD, fcntl($w, F_GETFD, 0) & ~FD_CLOEXEC);
 	($r, $w);
@@ -675,7 +673,7 @@ SKIP: {
 	my ($cb) = pop @_;
 	my $test_opt = shift // {};
 	local $lei_cwdfh;
-	use autodie qw(mkdir open opendir);
+	use autodie qw(mkdir);
 	opendir $lei_cwdfh, '.';
 	require_git(2.6, 1);
 	my $mods = $test_opt->{mods} // [ 'lei' ];
@@ -766,7 +764,7 @@ sub setup_public_inboxes () {
 				'--newsgroup', "t.v$V", "t$V",
 				"$test_home/t$V", "http://example.com/t$V",
 				"t$V\@example.com" ]) or xbail "init v$V";
-		unlink "$test_home/t$V/description" or xbail "unlink $!";
+		unlink "$test_home/t$V/description";
 	}
 	require PublicInbox::Config;
 	require PublicInbox::InboxWritable;
@@ -786,7 +784,7 @@ sub setup_public_inboxes () {
 		$im->done;
 	});
 	$seen or BAIL_OUT 'no imports';
-	open my $fh, '>', $stamp or BAIL_OUT "open $stamp: $!";
+	open my $fh, '>', $stamp;
 	@ret;
 }
 
@@ -815,13 +813,12 @@ sub create_coderepo ($$;@) {
 	my $scope = $lk->lock_for_scope;
 	my $tmpdir = delete $opt{tmpdir};
 	if (!-f "$dir/creat.stamp") {
-		opendir(my $dfh, '.') or xbail "opendir .: $!";
-		chdir($dir) or xbail "chdir($dir): $!";
+		opendir(my $dfh, '.');
+		chdir($dir);
 		local %ENV = (%ENV, %COMMIT_ENV);
 		$cb->($dir);
-		chdir($dfh) or xbail "cd -: $!";
-		open my $s, '>', "$dir/creat.stamp" or
-			BAIL_OUT "error creating $dir/creat.stamp: $!";
+		chdir($dfh);
+		open my $s, '>', "$dir/creat.stamp";
 	}
 	return $dir if !defined($tmpdir);
 	xsys_e([qw(/bin/cp -Rp), $dir, $tmpdir]);
@@ -868,8 +865,7 @@ sub create_inbox ($$;@) {
 				xsys_e([ qw(git gc -q) ], { GIT_DIR => $dir });
 			}
 		}
-		open my $s, '>', "$dir/creat.stamp" or
-			BAIL_OUT "error creating $dir/creat.stamp: $!";
+		open my $s, '>', "$dir/creat.stamp";
 	}
 	if ($tmpdir) {
 		undef $ibx;
@@ -904,8 +900,8 @@ sub test_httpd ($$;$$) {
 							ua => $ua);
 		$cb->() if $cb;
 		$td->join('TERM');
-		open my $fh, '<', $err or BAIL_OUT $!;
-		my $e = do { local $/; <$fh> };
+		open my $fh, '<', $err;
+		my $e = read_all($fh);
 		if ($e =~ s/^Plack::Middleware::ReverseProxy missing,\n//gms) {
 			$e =~ s/^URL generation for redirects .*\n//gms;
 		}
@@ -934,7 +930,6 @@ sub no_pollerfd ($) {
 
 sub cfg_new ($;@) {
 	my ($tmpdir, @body) = @_;
-	use autodie;
 	require PublicInbox::Config;
 	my $f = "$tmpdir/tmp_cfg";
 	open my $fh, '>', $f;

  parent reply	other threads:[~2023-10-17 23:38 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-17 23:37 [PATCH 00/30] autodie-ification and code simplifications Eric Wong
2023-10-17 23:37 ` [PATCH 01/30] lei_mirror: start converting to autodie Eric Wong
2023-10-17 23:37 ` [PATCH 02/30] lei_mirror: autodie most `close' calls Eric Wong
2023-10-17 23:37 ` [PATCH 03/30] lei_mirror: use autodie for most `open' calls Eric Wong
2023-10-17 23:37 ` [PATCH 04/30] git: introduce read_all function Eric Wong
2023-10-17 23:37 ` [PATCH 05/30] import: use read_all to detect short reads Eric Wong
2023-10-17 23:37 ` [PATCH 06/30] lei_mirror: use read_all Eric Wong
2023-10-17 23:37 ` [PATCH 07/30] use read_all in more places to improve safety Eric Wong
2023-10-17 23:37 ` [PATCH 08/30] xap_helper*: use autodie in more places Eric Wong
2023-10-17 23:37 ` [PATCH 09/30] xap_helper: die more easily in both implementations Eric Wong
2023-10-17 23:37 ` [PATCH 10/30] xap_helper: simplify SIGTERM exit checks Eric Wong
2023-10-17 23:37 ` [PATCH 11/30] xap_helper: autodie for getsockopt Eric Wong
2023-10-17 23:37 ` [PATCH 12/30] xap_client: autodie for pipe and socketpair Eric Wong
2023-10-17 23:37 ` [PATCH 13/30] xt/git-http-backend: remove Net::HTTP usage Eric Wong
2023-10-17 23:37 ` [PATCH 14/30] ds: introduce and use do_fork helper Eric Wong
2023-10-17 23:38 ` [PATCH 15/30] ds: get rid of SetLoopTimeout Eric Wong
2023-10-17 23:38 ` [PATCH 16/30] cindex: drop some unused functions Eric Wong
2023-10-17 23:38 ` [PATCH 17/30] syscall: common $F_SETPIPE_SZ definition Eric Wong
2023-10-17 23:38 ` [PATCH 18/30] t/lei-up: additional diagnostics for match failures Eric Wong
2023-10-17 23:38 ` Eric Wong [this message]
2023-10-17 23:38 ` [PATCH 20/30] test_common: only hide TCP port in messages Eric Wong
2023-10-17 23:38 ` [PATCH 21/30] test_common: use $cwdfh for every run_script command Eric Wong
2023-10-17 23:38 ` [PATCH 22/30] init: drop extraneous `+' Eric Wong
2023-10-17 23:38 ` [PATCH 23/30] init: use autodie to reduce distractions Eric Wong
2023-10-17 23:38 ` [PATCH 24/30] xt/mem-imapd-tls: remove unused/broken epoll imports Eric Wong
2023-10-17 23:38 ` [PATCH 25/30] xt/mem-imapd-tls: reduce FDs for lsof use Eric Wong
2023-10-17 23:38 ` [PATCH 26/30] lei: use autodie where appropriate Eric Wong
2023-10-17 23:38 ` [PATCH 27/30] lei_auth: update comments and use v5.12 Eric Wong
2023-10-17 23:38 ` [PATCH 28/30] lei_config: drop redundant open check Eric Wong
2023-10-17 23:38 ` [PATCH 29/30] convert: use read_all to simplify error checks Eric Wong
2023-10-17 23:38 ` [PATCH 30/30] idx_stack: use autodie + read_all Eric Wong
2023-10-19  1:14 ` [PATCH 31/30] lei: simplify startq/au_done wakeup notifications Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231017233815.1637932-20-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).