]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/trail.t
(no commit message)
[git.ikiwiki.info.git] / t / trail.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More;
5 use IkiWiki;
7 sub check_trail {
8         my $file=shift;
9         my $expected=shift;
10         my $trailname=shift || qr/\w+/;
11         my $blob=readfile("t/tmp/out/$file");
12         my ($trailline)=$blob=~/^trail=$trailname\s+(.*)$/m;
13         is($trailline, $expected, "expected $expected in $file");
14 }
16 sub check_no_trail {
17         my $file=shift;
18         my $trailname=shift || qr/\w+/;
19         my $blob=readfile("t/tmp/out/$file");
20         my ($trailline)=$blob=~/^trail=$trailname\s+(.*)$/m;
21         $trailline="" unless defined $trailline;
22         ok($trailline !~ /^trail=$trailname\s+/, "no trail $trailname in $file");
23 }
25 my $blob;
27 ok(! system("rm -rf t/tmp"));
28 ok(! system("mkdir t/tmp"));
30 my $installed = $ENV{INSTALLED_TESTS};
32 my @command;
33 if ($installed) {
34         @command = qw(ikiwiki);
35 }
36 else {
37         ok(! system("make -s ikiwiki.out"));
38         @command = qw(perl -I. ./ikiwiki.out
39                 --underlaydir=underlays/basewiki
40                 --set underlaydirbase=underlays
41                 --templatedir=templates);
42 }
44 push @command, qw(--set usedirs=0 --plugin trail --plugin inline
45         --url=http://example.com --cgiurl=http://example.com/ikiwiki.cgi
46         --rss --atom t/tmp/in t/tmp/out --verbose);
48 # Write files with a date in the past, so that when we refresh,
49 # the update is detected.
50 sub write_old_file {
51         my $name = shift;
52         my $content = shift;
54         writefile($name, "t/tmp/in", $content);
55         ok(utime(333333333, 333333333, "t/tmp/in/$name"));
56 }
58 # Use a rather stylized template to override the default rendering, to make
59 # it easy to search for the desired results
60 write_old_file("templates/trails.tmpl", <<EOF
61 <TMPL_LOOP TRAILLOOP>
62 <TMPL_IF __FIRST__><nav></TMPL_IF>
63 <div>
64 trail=<TMPL_VAR TRAILPAGE> n=<TMPL_VAR NEXTPAGE> p=<TMPL_VAR PREVPAGE>
65 </div>
66 <div>
67 <TMPL_IF PREVURL>
68 <a href="<TMPL_VAR PREVURL>">&lt; <TMPL_VAR PREVTITLE></a>
69 </TMPL_IF> |
70 <a href="<TMPL_VAR TRAILURL>">^ <TMPL_VAR TRAILTITLE> ^</a>
71 | <TMPL_IF NEXTURL>
72 <a href="<TMPL_VAR NEXTURL>"><TMPL_VAR NEXTTITLE> &gt;</a>
73 </TMPL_IF>
74 </div>
75 <TMPL_IF __LAST__></nav></TMPL_IF>
76 </TMPL_LOOP>
77 EOF
78 );
79 write_old_file("badger.mdwn", "[[!meta title=\"The Breezy Badger\"]]\ncontent of badger");
80 write_old_file("mushroom.mdwn", "content of mushroom");
81 write_old_file("snake.mdwn", "content of snake");
82 write_old_file("ratty.mdwn", "content of ratty");
83 write_old_file("mr_toad.mdwn", "content of mr toad");
84 write_old_file("add.mdwn", '[[!trailitems pagenames="add/a add/b add/c add/d add/e"]]');
85 write_old_file("add/b.mdwn", "b");
86 write_old_file("add/d.mdwn", "d");
87 write_old_file("del.mdwn", '[[!trailitems pages="del/*" sort=title]]');
88 write_old_file("del/a.mdwn", "a");
89 write_old_file("del/b.mdwn", "b");
90 write_old_file("del/c.mdwn", "c");
91 write_old_file("del/d.mdwn", "d");
92 write_old_file("del/e.mdwn", "e");
93 write_old_file("self_referential.mdwn", '[[!trailitems pagenames="self_referential" circular=yes]]');
94 write_old_file("sorting/linked.mdwn", "linked");
95 write_old_file("sorting/a/b.mdwn", "a/b");
96 write_old_file("sorting/a/c.mdwn", "a/c");
97 write_old_file("sorting/z/a.mdwn", "z/a");
98 write_old_file("sorting/beginning.mdwn", "beginning");
99 write_old_file("sorting/middle.mdwn", "middle");
100 write_old_file("sorting/end.mdwn", "end");
101 write_old_file("sorting/new.mdwn", "new");
102 write_old_file("sorting/old.mdwn", "old");
103 write_old_file("sorting/ancient.mdwn", "ancient");
104 # These three need to be in the appropriate age order
105 ok(utime(333333333, 333333333, "t/tmp/in/sorting/new.mdwn"));
106 ok(utime(222222222, 222222222, "t/tmp/in/sorting/old.mdwn"));
107 ok(utime(111111111, 111111111, "t/tmp/in/sorting/ancient.mdwn"));
108 write_old_file("sorting/linked2.mdwn", "linked2");
109 # This initially uses the default sort order: age for the inline, and path
110 # for trailitems. We change it later.
111 write_old_file("sorting.mdwn",
112         '[[!traillink linked]] ' .
113         '[[!trailitems pages="sorting/z/a or sorting/a/b or sorting/a/c"]] ' .
114         '[[!trailitems pagenames="sorting/beginning sorting/middle sorting/end"]] ' .
115         '[[!inline pages="sorting/old or sorting/ancient or sorting/new" trail="yes"]] ' .
116         '[[!traillink linked2]]');
117 write_old_file("limited/a.mdwn", "a");
118 write_old_file("limited/b.mdwn", "b");
119 write_old_file("limited/c.mdwn", "c");
120 write_old_file("limited/d.mdwn", "d");
121 write_old_file("limited.mdwn",
122         '[[!inline pages="limited/*" trail="yes" show=2 sort=title]]');
123 write_old_file("untrail/a.mdwn", "a");
124 write_old_file("untrail/b.mdwn", "b");
125 write_old_file("untrail.mdwn", "[[!traillink a]] [[!traillink b]]");
126 write_old_file("retitled/a.mdwn", "a");
127 write_old_file("retitled.mdwn",
128         '[[!meta title="the old title"]][[!traillink a]]');
130 write_old_file("meme.mdwn", <<EOF
131 [[!trail]]
132 * [[!traillink badger]]
133 * [[!traillink badger text="This is a link to badger, with a title"]]
134 * [[!traillink That_is_the_badger|badger]]
135 * [[!traillink badger]]
136 * [[!traillink mushroom]]
137 * [[!traillink mushroom]]
138 * [[!traillink snake]]
139 * [[!traillink snake]]
140 EOF
141 );
143 write_old_file("wind_in_the_willows.mdwn", <<EOF
144 [[!trailoptions circular=yes sort=title]]
145 [[!trailitems pages="ratty or badger or mr_toad"]]
146 [[!trailitem moley]]
147 EOF
148 );
150 ok(! system(@command));
151 ok(! system(@command, "--refresh"));
153 $blob = readfile("t/tmp/out/meme.html");
154 ok($blob =~ /<a href="(\.\/)?badger.html">badger<\/a>/m);
155 ok($blob =~ /<a href="(\.\/)?badger.html">This is a link to badger, with a title<\/a>/m);
156 ok($blob =~ /<a href="(\.\/)?badger.html">That is the badger<\/a>/m);
158 check_trail("badger.html", "n=mushroom p=", "meme");
159 check_trail("badger.html", "n=mr_toad p=ratty", "wind_in_the_willows");
161 ok(! -f "t/tmp/out/moley.html");
163 check_trail("mr_toad.html", "n=ratty p=badger", "wind_in_the_willows");
164 check_no_trail("mr_toad.html", "meme");
165 # meta title is respected for pages that have one
166 $blob = readfile("t/tmp/out/mr_toad.html");
167 ok($blob =~ /">&lt; The Breezy Badger<\/a>/m);
168 # pagetitle for pages that don't
169 ok($blob =~ /">ratty &gt;<\/a>/m);
171 check_no_trail("ratty.html", "meme");
172 check_trail("ratty.html", "n=badger p=mr_toad", "wind_in_the_willows");
174 check_trail("mushroom.html", "n=snake p=badger", "meme");
175 check_no_trail("mushroom.html", "wind_in_the_willows");
177 check_trail("snake.html", "n= p=mushroom", "meme");
178 check_no_trail("snake.html", "wind_in_the_willows");
180 check_trail("self_referential.html", "n= p=", "self_referential");
182 check_trail("add/b.html", "n=add/d p=", "add");
183 check_trail("add/d.html", "n= p=add/b", "add");
184 ok(! -f "t/tmp/out/add/a.html");
185 ok(! -f "t/tmp/out/add/c.html");
186 ok(! -f "t/tmp/out/add/e.html");
188 check_trail("del/a.html", "n=del/b p=");
189 check_trail("del/b.html", "n=del/c p=del/a");
190 check_trail("del/c.html", "n=del/d p=del/b");
191 check_trail("del/d.html", "n=del/e p=del/c");
192 check_trail("del/e.html", "n= p=del/d");
194 check_trail("sorting/linked.html", "n=sorting/a/b p=");
195 check_trail("sorting/a/b.html", "n=sorting/a/c p=sorting/linked");
196 check_trail("sorting/a/c.html", "n=sorting/z/a p=sorting/a/b");
197 check_trail("sorting/z/a.html", "n=sorting/beginning p=sorting/a/c");
198 check_trail("sorting/beginning.html", "n=sorting/middle p=sorting/z/a");
199 check_trail("sorting/middle.html", "n=sorting/end p=sorting/beginning");
200 check_trail("sorting/end.html", "n=sorting/new p=sorting/middle");
201 check_trail("sorting/new.html", "n=sorting/old p=sorting/end");
202 check_trail("sorting/old.html", "n=sorting/ancient p=sorting/new");
203 check_trail("sorting/ancient.html", "n=sorting/linked2 p=sorting/old");
204 check_trail("sorting/linked2.html", "n= p=sorting/ancient");
206 # If the inline has a limited number of pages, the trail still contains
207 # everything.
208 $blob = readfile("t/tmp/out/limited.html");
209 ok($blob =~ /<a href="(\.\/)?limited\/a.html">a<\/a>/m);
210 ok($blob =~ /<a href="(\.\/)?limited\/b.html">b<\/a>/m);
211 ok($blob !~ /<a href="(\.\/)?limited\/c.html">/m);
212 ok($blob !~ /<a href="(\.\/)?limited\/d.html">/m);
213 check_trail("limited/a.html", "n=limited/b p=");
214 check_trail("limited/b.html", "n=limited/c p=limited/a");
215 check_trail("limited/c.html", "n=limited/d p=limited/b");
216 check_trail("limited/d.html", "n= p=limited/c");
218 check_trail("untrail/a.html", "n=untrail/b p=");
219 check_trail("untrail/b.html", "n= p=untrail/a");
221 $blob = readfile("t/tmp/out/retitled/a.html");
222 ok($blob =~ /\^ the old title \^/m);
224 # Make some changes and refresh. These writefile calls don't set an
225 # old mtime, so they're strictly newer than the "old" files.
227 writefile("add/a.mdwn", "t/tmp/in", "a");
228 writefile("add/c.mdwn", "t/tmp/in", "c");
229 writefile("add/e.mdwn", "t/tmp/in", "e");
230 ok(unlink("t/tmp/in/del/a.mdwn"));
231 ok(unlink("t/tmp/in/del/c.mdwn"));
232 ok(unlink("t/tmp/in/del/e.mdwn"));
234 writefile("sorting.mdwn", "t/tmp/in",
235         readfile("t/tmp/in/sorting.mdwn") .
236         '[[!trailoptions sort="title" reverse="yes"]]'); 
238 writefile("retitled.mdwn", "t/tmp/in",
239         '[[!meta title="the new title"]][[!traillink a]]');
241 # If the inline has a limited number of pages, the trail still depends on
242 # everything.
243 writefile("limited.html", "t/tmp/out", "[this gets rebuilt]");
244 writefile("limited/c.mdwn", "t/tmp/in", '[[!meta title="New C page"]]c');
246 writefile("untrail.mdwn", "t/tmp/in", "no longer a trail");
248 ok(! system(@command, "--refresh"));
250 check_trail("add/a.html", "n=add/b p=");
251 check_trail("add/b.html", "n=add/c p=add/a");
252 check_trail("add/c.html", "n=add/d p=add/b");
253 check_trail("add/d.html", "n=add/e p=add/c");
254 check_trail("add/e.html", "n= p=add/d");
256 check_trail("del/b.html", "n=del/d p=");
257 check_trail("del/d.html", "n= p=del/b");
258 ok(! -f "t/tmp/out/del/a.html");
259 ok(! -f "t/tmp/out/del/c.html");
260 ok(! -f "t/tmp/out/del/e.html");
262 check_trail("sorting/old.html", "n=sorting/new p=");
263 check_trail("sorting/new.html", "n=sorting/middle p=sorting/old");
264 check_trail("sorting/middle.html", "n=sorting/linked2 p=sorting/new");
265 check_trail("sorting/linked2.html", "n=sorting/linked p=sorting/middle");
266 check_trail("sorting/linked.html", "n=sorting/end p=sorting/linked2");
267 check_trail("sorting/end.html", "n=sorting/a/c p=sorting/linked");
268 check_trail("sorting/a/c.html", "n=sorting/beginning p=sorting/end");
269 check_trail("sorting/beginning.html", "n=sorting/a/b p=sorting/a/c");
270 check_trail("sorting/a/b.html", "n=sorting/ancient p=sorting/beginning");
271 check_trail("sorting/ancient.html", "n=sorting/z/a p=sorting/a/b");
272 check_trail("sorting/z/a.html", "n= p=sorting/ancient");
274 # If the inline has a limited number of pages, the trail still depends on
275 # everything, so it gets rebuilt even though it doesn't strictly need it.
276 # This means we could use it as a way to recompute the order of members
277 # and the contents of their trail navbars, allowing us to fix the regression
278 # described in [[bugs/trail excess dependencies]] without a full content
279 # dependency.
280 $blob = readfile("t/tmp/out/limited.html");
281 ok($blob =~ /<a href="(\.\/)?limited\/a.html">a<\/a>/m);
282 ok($blob =~ /<a href="(\.\/)?limited\/b.html">b<\/a>/m);
283 ok($blob !~ /<a href="(\.\/)?limited\/c.html">/m);
284 ok($blob !~ /<a href="(\.\/)?limited\/d.html">/m);
285 check_trail("limited/a.html", "n=limited/b p=");
286 check_trail("limited/b.html", "n=limited/c p=limited/a");
287 check_trail("limited/c.html", "n=limited/d p=limited/b");
288 check_trail("limited/d.html", "n= p=limited/c");
289 # Also, b and d should pick up the change to c. This regressed with the
290 # change to using a presence dependency.
291 $blob = readfile("t/tmp/out/limited/b.html");
292 ok($blob =~ /New C page &gt;/m);
293 $blob = readfile("t/tmp/out/limited/d.html");
294 ok($blob =~ /&lt; New C page/m);
296 # Members of a retitled trail should pick up that change.
297 # This regressed with the change to using a presence dependency.
298 $blob = readfile("t/tmp/out/retitled/a.html");
299 ok($blob =~ /\^ the new title \^/m);
301 # untrail is no longer a trail, so these are no longer in it.
302 check_no_trail("untrail/a.html");
303 check_no_trail("untrail/b.html");
305 ok(! system("rm -rf t/tmp"));
307 done_testing();