not-truncated.t: Work correctly as an installed-test
[ikiwiki] / t / autoindex.t
1 #!/usr/bin/perl
2 package IkiWiki;
3
4 use warnings;
5 use strict;
6 use Test::More tests => 50;
7
8 BEGIN { use_ok("IkiWiki"); }
9 BEGIN { use_ok("IkiWiki::Render"); }
10 BEGIN { use_ok("IkiWiki::Plugin::aggregate"); }
11 BEGIN { use_ok("IkiWiki::Plugin::autoindex"); }
12 BEGIN { use_ok("IkiWiki::Plugin::html"); }
13 BEGIN { use_ok("IkiWiki::Plugin::mdwn"); }
14
15 ok(! system("rm -rf t/tmp; mkdir t/tmp"));
16
17 $config{verbose} = 1;
18 $config{srcdir} = 't/tmp/in';
19 $config{underlaydir} = 't/tmp/underlay';
20 $config{underlaydirbase} = '.';
21 $config{templatedir} = 'templates';
22 $config{usedirs} = 1;
23 $config{htmlext} = 'html';
24 $config{wiki_file_chars} = "-[:alnum:]+/.:_";
25 $config{userdir} = "users";
26 $config{tagbase} = "tags";
27 $config{default_pageext} = "mdwn";
28 $config{wiki_file_prune_regexps} = [qr/^\./];
29 $config{autoindex_commit} = 0;
30
31 is(checkconfig(), 1);
32
33 %oldrenderedfiles=%pagectime=();
34 %pagesources=%pagemtime=%oldlinks=%links=%depends=%typedlinks=%oldtypedlinks=
35 %destsources=%renderedfiles=%pagecase=%pagestate=();
36
37 # Pages that (we claim) were deleted in an earlier pass. We're using deleted,
38 # not autofile, to test backwards compat.
39 $wikistate{autoindex}{deleted}{deleted} = 1;
40 $wikistate{autoindex}{deleted}{expunged} = 1;
41 $wikistate{autoindex}{deleted}{reinstated} = 1;
42
43 foreach my $page (qw(tags/numbers deleted/bar reinstated reinstated/foo gone/bar)) {
44         # we use a non-default extension for these, so they're distinguishable
45         # from programmatically-created pages
46         $pagesources{$page} = "$page.html";
47         $pagemtime{$page} = $pagectime{$page} = 1000000;
48         writefile("$page.html", "t/tmp/in", "your ad here");
49 }
50
51 # a directory containing only an internal page shouldn't be indexed
52 $pagesources{"has_internal/internal"} = "has_internal/internal._aggregated";
53 $pagemtime{"has_internal/internal"} = 123456789;
54 $pagectime{"has_internal/internal"} = 123456789;
55 writefile("has_internal/internal._aggregated", "t/tmp/in", "this page is internal");
56
57 # a directory containing only a page in an underlay is now indexed
58 # (see [[transient autocreated tagbase is not transient autoindexed]])
59 $pagesources{"has_underlay/underlay"} = "has_underlay/underlay.mdwn";
60 $pagemtime{"has_underlay/underlay"} = 123456789;
61 $pagectime{"has_underlay/underlay"} = 123456789;
62 writefile("has_underlay/underlay.mdwn", "t/tmp/underlay", "this page is in an underlay");
63
64 # a directory containing only a transient page is now indexed
65 # (see [[transient autocreated tagbase is not transient autoindexed]])
66 $pagesources{"has_transient/transient"} = "has_transient/transient.mdwn";
67 $pagemtime{"has_transient/transient"} = 123456789;
68 $pagectime{"has_transient/transient"} = 123456789;
69 writefile("has_transient/transient.mdwn", "t/tmp/in/.ikiwiki/transient", "this page is transient");
70
71 # a directory containing only an attachment should be indexed
72 $pagesources{"attached/pie.jpg"} = "attached/pie.jpg";
73 $pagemtime{"attached/pie.jpg"} = 123456789;
74 $pagectime{"attached/pie.jpg"} = 123456789;
75 writefile("attached/pie.jpg", "t/tmp/in", "I lied, this isn't a real JPEG");
76
77 # "gone" disappeared just before this refresh pass so it still has a mtime
78 $pagemtime{gone} = $pagectime{gone} = 1000000;
79
80 my %pages;
81 my @del;
82
83 IkiWiki::Plugin::autoindex::refresh();
84
85 # this page is still on record as having been deleted, because it has
86 # a reason to be re-created
87 is($wikistate{autoindex}{autofile}{"deleted.mdwn"}, 1);
88 is($autofiles{"deleted.mdwn"}{plugin}, "autoindex");
89 %pages = ();
90 @del = ();
91 IkiWiki::gen_autofile("deleted.mdwn", \%pages, \@del);
92 is_deeply(\%pages, {});
93 is_deeply(\@del, []);
94 ok(! -f "t/tmp/in/deleted.mdwn");
95
96 # this page is tried as an autofile, but because it'll be in @del, it's not
97 # actually created
98 ok(! exists $wikistate{autoindex}{autofile}{"gone.mdwn"});
99 %pages = ();
100 @del = ("gone.mdwn");
101 is($autofiles{"gone.mdwn"}{plugin}, "autoindex");
102 IkiWiki::gen_autofile("gone.mdwn", \%pages, \@del);
103 is_deeply(\%pages, {});
104 is_deeply(\@del, ["gone.mdwn"]);
105 ok(! -f "t/tmp/in/gone.mdwn");
106
107 # this page does not exist and has no reason to be re-created, but we no longer
108 # have a special case for that - see
109 # [[todo/autoindex_should_use_add__95__autofile]] - so it won't be created
110 # even if it gains subpages later
111 is($wikistate{autoindex}{autofile}{"expunged.mdwn"}, 1);
112 ok(! exists $autofiles{"expunged.mdwn"});
113 ok(! -f "t/tmp/in/expunged.mdwn");
114
115 # a directory containing only an internal page shouldn't be indexed
116 ok(! exists $wikistate{autoindex}{autofile}{"has_internal.mdwn"});
117 ok(! exists $autofiles{"has_internal.mdwn"});
118 ok(! -f "t/tmp/in/has_internal.mdwn");
119
120 # a directory containing only a page in an underlay is now indexed
121 # (see [[transient autocreated tagbase is not transient autoindexed]])
122 ok(! exists $wikistate{autoindex}{autofile}{"has_underlay.mdwn"});
123 is($autofiles{"has_underlay.mdwn"}{plugin}, "autoindex");
124 %pages = ();
125 @del = ();
126 IkiWiki::gen_autofile("has_underlay.mdwn", \%pages, \@del);
127 is_deeply(\%pages, {"t/tmp/in/has_underlay" => 1});
128 is_deeply(\@del, []);
129 ok(! -f "t/tmp/in/has_underlay.mdwn");
130 ok(-s "t/tmp/in/.ikiwiki/transient/has_underlay.mdwn");
131
132 # a directory containing only a transient page is now indexed
133 # (see [[transient autocreated tagbase is not transient autoindexed]])
134 ok(! exists $wikistate{autoindex}{autofile}{"has_transient.mdwn"});
135 is($autofiles{"has_transient.mdwn"}{plugin}, "autoindex");
136 %pages = ();
137 @del = ();
138 IkiWiki::gen_autofile("has_transient.mdwn", \%pages, \@del);
139 is_deeply(\%pages, {"t/tmp/in/has_transient" => 1});
140 is_deeply(\@del, []);
141 ok(! -f "t/tmp/in/has_transient.mdwn");
142 ok(-s "t/tmp/in/.ikiwiki/transient/has_transient.mdwn");
143
144 # this page was re-created, but that no longer gets a special case
145 # (see [[todo/autoindex_should_use_add__95__autofile]]) so it's the same as
146 # deleted
147 is($wikistate{autoindex}{autofile}{"reinstated.mdwn"}, 1);
148 ok(! exists $autofiles{"reinstated.mdwn"});
149 ok(! -f "t/tmp/in/.ikiwiki/transient/reinstated.mdwn");
150
151 # needs creating (deferred; part of the autofile mechanism now)
152 ok(! exists $wikistate{autoindex}{autofile}{"tags.mdwn"});
153 %pages = ();
154 @del = ();
155 is($autofiles{"tags.mdwn"}{plugin}, "autoindex");
156 IkiWiki::gen_autofile("tags.mdwn", \%pages, \@del);
157 is_deeply(\%pages, {"t/tmp/in/tags" => 1});
158 is_deeply(\@del, []);
159 ok(! -s "t/tmp/in/tags.mdwn");
160 ok(-s "t/tmp/in/.ikiwiki/transient/tags.mdwn");
161
162 # needs creating because of an attachment
163 ok(! exists $wikistate{autoindex}{autofile}{"attached.mdwn"});
164 %pages = ();
165 @del = ();
166 is($autofiles{"attached.mdwn"}{plugin}, "autoindex");
167 IkiWiki::gen_autofile("attached.mdwn", \%pages, \@del);
168 is_deeply(\%pages, {"t/tmp/in/attached" => 1});
169 is_deeply(\@del, []);
170 ok(-s "t/tmp/in/.ikiwiki/transient/attached.mdwn");
171
172 1;