Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/Plerd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,17 @@ sub neighbor_basename {
sub publish_tag_indexes {
my $self = shift;

# tags.tt is the one non-critical template: if it's missing, publish the
# rest of the blog anyway rather than dying. Warn only if the blog actually
# uses tags and therefore loses content by skipping their pages.
unless ( -e $self->tags_template_file ) {
if ( $self->has_tags ) {
carp "This blog uses tags, but its template directory has no "
. "tags.tt file. Skipping publication of tag pages.\n";
}
return;
}

my $tag_map = $self->tags_map;

# Commentary: Ideally we'd just pass a sorted array of tag objects
Expand Down
76 changes: 76 additions & 0 deletions t/missing_tags_template.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use warnings;
use strict;
use Test::More;
use Test::Warn;
use Path::Class::Dir;
use Path::Class::File;
use URI;

use FindBin;
use lib "$FindBin::Bin/../lib";

use_ok( 'Plerd' );
use Plerd::Init;

# Regression test for GitHub issue #41: tags.tt is the one non-critical
# template. If it's missing, the blog should still publish. It should publish
# silently when the blog has no tags, and warn (rather than die) when the blog
# does use tags but can't render their pages.

sub fresh_blog {
my ( $name ) = @_;
my $blog_dir = Path::Class::Dir->new( "$FindBin::Bin/$name" );
$blog_dir->rmtree;
Plerd::Init::initialize( $blog_dir->stringify, 0 );

# Remove the tags template to simulate a blog that lacks it.
Path::Class::File->new( $blog_dir, 'templates', 'tags.tt' )->remove;

my $plerd = Plerd->new(
path => $blog_dir->stringify,
title => 'Test Blog',
author_name => 'Nobody',
author_email => 'nobody@example.com',
base_uri => URI->new( 'http://blog.example.com/' ),
);
return ( $blog_dir, $plerd );
}

# Case 1: a blog with no tags at all.
{
my ( $blog_dir, $plerd ) = fresh_blog( 'no_tags_blog' );
my $source = Path::Class::File->new( $blog_dir, 'source', '2018-01-01-hello.md' );
$source->spew( iomode => '>:encoding(utf8)', "title: Hello\n\nNo tags here.\n" );

warning_is { eval { $plerd->publish_all } } undef,
'Publishing a tag-free blog without tags.tt produces no warning.';
is( $@, '', 'Publishing a tag-free blog without tags.tt does not die.' );
ok(
-e Path::Class::File->new( $blog_dir, 'docroot', '2018-01-01-hello.html' ),
'The post still published.'
);

$blog_dir->rmtree;
}

# Case 2: a blog that does use tags.
{
my ( $blog_dir, $plerd ) = fresh_blog( 'tagged_blog' );
my $source = Path::Class::File->new( $blog_dir, 'source', '2018-01-02-tagged.md' );
$source->spew(
iomode => '>:encoding(utf8)',
"title: Tagged\ntags: foo, bar\n\nThis post has tags.\n"
);

warning_like { eval { $plerd->publish_all } } qr/tags\.tt/,
'Publishing a tagged blog without tags.tt warns about the missing template.';
is( $@, '', 'Publishing a tagged blog without tags.tt does not die.' );
ok(
-e Path::Class::File->new( $blog_dir, 'docroot', '2018-01-02-tagged.html' ),
'The post still published.'
);

$blog_dir->rmtree;
}

done_testing();