4 use Test::More; my $total_tests = 71;
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 # when are we bothering with "local $CWD" and when aren't we?
40 # inspect "Repository revision" (used in code)
41 # inspect "Sticky Options" (used in tests to verify existence of "-kb")
44 sub test_cvs_run_cvs {
45 # extract the stdout-redirect thing
46 # - prove that it silences stdout
47 # - prove that stderr comes through just fine
48 # prove that when cvs exits nonzero (fail), function exits false
49 # prove that when cvs exits zero (success), function exits true
50 # always pass -f, just in case
51 # steal from git.pm: safe_git(), run_or_{die,cry,non}
52 # - open() instead of system()
53 # always call cvs_run_cvs(), don't ever run 'cvs' directly
54 # - for cvs_info(), make it respect wantarray
57 sub test_cvs_run_cvsps {
58 # parameterize command like run_cvs()
59 # expose config vars for e.g. "--cvs-direct -z 30"
60 # always pass -x (unless proven otherwise)
61 # - but diff doesn't! optimization alert
62 # always pass -b HEAD (configurable like gitmaster_branch?)
65 sub test_cvs_parse_cvsps {
66 # extract method from rcs_recentchanges
67 # document expected changeset format
68 # document expected changeset delimiter
69 # try: cvsps -q -x -p && ls | sort -rn | head -100
70 # - benchmark against current impl (that uses File::ReadBackwards)
73 sub test_cvs_parse_log_accum {
74 # add new, preferred method for rcs_recentchanges to use
75 # teach log_accum to record commits (into transient?)
76 # script cvsps to bootstrap (or replace?) commit history
77 # teach ikiwiki-makerepo to set up log_accum and commit_prep
78 # why are NetBSD commit mails unreliable?
79 # - is it working for CVS commits and failing for web commits?
82 sub test_cvs_is_controlling {
84 # - if srcdir is in CVS, return true
85 # - else, return false
87 # - if dir is in CVS, return true
88 # - else, return false
90 # - is there anything that wants the answer? if so, answer
95 # TESTS FOR GENERAL PLUGIN API CALLS
97 sub test_checkconfig {
98 my $default_cvspath = 'ikiwiki';
100 undef $config{cvspath}; IkiWiki::checkconfig();
102 $config{cvspath}, $default_cvspath,
103 q{can provide default cvspath},
106 $config{cvspath} = "/$default_cvspath/"; IkiWiki::checkconfig();
108 $config{cvspath}, $default_cvspath,
109 q{can set typical cvspath and strip well-meaning slashes},
112 $config{cvspath} = "/$default_cvspath//subdir"; IkiWiki::checkconfig();
114 $config{cvspath}, "$default_cvspath/subdir",
115 q{can really sanitize cvspath as assumed by rcs_recentchanges},
118 my $default_num_wrappers = @{$config{wrappers}};
119 undef $config{cvs_wrapper}; IkiWiki::checkconfig();
121 @{$config{wrappers}}, $default_num_wrappers,
122 q{can start with no wrappers configured},
125 $config{cvs_wrapper} = $config{cvsrepo} . "/CVSROOT/post-commit";
126 IkiWiki::checkconfig();
128 @{$config{wrappers}}, ++$default_num_wrappers,
129 q{can add cvs_wrapper},
132 undef $config{cvs_wrapper};
133 $config{cvspath} = $default_cvspath;
134 IkiWiki::checkconfig();
138 # anything worth testing?
141 sub test_genwrapper {
142 # testable directly? affects rcs_add, but are we exercising this?
146 # TESTS FOR VCS PLUGIN API CALLS
148 sub test_rcs_update {
149 # can it assume we're under CVS control? or must it check?
150 # anything else worth testing?
153 sub test_rcs_prepedit {
154 # can it assume we're under CVS control? or must it check?
155 # for existing file, returns latest revision in repo
156 # - what's this used for? should it return latest revision in checkout?
157 # for new file, returns empty string
159 # netbsd web log says "could not open lock file"
160 # XXX does this work right?
161 # how about with un-added dirs in the srcdir?
162 # how about with cvsps.core lying around?
165 sub test_rcs_commit {
166 # can it assume we're under CVS control? or must it check?
167 # if someone else changed the page since rcs_prepedit was called:
168 # - try to merge into our working copy
169 # - if merge succeeds, proceed to commit
170 # - else, return page content with the conflict markers in it
172 # - if success, return undef
173 # - else, revert + return content with the conflict markers in it
174 # git.pm receives "session" param -- useful here?
175 # web commits start with "web commit {by,from} "
177 # XXX commit can fail due to "could not open lock file"
180 sub test_rcs_commit_staged {
181 # if commit succeeds, return undef
182 # else, warn and return error message (really? or just non-undef?)
186 my @changes = IkiWiki::rcs_recentchanges(3);
187 is_total_number_of_changes(\@changes, 0);
189 my $message = "add a top-level ASCII (non-UTF-8) page via VCS API";
190 my $file = q{test0.mdwn};
191 add_and_commit($file, $message, qq{# \$Id\$\n* some plain ASCII text});
192 is_newly_added($file);
193 is_in_keyword_substitution_mode($file, q{-kkv});
195 readfile($config{srcdir} . "/$file"),
196 qr/^# \$Id: $file,v 1\.1 .+\$$/m,
197 q{can expand RCS Id keyword},
199 my $generated_file = $config{destdir} . q{/test0/index.html};
200 ok(-e $generated_file, q{post-commit hook generates content});
202 readfile($generated_file),
203 qr/^<h1>\$Id: $file,v 1\.1 .+\$<\/h1>$/m,
204 q{can htmlize mdwn, including RCS Id},
206 @changes = IkiWiki::rcs_recentchanges(3);
207 is_total_number_of_changes(\@changes, 1);
208 is_most_recent_change(\@changes, stripext($file), $message);
210 $message = "add a top-level dir via VCS API";
213 IkiWiki::rcs_add($dir1);
214 # XXX test that the wrapper hangs here without our genwrapper()
215 # XXX test that the wrapper doesn't hang here with it
216 @changes = IkiWiki::rcs_recentchanges(3);
217 is_total_number_of_changes(\@changes, 1); # despite the dir add
223 @changes = IkiWiki::rcs_recentchanges(3);
224 is_total_number_of_changes(\@changes, 1); # dirs aren't tracked
226 $message = "add a non-ASCII (UTF-8) text file in an un-added dir";
227 can_mkdir($_) for (qw(test4 test4/test5));
228 $file = q{test4/test5/test1.mdwn};
229 add_and_commit($file, $message, readfile("t/test1.mdwn"));
230 is_newly_added($file);
231 is_in_keyword_substitution_mode($file, q{-kkv});
232 @changes = IkiWiki::rcs_recentchanges(3);
233 is_total_number_of_changes(\@changes, 2);
234 is_most_recent_change(\@changes, stripext($file), $message);
236 $message = "add a binary file in an un-added dir, and commit_staged";
238 $file = q{test6/test7.ico};
239 my $bindata_in = readfile("doc/favicon.ico", 1);
240 my $bindata_out = sub { readfile($config{srcdir} . "/$file", 1) };
241 writefile($file, $config{srcdir}, $bindata_in, 1);
242 is(&$bindata_out(), $bindata_in, q{binary files match before commit});
243 IkiWiki::rcs_add($file);
244 IkiWiki::rcs_commit_staged(message => $message);
245 is_newly_added($file);
246 is_in_keyword_substitution_mode($file, q{-kb});
247 is(&$bindata_out(), $bindata_in, q{binary files match after commit});
248 @changes = IkiWiki::rcs_recentchanges(3);
249 is_total_number_of_changes(\@changes, 3);
250 is_most_recent_change(\@changes, $file, $message);
252 unlink($config{srcdir} . "/$file"),
253 q{can remove file in order to re-fetch it from repo},
255 ok(! -e $config{srcdir} . "/$file", q{really removed file});
256 IkiWiki::rcs_update();
257 is(&$bindata_out(), $bindata_in, q{binary files match after re-fetch});
259 $message = "add a UTF-8 and a binary file in different dirs";
260 my $file1 = "test8/test9.mdwn";
261 my $file2 = "test10/test11.ico";
262 can_mkdir($_) for (qw(test8 test10));
263 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
264 writefile($file2, $config{srcdir}, $bindata_in, 1);
265 IkiWiki::rcs_add($_) for ($file1, $file2);
266 IkiWiki::rcs_commit_staged(message => $message);
267 is_newly_added($_) for ($file1, $file2);
268 is_in_keyword_substitution_mode($file1, q{-kkv});
269 is_in_keyword_substitution_mode($file2, q{-kb});
270 @changes = IkiWiki::rcs_recentchanges(3);
271 is_total_number_of_changes(\@changes, 3);
272 @changes = IkiWiki::rcs_recentchanges(4);
273 is_total_number_of_changes(\@changes, 4);
274 # XXX test for both files in the commit, and no other files
275 is_most_recent_change(\@changes, $file2, $message);
277 $message = "remove the UTF-8 and binary files we just added";
278 IkiWiki::rcs_remove($_) for ($file1, $file2);
279 IkiWiki::rcs_commit_staged(message => $message);
280 ok(! -d "$config{srcdir}/test8", q{empty dir pruned by post-commit});
281 ok(! -d "$config{srcdir}/test10", q{empty dir pruned by post-commit});
282 @changes = IkiWiki::rcs_recentchanges(11);
283 is_total_number_of_changes(\@changes, 5);
284 # XXX test for both files in the commit, and no other files
285 is_most_recent_change(\@changes, $file2, $message);
287 $message = "re-add UTF-8 and binary files with names swapped";
288 writefile($file2, $config{srcdir}, readfile('t/test2.mdwn'));
289 writefile($file1, $config{srcdir}, $bindata_in, 1);
290 IkiWiki::rcs_add($_) for ($file1, $file2);
291 IkiWiki::rcs_commit_staged(message => $message);
292 isnt_newly_added($_) for ($file1, $file2);
293 is_in_keyword_substitution_mode($file2, q{-kkv});
294 is_in_keyword_substitution_mode($file1, q{-kb});
295 @changes = IkiWiki::rcs_recentchanges(11);
296 is_total_number_of_changes(\@changes, 6);
297 # XXX test for both files in the commit, and no other files
298 is_most_recent_change(\@changes, $file2, $message);
300 # prevent web edits from attempting to create .../CVS/foo.mdwn
301 # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
302 # unless your "CVS" is something else and we've made it configurable
303 # also want a pre-commit hook for this?
309 # can it assume we're under CVS control? or must it check?
312 sub test_rcs_remove {
313 # can it assume we're under CVS control? or must it check?
314 # remove a top-level file
316 # - inspect recentchanges: one new change, file removed
317 # remove two files (in different dirs)
318 # - rcs_commit_staged
319 # - inspect recentchanges: one new change, both files removed
322 sub test_rcs_rename {
323 # can it assume we're under CVS control? or must it check?
324 # rename a file in the same dir
325 # - rcs_commit_staged
326 # - inspect recentchanges: one new change, one file removed, one added
327 # rename a file into a different dir
328 # - rcs_commit_staged
329 # - inspect recentchanges: one new change, one file removed, one added
330 # rename a file into a not-yet-existing dir
331 # - rcs_commit_staged
332 # - inspect recentchanges: one new change, one file removed, one added
333 # is it safe to use "mv"? what if $dest is somehow outside the wiki?
336 sub test_rcs_recentchanges {
337 my @changes = IkiWiki::rcs_recentchanges(3);
338 is_total_number_of_changes(\@changes, 0);
340 my $message = "Add a page via CVS directly";
341 my $file = q{test2.mdwn};
342 writefile($file, $config{srcdir}, readfile(q{t/test2.mdwn}));
343 system "cd $config{srcdir}"
344 . " && cvs add $file >/dev/null 2>&1";
345 system "cd $config{srcdir}"
346 . " && cvs commit -m \"$message\" $file >/dev/null";
348 @changes = IkiWiki::rcs_recentchanges(3);
349 is_total_number_of_changes(\@changes, 1);
350 is_most_recent_change(\@changes, stripext($file), $message);
352 # CVS commits run ikiwiki once for every committed file (!)
353 # - commit_prep alone should fix this
354 # CVS multi-dir commits show only the first dir in recentchanges
355 # - commit_prep might also fix this?
356 # CVS post-commit hook is amped off to avoid locking against itself
357 # - commit_prep probably doesn't fix this... but maybe?
358 # can it assume we're under CVS control? or must it check?
359 # don't worry whether we're called with a number (we always are)
360 # other rcs tests already inspect much of the returned structure
361 # CVS commits say "cvs" and get the right committer
362 # web commits say "web" and get the right committer
363 # - and don't start with "web commit {by,from} "
364 # "nickname" -- can we ever meaningfully set this?
366 # prefer log_accum, then cvsps, else die
367 # run the high-level recentchanges tests 2x (once for each method)
368 # - including in other test subs that check recentchanges?
372 my @changes = IkiWiki::rcs_recentchanges(3);
373 is_total_number_of_changes(\@changes, 0);
375 my $message = "add a UTF-8 and an ASCII file in different dirs";
376 my $file1 = "rcsdiff1/utf8.mdwn";
377 my $file2 = "rcsdiff2/ascii.mdwn";
378 my $contents2 = ''; $contents2 .= "$_. foo\n" for (1..11);
379 can_mkdir($_) for (qw(rcsdiff1 rcsdiff2));
380 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
381 writefile($file2, $config{srcdir}, $contents2);
382 IkiWiki::rcs_add($_) for ($file1, $file2);
383 IkiWiki::rcs_commit_staged(message => $message);
385 # XXX we rely on rcs_recentchanges() to be called first!
386 # XXX or else for no cvsps cache to exist yet...
387 # XXX because rcs_diff() doesn't pass -x (as an optimization)
388 @changes = IkiWiki::rcs_recentchanges(3);
389 is_total_number_of_changes(\@changes, 1);
393 my $maxlines = undef;
394 my $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
398 q{unbounded scalar diffs go all the way to 11},
400 my @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
402 $array_diffs[$#array_diffs],
404 q{unbounded array diffs go all the way to 11},
408 $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
412 q{bounded scalar diffs don't go all the way to 11},
414 @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
416 $array_diffs[$#array_diffs],
418 q{bounded array diffs don't go all the way to 11},
423 q{bounded array diffs contain expected maximum number of lines},
426 # can it assume we're under CVS control? or must it check?
429 sub test_rcs_getctime {
430 # can it assume we're under CVS control? or must it check?
431 # given a file, find its creation time, else return 0
432 # first implement in the obvious way
436 sub test_rcs_getmtime {
437 # can it assume we're under CVS control? or must it check?
438 # given a file, find its modification time, else return 0
439 # first implement in the obvious way
443 sub test_rcs_receive {
444 my $description = q{rcs_receive doesn't make sense for CVS};
445 exists $IkiWiki::hooks{rcs}{rcs_receive}
447 : pass($description);
450 sub test_rcs_preprevert {
451 # can it assume we're under CVS control? or must it check?
452 # given a patchset number, return structure describing what'd happen:
453 # - see doc/plugins/write.mdwn:rcs_receive()
454 # don't forget about attachments
457 sub test_rcs_revert {
458 # test rcs_recentchanges() real darn well
459 # extract read-backwards patchset parser from rcs_recentchanges()
460 # recentchanges: given max, return list of changeset/files/etc.
461 # revert: given changeset ID, return list of file/rev/action
463 # can it assume we're under CVS control? or must it check?
464 # given a patchset number, stage the revert for rcs_commit_staged()
465 # if commit succeeds, return undef
466 # else, warn and return error message (really? or just non-undef?)
470 my $test_methods = defined $ENV{TEST_METHOD}
472 : $default_test_methods;
474 _startup($test_methods eq $default_test_methods);
475 _runtests(_get_matching_test_subs($test_methods));
476 _shutdown($test_methods eq $default_test_methods);
482 # INTERNAL SUPPORT ROUTINES
484 sub _plan_for_test_more {
485 my $can_plan = shift;
487 foreach my $program (@required_programs) {
488 my $program_path = `which $program`;
490 return plan(skip_all => "$program not available")
491 unless -x $program_path;
494 foreach my $module (@required_modules) {
495 eval qq{use $module};
496 return plan(skip_all => "$module not available")
500 return plan(skip_all => "can't create $dir: $!")
502 return plan(skip_all => "can't remove $dir: $!")
505 return unless $can_plan;
507 return plan(tests => $total_tests);
510 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
512 use B qw/svref_2object/;
515 my ($coderef, $package) = @_;
516 my $cv = svref_2object($coderef);
517 return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
518 return $cv->GV->STASH->NAME eq $package;
525 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
526 } keys %{"$module\::"};
530 # support for xUnit-style testing, a la Test::Class
533 my $can_plan = shift;
534 _plan_for_test_more($can_plan);
535 hook(type => "genwrapper", id => "cvstest", call => \&_wrapper_paths);
536 _generate_test_config();
540 my $had_plan = shift;
541 done_testing() unless $had_plan;
545 _generate_test_repo();
549 system "rm -rf $dir";
561 sub _get_matching_test_subs {
564 return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
567 sub _generate_test_config {
568 %config = IkiWiki::defaultconfig();
569 $config{rcs} = "cvs";
570 $config{srcdir} = "$dir/src";
571 $config{allow_symlinks_before_srcdir} = 1;
572 $config{destdir} = "$dir/dest";
573 $config{cvsrepo} = "$dir/repo";
574 $config{cvspath} = "ikiwiki";
575 use Cwd; $config{templatedir} = getcwd() . '/templates';
576 IkiWiki::loadplugins();
577 IkiWiki::checkconfig();
580 sub _generate_test_repo {
581 die "can't create $dir: $!"
584 my $cvs = "cvs -d $config{cvsrepo}";
585 my $dn = ">/dev/null";
587 system "$cvs init $dn";
588 system "mkdir $dir/$config{cvspath} $dn";
589 system "cd $dir/$config{cvspath} && "
590 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
591 system "rm -rf $dir/$config{cvspath} $dn";
592 system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
594 _generate_and_configure_post_commit_hook();
597 sub _generate_and_configure_post_commit_hook {
598 $config{cvs_wrapper} = $config{cvsrepo} . "/CVSROOT/test-post";
599 $config{wrapper} = $config{cvs_wrapper};
601 require IkiWiki::Wrapper;
604 $IkiWiki::program_to_wrap = 'ikiwiki.out';
605 # XXX substitute its interpreter to Makefile's $(PERL)
606 # XXX best solution: do this to all scripts during build
608 IkiWiki::gen_wrapper();
610 my $cvs = "cvs -d $config{cvsrepo}";
611 my $dn = ">/dev/null";
613 system "mkdir $config{destdir} $dn";
614 system "cd $dir && $cvs co CVSROOT $dn && cd CVSROOT && " .
615 "echo 'DEFAULT $config{cvsrepo}/CVSROOT/test-post %{sVv} &' "
617 . "$cvs commit -m 'test repo setup' $dn && "
618 . "cd .. && rm -rf CVSROOT";
622 my ($file, $message, $contents) = @_;
623 writefile($file, $config{srcdir}, $contents);
624 IkiWiki::rcs_add($file);
635 mkdir($config{srcdir} . "/$dir"),
640 sub is_newly_added { _newly_added_or_not(shift, 1) }
641 sub isnt_newly_added { _newly_added_or_not(shift, 0) }
642 sub _newly_added_or_not {
643 my ($file, $expected_new) = @_;
646 $func = \&Test::More::is;
650 $func = \&Test::More::isnt;
654 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
656 qq{$file $word newly added to CVS},
660 sub is_in_keyword_substitution_mode {
661 my ($file, $mode) = @_;
663 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
665 qq{$file is in CVS with expected keyword substitution mode},
669 sub is_total_number_of_changes {
670 my ($changes, $expected_total) = @_;
674 qq{total commits == $expected_total},
678 sub is_most_recent_change {
679 my ($changes, $page, $message) = @_;
681 $changes->[0]{message}[0]{"line"},
683 q{most recent commit's first message line matches},
686 $changes->[0]{pages}[0]{"page"},
688 q{most recent commit's first pagename matches},
693 my ($file, $extension) = @_;
694 $extension = '\..+?' unless defined $extension;
695 $file =~ s|$extension$||g;
700 return qq{newenviron[i++]="PERL5LIB=$ENV{PERL5LIB}";};