Merge branch 'cvs' of github.com:schmonz/ikiwiki into cvs
[ikiwiki] / t / cvs.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More; my $total_tests = 9;
5 use IkiWiki;
6
7 my $default_test_methods = '^test_*';
8 my $dir = "/tmp/ikiwiki-test-cvs.$$";
9
10 # TESTS FOR GENERAL META-BEHAVIOR
11
12 sub test_web_add_and_commit {
13         my $message = "Add a page via VCS API";
14         writefile('test1.mdwn', $config{srcdir}, readfile("t/test1.mdwn"));
15         IkiWiki::rcs_add("test1.mdwn");
16         IkiWiki::rcs_commit(
17                 file => "test1.mdwn",
18                 message => $message,
19                 token => "moo",
20         );
21
22         my @changes = IkiWiki::rcs_recentchanges(3);
23         is(
24                 $#changes,
25                 0,
26                 q{1 total commit},
27         );
28         is(
29                 $changes[0]{message}[0]{"line"},
30                 $message,
31                 q{first line of most recent commit message matches},
32         );
33         is(
34                 $changes[0]{pages}[0]{"page"},
35                 "test1",
36                 q{first pagename from most recent commit matches},
37         );
38
39         # prevent web edits from attempting to create .../CVS/foo.mdwn
40         # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
41         # unless your "CVS" is something else and we've made it configurable
42         # how much of the web-edit workflow are we actually testing?
43         # because we want to test comments:
44         # - when the first comment for page.mdwn is added, and page/ is
45         #   created to hold the comment, page/ isn't added to CVS control,
46         #   so the comment isn't either
47         # - side effect for moderated comments: after approval they
48         #   show up normally AND are still pending, too
49         # - comments.pm treats rcs_commit_staged() as returning conflicts?
50 }
51
52 sub test_manual_add_and_commit {
53         my $message = "Add a page via CVS directly";
54         writefile('test2.mdwn', $config{srcdir}, readfile("t/test2.mdwn"));
55         system "cd $config{srcdir}"
56                 . " && cvs add test2.mdwn >/dev/null 2>&1";
57         system "cd $config{srcdir}"
58                 . " && cvs commit -m \"$message\" test2.mdwn >/dev/null";
59
60         my @changes = IkiWiki::rcs_recentchanges(3);
61         is(
62                 $#changes,
63                 0,
64                 q{1 total commit},
65         );
66         is(
67                 $changes[0]{message}[0]{"line"},
68                 $message,
69                 q{first line of most recent commit message matches},
70         );
71         is(
72                 $changes[0]{pages}[0]{"page"},
73                 "test2",
74                 q{first pagename from most recent commit matches},
75         );
76
77         # CVS commits run ikiwiki once for every committed file (!)
78         # - commit_prep alone should fix this
79         # CVS multi-dir commits show only the first dir in recentchanges
80         # - commit_prep might also fix this?
81         # CVS post-commit hook is amped off to avoid locking against itself
82         # - commit_prep probably doesn't fix this... but maybe?
83 }
84
85 sub test_chdir_magic {
86         # cvs.pm operations are always occurring inside $config{srcdir}
87         # other ikiwiki operations are occurring wherever, and are unaffected
88         # when are we bothering with "local $CWD" and when aren't we?
89 }
90
91 sub test_cvs_info {
92         # inspect "Repository revision" (used in code)
93         # inspect "Sticky Options" (used in tests to verify existence of "-kb")
94 }
95
96 sub test_cvs_run_cvs {
97         # extract the stdout-redirect thing
98         # - prove that it silences stdout
99         # - prove that stderr comes through just fine
100         # prove that when cvs exits nonzero (fail), function exits false
101         # prove that when cvs exits zero (success), function exits true
102         # always pass -f, just in case
103         # steal from git.pm: safe_git(), run_or_{die,cry,non}
104         # - open() instead of system()
105         # always call cvs_run_cvs(), don't ever run 'cvs' directly
106 }
107
108 sub test_cvs_run_cvsps {
109         # parameterize command like run_cvs()
110         # expose config vars for e.g. "--cvs-direct -z 30"
111         # always pass -x (unless proven otherwise)
112         # always pass -b HEAD (configurable like gitmaster_branch?)
113 }
114
115 sub test_cvs_parse_cvsps {
116         # extract method from rcs_recentchanges
117         # document expected changeset format
118         # document expected changeset delimiter
119         # try: cvsps -q -x -p && ls | sort -rn | head -100
120         # - benchmark against current impl (that uses File::ReadBackwards)
121 }
122
123 sub test_cvs_parse_log_accum {
124         # add new, preferred method for rcs_recentchanges to use
125         # teach log_accum to record commits (into transient?)
126         # script cvsps to bootstrap (or replace?) commit history
127         # teach ikiwiki-makerepo to set up log_accum and commit_prep
128         # why are NetBSD commit mails unreliable?
129         # - is it working for CVS commits and failing for web commits?
130 }
131
132 sub test_cvs_is_controlling {
133         # with no args:
134         # - if srcdir is in CVS, return true
135         # - else, return false
136         # with a dir arg:
137         # - if dir is in CVS, return true
138         # - else, return false
139         # with a file arg:
140         # - is there anything that wants the answer? if so, answer
141         # - else, die
142 }
143
144
145 # TESTS FOR GENERAL PLUGIN API CALLS
146
147 sub test_checkconfig {
148         # undef cvspath, expect "ikiwiki"
149         # define cvspath normally, get it back
150         # define cvspath in a subdir, get it back?
151         # define cvspath with extra slashes, get sanitized version back
152         # - yoink test_extra_path_slashes
153         # undef cvs_wrapper, expect $config{wrappers} same size as before
154
155         my $initial_cvspath = $config{cvspath};
156         $config{cvspath} = "/ikiwiki//";
157         IkiWiki::checkconfig();
158         is(
159                 $config{cvspath},
160                 $initial_cvspath,
161                 q{rcs_recentchanges assumes checkconfig has sanitized cvspath},
162         );
163 }
164
165 sub test_getsetup {
166         # anything worth testing?
167 }
168
169 sub test_genwrapper {
170         # testable directly? affects rcs_add, but are we exercising this?
171 }
172
173
174 # TESTS FOR VCS PLUGIN API CALLS
175
176 sub test_rcs_update {
177         # can it assume we're under CVS control? or must it check?
178         # anything else worth testing?
179 }
180
181 sub test_rcs_prepedit {
182         # can it assume we're under CVS control? or must it check?
183         # for existing file, returns latest revision in repo
184         # - what's this used for? should it return latest revision in checkout?
185         # for new file, returns empty string
186 }
187
188 sub test_rcs_commit {
189         # can it assume we're under CVS control? or must it check?
190         # if someone else changed the page since rcs_prepedit was called:
191         # - try to merge into our working copy
192         # - if merge succeeds, proceed to commit
193         # - else, return page content with the conflict markers in it
194         # commit:
195         # - if success, return undef
196         # - else, revert + return content with the conflict markers in it
197         # git.pm receives "session" param -- useful here?
198         # web commits start with "web commit {by,from} "
199         # seeing File::chdir errors on commit?
200 }
201
202 sub test_rcs_commit_staged {
203         # if commit succeeds, return undef
204         # else, warn and return error message (really? or just non-undef?)
205 }
206
207 sub test_rcs_add {
208         my $dir1 = "test3";
209         my $dir2 = "test4/test5";
210         ok(
211                 mkdir($config{srcdir} . "/$dir1"),
212                 qq{can make $dir1},
213         );
214         IkiWiki::rcs_add($dir1);
215         IkiWiki::rcs_commit(
216                 file => $dir1,
217                 message => "shouldn't happen",
218                 token => "oom",
219         );
220
221         # can it assume we're under CVS control? or must it check?
222         # add a top-level text file
223         # - rcs_commit it
224         # - inspect recentchanges: new change, no -kb
225         # add a top-level dir
226         # - test mustn't hang (does it hang if we comment out genwrapper?)
227         # - inspect recentchanges: no new change
228         # - rcs_commit it
229         # - reinspect recentchanges: still no new change
230         # add a text file in that dir
231         # - rcs_commit_staged
232         # - inspect recentchanges: new change, no -kb
233         # add a top-level dir + add a binary file in it
234         # - rcs_commit_staged
235         # - inspect recentchanges: new change, yes -kb
236         # add a top-level dir + subdir + add one text and one binary file in it
237         # - rcs_commit_staged
238         # - inspect recentchanges: one new change, two files, one -kb, one not
239
240         # extract method: filetype-guessing
241         # add a binary file, remove it, add a text file by same name, no -kb?
242         # add a text file, remove it, add a binary file by same name, -kb?
243 }
244
245 sub test_rcs_remove {
246         # can it assume we're under CVS control? or must it check?
247         # remove a top-level file
248         # - rcs_commit
249         # - inspect recentchanges: one new change, file removed
250         # remove two files (in different dirs)
251         # - rcs_commit_staged
252         # - inspect recentchanges: one new change, both files removed
253 }
254
255 sub test_rcs_rename {
256         # can it assume we're under CVS control? or must it check?
257         # rename a file in the same dir
258         # - rcs_commit_staged
259         # - inspect recentchanges: one new change, one file removed, one added
260         # rename a file into a different dir
261         # - rcs_commit_staged
262         # - inspect recentchanges: one new change, one file removed, one added
263         # rename a file into a not-yet-existing dir
264         # - rcs_commit_staged
265         # - inspect recentchanges: one new change, one file removed, one added
266         # is it safe to use "mv"? what if $dest is somehow outside the wiki?
267 }
268
269 sub test_rcs_recentchanges {
270         # can it assume we're under CVS control? or must it check?
271         # don't worry whether we're called with a number (we always are)
272         # other rcs tests already inspect much of the returned structure
273         # CVS commits say "cvs" and get the right committer
274         # web commits say "web" and get the right committer
275         # - and don't start with "web commit {by,from} "
276         # "nickname" -- can we ever meaningfully set this?
277
278         # prefer log_accum, then cvsps, else die
279         # run the high-level recentchanges tests 2x (once for each method)
280         # - including in other test subs that check recentchanges?
281 }
282
283 sub test_rcs_diff {
284         # can it assume we're under CVS control? or must it check?
285         # in list context, return all lines (with \n), up to $maxlines if set
286         # in scalar context, return the whole diff, up to $maxlines if set
287 }
288
289 sub test_rcs_getctime {
290         # can it assume we're under CVS control? or must it check?
291         # given a file, find its creation time, else return 0
292         # first implement in the obvious way
293         # then cache
294 }
295
296 sub test_rcs_getmtime {
297         # can it assume we're under CVS control? or must it check?
298         # given a file, find its modification time, else return 0
299         # first implement in the obvious way
300         # then cache
301 }
302
303 sub test_rcs_receive {
304         pass(q{rcs_receive doesn't make sense for CVS});
305 }
306
307 sub test_rcs_preprevert {
308         # can it assume we're under CVS control? or must it check?
309         # given a patchset number, return structure describing what'd happen:
310         # - see doc/plugins/write.mdwn:rcs_receive()
311         # don't forget about attachments
312 }
313
314 sub test_rcs_revert {
315         # can it assume we're under CVS control? or must it check?
316         # given a patchset number, stage the revert for rcs_commit_staged()
317         # if commit succeeds, return undef
318         # else, warn and return error message (really? or just non-undef?)
319 }
320
321 sub main {
322         my $test_methods = defined $ENV{TEST_METHOD} 
323                          ? $ENV{TEST_METHOD}
324                          : $default_test_methods;
325
326         _startup($test_methods eq $default_test_methods);
327         _runtests(_get_matching_test_subs($test_methods));
328         _shutdown($test_methods eq $default_test_methods);
329 }
330
331 main();
332
333
334 # INTERNAL SUPPORT ROUTINES
335
336 sub _plan_for_test_more {
337         my $can_plan = shift;
338
339         foreach my $program (qw(
340                 cvs
341                 cvsps
342         )) {
343                 my $program_path = `which $program`;
344                 chomp $program_path;
345                 return plan(skip_all => "$program not available")
346                         unless -x $program_path;
347         }
348
349         foreach my $module (qw(
350                 File::chdir
351                 File::MimeInfo
352                 Date::Parse
353                 File::Temp
354                 File::ReadBackwards
355         )) {
356                 eval qq{use $module};
357                 return plan(skip_all => "$module not available")
358                         if $@;
359         }
360
361         return plan(skip_all => "can't create $dir: $!")
362                 unless mkdir($dir);
363         return plan(skip_all => "can't remove $dir: $!")
364                 unless rmdir($dir);
365
366         return unless $can_plan;
367
368         return plan(tests => $total_tests);
369 }
370
371 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
372
373 use B qw/svref_2object/;
374
375 sub in_package {
376         my ($coderef, $package) = @_;
377         my $cv = svref_2object($coderef);
378         return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
379         return $cv->GV->STASH->NAME eq $package;
380 }
381
382 sub list_module {
383         my $module = shift;
384         no strict 'refs';
385         return grep {
386                 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
387         } keys %{"$module\::"};
388 }
389
390
391 # support for xUnit-style testing, a la Test::Class
392
393 sub _startup {
394         my $can_plan = shift;
395         _plan_for_test_more($can_plan);
396         _generate_test_config();
397 }
398
399 sub _shutdown {
400         my $had_plan = shift;
401         done_testing() unless $had_plan;
402 }
403
404 sub _setup {
405         _generate_test_repo();
406 }
407
408 sub _teardown {
409         system "rm -rf $dir";
410 }
411
412 sub _runtests {
413         my @coderefs = (@_);
414         for (@coderefs) {
415                 _setup();
416                 $_->();
417                 _teardown();
418         }
419 }
420
421 sub _get_matching_test_subs {
422         my $re = shift;
423         no strict 'refs';
424         return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
425 }
426
427 sub _generate_test_config {
428         %config = IkiWiki::defaultconfig();
429         $config{rcs} = "cvs";
430         $config{srcdir} = "$dir/src";
431         $config{cvsrepo} = "$dir/repo";
432         $config{cvspath} = "ikiwiki";
433         IkiWiki::loadplugins();
434         IkiWiki::checkconfig();
435 }
436
437 sub _generate_test_repo {
438         die "can't create $dir: $!"
439                 unless mkdir($dir);
440
441         my $cvs = "cvs -d $config{cvsrepo}";
442         my $dn = ">/dev/null";
443         system "$cvs init $dn";
444         system "mkdir $dir/$config{cvspath} $dn";
445         system "cd $dir/$config{cvspath} && "
446                 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
447         system "rm -rf $dir/$config{cvspath} $dn";
448         system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
449 }