4 use Test::More; my $total_tests = 72;
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);
392 $changes[0]->{pages}->[0]->{diffurl},
394 q{path separators are preserved when UTF-8scaping filename},
399 my $maxlines = undef;
400 my $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
404 q{unbounded scalar diffs go all the way to 11},
406 my @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
408 $array_diffs[$#array_diffs],
410 q{unbounded array diffs go all the way to 11},
414 $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
418 q{bounded scalar diffs don't go all the way to 11},
420 @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
422 $array_diffs[$#array_diffs],
424 q{bounded array diffs don't go all the way to 11},
429 q{bounded array diffs contain expected maximum number of lines},
432 # can it assume we're under CVS control? or must it check?
435 sub test_rcs_getctime {
436 # can it assume we're under CVS control? or must it check?
437 # given a file, find its creation time, else return 0
438 # first implement in the obvious way
442 sub test_rcs_getmtime {
443 # can it assume we're under CVS control? or must it check?
444 # given a file, find its modification time, else return 0
445 # first implement in the obvious way
449 sub test_rcs_receive {
450 my $description = q{rcs_receive doesn't make sense for CVS};
451 exists $IkiWiki::hooks{rcs}{rcs_receive}
453 : pass($description);
456 sub test_rcs_preprevert {
457 # can it assume we're under CVS control? or must it check?
458 # given a patchset number, return structure describing what'd happen:
459 # - see doc/plugins/write.mdwn:rcs_receive()
460 # don't forget about attachments
463 sub test_rcs_revert {
464 # test rcs_recentchanges() real darn well
465 # extract read-backwards patchset parser from rcs_recentchanges()
466 # recentchanges: given max, return list of changeset/files/etc.
467 # revert: given changeset ID, return list of file/rev/action
469 # can it assume we're under CVS control? or must it check?
470 # given a patchset number, stage the revert for rcs_commit_staged()
471 # if commit succeeds, return undef
472 # else, warn and return error message (really? or just non-undef?)
476 my $test_methods = defined $ENV{TEST_METHOD}
478 : $default_test_methods;
480 _startup($test_methods eq $default_test_methods);
481 _runtests(_get_matching_test_subs($test_methods));
482 _shutdown($test_methods eq $default_test_methods);
488 # INTERNAL SUPPORT ROUTINES
490 sub _plan_for_test_more {
491 my $can_plan = shift;
493 foreach my $program (@required_programs) {
494 my $program_path = `which $program`;
496 return plan(skip_all => "$program not available")
497 unless -x $program_path;
500 foreach my $module (@required_modules) {
501 eval qq{use $module};
502 return plan(skip_all => "$module not available")
506 return plan(skip_all => "can't create $dir: $!")
508 return plan(skip_all => "can't remove $dir: $!")
511 return unless $can_plan;
513 return plan(tests => $total_tests);
516 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
518 use B qw/svref_2object/;
521 my ($coderef, $package) = @_;
522 my $cv = svref_2object($coderef);
523 return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
524 return $cv->GV->STASH->NAME eq $package;
531 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
532 } keys %{"$module\::"};
536 # support for xUnit-style testing, a la Test::Class
539 my $can_plan = shift;
540 _plan_for_test_more($can_plan);
541 hook(type => "genwrapper", id => "cvstest", call => \&_wrapper_paths);
542 _generate_test_config();
546 my $had_plan = shift;
547 done_testing() unless $had_plan;
551 _generate_test_repo();
555 system "rm -rf $dir";
567 sub _get_matching_test_subs {
570 return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
573 sub _generate_test_config {
574 %config = IkiWiki::defaultconfig();
575 $config{rcs} = "cvs";
576 $config{srcdir} = "$dir/src";
577 $config{allow_symlinks_before_srcdir} = 1;
578 $config{destdir} = "$dir/dest";
579 $config{cvsrepo} = "$dir/repo";
580 $config{cvspath} = "ikiwiki";
581 use Cwd; $config{templatedir} = getcwd() . '/templates';
582 $config{diffurl} = "/nonexistent/cvsweb/[[file]]";
583 IkiWiki::loadplugins();
584 IkiWiki::checkconfig();
587 sub _generate_test_repo {
588 die "can't create $dir: $!"
591 my $cvs = "cvs -d $config{cvsrepo}";
592 my $dn = ">/dev/null";
594 system "$cvs init $dn";
595 system "mkdir $dir/$config{cvspath} $dn";
596 system "cd $dir/$config{cvspath} && "
597 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
598 system "rm -rf $dir/$config{cvspath} $dn";
599 system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
601 _generate_and_configure_post_commit_hook();
604 sub _generate_and_configure_post_commit_hook {
605 $config{cvs_wrapper} = $config{cvsrepo} . "/CVSROOT/test-post";
606 $config{wrapper} = $config{cvs_wrapper};
608 require IkiWiki::Wrapper;
611 $IkiWiki::program_to_wrap = 'ikiwiki.out';
612 # XXX substitute its interpreter to Makefile's $(PERL)
613 # XXX best solution: do this to all scripts during build
615 IkiWiki::gen_wrapper();
617 my $cvs = "cvs -d $config{cvsrepo}";
618 my $dn = ">/dev/null";
620 system "mkdir $config{destdir} $dn";
621 system "cd $dir && $cvs co CVSROOT $dn && cd CVSROOT && " .
622 "echo 'DEFAULT $config{cvsrepo}/CVSROOT/test-post %{sVv} &' "
624 . "$cvs commit -m 'test repo setup' $dn && "
625 . "cd .. && rm -rf CVSROOT";
629 my ($file, $message, $contents) = @_;
630 writefile($file, $config{srcdir}, $contents);
631 IkiWiki::rcs_add($file);
642 mkdir($config{srcdir} . "/$dir"),
647 sub is_newly_added { _newly_added_or_not(shift, 1) }
648 sub isnt_newly_added { _newly_added_or_not(shift, 0) }
649 sub _newly_added_or_not {
650 my ($file, $expected_new) = @_;
653 $func = \&Test::More::is;
657 $func = \&Test::More::isnt;
661 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
663 qq{$file $word newly added to CVS},
667 sub is_in_keyword_substitution_mode {
668 my ($file, $mode) = @_;
670 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
672 qq{$file is in CVS with expected keyword substitution mode},
676 sub is_total_number_of_changes {
677 my ($changes, $expected_total) = @_;
681 qq{total commits == $expected_total},
685 sub is_most_recent_change {
686 my ($changes, $page, $message) = @_;
688 $changes->[0]{message}[0]{"line"},
690 q{most recent commit's first message line matches},
693 $changes->[0]{pages}[0]{"page"},
695 q{most recent commit's first pagename matches},
700 my ($file, $extension) = @_;
701 $extension = '\..+?' unless defined $extension;
702 $file =~ s|$extension$||g;
707 return qq{newenviron[i++]="PERL5LIB=$ENV{PERL5LIB}";};