From: Eric Wong <e@yhbt.net>
To: meta@public-inbox.org
Subject: [PATCH 3/8] inbox: don't memoize missing description|cloneurl
Date: Sat, 18 Apr 2020 03:38:48 +0000 [thread overview]
Message-ID: <20200418033853.9798-4-e@yhbt.net> (raw)
In-Reply-To: <20200418033853.9798-1-e@yhbt.net>
It's probably common to have inboxes initially setup without
these files properly configured, so don't memoize at that stage.
---
lib/PublicInbox/Inbox.pm | 13 ++++++++-----
t/inbox.t | 19 +++++++++++++++++++
2 files changed, 27 insertions(+), 5 deletions(-)
diff --git a/lib/PublicInbox/Inbox.pm b/lib/PublicInbox/Inbox.pm
index 95ffd039..e49f85fc 100644
--- a/lib/PublicInbox/Inbox.pm
+++ b/lib/PublicInbox/Inbox.pm
@@ -219,19 +219,22 @@ sub try_cat {
sub description {
my ($self) = @_;
- $self->{description} //= do {
+ ($self->{description} //= do {
my $desc = try_cat("$self->{inboxdir}/description");
local $/ = "\n";
chomp $desc;
$desc =~ s/\s+/ /smg;
- $desc eq '' ? '($INBOX_DIR/description missing)' : $desc;
- };
+ $desc eq '' ? undef : $desc;
+ }) // '($INBOX_DIR/description missing)';
}
sub cloneurl {
my ($self) = @_;
- $self->{cloneurl} //=
- [ split(/\s+/s, try_cat("$self->{inboxdir}/cloneurl")) ];
+ ($self->{cloneurl} //= do {
+ my $s = try_cat("$self->{inboxdir}/cloneurl");
+ my @urls = split(/\s+/s, $s);
+ scalar(@urls) ? \@urls : undef
+ }) // [];
}
sub base_url {
diff --git a/t/inbox.t b/t/inbox.t
index 5f86440d..b59d5dba 100644
--- a/t/inbox.t
+++ b/t/inbox.t
@@ -4,12 +4,31 @@ use strict;
use warnings;
use Test::More;
use_ok 'PublicInbox::Inbox';
+use File::Temp 0.19 ();
my $x = PublicInbox::Inbox->new({url => [ '//example.com/test/' ]});
is($x->base_url, 'https://example.com/test/', 'expanded protocol-relative');
$x = PublicInbox::Inbox->new({url => [ 'http://example.com/test' ]});
is($x->base_url, 'http://example.com/test/', 'added trailing slash');
$x = PublicInbox::Inbox->new({});
+
is($x->base_url, undef, 'undef base_url allowed');
+my $tmpdir = File::Temp->newdir('pi-inbox-XXXXXX', TMPDIR => 1);
+$x->{inboxdir} = $tmpdir->dirname;
+is_deeply($x->cloneurl, [], 'no cloneurls');
+is($x->description, '($INBOX_DIR/description missing)', 'default description');
+{
+ open my $fh, '>', "$x->{inboxdir}/cloneurl" or die;
+ print $fh "https://example.com/inbox\n" or die;
+ close $fh or die;
+ open $fh, '>', "$x->{inboxdir}/description" or die;
+ print $fh "blah\n" or die;
+ close $fh or die;
+}
+is_deeply($x->cloneurl, ['https://example.com/inbox'], 'cloneurls update');
+is($x->description, 'blah', 'description updated');
+is(unlink(glob("$x->{inboxdir}/*")), 2, 'unlinked cloneurl & description');
+is_deeply($x->cloneurl, ['https://example.com/inbox'], 'cloneurls memoized');
+is($x->description, 'blah', 'description memoized');
done_testing();
next prev parent reply other threads:[~2020-04-18 3:38 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-18 3:38 [PATCH 0/8] some small yak shaving things Eric Wong
2020-04-18 3:38 ` [PATCH 1/8] inboxwritable: mime_from_path: reuse in more places Eric Wong
2020-04-18 3:38 ` [PATCH 2/8] searchidx: die on cat-file failures Eric Wong
2020-04-18 3:38 ` Eric Wong [this message]
2020-04-18 3:38 ` [PATCH 4/8] inbox: replace `eval {}' with `do {}' where appropriate Eric Wong
2020-04-18 3:38 ` [PATCH 5/8] favor `do {}' over `eval {}' for localized slurp Eric Wong
2020-04-18 3:38 ` [PATCH 6/8] wwwatomstream: move {emit_header} field to $self Eric Wong
2020-04-18 3:38 ` [PATCH 7/8] mbox: use per-message line-ending for From_ line Eric Wong
2020-04-18 3:38 ` [PATCH 8/8] reduce scope of mbox From_ line removal 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: http://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=20200418033853.9798-4-e@yhbt.net \
--to=e@yhbt.net \
--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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/public-inbox.git
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).