App-Dapper

 view release on metacpan or  search on metacpan

t/54-init-templates.t  view on Meta::CPAN

use 5.010;
use strict;
use warnings;
use utf8;
use Test::More;
binmode(Test::More->builder->$_, q(:encoding(UTF-8))) for qw(output failure_output todo_output);

# App::Dapper::Init and the theme modules: the list of templates, their
# files and notes, how init maps them onto a project, and templates from a
# directory or a git repository.

use lib 't/lib';
use DapperTest;
use App::Dapper;
use App::Dapper::Init;
use App::Dapper::Theme::Portfolio;
use App::Dapper::Theme::Business;
use App::Dapper::Theme::Docs;
use File::Spec;
use File::Temp qw(tempdir);

my $has_git = grep { -x "$_/git" } File::Spec->path();

# Run code with standard output captured; return what it printed
sub capture_stdout {
    my ($code) = @_;
    my $out = '';
    {
        local *STDOUT;
        open(STDOUT, '>', \$out) or die $!;
        $code->();
    }
    return $out;
}

# Run code with standard error thrown away (git warns about shallow local clones)
sub without_stderr {
    my ($code) = @_;
    open(my $stderr, '>&', \*STDERR) or die $!;
    open(STDERR, '>', File::Spec->devnull) or die $!;
    my @result = eval { $code->() };
    my $error = $@;
    open(STDERR, '>&', $stderr) or die $!;
    die $error if $error;
    return wantarray ? @result : $result[0];
}

sub init_quietly {
    my (@args) = @_;
    local $App::Dapper::Utils::QUIET = 1;
    return App::Dapper::Init::init(@args);
}

my %THEME_FILES = (
    portfolio => [qw(_config.yml _data/profile.yml _layout/index.html _layout/project.html _source/about.md _source/contact.html
                     _source/index.html _source/projects/_schema.yml _source/projects/night-market.md _source/projects/riverside-books.md
                     css/site.css images/project-1.svg images/project-2.svg)],
    business  => [qw(_config.yml _data/business.yml _layout/index.html _layout/post.html _source/about.md _source/contact.html
                     _source/index.html _source/news.html _source/news/_schema.yml _source/news/now-open-sundays.md css/site.css
                     images/storefront.svg)],
    docs      => [qw(_config.yml _data/nav.yml _layout/index.html _source/_schema.yml _source/getting-started.md
                     _source/guide/configuration.md _source/guide/writing-pages.md _source/index.md css/site.css)],
    blog      => [qw(_config.yml _data/menu.yml _layout/index.html _layout/list.html _layout/post.html _source/about.md
                     _source/blog/_schema.yml _source/blog/welcome.md _source/blog/writing-posts.md _source/index.html css/site.css)],
    default   => [qw(_config.yml _layout/index.html _source/index.md)],
);

subtest 'templates and descriptions' => sub {
    is_deeply([ App::Dapper::Init::templates() ], [qw(blog business default docs portfolio)], 'the built-in templates, sorted');
    is(App::Dapper::Init::description('default'), 'The smallest project: a project file, one layout and one page', 'default');
    is(App::Dapper::Init::description('blog'), 'A blog: paginated home page, posts, category and tag pages, feed, a schema for posts', 'blog');
    is(App::Dapper::Init::description('portfolio'), 'A portfolio: a grid of projects with images, and about and contact pages', 'portfolio');
    is(App::Dapper::Init::description('business'), 'A small business: services, opening hours, contact details and news, from one data file', 'business');
    is(App::Dapper::Init::description('docs'), 'Documentation: a sidebar from a data file, previous and next links, code highlighting and search', 'docs');
    is(App::Dapper::Init::description('nope'), undef, 'an unknown template has no description');

    is(App::Dapper::Init::description('portfolio'), App::Dapper::Theme::Portfolio->description(), 'portfolio comes from its theme');
    is(App::Dapper::Init::description('business'), App::Dapper::Theme::Business->description(), 'business comes from its theme');
    is(App::Dapper::Init::description('docs'), App::Dapper::Theme::Docs->description(), 'docs comes from its theme');
    for my $name (App::Dapper::Init::templates()) {
        my $description = App::Dapper::Init::description($name);
        ok(length $description < 100, "$name: the description fits on a line");
        unlike($description, qr/\.$/, "$name: without a full stop");
    }
};

