Merge remote branch 'davrieb/autotag' into autotag
[ikiwiki.git] / t / index.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use IkiWiki;
5
6 package IkiWiki; # use internal variables
7 use Test::More tests => 31;
8
9 $config{wikistatedir}="/tmp/ikiwiki-test.$$";
10 system "rm -rf $config{wikistatedir}";
11
12 ok(! loadindex(), "loading nonexistent index file");
13
14 # Load standard plugins.
15 ok(loadplugin("meta"), "meta plugin loaded");
16 ok(loadplugin("mdwn"), "mdwn plugin loaded");
17
18 # Set up a default state.
19 $pagesources{"Foo"}="Foo.mdwn";
20 $pagesources{"bar"}="bar.mdwn";
21 $pagesources{"bar.png"}="bar.png";
22 my $now=time();
23 $pagemtime{"Foo"}=$now;
24 $pagemtime{"bar"}=$now-1000;
25 $pagemtime{"bar.png"}=$now;
26 $pagectime{"Foo"}=$now;
27 $pagectime{"bar"}=$now-100000;
28 $pagectime{"bar.png"}=$now-100000;
29 $renderedfiles{"Foo"}=["Foo.html"];
30 $renderedfiles{"bar"}=["bar.html", "bar.rss", "sparkline-foo.gif"];
31 $renderedfiles{"bar.png"}=["bar.png"];
32 $links{"Foo"}=["bar.png"];
33 $links{"bar"}=["Foo", "new-page"];
34 $typedlinks{"bar"}={tag => {"Foo" => 1}};
35 $links{"bar.png"}=[];
36 $depends{"Foo"}={};
37 $depends{"bar"}={"foo*" => 1};
38 $depends{"bar.png"}={};
39 $pagestate{"bar"}{meta}{title}="a page about bar";
40 $pagestate{"bar"}{meta}{moo}="mooooo";
41 # only loaded plugins save state, so this should not be saved out
42 $pagestate{"bar"}{nosuchplugin}{moo}="mooooo";
43
44 ok(saveindex(), "save index");
45 ok(-s "$config{wikistatedir}/indexdb", "index file created");
46
47 # Clear state.
48 %oldrenderedfiles=%pagectime=();
49 %pagesources=%pagemtime=%oldlinks=%links=%depends=%typedlinks=%oldtypedlinks=
50 %destsources=%renderedfiles=%pagecase=%pagestate=();
51
52 ok(loadindex(), "load index");
53 is_deeply(\%pagesources, {
54         Foo => "Foo.mdwn",
55         bar => "bar.mdwn",
56         "bar.png" => "bar.png",
57 }, "%pagesources loaded correctly");
58 is_deeply(\%pagemtime, {
59         Foo => $now,
60         bar => $now-1000,
61         "bar.png" => $now,
62 }, "%pagemtime loaded correctly");
63 is_deeply(\%pagectime, {
64         Foo => $now,
65         bar => $now-100000,
66         "bar.png" => $now-100000,
67 }, "%pagemtime loaded correctly");
68 is_deeply(\%renderedfiles, {
69         Foo => ["Foo.html"],
70         bar => ["bar.html", "bar.rss", "sparkline-foo.gif"],
71         "bar.png" => ["bar.png"],
72 }, "%renderedfiles loaded correctly");
73 is_deeply(\%oldrenderedfiles, {
74         Foo => ["Foo.html"],
75         bar => ["bar.html", "bar.rss", "sparkline-foo.gif"],
76         "bar.png" => ["bar.png"],
77 }, "%oldrenderedfiles loaded correctly");
78 is_deeply(\%links, {
79         Foo => ["bar.png"],
80         bar => ["Foo", "new-page"],
81         "bar.png" => [],
82 }, "%links loaded correctly");
83 is_deeply(\%depends, {
84         Foo => {},
85         bar => {"foo*" => 1},
86         "bar.png" => {},
87 }, "%depends loaded correctly");
88 is_deeply(\%pagestate, {
89         bar => {
90                 meta => {
91                         title => "a page about bar",
92                         moo => "mooooo",
93                 },
94         },
95 }, "%pagestate loaded correctly");
96 is_deeply(\%pagecase, {
97         foo => "Foo",
98         bar => "bar",
99         "bar.png" => "bar.png"
100 }, "%pagecase generated correctly");
101 is_deeply(\%destsources, {
102         "Foo.html" => "Foo",
103         "bar.html" => "bar",
104         "bar.rss" => "bar",
105         "sparkline-foo.gif" => "bar",
106         "bar.png" => "bar.png",
107 }, "%destsources generated correctly");
108 is_deeply(\%typedlinks, {
109         bar => {tag => {"Foo" => 1}},
110 }, "%typedlinks loaded correctly");
111 is_deeply(\%oldtypedlinks, {
112         bar => {tag => {"Foo" => 1}},
113 }, "%oldtypedlinks loaded correctly");
114
115 # Clear state.
116 %oldrenderedfiles=%pagectime=();
117 %pagesources=%pagemtime=%oldlinks=%links=%depends=%typedlinks=%oldtypedlinks=
118 %destsources=%renderedfiles=%pagecase=%pagestate=();
119
120 # When state is loaded for a wiki rebuild, only ctime and oldrenderedfiles
121 # are retained.
122 $config{rebuild}=1;
123 ok(loadindex(), "load index");
124 is_deeply(\%pagesources, {
125 }, "%pagesources loaded correctly");
126 is_deeply(\%pagemtime, {
127 }, "%pagemtime loaded correctly");
128 is_deeply(\%pagectime, {
129         Foo => $now,
130         bar => $now-100000,
131         "bar.png" => $now-100000,
132 }, "%pagemtime loaded correctly");
133 is_deeply(\%renderedfiles, {
134 }, "%renderedfiles loaded correctly");
135 is_deeply(\%oldrenderedfiles, {
136         Foo => ["Foo.html"],
137         bar => ["bar.html", "bar.rss", "sparkline-foo.gif"],
138         "bar.png" => ["bar.png"],
139 }, "%oldrenderedfiles loaded correctly");
140 is_deeply(\%links, {
141 }, "%links loaded correctly");
142 is_deeply(\%depends, {
143 }, "%depends loaded correctly");
144 is_deeply(\%pagestate, {
145 }, "%pagestate loaded correctly");
146 is_deeply(\%pagecase, {
147 }, "%pagecase generated correctly");
148 is_deeply(\%destsources, {
149 }, "%destsources generated correctly");
150 is_deeply(\%typedlinks, {
151 }, "%typedlinks cleared correctly");
152 is_deeply(\%oldtypedlinks, {
153 }, "%oldtypedlinks cleared correctly");
154
155 system "rm -rf $config{wikistatedir}";