More testings and fixings:
[ikiwiki] / t / cvs.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More; my $total_tests = 69;
5 use IkiWiki;
6
7 my $default_test_methods = '^test_*';
8 my @required_programs = qw(
9         cvs
10         cvsps
11 );
12 my @required_modules = qw(
13         File::chdir
14         File::MimeInfo
15         Date::Parse
16         File::Temp
17         File::ReadBackwards
18 );
19 my $dir = "/tmp/ikiwiki-test-cvs.$$";
20
21 # TESTS FOR GENERAL META-BEHAVIOR
22
23 sub test_web_comments {
24         # how much of the web-edit workflow are we actually testing?
25         # because we want to test comments:
26         # - when the first comment for page.mdwn is added, and page/ is
27         #   created to hold the comment, page/ isn't added to CVS control,
28         #   so the comment isn't either
29         #   - can't reproduce after chmod g+s ikiwiki.cgi (20120204)
30         # - side effect for moderated comments: after approval they
31         #   show up normally AND are still pending, too
32         # - comments.pm treats rcs_commit_staged() as returning conflicts?
33 }
34
35 sub test_chdir_magic {
36         # cvs.pm operations are always occurring inside $config{srcdir}
37         # other ikiwiki operations are occurring wherever, and are unaffected
38         # when are we bothering with "local $CWD" and when aren't we?
39         # after commit, presumably only with post-commit hook enabled:
40         #> Use of chdir('') or chdir(undef) as chdir() is deprecated at
41         #> /usr/pkg/lib/perl5/vendor_perl/5.14.0/File/chdir.pm line 45.
42 }
43
44 sub test_cvs_info {
45         # inspect "Repository revision" (used in code)
46         # inspect "Sticky Options" (used in tests to verify existence of "-kb")
47 }
48
49 sub test_cvs_run_cvs {
50         # extract the stdout-redirect thing
51         # - prove that it silences stdout
52         # - prove that stderr comes through just fine
53         # prove that when cvs exits nonzero (fail), function exits false
54         # prove that when cvs exits zero (success), function exits true
55         # always pass -f, just in case
56         # steal from git.pm: safe_git(), run_or_{die,cry,non}
57         # - open() instead of system()
58         # always call cvs_run_cvs(), don't ever run 'cvs' directly
59 }
60
61 sub test_cvs_run_cvsps {
62         # parameterize command like run_cvs()
63         # expose config vars for e.g. "--cvs-direct -z 30"
64         # always pass -x (unless proven otherwise)
65         # - but diff doesn't! optimization alert
66         # always pass -b HEAD (configurable like gitmaster_branch?)
67 }
68
69 sub test_cvs_parse_cvsps {
70         # extract method from rcs_recentchanges
71         # document expected changeset format
72         # document expected changeset delimiter
73         # try: cvsps -q -x -p && ls | sort -rn | head -100
74         # - benchmark against current impl (that uses File::ReadBackwards)
75 }
76
77 sub test_cvs_parse_log_accum {
78         # add new, preferred method for rcs_recentchanges to use
79         # teach log_accum to record commits (into transient?)
80         # script cvsps to bootstrap (or replace?) commit history
81         # teach ikiwiki-makerepo to set up log_accum and commit_prep
82         # why are NetBSD commit mails unreliable?
83         # - is it working for CVS commits and failing for web commits?
84 }
85
86 sub test_cvs_is_controlling {
87         # with no args:
88         # - if srcdir is in CVS, return true
89         # - else, return false
90         # with a dir arg:
91         # - if dir is in CVS, return true
92         # - else, return false
93         # with a file arg:
94         # - is there anything that wants the answer? if so, answer
95         # - else, die
96 }
97
98
99 # TESTS FOR GENERAL PLUGIN API CALLS
100
101 sub test_checkconfig {
102         my $default_cvspath = 'ikiwiki';
103
104         undef $config{cvspath}; IkiWiki::checkconfig();
105         is(
106                 $config{cvspath}, $default_cvspath,
107                 q{can provide default cvspath},
108         );
109
110         $config{cvspath} = "/$default_cvspath/"; IkiWiki::checkconfig();
111         is(
112                 $config{cvspath}, $default_cvspath,
113                 q{can set typical cvspath and strip well-meaning slashes},
114         );
115
116         $config{cvspath} = "/$default_cvspath//subdir"; IkiWiki::checkconfig();
117         is(
118                 $config{cvspath}, "$default_cvspath/subdir",
119                 q{can really sanitize cvspath as assumed by rcs_recentchanges},
120         );
121
122         my $default_num_wrappers = @{$config{wrappers}};
123         undef $config{cvs_wrapper}; IkiWiki::checkconfig();
124         is(
125                 @{$config{wrappers}}, $default_num_wrappers,
126                 q{can start with no wrappers configured},
127         );
128
129         $config{cvs_wrapper} = $config{cvsrepo} . "/CVSROOT/post-commit";
130         IkiWiki::checkconfig();
131         is(
132                 @{$config{wrappers}}, ++$default_num_wrappers,
133                 q{can add cvs_wrapper},
134         );
135
136         undef $config{cvs_wrapper};
137         $config{cvspath} = $default_cvspath;
138         IkiWiki::checkconfig();
139 }
140
141 sub test_getsetup {
142         # anything worth testing?
143 }
144
145 sub test_genwrapper {
146         # testable directly? affects rcs_add, but are we exercising this?
147 }
148
149
150 # TESTS FOR VCS PLUGIN API CALLS
151
152 sub test_rcs_update {
153         # can it assume we're under CVS control? or must it check?
154         # anything else worth testing?
155 }
156
157 sub test_rcs_prepedit {
158         # can it assume we're under CVS control? or must it check?
159         # for existing file, returns latest revision in repo
160         # - what's this used for? should it return latest revision in checkout?
161         # for new file, returns empty string
162
163         # netbsd web log says "could not open lock file"
164         # XXX does this work right?
165         # how about with un-added dirs in the srcdir?
166         # how about with cvsps.core lying around?
167 }
168
169 sub test_rcs_commit {
170         # can it assume we're under CVS control? or must it check?
171         # if someone else changed the page since rcs_prepedit was called:
172         # - try to merge into our working copy
173         # - if merge succeeds, proceed to commit
174         # - else, return page content with the conflict markers in it
175         # commit:
176         # - if success, return undef
177         # - else, revert + return content with the conflict markers in it
178         # git.pm receives "session" param -- useful here?
179         # web commits start with "web commit {by,from} "
180         # seeing File::chdir errors on commit?
181
182         # XXX commit can fail due to "could not open lock file"
183 }
184
185 sub test_rcs_commit_staged {
186         # if commit succeeds, return undef
187         # else, warn and return error message (really? or just non-undef?)
188 }
189
190 sub test_rcs_add {
191         my @changes = IkiWiki::rcs_recentchanges(3);
192         is_total_number_of_changes(\@changes, 0);
193
194         my $message = "add a top-level ASCII (non-UTF-8) page via VCS API";
195         my $file = q{test0.mdwn};
196         add_and_commit($file, $message, qq{# \$Id\$\n* some plain ASCII text});
197         is_newly_added($file);
198         is_in_keyword_substitution_mode($file, q{-kkv});
199         like(
200                 readfile($config{srcdir} . "/$file"),
201                 qr/^# \$Id: $file,v 1.1 .+\$$/m,
202                 q{can expand RCS Id keyword},
203         );
204         @changes = IkiWiki::rcs_recentchanges(3);
205         is_total_number_of_changes(\@changes, 1);
206         is_most_recent_change(\@changes, stripext($file), $message);
207
208         $message = "add a top-level dir via VCS API";
209         my $dir1 = q{test3};
210         can_mkdir($dir1);
211         IkiWiki::rcs_add($dir1);
212         # XXX test that the wrapper hangs here without our genwrapper()
213         # XXX test that the wrapper doesn't hang here with it
214         @changes = IkiWiki::rcs_recentchanges(3);
215         is_total_number_of_changes(\@changes, 1);       # despite the dir add
216         IkiWiki::rcs_commit(
217                 file => $dir1,
218                 message => $message,
219                 token => "oom",
220         );
221         @changes = IkiWiki::rcs_recentchanges(3);
222         is_total_number_of_changes(\@changes, 1);       # dirs aren't tracked
223
224         $message = "add a non-ASCII (UTF-8) text file in an un-added dir";
225         can_mkdir($_) for (qw(test4 test4/test5));
226         $file = q{test4/test5/test1.mdwn};
227         add_and_commit($file, $message, readfile("t/test1.mdwn"));
228         is_newly_added($file);
229         is_in_keyword_substitution_mode($file, q{-kkv});
230         @changes = IkiWiki::rcs_recentchanges(3);
231         is_total_number_of_changes(\@changes, 2);
232         is_most_recent_change(\@changes, stripext($file), $message);
233
234         $message = "add a binary file in an un-added dir, and commit_staged";
235         can_mkdir(q{test6});
236         $file = q{test6/test7.ico};
237         my $bindata_in = readfile("doc/favicon.ico", 1);
238         my $bindata_out = sub { readfile($config{srcdir} . "/$file", 1) };
239         writefile($file, $config{srcdir}, $bindata_in, 1);
240         is(&$bindata_out(), $bindata_in, q{binary files match before commit});
241         IkiWiki::rcs_add($file);
242         IkiWiki::rcs_commit_staged(message => $message);
243         is_newly_added($file);
244         is_in_keyword_substitution_mode($file, q{-kb});
245         is(&$bindata_out(), $bindata_in, q{binary files match after commit});
246         @changes = IkiWiki::rcs_recentchanges(3);
247         is_total_number_of_changes(\@changes, 3);
248         is_most_recent_change(\@changes, $file, $message);
249         ok(
250                 unlink($config{srcdir} . "/$file"),
251                 q{can remove file in order to re-fetch it from repo},
252         );
253         ok(! -e $config{srcdir} . "/$file", q{really removed file});
254         IkiWiki::rcs_update();
255         is(&$bindata_out(), $bindata_in, q{binary files match after re-fetch});
256
257         $message = "add a UTF-8 and a binary file in different dirs";
258         my $file1 = "test8/test9.mdwn";
259         my $file2 = "test10/test11.ico";
260         can_mkdir($_) for (qw(test8 test10));
261         writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
262         writefile($file2, $config{srcdir}, $bindata_in, 1);
263         IkiWiki::rcs_add($_) for ($file1, $file2);
264         IkiWiki::rcs_commit_staged(message => $message);
265         is_newly_added($_) for ($file1, $file2);
266         is_in_keyword_substitution_mode($file1, q{-kkv});
267         is_in_keyword_substitution_mode($file2, q{-kb});
268         @changes = IkiWiki::rcs_recentchanges(3);
269         is_total_number_of_changes(\@changes, 3);
270         @changes = IkiWiki::rcs_recentchanges(4);
271         is_total_number_of_changes(\@changes, 4);
272         # XXX test for both files in the commit, and no other files
273         is_most_recent_change(\@changes, $file2, $message);
274
275         $message = "remove the UTF-8 and binary files we just added";
276         IkiWiki::rcs_remove($_) for ($file1, $file2);
277         IkiWiki::rcs_commit_staged(message => $message);
278         ok(-d "$config{srcdir}/test8", q{empty dir not pruned (1)});
279         @changes = IkiWiki::rcs_recentchanges(11);
280         ok(-d "$config{srcdir}/test8", q{empty dir not pruned (2)});
281         is_total_number_of_changes(\@changes, 5);
282         # XXX test for both files in the commit, and no other files
283         is_most_recent_change(\@changes, $file2, $message);
284
285         $message = "re-add UTF-8 and binary files with names swapped";
286         writefile($file2, $config{srcdir}, readfile('t/test2.mdwn'));
287         writefile($file1, $config{srcdir}, $bindata_in, 1);
288         IkiWiki::rcs_add($_) for ($file1, $file2);
289         IkiWiki::rcs_commit_staged(message => $message);
290         isnt_newly_added($_) for ($file1, $file2);
291         is_in_keyword_substitution_mode($file2, q{-kkv});
292         is_in_keyword_substitution_mode($file1, q{-kb});
293         @changes = IkiWiki::rcs_recentchanges(11);
294         is_total_number_of_changes(\@changes, 6);
295         # XXX test for both files in the commit, and no other files
296         is_most_recent_change(\@changes, $file2, $message);
297
298         # prevent web edits from attempting to create .../CVS/foo.mdwn
299         # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
300         # unless your "CVS" is something else and we've made it configurable
301
302         # can it assume we're under CVS control? or must it check?
303
304         # extract method: filetype-guessing
305         # add a binary file, remove it, add a text file by same name, no -kb?
306         # add a text file, remove it, add a binary file by same name, -kb?
307 }
308
309 sub test_rcs_remove {
310         # can it assume we're under CVS control? or must it check?
311         # remove a top-level file
312         # - rcs_commit
313         # - inspect recentchanges: one new change, file removed
314         # remove two files (in different dirs)
315         # - rcs_commit_staged
316         # - inspect recentchanges: one new change, both files removed
317 }
318
319 sub test_rcs_rename {
320         # can it assume we're under CVS control? or must it check?
321         # rename a file in the same dir
322         # - rcs_commit_staged
323         # - inspect recentchanges: one new change, one file removed, one added
324         # rename a file into a different dir
325         # - rcs_commit_staged
326         # - inspect recentchanges: one new change, one file removed, one added
327         # rename a file into a not-yet-existing dir
328         # - rcs_commit_staged
329         # - inspect recentchanges: one new change, one file removed, one added
330         # is it safe to use "mv"? what if $dest is somehow outside the wiki?
331 }
332
333 sub test_rcs_recentchanges {
334         my @changes = IkiWiki::rcs_recentchanges(3);
335         is_total_number_of_changes(\@changes, 0);
336
337         my $message = "Add a page via CVS directly";
338         my $file = q{test2.mdwn};
339         writefile($file, $config{srcdir}, readfile(q{t/test2.mdwn}));
340         system "cd $config{srcdir}"
341                 . " && cvs add $file >/dev/null 2>&1";
342         system "cd $config{srcdir}"
343                 . " && cvs commit -m \"$message\" $file >/dev/null";
344
345         @changes = IkiWiki::rcs_recentchanges(3);
346         is_total_number_of_changes(\@changes, 1);
347         is_most_recent_change(\@changes, stripext($file), $message);
348
349         # CVS commits run ikiwiki once for every committed file (!)
350         # - commit_prep alone should fix this
351         # CVS multi-dir commits show only the first dir in recentchanges
352         # - commit_prep might also fix this?
353         # CVS post-commit hook is amped off to avoid locking against itself
354         # - commit_prep probably doesn't fix this... but maybe?
355         # can it assume we're under CVS control? or must it check?
356         # don't worry whether we're called with a number (we always are)
357         # other rcs tests already inspect much of the returned structure
358         # CVS commits say "cvs" and get the right committer
359         # web commits say "web" and get the right committer
360         # - and don't start with "web commit {by,from} "
361         # "nickname" -- can we ever meaningfully set this?
362
363         # prefer log_accum, then cvsps, else die
364         # run the high-level recentchanges tests 2x (once for each method)
365         # - including in other test subs that check recentchanges?
366 }
367
368 sub test_rcs_diff {
369         my @changes = IkiWiki::rcs_recentchanges(3);
370         is_total_number_of_changes(\@changes, 0);
371
372         my $message = "add a UTF-8 and an ASCII file in different dirs";
373         my $file1 = "rcsdiff1/utf8.mdwn";
374         my $file2 = "rcsdiff2/ascii.mdwn";
375         my $contents2 = ''; $contents2 .= "$_. foo\n" for (1..11);
376         can_mkdir($_) for (qw(rcsdiff1 rcsdiff2));
377         writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
378         writefile($file2, $config{srcdir}, $contents2);
379         IkiWiki::rcs_add($_) for ($file1, $file2);
380         IkiWiki::rcs_commit_staged(message => $message);
381
382         # XXX we rely on rcs_recentchanges() to be called first!
383         # XXX or else for no cvsps cache to exist yet...
384         # XXX because rcs_diff() doesn't pass -x (as an optimization)
385         @changes = IkiWiki::rcs_recentchanges(3);
386         is_total_number_of_changes(\@changes, 1);
387
388         my $changeset = 1;
389
390         my $maxlines = undef;
391         my $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
392         like(
393                 $scalar_diffs,
394                 qr/^\+11\. foo$/m,
395                 q{unbounded scalar diffs go all the way to 11},
396         );
397         my @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
398         is(
399                 $array_diffs[$#array_diffs],
400                 "+11. foo\n",
401                 q{unbounded array diffs go all the way to 11},
402         );
403
404         $maxlines = 8;
405         $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
406         unlike(
407                 $scalar_diffs,
408                 qr/^\+11\. foo$/m,
409                 q{bounded scalar diffs don't go all the way to 11},
410         );
411         @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
412         isnt(
413                 $array_diffs[$#array_diffs],
414                 "+11. foo\n",
415                 q{bounded array diffs don't go all the way to 11},
416         );
417         is(
418                 scalar @array_diffs,
419                 $maxlines,
420                 q{bounded array diffs contain expected maximum number of lines},
421         );
422
423         # can it assume we're under CVS control? or must it check?
424 }
425
426 sub test_rcs_getctime {
427         # can it assume we're under CVS control? or must it check?
428         # given a file, find its creation time, else return 0
429         # first implement in the obvious way
430         # then cache
431 }
432
433 sub test_rcs_getmtime {
434         # can it assume we're under CVS control? or must it check?
435         # given a file, find its modification time, else return 0
436         # first implement in the obvious way
437         # then cache
438 }
439
440 sub test_rcs_receive {
441         my $description = q{rcs_receive doesn't make sense for CVS};
442         exists $IkiWiki::hooks{rcs}{rcs_receive}
443                 ? fail($description)
444                 : pass($description);
445 }
446
447 sub test_rcs_preprevert {
448         # can it assume we're under CVS control? or must it check?
449         # given a patchset number, return structure describing what'd happen:
450         # - see doc/plugins/write.mdwn:rcs_receive()
451         # don't forget about attachments
452 }
453
454 sub test_rcs_revert {
455         # test rcs_recentchanges() real darn well
456         # extract read-backwards patchset parser from rcs_recentchanges()
457         # recentchanges: given max, return list of changeset/files/etc.
458         # revert: given changeset ID, return list of file/rev/action
459         #
460         # can it assume we're under CVS control? or must it check?
461         # given a patchset number, stage the revert for rcs_commit_staged()
462         # if commit succeeds, return undef
463         # else, warn and return error message (really? or just non-undef?)
464 }
465
466 sub main {
467         my $test_methods = defined $ENV{TEST_METHOD} 
468                          ? $ENV{TEST_METHOD}
469                          : $default_test_methods;
470
471         _startup($test_methods eq $default_test_methods);
472         _runtests(_get_matching_test_subs($test_methods));
473         _shutdown($test_methods eq $default_test_methods);
474 }
475
476 main();
477
478
479 # INTERNAL SUPPORT ROUTINES
480
481 sub _plan_for_test_more {
482         my $can_plan = shift;
483
484         foreach my $program (@required_programs) {
485                 my $program_path = `which $program`;
486                 chomp $program_path;
487                 return plan(skip_all => "$program not available")
488                         unless -x $program_path;
489         }
490
491         foreach my $module (@required_modules) {
492                 eval qq{use $module};
493                 return plan(skip_all => "$module not available")
494                         if $@;
495         }
496
497         return plan(skip_all => "can't create $dir: $!")
498                 unless mkdir($dir);
499         return plan(skip_all => "can't remove $dir: $!")
500                 unless rmdir($dir);
501
502         return unless $can_plan;
503
504         return plan(tests => $total_tests);
505 }
506
507 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
508
509 use B qw/svref_2object/;
510
511 sub in_package {
512         my ($coderef, $package) = @_;
513         my $cv = svref_2object($coderef);
514         return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
515         return $cv->GV->STASH->NAME eq $package;
516 }
517
518 sub list_module {
519         my $module = shift;
520         no strict 'refs';
521         return grep {
522                 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
523         } keys %{"$module\::"};
524 }
525
526
527 # support for xUnit-style testing, a la Test::Class
528
529 sub _startup {
530         my $can_plan = shift;
531         _plan_for_test_more($can_plan);
532         _generate_test_config();
533 }
534
535 sub _shutdown {
536         my $had_plan = shift;
537         done_testing() unless $had_plan;
538 }
539
540 sub _setup {
541         _generate_test_repo();
542 }
543
544 sub _teardown {
545         system "rm -rf $dir";
546 }
547
548 sub _runtests {
549         my @coderefs = (@_);
550         for (@coderefs) {
551                 _setup();
552                 $_->();
553                 _teardown();
554         }
555 }
556
557 sub _get_matching_test_subs {
558         my $re = shift;
559         no strict 'refs';
560         return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
561 }
562
563 sub _generate_test_config {
564         %config = IkiWiki::defaultconfig();
565         $config{rcs} = "cvs";
566         $config{srcdir} = "$dir/src";
567         $config{cvsrepo} = "$dir/repo";
568         $config{cvspath} = "ikiwiki";
569         IkiWiki::loadplugins();
570         IkiWiki::checkconfig();
571 }
572
573 sub _generate_test_repo {
574         die "can't create $dir: $!"
575                 unless mkdir($dir);
576
577         my $cvs = "cvs -d $config{cvsrepo}";
578         my $dn = ">/dev/null";
579         system "$cvs init $dn";
580         system "mkdir $dir/$config{cvspath} $dn";
581         system "cd $dir/$config{cvspath} && "
582                 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
583         system "rm -rf $dir/$config{cvspath} $dn";
584         system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
585 }
586
587 sub add_and_commit {
588         my ($file, $message, $contents) = @_;
589         writefile($file, $config{srcdir}, $contents);
590         IkiWiki::rcs_add($file);
591         IkiWiki::rcs_commit(
592                 file => $file,
593                 message => $message,
594                 token => "moo",
595         );
596 }
597
598 sub can_mkdir {
599         my $dir = shift;
600         ok(
601                 mkdir($config{srcdir} . "/$dir"),
602                 qq{can mkdir $dir},
603         );
604 }
605
606 sub is_newly_added { _newly_added_or_not(shift, 1) }
607 sub isnt_newly_added { _newly_added_or_not(shift, 0) }
608 sub _newly_added_or_not {
609         my ($file, $expected_new) = @_;
610         my ($func, $word);
611         if ($expected_new) {
612                 $func = \&Test::More::is;
613                 $word = q{is};
614         }
615         else {
616                 $func = \&Test::More::isnt;
617                 $word = q{isn't};
618         }
619         $func->(
620                 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
621                 '1.1',
622                 qq{$file $word newly added to CVS},
623         );
624 }
625
626 sub is_in_keyword_substitution_mode {
627         my ($file, $mode) = @_;
628         is(
629                 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
630                 $mode,
631                 qq{$file is in CVS with expected keyword substitution mode},
632         );
633 }
634
635 sub is_total_number_of_changes {
636         my ($changes, $expected_total) = @_;
637         is(
638                 $#{$changes},
639                 $expected_total - 1,
640                 qq{total commits == $expected_total},
641         );
642 }
643
644 sub is_most_recent_change {
645         my ($changes, $page, $message) = @_;
646         is(
647                 $changes->[0]{message}[0]{"line"},
648                 $message,
649                 q{most recent commit's first message line matches},
650         );
651         is(
652                 $changes->[0]{pages}[0]{"page"},
653                 $page,
654                 q{most recent commit's first pagename matches},
655         );
656 }
657
658 sub stripext {
659         my ($file, $extension) = @_;
660         $extension = '\..+?' unless defined $extension;
661         $file =~ s|$extension$||g;
662         return $file;
663 }