summaryrefslogtreecommitdiff
path: root/t/data/regenerate-data
blob: dc9fbb3087c6cf632ac63328409686b3d7067869 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/perl
#
# Development helper program to regenerate test data.
#
# When the templates in the DocKnot package change, all of the tests will fail
# because the output is out of date.  This program regenerates all of the test
# output data using the local instance of App::DocKnot.  It should be run from
# the root of the DocKnot distribution directory.
#
# SPDX-License-Identifier: MIT

use 5.018;
use autodie;
use warnings;

use File::Spec;

use lib 'blib/lib';

use App::DocKnot;
use App::DocKnot::Generate;
use App::DocKnot::Spin;
use Pod::Thread;

# For each subdirectory of t/data/generate, regenerate each file in the output
# subdirectory using the metadata subdirectory and the template matching the
# output name.  Special-case the docknot subdirectory, which uses DocKnot's
# own metadata.
my $data = File::Spec->catdir('t', 'data', 'generate');
opendir(my $datadir, $data);
my @packages = grep { -d File::Spec->catdir($data, $_) }
  File::Spec->no_upwards(readdir($datadir));
closedir($datadir);
for my $package (@packages) {
    my $metadata;
    if ($package eq 'docknot') {
        $metadata = File::Spec->catdir('docs', 'docknot.yaml');
    } else {
        $metadata = File::Spec->catdir($data, $package, 'docknot.yaml');
    }
    my $output = File::Spec->catdir($data, $package, 'output');
    opendir(my $outputdir, $output);
    for my $template (File::Spec->no_upwards(readdir($outputdir))) {
        my $docknot = App::DocKnot::Generate->new({ metadata => $metadata });
        my $outpath = File::Spec->catdir($output, $template);
        $docknot->generate_output($template, $outpath);
    }
    closedir($outputdir);
}

# The test of spinning a tree of files uses a reference to App::DocKnot's own
# POD documentation.  Regenerate the expected output in case the POD has
# changed.
my $source    = File::Spec->catdir('lib', 'App', 'DocKnot.pm');
my $podthread = Pod::Thread->new(navbar => 1);
my $spin      = App::DocKnot::Spin->new();
my $thread;
$podthread->output_string(\$thread);
$podthread->parse_file($source);
my $html;
open(my $in_fh,  '<', \$thread);
open(my $out_fh, '>', \$html);
$spin->_spin($in_fh, q{-}, $out_fh, q{-});
close($in_fh);
close($out_fh);

# Add the additional metadata that should be added by spin.
my $links = <<'EOD';
  <link rel="stylesheet" href="/~eagle/styles/pod.css" type="text/css" />
  <link rel="next" href="app-docknot-command.html"
        title="App::DocKnot::Command" />
  <link rel="up" href="../" title="DocKnot" />
  <link rel="top" href="../../../" />
EOD
my $comment = '<!-- Spun by spin %VERSION% on %DATE% -->';
my $navbar  = <<'EOD';
<table class="navbar"><tr>
  <td class="navleft"></td>
  <td>
    <a href="../../../">Russ Allbery</a>
    &gt; <a href="../../">Software</a>
    &gt; <a href="../">DocKnot</a>
  </td>
  <td class="navright"><a href="app-docknot-command.html">App::DocKnot::Command</a>&nbsp;&gt;</td>
</tr></table>

EOD
my $address = <<'EOD';
<address>
    Last <a href="https://www.eyrie.org/~eagle/software/web/">spun</a>
    %DATE% from POD modified %DATE%
</address>
EOD
$html =~ s{ (</head>) }{$links$1}xms;
$html =~ s{ <!-- [ ] Spun .*? [ ] --> }{$comment}xms;
$html =~ s{ (<body> \n) }{$1$navbar}xms;
$html =~ s{ (</body>) }{$navbar$address$1}xms;

# Replace the expected data file.
my $output = File::Spec->catdir(
    't',   'data', 'spin', 'output', 'software', 'docknot',
    'api', 'app-docknot.html',
);
open(my $fh, '>', $output);
print {$fh} $html or die "Cannot write to $output: $!\n";
close($fh);