4 use Test::More; my $total_tests = 69;
7 my $default_test_methods = '^test_*';
8 my @required_programs = qw(
12 my @required_modules = qw(
19 my $dir = "/tmp/ikiwiki-test-cvs.$$";
21 # TESTS FOR GENERAL META-BEHAVIOR
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?
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.
45 # inspect "Repository revision" (used in code)
46 # inspect "Sticky Options" (used in tests to verify existence of "-kb")
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 # - for cvs_info(), make it respect wantarray
62 sub test_cvs_run_cvsps {
63 # parameterize command like run_cvs()
64 # expose config vars for e.g. "--cvs-direct -z 30"
65 # always pass -x (unless proven otherwise)
66 # - but diff doesn't! optimization alert
67 # always pass -b HEAD (configurable like gitmaster_branch?)
70 sub test_cvs_parse_cvsps {
71 # extract method from rcs_recentchanges
72 # document expected changeset format
73 # document expected changeset delimiter
74 # try: cvsps -q -x -p && ls | sort -rn | head -100
75 # - benchmark against current impl (that uses File::ReadBackwards)
78 sub test_cvs_parse_log_accum {
79 # add new, preferred method for rcs_recentchanges to use
80 # teach log_accum to record commits (into transient?)
81 # script cvsps to bootstrap (or replace?) commit history
82 # teach ikiwiki-makerepo to set up log_accum and commit_prep
83 # why are NetBSD commit mails unreliable?
84 # - is it working for CVS commits and failing for web commits?
87 sub test_cvs_is_controlling {
89 # - if srcdir is in CVS, return true
90 # - else, return false
92 # - if dir is in CVS, return true
93 # - else, return false
95 # - is there anything that wants the answer? if so, answer
100 # TESTS FOR GENERAL PLUGIN API CALLS
102 sub test_checkconfig {
103 my $default_cvspath = 'ikiwiki';
105 undef $config{cvspath}; IkiWiki::checkconfig();
107 $config{cvspath}, $default_cvspath,
108 q{can provide default cvspath},
111 $config{cvspath} = "/$default_cvspath/"; IkiWiki::checkconfig();
113 $config{cvspath}, $default_cvspath,
114 q{can set typical cvspath and strip well-meaning slashes},
117 $config{cvspath} = "/$default_cvspath//subdir"; IkiWiki::checkconfig();
119 $config{cvspath}, "$default_cvspath/subdir",
120 q{can really sanitize cvspath as assumed by rcs_recentchanges},
123 my $default_num_wrappers = @{$config{wrappers}};
124 undef $config{cvs_wrapper}; IkiWiki::checkconfig();
126 @{$config{wrappers}}, $default_num_wrappers,
127 q{can start with no wrappers configured},
130 $config{cvs_wrapper} = $config{cvsrepo} . "/CVSROOT/post-commit";
131 IkiWiki::checkconfig();
133 @{$config{wrappers}}, ++$default_num_wrappers,
134 q{can add cvs_wrapper},
137 undef $config{cvs_wrapper};
138 $config{cvspath} = $default_cvspath;
139 IkiWiki::checkconfig();
143 # anything worth testing?
146 sub test_genwrapper {
147 # testable directly? affects rcs_add, but are we exercising this?
151 # TESTS FOR VCS PLUGIN API CALLS
153 sub test_rcs_update {
154 # can it assume we're under CVS control? or must it check?
155 # anything else worth testing?
158 sub test_rcs_prepedit {
159 # can it assume we're under CVS control? or must it check?
160 # for existing file, returns latest revision in repo
161 # - what's this used for? should it return latest revision in checkout?
162 # for new file, returns empty string
164 # netbsd web log says "could not open lock file"
165 # XXX does this work right?
166 # how about with un-added dirs in the srcdir?
167 # how about with cvsps.core lying around?
170 sub test_rcs_commit {
171 # can it assume we're under CVS control? or must it check?
172 # if someone else changed the page since rcs_prepedit was called:
173 # - try to merge into our working copy
174 # - if merge succeeds, proceed to commit
175 # - else, return page content with the conflict markers in it
177 # - if success, return undef
178 # - else, revert + return content with the conflict markers in it
179 # git.pm receives "session" param -- useful here?
180 # web commits start with "web commit {by,from} "
181 # seeing File::chdir errors on commit?
183 # XXX commit can fail due to "could not open lock file"
186 sub test_rcs_commit_staged {
187 # if commit succeeds, return undef
188 # else, warn and return error message (really? or just non-undef?)
192 my @changes = IkiWiki::rcs_recentchanges(3);
193 is_total_number_of_changes(\@changes, 0);
195 my $message = "add a top-level ASCII (non-UTF-8) page via VCS API";
196 my $file = q{test0.mdwn};
197 add_and_commit($file, $message, qq{# \$Id\$\n* some plain ASCII text});
198 is_newly_added($file);
199 is_in_keyword_substitution_mode($file, q{-kkv});
201 readfile($config{srcdir} . "/$file"),
202 qr/^# \$Id: $file,v 1.1 .+\$$/m,
203 q{can expand RCS Id keyword},
205 @changes = IkiWiki::rcs_recentchanges(3);
206 is_total_number_of_changes(\@changes, 1);
207 is_most_recent_change(\@changes, stripext($file), $message);
209 $message = "add a top-level dir via VCS API";
212 IkiWiki::rcs_add($dir1);
213 # XXX test that the wrapper hangs here without our genwrapper()
214 # XXX test that the wrapper doesn't hang here with it
215 @changes = IkiWiki::rcs_recentchanges(3);
216 is_total_number_of_changes(\@changes, 1); # despite the dir add
222 @changes = IkiWiki::rcs_recentchanges(3);
223 is_total_number_of_changes(\@changes, 1); # dirs aren't tracked
225 $message = "add a non-ASCII (UTF-8) text file in an un-added dir";
226 can_mkdir($_) for (qw(test4 test4/test5));
227 $file = q{test4/test5/test1.mdwn};
228 add_and_commit($file, $message, readfile("t/test1.mdwn"));
229 is_newly_added($file);
230 is_in_keyword_substitution_mode($file, q{-kkv});
231 @changes = IkiWiki::rcs_recentchanges(3);
232 is_total_number_of_changes(\@changes, 2);
233 is_most_recent_change(\@changes, stripext($file), $message);
235 $message = "add a binary file in an un-added dir, and commit_staged";
237 $file = q{test6/test7.ico};
238 my $bindata_in = readfile("doc/favicon.ico", 1);
239 my $bindata_out = sub { readfile($config{srcdir} . "/$file", 1) };
240 writefile($file, $config{srcdir}, $bindata_in, 1);
241 is(&$bindata_out(), $bindata_in, q{binary files match before commit});
242 IkiWiki::rcs_add($file);
243 IkiWiki::rcs_commit_staged(message => $message);
244 is_newly_added($file);
245 is_in_keyword_substitution_mode($file, q{-kb});
246 is(&$bindata_out(), $bindata_in, q{binary files match after commit});
247 @changes = IkiWiki::rcs_recentchanges(3);
248 is_total_number_of_changes(\@changes, 3);
249 is_most_recent_change(\@changes, $file, $message);
251 unlink($config{srcdir} . "/$file"),
252 q{can remove file in order to re-fetch it from repo},
254 ok(! -e $config{srcdir} . "/$file", q{really removed file});
255 IkiWiki::rcs_update();
256 is(&$bindata_out(), $bindata_in, q{binary files match after re-fetch});
258 $message = "add a UTF-8 and a binary file in different dirs";
259 my $file1 = "test8/test9.mdwn";
260 my $file2 = "test10/test11.ico";
261 can_mkdir($_) for (qw(test8 test10));
262 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
263 writefile($file2, $config{srcdir}, $bindata_in, 1);
264 IkiWiki::rcs_add($_) for ($file1, $file2);
265 IkiWiki::rcs_commit_staged(message => $message);
266 is_newly_added($_) for ($file1, $file2);
267 is_in_keyword_substitution_mode($file1, q{-kkv});
268 is_in_keyword_substitution_mode($file2, q{-kb});
269 @changes = IkiWiki::rcs_recentchanges(3);
270 is_total_number_of_changes(\@changes, 3);
271 @changes = IkiWiki::rcs_recentchanges(4);
272 is_total_number_of_changes(\@changes, 4);
273 # XXX test for both files in the commit, and no other files
274 is_most_recent_change(\@changes, $file2, $message);
276 $message = "remove the UTF-8 and binary files we just added";
277 IkiWiki::rcs_remove($_) for ($file1, $file2);
278 IkiWiki::rcs_commit_staged(message => $message);
279 ok(-d "$config{srcdir}/test8", q{empty dir not pruned (1)});
280 @changes = IkiWiki::rcs_recentchanges(11);
281 ok(-d "$config{srcdir}/test8", q{empty dir not pruned (2)});
282 is_total_number_of_changes(\@changes, 5);
283 # XXX test for both files in the commit, and no other files
284 is_most_recent_change(\@changes, $file2, $message);
286 $message = "re-add UTF-8 and binary files with names swapped";
287 writefile($file2, $config{srcdir}, readfile('t/test2.mdwn'));
288 writefile($file1, $config{srcdir}, $bindata_in, 1);
289 IkiWiki::rcs_add($_) for ($file1, $file2);
290 IkiWiki::rcs_commit_staged(message => $message);
291 isnt_newly_added($_) for ($file1, $file2);
292 is_in_keyword_substitution_mode($file2, q{-kkv});
293 is_in_keyword_substitution_mode($file1, q{-kb});
294 @changes = IkiWiki::rcs_recentchanges(11);
295 is_total_number_of_changes(\@changes, 6);
296 # XXX test for both files in the commit, and no other files
297 is_most_recent_change(\@changes, $file2, $message);
299 # prevent web edits from attempting to create .../CVS/foo.mdwn
300 # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
301 # unless your "CVS" is something else and we've made it configurable
302 # also want a pre-commit hook for this?
308 # can it assume we're under CVS control? or must it check?
311 sub test_rcs_remove {
312 # can it assume we're under CVS control? or must it check?
313 # remove a top-level file
315 # - inspect recentchanges: one new change, file removed
316 # remove two files (in different dirs)
317 # - rcs_commit_staged
318 # - inspect recentchanges: one new change, both files removed
321 sub test_rcs_rename {
322 # can it assume we're under CVS control? or must it check?
323 # rename a file in the same dir
324 # - rcs_commit_staged
325 # - inspect recentchanges: one new change, one file removed, one added
326 # rename a file into a different dir
327 # - rcs_commit_staged
328 # - inspect recentchanges: one new change, one file removed, one added
329 # rename a file into a not-yet-existing dir
330 # - rcs_commit_staged
331 # - inspect recentchanges: one new change, one file removed, one added
332 # is it safe to use "mv"? what if $dest is somehow outside the wiki?
335 sub test_rcs_recentchanges {
336 my @changes = IkiWiki::rcs_recentchanges(3);
337 is_total_number_of_changes(\@changes, 0);
339 my $message = "Add a page via CVS directly";
340 my $file = q{test2.mdwn};
341 writefile($file, $config{srcdir}, readfile(q{t/test2.mdwn}));
342 system "cd $config{srcdir}"
343 . " && cvs add $file >/dev/null 2>&1";
344 system "cd $config{srcdir}"
345 . " && cvs commit -m \"$message\" $file >/dev/null";
347 @changes = IkiWiki::rcs_recentchanges(3);
348 is_total_number_of_changes(\@changes, 1);
349 is_most_recent_change(\@changes, stripext($file), $message);
351 # CVS commits run ikiwiki once for every committed file (!)
352 # - commit_prep alone should fix this
353 # CVS multi-dir commits show only the first dir in recentchanges
354 # - commit_prep might also fix this?
355 # CVS post-commit hook is amped off to avoid locking against itself
356 # - commit_prep probably doesn't fix this... but maybe?
357 # can it assume we're under CVS control? or must it check?
358 # don't worry whether we're called with a number (we always are)
359 # other rcs tests already inspect much of the returned structure
360 # CVS commits say "cvs" and get the right committer
361 # web commits say "web" and get the right committer
362 # - and don't start with "web commit {by,from} "
363 # "nickname" -- can we ever meaningfully set this?
365 # prefer log_accum, then cvsps, else die
366 # run the high-level recentchanges tests 2x (once for each method)
367 # - including in other test subs that check recentchanges?
371 my @changes = IkiWiki::rcs_recentchanges(3);
372 is_total_number_of_changes(\@changes, 0);
374 my $message = "add a UTF-8 and an ASCII file in different dirs";
375 my $file1 = "rcsdiff1/utf8.mdwn";
376 my $file2 = "rcsdiff2/ascii.mdwn";
377 my $contents2 = ''; $contents2 .= "$_. foo\n" for (1..11);
378 can_mkdir($_) for (qw(rcsdiff1 rcsdiff2));
379 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
380 writefile($file2, $config{srcdir}, $contents2);
381 IkiWiki::rcs_add($_) for ($file1, $file2);
382 IkiWiki::rcs_commit_staged(message => $message);
384 # XXX we rely on rcs_recentchanges() to be called first!
385 # XXX or else for no cvsps cache to exist yet...
386 # XXX because rcs_diff() doesn't pass -x (as an optimization)
387 @changes = IkiWiki::rcs_recentchanges(3);
388 is_total_number_of_changes(\@changes, 1);
392 my $maxlines = undef;
393 my $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
397 q{unbounded scalar diffs go all the way to 11},
399 my @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
401 $array_diffs[$#array_diffs],
403 q{unbounded array diffs go all the way to 11},
407 $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
411 q{bounded scalar diffs don't go all the way to 11},
413 @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
415 $array_diffs[$#array_diffs],
417 q{bounded array diffs don't go all the way to 11},
422 q{bounded array diffs contain expected maximum number of lines},
425 # can it assume we're under CVS control? or must it check?
428 sub test_rcs_getctime {
429 # can it assume we're under CVS control? or must it check?
430 # given a file, find its creation time, else return 0
431 # first implement in the obvious way
435 sub test_rcs_getmtime {
436 # can it assume we're under CVS control? or must it check?
437 # given a file, find its modification time, else return 0
438 # first implement in the obvious way
442 sub test_rcs_receive {
443 my $description = q{rcs_receive doesn't make sense for CVS};
444 exists $IkiWiki::hooks{rcs}{rcs_receive}
446 : pass($description);
449 sub test_rcs_preprevert {
450 # can it assume we're under CVS control? or must it check?
451 # given a patchset number, return structure describing what'd happen:
452 # - see doc/plugins/write.mdwn:rcs_receive()
453 # don't forget about attachments
456 sub test_rcs_revert {
457 # test rcs_recentchanges() real darn well
458 # extract read-backwards patchset parser from rcs_recentchanges()
459 # recentchanges: given max, return list of changeset/files/etc.
460 # revert: given changeset ID, return list of file/rev/action
462 # can it assume we're under CVS control? or must it check?
463 # given a patchset number, stage the revert for rcs_commit_staged()
464 # if commit succeeds, return undef
465 # else, warn and return error message (really? or just non-undef?)
469 my $test_methods = defined $ENV{TEST_METHOD}
471 : $default_test_methods;
473 _startup($test_methods eq $default_test_methods);
474 _runtests(_get_matching_test_subs($test_methods));
475 _shutdown($test_methods eq $default_test_methods);
481 # INTERNAL SUPPORT ROUTINES
483 sub _plan_for_test_more {
484 my $can_plan = shift;
486 foreach my $program (@required_programs) {
487 my $program_path = `which $program`;
489 return plan(skip_all => "$program not available")
490 unless -x $program_path;
493 foreach my $module (@required_modules) {
494 eval qq{use $module};
495 return plan(skip_all => "$module not available")
499 return plan(skip_all => "can't create $dir: $!")
501 return plan(skip_all => "can't remove $dir: $!")
504 return unless $can_plan;
506 return plan(tests => $total_tests);
509 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
511 use B qw/svref_2object/;
514 my ($coderef, $package) = @_;
515 my $cv = svref_2object($coderef);
516 return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
517 return $cv->GV->STASH->NAME eq $package;
524 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
525 } keys %{"$module\::"};
529 # support for xUnit-style testing, a la Test::Class
532 my $can_plan = shift;
533 _plan_for_test_more($can_plan);
534 _generate_test_config();
538 my $had_plan = shift;
539 done_testing() unless $had_plan;
543 _generate_test_repo();
547 system "rm -rf $dir";
559 sub _get_matching_test_subs {
562 return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
565 sub _generate_test_config {
566 %config = IkiWiki::defaultconfig();
567 $config{rcs} = "cvs";
568 $config{srcdir} = "$dir/src";
569 $config{cvsrepo} = "$dir/repo";
570 $config{cvspath} = "ikiwiki";
571 IkiWiki::loadplugins();
572 IkiWiki::checkconfig();
575 sub _generate_test_repo {
576 die "can't create $dir: $!"
579 my $cvs = "cvs -d $config{cvsrepo}";
580 my $dn = ">/dev/null";
581 system "$cvs init $dn";
582 system "mkdir $dir/$config{cvspath} $dn";
583 system "cd $dir/$config{cvspath} && "
584 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
585 system "rm -rf $dir/$config{cvspath} $dn";
586 system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
590 my ($file, $message, $contents) = @_;
591 writefile($file, $config{srcdir}, $contents);
592 IkiWiki::rcs_add($file);
603 mkdir($config{srcdir} . "/$dir"),
608 sub is_newly_added { _newly_added_or_not(shift, 1) }
609 sub isnt_newly_added { _newly_added_or_not(shift, 0) }
610 sub _newly_added_or_not {
611 my ($file, $expected_new) = @_;
614 $func = \&Test::More::is;
618 $func = \&Test::More::isnt;
622 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
624 qq{$file $word newly added to CVS},
628 sub is_in_keyword_substitution_mode {
629 my ($file, $mode) = @_;
631 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
633 qq{$file is in CVS with expected keyword substitution mode},
637 sub is_total_number_of_changes {
638 my ($changes, $expected_total) = @_;
642 qq{total commits == $expected_total},
646 sub is_most_recent_change {
647 my ($changes, $page, $message) = @_;
649 $changes->[0]{message}[0]{"line"},
651 q{most recent commit's first message line matches},
654 $changes->[0]{pages}[0]{"page"},
656 q{most recent commit's first pagename matches},
661 my ($file, $extension) = @_;
662 $extension = '\..+?' unless defined $extension;
663 $file =~ s|$extension$||g;