user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH 3/8] inbox: don't memoize missing description|cloneurl
  @ 2020-04-18  3:38  7% ` Eric Wong
  0 siblings, 0 replies; 1+ results
From: Eric Wong @ 2020-04-18  3:38 UTC (permalink / raw)
  To: meta

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();

^ permalink raw reply related	[relevance 7%]

Results 1-1 of 1 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2020-04-18  3:38     [PATCH 0/8] some small yak shaving things Eric Wong
2020-04-18  3:38  7% ` [PATCH 3/8] inbox: don't memoize missing description|cloneurl Eric Wong

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