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
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?)
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)
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?
86 sub test_cvs_is_controlling {
88 # - if srcdir is in CVS, return true
89 # - else, return false
91 # - if dir is in CVS, return true
92 # - else, return false
94 # - is there anything that wants the answer? if so, answer
99 # TESTS FOR GENERAL PLUGIN API CALLS
101 sub test_checkconfig {
102 my $default_cvspath = 'ikiwiki';
104 undef $config{cvspath}; IkiWiki::checkconfig();
106 $config{cvspath}, $default_cvspath,
107 q{can provide default cvspath},
110 $config{cvspath} = "/$default_cvspath/"; IkiWiki::checkconfig();
112 $config{cvspath}, $default_cvspath,
113 q{can set typical cvspath and strip well-meaning slashes},
116 $config{cvspath} = "/$default_cvspath//subdir"; IkiWiki::checkconfig();
118 $config{cvspath}, "$default_cvspath/subdir",
119 q{can really sanitize cvspath as assumed by rcs_recentchanges},
122 my $default_num_wrappers = @{$config{wrappers}};
123 undef $config{cvs_wrapper}; IkiWiki::checkconfig();
125 @{$config{wrappers}}, $default_num_wrappers,
126 q{can start with no wrappers configured},
129 $config{cvs_wrapper} = $config{cvsrepo} . "/CVSROOT/post-commit";
130 IkiWiki::checkconfig();
132 @{$config{wrappers}}, ++$default_num_wrappers,
133 q{can add cvs_wrapper},
136 undef $config{cvs_wrapper};
137 $config{cvspath} = $default_cvspath;
138 IkiWiki::checkconfig();
142 # anything worth testing?
145 sub test_genwrapper {
146 # testable directly? affects rcs_add, but are we exercising this?
150 # TESTS FOR VCS PLUGIN API CALLS
152 sub test_rcs_update {
153 # can it assume we're under CVS control? or must it check?
154 # anything else worth testing?
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
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?
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
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?
182 # XXX commit can fail due to "could not open lock file"
185 sub test_rcs_commit_staged {
186 # if commit succeeds, return undef
187 # else, warn and return error message (really? or just non-undef?)
191 my @changes = IkiWiki::rcs_recentchanges(3);
192 is_total_number_of_changes(\@changes, 0);
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});
200 readfile($config{srcdir} . "/$file"),
201 qr/^# \$Id: $file,v 1.1 .+\$$/m,
202 q{can expand RCS Id keyword},
204 @changes = IkiWiki::rcs_recentchanges(3);
205 is_total_number_of_changes(\@changes, 1);
206 is_most_recent_change(\@changes, stripext($file), $message);
208 $message = "add a top-level dir via VCS API";
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
221 @changes = IkiWiki::rcs_recentchanges(3);
222 is_total_number_of_changes(\@changes, 1); # dirs aren't tracked
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);
234 $message = "add a binary file in an un-added dir, and commit_staged";
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);
250 unlink($config{srcdir} . "/$file"),
251 q{can remove file in order to re-fetch it from repo},
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});
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);
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);
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);
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
302 # can it assume we're under CVS control? or must it check?
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?
309 sub test_rcs_remove {
310 # can it assume we're under CVS control? or must it check?
311 # remove a top-level file
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
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?
333 sub test_rcs_recentchanges {
334 my @changes = IkiWiki::rcs_recentchanges(3);
335 is_total_number_of_changes(\@changes, 0);
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";
345 @changes = IkiWiki::rcs_recentchanges(3);
346 is_total_number_of_changes(\@changes, 1);
347 is_most_recent_change(\@changes, stripext($file), $message);
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?
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?
369 my @changes = IkiWiki::rcs_recentchanges(3);
370 is_total_number_of_changes(\@changes, 0);
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);
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);
390 my $maxlines = undef;
391 my $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
395 q{unbounded scalar diffs go all the way to 11},
397 my @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
399 $array_diffs[$#array_diffs],
401 q{unbounded array diffs go all the way to 11},
405 $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
409 q{bounded scalar diffs don't go all the way to 11},
411 @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
413 $array_diffs[$#array_diffs],
415 q{bounded array diffs don't go all the way to 11},
420 q{bounded array diffs contain expected maximum number of lines},
423 # can it assume we're under CVS control? or must it check?
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
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
440 sub test_rcs_receive {
441 my $description = q{rcs_receive doesn't make sense for CVS};
442 exists $IkiWiki::hooks{rcs}{rcs_receive}
444 : pass($description);
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
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
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?)
467 my $test_methods = defined $ENV{TEST_METHOD}
469 : $default_test_methods;
471 _startup($test_methods eq $default_test_methods);
472 _runtests(_get_matching_test_subs($test_methods));
473 _shutdown($test_methods eq $default_test_methods);
479 # INTERNAL SUPPORT ROUTINES
481 sub _plan_for_test_more {
482 my $can_plan = shift;
484 foreach my $program (@required_programs) {
485 my $program_path = `which $program`;
487 return plan(skip_all => "$program not available")
488 unless -x $program_path;
491 foreach my $module (@required_modules) {
492 eval qq{use $module};
493 return plan(skip_all => "$module not available")
497 return plan(skip_all => "can't create $dir: $!")
499 return plan(skip_all => "can't remove $dir: $!")
502 return unless $can_plan;
504 return plan(tests => $total_tests);
507 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
509 use B qw/svref_2object/;
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;
522 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
523 } keys %{"$module\::"};
527 # support for xUnit-style testing, a la Test::Class
530 my $can_plan = shift;
531 _plan_for_test_more($can_plan);
532 _generate_test_config();
536 my $had_plan = shift;
537 done_testing() unless $had_plan;
541 _generate_test_repo();
545 system "rm -rf $dir";
557 sub _get_matching_test_subs {
560 return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
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();
573 sub _generate_test_repo {
574 die "can't create $dir: $!"
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";
588 my ($file, $message, $contents) = @_;
589 writefile($file, $config{srcdir}, $contents);
590 IkiWiki::rcs_add($file);
601 mkdir($config{srcdir} . "/$dir"),
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) = @_;
612 $func = \&Test::More::is;
616 $func = \&Test::More::isnt;
620 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
622 qq{$file $word newly added to CVS},
626 sub is_in_keyword_substitution_mode {
627 my ($file, $mode) = @_;
629 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
631 qq{$file is in CVS with expected keyword substitution mode},
635 sub is_total_number_of_changes {
636 my ($changes, $expected_total) = @_;
640 qq{total commits == $expected_total},
644 sub is_most_recent_change {
645 my ($changes, $page, $message) = @_;
647 $changes->[0]{message}[0]{"line"},
649 q{most recent commit's first message line matches},
652 $changes->[0]{pages}[0]{"page"},
654 q{most recent commit's first pagename matches},
659 my ($file, $extension) = @_;
660 $extension = '\..+?' unless defined $extension;
661 $file =~ s|$extension$||g;