App-Dapper

 view release on metacpan or  search on metacpan

t/36-output-clean.t  view on Meta::CPAN

    ok(-d '_output/somedir', 'directories are never removed directly');
    is_deeply(manifest()->{'/some/other/output'}, ['other.html'], 'entries for other output directories are kept');
    is_deeply(manifest()->{$output}, [qw(a.html a.md llms-full.txt llms.txt)], 'and this one is replaced');

    App::Dapper::Utils::write_file('.dapper/manifest.json', '{not json');
    eval { quiet_build() };
    is($@, '', 'a broken manifest is not an error');
    is_deeply([keys %{ manifest() }], [$output], 'and is replaced');

    App::Dapper::Utils::write_file('.dapper/manifest.json', '["a.html"]');
    quiet_build();
    is(ref manifest(), 'HASH', 'a manifest that is not a hash is replaced');

    App::Dapper::Utils::write_file('.dapper/manifest.json', JSON->new->encode({ $output => 'not a list' }));
    quiet_build();
    ok(-f '_output/a.html', 'an entry that is not a list removes nothing');
    is(ref manifest()->{$output}, 'ARRAY', 'and is replaced');
};

subtest 'clean: false and collect mode' => sub {
    make_site({ '_config.yml' => "---\nname: x\nclean: false\n", '_source/a.md' => "---\ntitle: A\n---\nx\n" });
    quiet_build();
    ok(!-e '.dapper/manifest.json', 'no manifest with clean: false');
    rename '_source/a.md', '_source/b.md';
    quiet_build();
    ok(-f '_output/a.html' && -f '_output/b.html', 'nothing removed');

    make_site({ '_config.yml' => "---\nname: x\nclean: 'no'\n", '_source/a.md' => "---\ntitle: A\n---\nx\n" });
    quiet_build();
    ok(!-e '.dapper/manifest.json', 'clean: no');

    make_site({ '_source/a.md' => "---\ntitle: A\n---\nx\n" });
    quiet_build();
    rename '_source/a.md', '_source/b.md';
    my $d = App::Dapper->new();
    $d->{quiet} = 1;
    $d->{collect} = 1;
    $d->build();
    ok(-f '_output/a.html', 'nothing removed when collecting problems');
    is_deeply(manifest()->{abs_path('_output')}, [qw(a.html a.md llms-full.txt llms.txt)], 'and the manifest is untouched');
};

subtest 'live reload' => sub {
    make_site({
        '_source/a.md'      => "---\ntitle: A\n---\nx\n",
        '_source/upper.md'  => "---\nlayout: upper\n---\nx\n",
        '_source/twice.md'  => "---\nlayout: twice\n---\nx\n",
        '_source/nobody.md' => "---\nlayout: bare\n---\nx\n",
        '_source/old.md'    => "---\nlayout: bare\nextension: .htm\n---\nx\n",
        '_source/data.json' => "---\nlayout: bare\nextension: .json\n---\n{}\n",
        '_layout/index.html' => "<html><body>[% page.content %]</body></html>",
        '_layout/upper.html' => "<HTML><BODY>B</BODY></HTML>",
        '_layout/twice.html' => "<body>1</body><body>2</body>",
        '_layout/bare.html'  => "[% page.content %]",
    });
    my $d = App::Dapper->new();
    $d->{quiet} = 1;
    $d->{live_reload} = 1;
    $d->build();
    my $script = App::Dapper::live_reload_script();
    like($script, qr{^<script>\n.*fetch\('/\.dapper-reload\?' \+ Date\.now\(\).*location\.reload\(\).*setTimeout\(poll, 1000\).*</script>\n\z}s,
        'the script polls /.dapper-reload every second');
    is(slurp('_output/a.html'), "<html><body><p>x</p>\n${script}</body></html>", 'inserted right before </body>');
    is(slurp('_output/upper.html'), "<HTML><BODY>B${script}</BODY></HTML>", 'case-insensitive');
    is(slurp('_output/twice.html'), "<body>1${script}</body><body>2</body>", 'only once');
    is(slurp('_output/nobody.html'), "<p>x</p>\n${script}", 'appended without a </body>');
    is(slurp('_output/old.htm'), "<p>x</p>\n${script}", '.htm is HTML too');
    is(slurp('_output/data.json'), "{}\n", 'other files are left alone');
    like(slurp('_output/.dapper-reload'), qr/^\d+-\d+\n\z/, 'the marker');
    ok($d->{written}->{'.dapper-reload'}, 'is remembered as written');
    ok(!-e '_output/a.md' || slurp('_output/a.md') !~ /dapper-reload/, 'not in the Markdown copy');
};

subtest 'generated files and pages that write the same file' => sub {
    make_site({
        '_config.yml'         => "---\nname: x\nurl: https://example.com\nfeed: true\nredirects:\n  /old/: /a.html\n  /a.html: /b/\n",
        '_source/a.md'        => "---\ntitle: A\ndate: 2014-01-01\n---\nx",
        '_source/sitemap.xml' => "---\nextension: .xml\nlayout: raw\n---\nmine",
        '_source/llms.txt'    => "---\nextension: .txt\nlayout: raw\n---\nmy llms",
        '_layout/raw.html'    => "[% page.content %]",
    });
    my @warnings;
    local $SIG{__WARN__} = sub { push @warnings, @_ };
    my $d = quiet_build();
    is_deeply([sort @warnings], [
        "warning: a page in the source directory already writes _output/llms.txt, so Dapper did not generate one\n",
        "warning: a page in the source directory already writes _output/sitemap.xml, so Dapper did not generate one\n",
        "warning: a page is still at /a.html, so the redirect from it to /b/ was not written\n",
    ], 'a warning for each clash');
    is(slurp('_output/sitemap.xml'), 'mine', 'the page wins over the sitemap');
    is(slurp('_output/llms.txt'), 'my llms', 'and over llms.txt');
    ok(-f '_output/llms-full.txt', 'llms-full.txt is still generated');
    ok(-f '_output/feed.xml', 'the feed is generated');
    like(slurp('_output/old/index.html'), qr{/a\.html}, 'the redirect that does not clash is written');
    is($d->{page_owner}->{'_output/old/index.html'}, 'the redirect from /old/', 'and owns its file');
    is($d->{page_owner}->{'_output/sitemap.xml'}, '_source/sitemap.xml', 'pages own theirs');

    @warnings = ();
    $d = App::Dapper->new();
    $d->{quiet} = 1;
    $d->{collect} = 1;
    $d->build();
    is_deeply([sort map { "$_->{severity}: $_->{file}" } @{ $d->{problems} }],
        ['warning: _output/a.html', 'warning: _output/llms.txt', 'warning: _output/sitemap.xml'], 'collected as warnings');
    is_deeply(\@warnings, [], 'not printed when collecting');
};

subtest 'generated files that need the site address' => sub {
    my @warnings;
    local $SIG{__WARN__} = sub { push @warnings, @_ };
    make_site({ '_config.yml' => "---\nname: x\nsitemap: true\n", '_source/a.md' => "---\n---\nx" });
    quiet_build();
    is_deeply(\@warnings, ["warning: the sitemap needs the site's address; set url in _config.yml\n"], 'a sitemap asked for');
    ok(!-e '_output/sitemap.xml', 'is not written without url');

    @warnings = ();
    make_site({ '_source/a.md' => "---\n---\nx" });
    quiet_build();
    is_deeply(\@warnings, [], 'the default sitemap setting stays quiet');

    @warnings = ();



( run in 1.004 second using v1.01-cache-2.11-cpan-234fa220139 )