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 6/6] t/{config,solver_git}: simplify error handling
Date: Mon, 30 Sep 2024 21:30:08 +0000	[thread overview]
Message-ID: <20240930213008.4014512-7-e@80x24.org> (raw)
In-Reply-To: <20240930213008.4014512-1-e@80x24.org>

autodie and the newish PublicInbox::IO::write_file make error
handling more consistent and less noisy to people reading the
code.  We'll also avoid testing `git config' set behavior
of git(1) and instead bail out via `xsys_e()' if it fails
unexpectedly due to hardware problems or bugs in git.
---
 t/config.t     | 13 ++++++-------
 t/solver_git.t | 34 +++++++++++++++-------------------
 2 files changed, 21 insertions(+), 26 deletions(-)

diff --git a/t/config.t b/t/config.t
index 956ad35c..85005df9 100644
--- a/t/config.t
+++ b/t/config.t
@@ -3,6 +3,7 @@
 use v5.12;
 use PublicInbox::TestCommon;
 use PublicInbox::Import;
+use PublicInbox::IO qw(write_file);
 use_ok 'PublicInbox';
 ok(defined(eval('$PublicInbox::VERSION')), 'VERSION defined');
 use_ok 'PublicInbox::Config';
@@ -12,13 +13,11 @@ my $validate_git_behavior = $ENV{TEST_VALIDATE_GIT_BEHAVIOR};
 
 {
 	my $f = "$tmpdir/bool_config";
-	open my $fh, '>', $f;
-	print $fh <<EOM;
+	write_file '>', $f, <<EOM;
 [imap]
 	debug
 	port = 2
 EOM
-	close $fh;
 	my $cfg = PublicInbox::Config->git_config_dump($f);
 	$validate_git_behavior and
 		is(xqx([qw(git config -f), $f, qw(--bool imap.debug)]),
@@ -32,7 +31,7 @@ EOM
 	my $inboxdir = "$tmpdir/new\nline";
 	my @cmd = ('git', "--git-dir=$tmpdir",
 		qw(config publicinbox.foo.inboxdir), $inboxdir);
-	is(xsys(@cmd), 0, "set config");
+	xsys_e(@cmd);
 
 	my $tmp = PublicInbox::Config->new("$tmpdir/config");
 
@@ -212,17 +211,17 @@ for my $s (@valid) {
 
 {
 	my $f = "$tmpdir/ordered";
-	open my $fh, '>', $f or die "open: $!";
+	open my $fh, '>', $f;
 	my @expect;
 	foreach my $i (0..3) {
 		push @expect, "$i";
-		print $fh <<"" or die "print: $!";
+		print $fh <<"";
 [publicinbox "$i"]
 	inboxdir = /path/to/$i.git
 	address = $i\@example.com
 
 	}
-	close $fh or die "close: $!";
+	close $fh;
 	my $cfg = PublicInbox::Config->new($f);
 	my @result;
 	$cfg->each_inbox(sub { push @result, $_[0]->{name} });
diff --git a/t/solver_git.t b/t/solver_git.t
index 6cc5eeba..cebcf47f 100644
--- a/t/solver_git.t
+++ b/t/solver_git.t
@@ -9,7 +9,7 @@ use PublicInbox::ContentHash qw(git_sha);
 use PublicInbox::Spawn qw(run_qx which);
 use File::Path qw(remove_tree);
 use PublicInbox::IO qw(write_file);
-use autodie qw(symlink unlink);
+use autodie qw(close mkdir open rename symlink unlink);
 require_mods qw(DBD::SQLite Xapian);
 require PublicInbox::SolverGit;
 my $rdr = { 2 => \(my $null) };
@@ -35,8 +35,7 @@ my $ibx = create_inbox 'v2', version => 2,
 };
 my $md = "$tmpdir/md";
 File::Path::make_path(map { $md.$_ } (qw(/cur /new /tmp)));
-symlink(abs_path('t/solve/0001-simple-mod.patch'), "$md/cur/foo:2,") or
-	xbail "symlink: $!";
+symlink abs_path('t/solve/0001-simple-mod.patch'), "$md/cur/foo:2,";
 
 my $v1_0_0_rev = '8a918a8523bc9904123460f85999d75f6d604916';
 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
@@ -45,7 +44,7 @@ my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
 my $non_existent = 'ee5e32211bf62ab6531bdf39b84b6920d0b6775a';
 my $stderr_empty = sub {
 	my ($msg) = @_;
-	open my $efh, '<', "$tmpdir/stderr.log" or xbail $!;
+	open my $efh, '<', "$tmpdir/stderr.log";
 	my @l = <$efh>;
 	@l = grep(!/reverse ?proxy/i, @l);
 	is_xdeeply(\@l, [], $msg // 'stderr.log is empty');
@@ -158,7 +157,7 @@ my $git = PublicInbox::Git->new($git_dir);
 $ibx->{-repo_objs} = [ $git ];
 my $res;
 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
-open my $log, '+>>', "$tmpdir/solve.log" or die "open: $!";
+open my $log, '+>>', "$tmpdir/solve.log";
 my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
 		'HTTP_HOST' => 'example.com' };
 $solver->solve($psgi_env, $log, '69df7d5', {});
@@ -230,7 +229,7 @@ SKIP: {
 	my $lk = PublicInbox::Lock->new($l);
 	my $acq = $lk->lock_for_scope;
 	my $stamp = "$binfoo/stamp-";
-	if (open my $fh, '<', $stamp) {
+	if (CORE::open my $fh, '<', $stamp) {
 		%oid = map { chomp; split(/=/, $_) } (<$fh>);
 	} else {
 		PublicInbox::Import::init_bare($binfoo);
@@ -243,7 +242,7 @@ SKIP: {
 			$oid{$label} = $x;
 		}
 
-		open my $null, '<', '/dev/null' or xbail "open /dev/null: $!";
+		open my $null, '<', '/dev/null';
 		my $t = xqx([qw(git mktree)], $env, { 0 => $null });
 		xbail "mktree: $?" if $?;
 		chomp($t);
@@ -264,20 +263,19 @@ SKIP: {
 		chomp($c);
 		$oid{'8859-parent'} = $c;
 
-		open my $fh, '>', "$stamp.$$" or BAIL_OUT;
+		open my $fh, '>', "$stamp.$$";
 		while (my ($k, $v) = each %oid) {
-			print $fh "$k=$v\n" or xbail "print: $!";
+			print $fh "$k=$v\n";
 		}
-		close $fh or BAIL_OUT;
-		rename("$stamp.$$", $stamp) or BAIL_OUT;
+		close $fh;
+		rename "$stamp.$$", $stamp;
 	}
 	undef $acq;
 	# ensure the PSGI frontend (ViewVCS) works:
 	my $name = $ibx->{name};
 	my $cfgpfx = "publicinbox.$name";
 	my $cfgpath = "$tmpdir/httpd-config";
-	open my $cfgfh, '>', $cfgpath or die;
-	print $cfgfh <<EOF or die;
+	write_file '>', $cfgpath, <<EOF;
 [coderepo]
 	snapshots = tar.gz
 [publicinbox "$name"]
@@ -295,7 +293,6 @@ SKIP: {
 [coderepo "goner"]
 	dir = $gone_repo
 EOF
-	close $cfgfh or die;
 	my $exp_digest;
 	{
 		my $exp = xqx([qw(git archive --format=tar.gz
@@ -354,13 +351,12 @@ EOF
 		if (defined $ENV{PLACK_TEST_EXTERNALSERVER_URI}) {
 			$stderr_empty->('nothing in stderr.log, yet');
 		} else {
-			open $olderr, '>&', \*STDERR or xbail "open: $!";
-			open STDERR, '+>>', "$tmpdir/stderr.log" or
-				xbail "open: $!";
+			open $olderr, '>&', \*STDERR;
+			open STDERR, '+>>', "$tmpdir/stderr.log";
 		}
 		$res = $cb->(GET('/binfoo/'));
 		defined($ENV{PLACK_TEST_EXTERNALSERVER_URI}) or
-			open STDERR, '>&', $olderr or xbail "open: $!";
+			open STDERR, '>&', $olderr;
 		is($res->code, 200, 'coderepo summary (binfoo)');
 		$stderr_empty->();
 
@@ -486,7 +482,7 @@ EOF
 	test_httpd($env, $client, 7, sub {
 	SKIP: {
 		require_cmd('curl', 1) or skip 'no curl', 1;
-		mkdir "$tmpdir/ext" // xbail "mkdir $!";
+		mkdir "$tmpdir/ext";
 		my $rurl = "$ENV{PLACK_TEST_EXTERNALSERVER_URI}/$name";
 		test_lei({tmpdir => "$tmpdir/ext"}, sub {
 			lei_ok(qw(blob --no-mail 69df7d5 -I), $rurl);

      parent reply	other threads:[~2024-09-30 21:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-30 21:30 [PATCH 0/6] test updates + coderepo removal workaround Eric Wong
2024-09-30 21:30 ` [PATCH 1/6] t/www_static: modernize test Eric Wong
2024-09-30 21:30 ` [PATCH 2/6] t/www_static: test with our -httpd server, too Eric Wong
2024-09-30 21:30 ` [PATCH 3/6] t/www_static: ensure If-Modified-Since handling works Eric Wong
2024-09-30 21:30 ` [PATCH 4/6] www: improve handling of missing coderepos Eric Wong
2024-09-30 21:30 ` [PATCH 5/6] xt/solver: use `psgi' shortcut for require_mods() Eric Wong
2024-09-30 21:30 ` Eric Wong [this message]

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=20240930213008.4014512-7-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).