subtest 'template files and notes' => sub {
    for my $name (sort keys %THEME_FILES) {
        my ($files, $notes) = App::Dapper::Init::template_files($name);
        is(ref $files, 'HASH', "$name: a hash of files");
        is_deeply([ sort keys %$files ], $THEME_FILES{$name}, "$name: exactly these files");
        ok(!grep({ ref $_ } values %$files), "$name: contents are text");
        like($files->{'_config.yml'}, qr/^#deploy:$/m, "$name: the project file shows how to deploy");
        like($files->{'_config.yml'}, qr/^#  command: rsync -av --delete _output\/ me\@example\.com:\/var\/www\/site\/$/m, "$name: including a command");
        if ($name eq 'default') {
            is($notes, undef, 'default: no notes');
        }
        else {
            like($notes, qr/\A## About (?:this|these) /, "$name: notes for AGENTS.md");
            like($files->{'_config.yml'}, qr/^url: https:\/\/example\.com$/m, "$name: a url");
        }
    }

t/54-init-templates.t  view on Meta::CPAN

    like($out, qr/^Creating lay\/index\.html$/m, 'reports the files it copies');
    unlike($out, qr/Creating \.gitignore\n.*Creating \.gitignore/s, 'once each');
    ok(-f 'site.yml' && -f 'src/index.md' && -f 'lay/index.html' && -f 'src/deep/er/page.md', 'mapped onto the directories given');
    is(slurp('src/index.md'), "---\ntitle: Home\n---\nHello, café.\n", 'text copied');
    is(slurp('bin.dat', ':raw'), join('', map { chr } 0 .. 255), 'binary copied byte for byte');
    is(slurp('.gitignore'), "public/\n", "the theme's .gitignore is used rather than Dapper's");
    ok(!-e '.git' && !-e '_output' && !-e '.dapper' && !-e 'node_modules' && !-e '.DS_Store' && !-e 'images/.DS_Store', 'skipped files are not copied');

    make_site({ 'README.md' => "mine\n" }, bare => 1);
    eval { init_quietly('_source', '_output', '_layout', '_config.yml', $theme) };
    is($@, "error: refusing to initialize, these files already exist: README.md\n", "a theme's files are not written over either");

    my $empty = tempdir(CLEANUP => 1);
    eval { App::Dapper::Init::template_files($empty) };
    is($@, "error: the template $empty has no files\n", 'an empty directory');
    my $only_skipped = tempdir(CLEANUP => 1);
    spew("$only_skipped/.git/HEAD", "x\n");
    spew("$only_skipped/.DS_Store", "x");
    eval { App::Dapper::Init::template_files($only_skipped) };
    is($@, "error: the template $only_skipped has no files\n", 'a directory with only skipped files');

    eval { App::Dapper::Init::template_files("$empty/missing") };
    like($@, qr/^error: there is no template called "\Q$empty\E\/missing"/, 'a missing directory');
};

subtest 'App::Dapper init with a directory template keeps its AGENTS.md and CLAUDE.md' => sub {
    my $theme = tempdir(CLEANUP => 1);
    spew("$theme/_config.yml", "---\nname: Noted\n");
    spew("$theme/_layout/index.html", "[% page.content %]");
    spew("$theme/_source/index.md", "---\ntitle: Home\n---\nx\n");
    spew("$theme/AGENTS.md", "# Theme notes\n");
    spew("$theme/CLAUDE.md", "Theme Claude notes\n");
    make_site({}, bare => 1);
    my $d = App::Dapper->new();
    $d->{quiet} = 1;
    $d->{template} = $theme;
    capture_stdout(sub { $d->init() });
    like(slurp('AGENTS.md'), qr/\A# Theme notes\n\n<!-- dapper:begin .*<!-- dapper:end -->\n\z/s, "the theme's AGENTS.md comes first, then the guide");
    like(slurp('AGENTS.md'), qr/# Working on Noted/, 'the guide is about the new site');
    is(slurp('CLAUDE.md'), "Theme Claude notes\n", "the theme's CLAUDE.md is kept");
};

subtest 'a template from git' => sub {
    plan skip_all => 'git is not installed' unless $has_git;
    local $ENV{GIT_AUTHOR_NAME} = 'Test';
    local $ENV{GIT_AUTHOR_EMAIL} = 'test@example.com';
    local $ENV{GIT_COMMITTER_NAME} = 'Test';
    local $ENV{GIT_COMMITTER_EMAIL} = 'test@example.com';

    my $base = tempdir(CLEANUP => 1);
    my $work = "$base/work";
    system('git', 'init', '-q', $work) == 0 or plan skip_all => 'git init failed';
    spew("$work/_config.yml", "---\nname: From git\n");
    spew("$work/_layout/index.html", "[% page.content %]");
    spew("$work/_source/index.md", "---\ntitle: Home\n---\nHello from git.\n");
    spew("$work/images/a.bin", "\x00\x01\x02\xff", ':raw');
    system("cd '$work' && git add -A && git commit -q -m theme") == 0 or plan skip_all => 'git commit failed';
    my $bare = "$base/theme.git";
    system('git', 'clone', '-q', '--bare', $work, $bare) == 0 or plan skip_all => 'git clone --bare failed';

    my ($files, $notes) = without_stderr(sub { App::Dapper::Init::template_files($bare) });
    is_deeply([ sort keys %$files ], [qw(_config.yml _layout/index.html _source/index.md images/a.bin)], 'a bare repository ending in .git is cloned; .git is left out');
    is($files->{'images/a.bin'}->{raw}, "\x00\x01\x02\xff", 'bytes kept');
    is($notes, undef, 'no notes');

    ($files) = without_stderr(sub { App::Dapper::Init::template_files("file://$bare") });
    ok($files->{'_source/index.md'}, 'a file:// URL ending in .git');

    make_site({}, bare => 1);
    my $d = App::Dapper->new();
    $d->{quiet} = 1;
    $d->{template} = $bare;
    without_stderr(sub { capture_stdout(sub { $d->init() }) });
    is(slurp('_source/index.md'), "---\ntitle: Home\n---\nHello from git.\n", 'init copies the clone');
    ok(!-e '.git', 'without .git');
    ok(-f 'AGENTS.md', 'and adds AGENTS.md');
    like(slurp('AGENTS.md'), qr/# Working on From git/, 'about the site');

    my $error;
    {
        open(my $stderr, '>&', \*STDERR);
        open(STDERR, '>', File::Spec->devnull);
        eval { App::Dapper::Init::template_files("$base/missing.git") };
        $error = $@;
        open(STDERR, '>&', $stderr);
    }
    is($error, "error: could not clone the template from $base/missing.git (is git installed, and is the URL right?)\n", 'a repository that is not there');

    my ($plain) = eval { without_stderr(sub { App::Dapper::Init::template_files("file://$work") }) };
    ok($plain && $plain->{'_source/index.md'}, 'a file:// URL without .git is cloned');
};

subtest 'a git URL without git' => sub {
    local $ENV{PATH} = tempdir(CLEANUP => 1);
    my $error;
    {
        open(my $stderr, '>&', \*STDERR);
        open(STDERR, '>', File::Spec->devnull);
        eval { App::Dapper::Init::template_files('/nowhere/theme.git') };
        $error = $@;
        open(STDERR, '>&', $stderr);
    }
    is($error, "error: could not clone the template from /nowhere/theme.git (is git installed, and is the URL right?)\n", 'asks whether git is installed');
};

done_testing();



( run in 0.647 second using v1.01-cache-2.11-cpan-60884a8df76 )