4 use Test::More; my $total_tests = 72;
7 my $installed = $ENV{INSTALLED_TESTS};
9 my $default_test_methods = '^test_*';
10 my @required_programs = qw(
14 my @required_modules = qw(
21 my $dir = "/tmp/ikiwiki-test-cvs.$$";
23 # TESTS FOR GENERAL META-BEHAVIOR
25 sub test_web_comments {
26 # how much of the web-edit workflow are we actually testing?
27 # because we want to test comments:
28 # - when the first comment for page.mdwn is added, and page/ is
29 # created to hold the comment, page/ isn't added to CVS control,
30 # so the comment isn't either
31 # - can't reproduce after chmod g+s ikiwiki.cgi (20120204)
32 # - side effect for moderated comments: after approval they
33 # show up normally AND are still pending, too
34 # - comments.pm treats rcs_commit_staged() as returning conflicts?
37 sub test_chdir_magic {
38 # when are we bothering with "local $CWD" and when aren't we?
42 # inspect "Repository revision" (used in code)
43 # inspect "Sticky Options" (used in tests to verify existence of "-kb")
46 sub test_cvs_run_cvs {
47 # extract the stdout-redirect thing
48 # - prove that it silences stdout
49 # - prove that stderr comes through just fine
50 # prove that when cvs exits nonzero (fail), function exits false
51 # prove that when cvs exits zero (success), function exits true
52 # always pass -f, just in case
53 # steal from git.pm: safe_git(), run_or_{die,cry,non}
54 # - open() instead of system()
55 # always call cvs_run_cvs(), don't ever run 'cvs' directly
56 # - for cvs_info(), make it respect wantarray
59 sub test_cvs_run_cvsps {
60 # parameterize command like run_cvs()
61 # expose config vars for e.g. "--cvs-direct -z 30"
62 # always pass -x (unless proven otherwise)
63 # - but diff doesn't! optimization alert
64 # always pass -b HEAD (configurable like gitmaster_branch?)
67 sub test_cvs_parse_cvsps {
68 # extract method from rcs_recentchanges
69 # document expected changeset format
70 # document expected changeset delimiter
71 # try: cvsps -q -x -p && ls | sort -rn | head -100
72 # - benchmark against current impl (that uses File::ReadBackwards)
75 sub test_cvs_parse_log_accum {
76 # add new, preferred method for rcs_recentchanges to use
77 # teach log_accum to record commits (into transient?)
78 # script cvsps to bootstrap (or replace?) commit history
79 # teach ikiwiki-makerepo to set up log_accum and commit_prep
80 # why are NetBSD commit mails unreliable?
81 # - is it working for CVS commits and failing for web commits?
84 sub test_cvs_is_controlling {
86 # - if srcdir is in CVS, return true
87 # - else, return false
89 # - if dir is in CVS, return true
90 # - else, return false
92 # - is there anything that wants the answer? if so, answer
97 # TESTS FOR GENERAL PLUGIN API CALLS
99 sub test_checkconfig {
100 my $default_cvspath = 'ikiwiki';
102 undef $config{cvspath}; IkiWiki::checkconfig();
104 $config{cvspath}, $default_cvspath,
105 q{can provide default cvspath},
108 $config{cvspath} = "/$default_cvspath/"; IkiWiki::checkconfig();
110 $config{cvspath}, $default_cvspath,
111 q{can set typical cvspath and strip well-meaning slashes},
114 $config{cvspath} = "/$default_cvspath//subdir"; IkiWiki::checkconfig();
116 $config{cvspath}, "$default_cvspath/subdir",
117 q{can really sanitize cvspath as assumed by rcs_recentchanges},
120 my $default_num_wrappers = @{$config{wrappers}};
121 undef $config{cvs_wrapper}; IkiWiki::checkconfig();
123 @{$config{wrappers}}, $default_num_wrappers,
124 q{can start with no wrappers configured},
127 $config{cvs_wrapper} = $config{cvsrepo} . "/CVSROOT/post-commit";
128 IkiWiki::checkconfig();
130 @{$config{wrappers}}, ++$default_num_wrappers,
131 q{can add cvs_wrapper},
134 undef $config{cvs_wrapper};
135 $config{cvspath} = $default_cvspath;
136 IkiWiki::checkconfig();
140 # anything worth testing?
143 sub test_genwrapper {
144 # testable directly? affects rcs_add, but are we exercising this?
148 # TESTS FOR VCS PLUGIN API CALLS
150 sub test_rcs_update {
151 # can it assume we're under CVS control? or must it check?
152 # anything else worth testing?
155 sub test_rcs_prepedit {
156 # can it assume we're under CVS control? or must it check?
157 # for existing file, returns latest revision in repo
158 # - what's this used for? should it return latest revision in checkout?
159 # for new file, returns empty string
161 # netbsd web log says "could not open lock file"
162 # XXX does this work right?
163 # how about with un-added dirs in the srcdir?
164 # how about with cvsps.core lying around?
167 sub test_rcs_commit {
168 # can it assume we're under CVS control? or must it check?
169 # if someone else changed the page since rcs_prepedit was called:
170 # - try to merge into our working copy
171 # - if merge succeeds, proceed to commit
172 # - else, return page content with the conflict markers in it
174 # - if success, return undef
175 # - else, revert + return content with the conflict markers in it
176 # git.pm receives "session" param -- useful here?
177 # web commits start with "web commit {by,from} "
179 # XXX commit can fail due to "could not open lock file"
182 sub test_rcs_commit_staged {
183 # if commit succeeds, return undef
184 # else, warn and return error message (really? or just non-undef?)
188 my @changes = IkiWiki::rcs_recentchanges(3);
189 is_total_number_of_changes(\@changes, 0);
191 my $message = "add a top-level ASCII (non-UTF-8) page via VCS API";
192 my $file = q{test0.mdwn};
193 add_and_commit($file, $message, qq{# \$Id\$\n* some plain ASCII text});
194 is_newly_added($file);
195 is_in_keyword_substitution_mode($file, q{-kkv});
197 readfile($config{srcdir} . "/$file"),
198 qr/^# \$Id: $file,v 1\.1 .+\$$/m,
199 q{can expand RCS Id keyword},
201 my $generated_file = $config{destdir} . q{/test0/index.html};
202 ok(-e $generated_file, q{post-commit hook generates content});
204 readfile($generated_file),
205 qr/^<h1>\$Id: $file,v 1\.1 .+\$<\/h1>$/m,
206 q{can htmlize mdwn, including RCS Id},
208 @changes = IkiWiki::rcs_recentchanges(3);
209 is_total_number_of_changes(\@changes, 1);
210 is_most_recent_change(\@changes, stripext($file), $message);
212 $message = "add a top-level dir via VCS API";
215 IkiWiki::rcs_add($dir1);
216 # XXX test that the wrapper hangs here without our genwrapper()
217 # XXX test that the wrapper doesn't hang here with it
218 @changes = IkiWiki::rcs_recentchanges(3);
219 is_total_number_of_changes(\@changes, 1); # despite the dir add
225 @changes = IkiWiki::rcs_recentchanges(3);
226 is_total_number_of_changes(\@changes, 1); # dirs aren't tracked
228 $message = "add a non-ASCII (UTF-8) text file in an un-added dir";
229 can_mkdir($_) for (qw(test4 test4/test5));
230 $file = q{test4/test5/test1.mdwn};
231 add_and_commit($file, $message, readfile("t/test1.mdwn"));
232 is_newly_added($file);
233 is_in_keyword_substitution_mode($file, q{-kkv});
234 @changes = IkiWiki::rcs_recentchanges(3);
235 is_total_number_of_changes(\@changes, 2);
236 is_most_recent_change(\@changes, stripext($file), $message);
238 $message = "add a binary file in an un-added dir, and commit_staged";
240 $file = q{test6/test7.ico};
241 my $bindata_in = readfile("doc/favicon.ico", 1);
242 my $bindata_out = sub { readfile($config{srcdir} . "/$file", 1) };
243 writefile($file, $config{srcdir}, $bindata_in, 1);
244 is(&$bindata_out(), $bindata_in, q{binary files match before commit});
245 IkiWiki::rcs_add($file);
246 IkiWiki::rcs_commit_staged(message => $message);
247 is_newly_added($file);
248 is_in_keyword_substitution_mode($file, q{-kb});
249 is(&$bindata_out(), $bindata_in, q{binary files match after commit});
250 @changes = IkiWiki::rcs_recentchanges(3);
251 is_total_number_of_changes(\@changes, 3);
252 is_most_recent_change(\@changes, $file, $message);
254 unlink($config{srcdir} . "/$file"),
255 q{can remove file in order to re-fetch it from repo},
257 ok(! -e $config{srcdir} . "/$file", q{really removed file});
258 IkiWiki::rcs_update();
259 is(&$bindata_out(), $bindata_in, q{binary files match after re-fetch});
261 $message = "add a UTF-8 and a binary file in different dirs";
262 my $file1 = "test8/test9.mdwn";
263 my $file2 = "test10/test11.ico";
264 can_mkdir($_) for (qw(test8 test10));
265 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
266 writefile($file2, $config{srcdir}, $bindata_in, 1);
267 IkiWiki::rcs_add($_) for ($file1, $file2);
268 IkiWiki::rcs_commit_staged(message => $message);
269 is_newly_added($_) for ($file1, $file2);
270 is_in_keyword_substitution_mode($file1, q{-kkv});
271 is_in_keyword_substitution_mode($file2, q{-kb});
272 @changes = IkiWiki::rcs_recentchanges(3);
273 is_total_number_of_changes(\@changes, 3);
274 @changes = IkiWiki::rcs_recentchanges(4);
275 is_total_number_of_changes(\@changes, 4);
276 # XXX test for both files in the commit, and no other files
277 is_most_recent_change(\@changes, $file2, $message);
279 $message = "remove the UTF-8 and binary files we just added";
280 IkiWiki::rcs_remove($_) for ($file1, $file2);
281 IkiWiki::rcs_commit_staged(message => $message);
282 ok(! -d "$config{srcdir}/test8", q{empty dir pruned by post-commit});
283 ok(! -d "$config{srcdir}/test10", q{empty dir pruned by post-commit});
284 @changes = IkiWiki::rcs_recentchanges(11);
285 is_total_number_of_changes(\@changes, 5);
286 # XXX test for both files in the commit, and no other files
287 is_most_recent_change(\@changes, $file2, $message);
289 $message = "re-add UTF-8 and binary files with names swapped";
290 writefile($file2, $config{srcdir}, readfile('t/test2.mdwn'));
291 writefile($file1, $config{srcdir}, $bindata_in, 1);
292 IkiWiki::rcs_add($_) for ($file1, $file2);
293 IkiWiki::rcs_commit_staged(message => $message);
294 isnt_newly_added($_) for ($file1, $file2);
295 is_in_keyword_substitution_mode($file2, q{-kkv});
296 is_in_keyword_substitution_mode($file1, q{-kb});
297 @changes = IkiWiki::rcs_recentchanges(11);
298 is_total_number_of_changes(\@changes, 6);
299 # XXX test for both files in the commit, and no other files
300 is_most_recent_change(\@changes, $file2, $message);
302 # prevent web edits from attempting to create .../CVS/foo.mdwn
303 # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
304 # unless your "CVS" is something else and we've made it configurable
305 # also want a pre-commit hook for this?
311 # can it assume we're under CVS control? or must it check?
314 sub test_rcs_remove {
315 # can it assume we're under CVS control? or must it check?
316 # remove a top-level file
318 # - inspect recentchanges: one new change, file removed
319 # remove two files (in different dirs)
320 # - rcs_commit_staged
321 # - inspect recentchanges: one new change, both files removed
324 sub test_rcs_rename {
325 # can it assume we're under CVS control? or must it check?
326 # rename a file in the same dir
327 # - rcs_commit_staged
328 # - inspect recentchanges: one new change, one file removed, one added
329 # rename a file into a different dir
330 # - rcs_commit_staged
331 # - inspect recentchanges: one new change, one file removed, one added
332 # rename a file into a not-yet-existing dir
333 # - rcs_commit_staged
334 # - inspect recentchanges: one new change, one file removed, one added
335 # is it safe to use "mv"? what if $dest is somehow outside the wiki?
338 sub test_rcs_recentchanges {
339 my @changes = IkiWiki::rcs_recentchanges(3);
340 is_total_number_of_changes(\@changes, 0);
342 my $message = "Add a page via CVS directly";
343 my $file = q{test2.mdwn};
344 writefile($file, $config{srcdir}, readfile(q{t/test2.mdwn}));
345 system "cd $config{srcdir}"
346 . " && cvs add $file >/dev/null 2>&1";
347 system "cd $config{srcdir}"
348 . " && cvs commit -m \"$message\" $file >/dev/null";
350 @changes = IkiWiki::rcs_recentchanges(3);
351 is_total_number_of_changes(\@changes, 1);
352 is_most_recent_change(\@changes, stripext($file), $message);
354 # CVS commits run ikiwiki once for every committed file (!)
355 # - commit_prep alone should fix this
356 # CVS multi-dir commits show only the first dir in recentchanges
357 # - commit_prep might also fix this?
358 # CVS post-commit hook is amped off to avoid locking against itself
359 # - commit_prep probably doesn't fix this... but maybe?
360 # can it assume we're under CVS control? or must it check?
361 # don't worry whether we're called with a number (we always are)
362 # other rcs tests already inspect much of the returned structure
363 # CVS commits say "cvs" and get the right committer
364 # web commits say "web" and get the right committer
365 # - and don't start with "web commit {by,from} "
366 # "nickname" -- can we ever meaningfully set this?
368 # prefer log_accum, then cvsps, else die
369 # run the high-level recentchanges tests 2x (once for each method)
370 # - including in other test subs that check recentchanges?
374 my @changes = IkiWiki::rcs_recentchanges(3);
375 is_total_number_of_changes(\@changes, 0);
377 my $message = "add a UTF-8 and an ASCII file in different dirs";
378 my $file1 = "rcsdiff1/utf8.mdwn";
379 my $file2 = "rcsdiff2/ascii.mdwn";
380 my $contents2 = ''; $contents2 .= "$_. foo\n" for (1..11);
381 can_mkdir($_) for (qw(rcsdiff1 rcsdiff2));
382 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
383 writefile($file2, $config{srcdir}, $contents2);
384 IkiWiki::rcs_add($_) for ($file1, $file2);
385 IkiWiki::rcs_commit_staged(message => $message);
387 # XXX we rely on rcs_recentchanges() to be called first!
388 # XXX or else for no cvsps cache to exist yet...
389 # XXX because rcs_diff() doesn't pass -x (as an optimization)
390 @changes = IkiWiki::rcs_recentchanges(3);
391 is_total_number_of_changes(\@changes, 1);
394 $changes[0]->{pages}->[0]->{diffurl},
396 q{path separators are preserved when UTF-8scaping filename},
401 my $maxlines = undef;
402 my $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
406 q{unbounded scalar diffs go all the way to 11},
408 my @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
410 $array_diffs[$#array_diffs],
412 q{unbounded array diffs go all the way to 11},
416 $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
420 q{bounded scalar diffs don't go all the way to 11},
422 @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
424 $array_diffs[$#array_diffs],
426 q{bounded array diffs don't go all the way to 11},
431 q{bounded array diffs contain expected maximum number of lines},
434 # can it assume we're under CVS control? or must it check?
437 sub test_rcs_getctime {
438 # can it assume we're under CVS control? or must it check?
439 # given a file, find its creation time, else return 0
440 # first implement in the obvious way
444 sub test_rcs_getmtime {
445 # can it assume we're under CVS control? or must it check?
446 # given a file, find its modification time, else return 0
447 # first implement in the obvious way
451 sub test_rcs_receive {
452 my $description = q{rcs_receive doesn't make sense for CVS};
453 exists $IkiWiki::hooks{rcs}{rcs_receive}
455 : pass($description);
458 sub test_rcs_preprevert {
459 # can it assume we're under CVS control? or must it check?
460 # given a patchset number, return structure describing what'd happen:
461 # - see doc/plugins/write.mdwn:rcs_receive()
462 # don't forget about attachments
465 sub test_rcs_revert {
466 # test rcs_recentchanges() real darn well
467 # extract read-backwards patchset parser from rcs_recentchanges()
468 # recentchanges: given max, return list of changeset/files/etc.
469 # revert: given changeset ID, return list of file/rev/action
471 # can it assume we're under CVS control? or must it check?
472 # given a patchset number, stage the revert for rcs_commit_staged()
473 # if commit succeeds, return undef
474 # else, warn and return error message (really? or just non-undef?)
478 my $test_methods = defined $ENV{TEST_METHOD}
480 : $default_test_methods;
482 _startup($test_methods eq $default_test_methods);
483 _runtests(_get_matching_test_subs($test_methods));
484 _shutdown($test_methods eq $default_test_methods);
490 # INTERNAL SUPPORT ROUTINES
492 sub _plan_for_test_more {
493 my $can_plan = shift;
495 foreach my $program (@required_programs) {
496 my $program_path = `which $program`;
498 return plan(skip_all => "$program not available")
499 unless -x $program_path;
502 foreach my $module (@required_modules) {
503 eval qq{use $module};
504 return plan(skip_all => "$module not available")
508 return plan(skip_all => "can't create $dir: $!")
510 return plan(skip_all => "can't remove $dir: $!")
513 return unless $can_plan;
515 return plan(tests => $total_tests);
518 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
520 use B qw/svref_2object/;
523 my ($coderef, $package) = @_;
524 my $cv = svref_2object($coderef);
525 return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
526 return $cv->GV->STASH->NAME eq $package;
533 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
534 } keys %{"$module\::"};
538 # support for xUnit-style testing, a la Test::Class
541 my $can_plan = shift;
542 _plan_for_test_more($can_plan);
543 hook(type => "genwrapper", id => "cvstest", call => \&_wrapper_paths);
544 _generate_test_config();
548 my $had_plan = shift;
549 done_testing() unless $had_plan;
553 _generate_test_repo();
557 system "rm -rf $dir";
569 sub _get_matching_test_subs {
572 return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
575 sub _generate_test_config {
576 %config = IkiWiki::defaultconfig();
577 $config{rcs} = "cvs";
578 $config{srcdir} = "$dir/src";
579 $config{allow_symlinks_before_srcdir} = 1;
580 $config{destdir} = "$dir/dest";
581 $config{cvsrepo} = "$dir/repo";
582 $config{cvspath} = "ikiwiki";
583 use Cwd; $config{templatedir} = getcwd() . '/templates';
584 $config{diffurl} = "/nonexistent/cvsweb/[[file]]";
585 IkiWiki::loadplugins();
586 IkiWiki::checkconfig();
589 sub _generate_test_repo {
590 die "can't create $dir: $!"
593 my $cvs = "cvs -d $config{cvsrepo}";
594 my $dn = ">/dev/null";
596 system "$cvs init $dn";
597 system "mkdir $dir/$config{cvspath} $dn";
598 system "cd $dir/$config{cvspath} && "
599 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
600 system "rm -rf $dir/$config{cvspath} $dn";
601 system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
603 _generate_and_configure_post_commit_hook();
606 sub _generate_and_configure_post_commit_hook {
607 $config{cvs_wrapper} = $config{cvsrepo} . "/CVSROOT/test-post";
608 $config{wrapper} = $config{cvs_wrapper};
610 require IkiWiki::Wrapper;
612 $IkiWiki::program_to_wrap = `which ikiwiki`;
615 $IkiWiki::program_to_wrap = './ikiwiki.out';
617 # XXX substitute its interpreter to Makefile's $(PERL)
618 # XXX best solution: do this to all scripts during build
619 IkiWiki::gen_wrapper();
621 my $cvs = "cvs -d $config{cvsrepo}";
622 my $dn = ">/dev/null";
624 system "mkdir $config{destdir} $dn";
625 system "cd $dir && $cvs co CVSROOT $dn && cd CVSROOT && " .
626 "echo 'DEFAULT $config{cvsrepo}/CVSROOT/test-post %{sVv} &' "
628 . "$cvs commit -m 'test repo setup' $dn && "
629 . "cd .. && rm -rf CVSROOT";
633 my ($file, $message, $contents) = @_;
634 writefile($file, $config{srcdir}, $contents);
635 IkiWiki::rcs_add($file);
646 mkdir($config{srcdir} . "/$dir"),
651 sub is_newly_added { _newly_added_or_not(shift, 1) }
652 sub isnt_newly_added { _newly_added_or_not(shift, 0) }
653 sub _newly_added_or_not {
654 my ($file, $expected_new) = @_;
657 $func = \&Test::More::is;
661 $func = \&Test::More::isnt;
665 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
667 qq{$file $word newly added to CVS},
671 sub is_in_keyword_substitution_mode {
672 my ($file, $mode) = @_;
674 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
676 qq{$file is in CVS with expected keyword substitution mode},
680 sub is_total_number_of_changes {
681 my ($changes, $expected_total) = @_;
685 qq{total commits == $expected_total},
689 sub is_most_recent_change {
690 my ($changes, $page, $message) = @_;
692 $changes->[0]{message}[0]{"line"},
694 q{most recent commit's first message line matches},
697 $changes->[0]{pages}[0]{"page"},
699 q{most recent commit's first pagename matches},
704 my ($file, $extension) = @_;
705 $extension = '\..+?' unless defined $extension;
706 $file =~ s|$extension$||g;
711 return qq{newenviron[i++]="PERL5LIB=$ENV{PERL5LIB}";};