4 use Test::More; my $total_tests = 53;
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},
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} "
176 # seeing File::chdir errors on commit?
178 # XXX commit can fail due to "could not open lock file"
181 sub test_rcs_commit_staged {
182 # if commit succeeds, return undef
183 # else, warn and return error message (really? or just non-undef?)
187 my @changes = IkiWiki::rcs_recentchanges(3);
188 is_total_number_of_changes(\@changes, 0);
190 my $message = "add a top-level ASCII (non-UTF-8) page via VCS API";
191 my $file = q{test0.mdwn};
192 add_and_commit($file, $message, qq{# \$Id\$\n* some plain ASCII text});
193 is_newly_added($file);
194 is_in_keyword_substitution_mode($file, undef);
196 readfile($config{srcdir} . "/$file"),
197 qr/^# \$Id: $file,v 1.1 .+\$$/m,
198 q{can expand RCS Id keyword},
200 @changes = IkiWiki::rcs_recentchanges(3);
201 is_total_number_of_changes(\@changes, 1);
202 is_most_recent_change(\@changes, stripext($file), $message);
204 $message = "add a top-level dir via VCS API";
207 IkiWiki::rcs_add($dir1);
208 # XXX test that the wrapper hangs here without our genwrapper()
209 # XXX test that the wrapper doesn't hang here with it
210 @changes = IkiWiki::rcs_recentchanges(3);
211 is_total_number_of_changes(\@changes, 1); # despite the dir add
217 @changes = IkiWiki::rcs_recentchanges(3);
218 is_total_number_of_changes(\@changes, 1); # dirs aren't tracked
220 $message = "add a non-ASCII (UTF-8) text file in an un-added dir";
221 can_mkdir($_) for (qw(test4 test4/test5));
222 $file = q{test4/test5/test1.mdwn};
223 add_and_commit($file, $message, readfile("t/test1.mdwn"));
224 is_newly_added($file);
225 is_in_keyword_substitution_mode($file, undef);
226 @changes = IkiWiki::rcs_recentchanges(3);
227 is_total_number_of_changes(\@changes, 2);
228 is_most_recent_change(\@changes, stripext($file), $message);
230 $message = "add a binary file in an un-added dir, and commit_staged";
232 $file = q{test6/test7.ico};
233 my $bindata_in = readfile("doc/favicon.ico", 1);
234 my $bindata_out = sub { readfile($config{srcdir} . "/$file", 1) };
235 writefile($file, $config{srcdir}, $bindata_in, 1);
236 is(&$bindata_out(), $bindata_in, q{binary files match before commit});
237 IkiWiki::rcs_add($file);
238 IkiWiki::rcs_commit_staged(message => $message);
239 is_newly_added($file);
240 is_in_keyword_substitution_mode($file, q{-kb});
241 is(&$bindata_out(), $bindata_in, q{binary files match after commit});
242 @changes = IkiWiki::rcs_recentchanges(3);
243 is_total_number_of_changes(\@changes, 3);
244 is_most_recent_change(\@changes, $file, $message);
246 unlink($config{srcdir} . "/$file"),
247 q{can remove file in order to re-fetch it from repo},
249 ok(! -e $config{srcdir} . "/$file", q{really removed file});
250 IkiWiki::rcs_update();
251 is(&$bindata_out(), $bindata_in, q{binary files match after re-fetch});
253 $message = "add a UTF-8 and a binary file in different dirs";
254 my $file1 = "test8/test9.mdwn";
255 my $file2 = "test10/test11.ico";
256 can_mkdir($_) for (qw(test8 test10));
257 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
258 writefile($file2, $config{srcdir}, $bindata_in, 1);
259 IkiWiki::rcs_add($_) for ($file1, $file2);
260 IkiWiki::rcs_commit_staged(message => $message);
261 is_newly_added($_) for ($file1, $file2);
262 is_in_keyword_substitution_mode($file1, undef);
263 is_in_keyword_substitution_mode($file2, '-kb');
264 @changes = IkiWiki::rcs_recentchanges(3);
265 is_total_number_of_changes(\@changes, 3);
266 @changes = IkiWiki::rcs_recentchanges(4);
267 is_total_number_of_changes(\@changes, 4);
268 # XXX test for both files in the commit, and no other files
269 is_most_recent_change(\@changes, $file2, $message);
271 # prevent web edits from attempting to create .../CVS/foo.mdwn
272 # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
273 # unless your "CVS" is something else and we've made it configurable
275 # can it assume we're under CVS control? or must it check?
277 # extract method: filetype-guessing
278 # add a binary file, remove it, add a text file by same name, no -kb?
279 # add a text file, remove it, add a binary file by same name, -kb?
282 sub test_rcs_remove {
283 # can it assume we're under CVS control? or must it check?
284 # remove a top-level file
286 # - inspect recentchanges: one new change, file removed
287 # remove two files (in different dirs)
288 # - rcs_commit_staged
289 # - inspect recentchanges: one new change, both files removed
292 sub test_rcs_rename {
293 # can it assume we're under CVS control? or must it check?
294 # rename a file in the same dir
295 # - rcs_commit_staged
296 # - inspect recentchanges: one new change, one file removed, one added
297 # rename a file into a different dir
298 # - rcs_commit_staged
299 # - inspect recentchanges: one new change, one file removed, one added
300 # rename a file into a not-yet-existing dir
301 # - rcs_commit_staged
302 # - inspect recentchanges: one new change, one file removed, one added
303 # is it safe to use "mv"? what if $dest is somehow outside the wiki?
306 sub test_rcs_recentchanges {
307 my @changes = IkiWiki::rcs_recentchanges(3);
308 is_total_number_of_changes(\@changes, 0);
310 my $message = "Add a page via CVS directly";
311 my $file = q{test2.mdwn};
312 writefile($file, $config{srcdir}, readfile(q{t/test2.mdwn}));
313 system "cd $config{srcdir}"
314 . " && cvs add $file >/dev/null 2>&1";
315 system "cd $config{srcdir}"
316 . " && cvs commit -m \"$message\" $file >/dev/null";
318 @changes = IkiWiki::rcs_recentchanges(3);
319 is_total_number_of_changes(\@changes, 1);
320 is_most_recent_change(\@changes, stripext($file), $message);
322 # CVS commits run ikiwiki once for every committed file (!)
323 # - commit_prep alone should fix this
324 # CVS multi-dir commits show only the first dir in recentchanges
325 # - commit_prep might also fix this?
326 # CVS post-commit hook is amped off to avoid locking against itself
327 # - commit_prep probably doesn't fix this... but maybe?
328 # can it assume we're under CVS control? or must it check?
329 # don't worry whether we're called with a number (we always are)
330 # other rcs tests already inspect much of the returned structure
331 # CVS commits say "cvs" and get the right committer
332 # web commits say "web" and get the right committer
333 # - and don't start with "web commit {by,from} "
334 # "nickname" -- can we ever meaningfully set this?
336 # prefer log_accum, then cvsps, else die
337 # run the high-level recentchanges tests 2x (once for each method)
338 # - including in other test subs that check recentchanges?
342 my @changes = IkiWiki::rcs_recentchanges(3);
343 is_total_number_of_changes(\@changes, 0);
345 my $message = "add a UTF-8 and an ASCII file in different dirs";
346 my $file1 = "rcsdiff1/utf8.mdwn";
347 my $file2 = "rcsdiff2/ascii.mdwn";
348 my $contents2 = ''; $contents2 .= "$_. foo\n" for (1..11);
349 can_mkdir($_) for (qw(rcsdiff1 rcsdiff2));
350 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
351 writefile($file2, $config{srcdir}, $contents2);
352 IkiWiki::rcs_add($_) for ($file1, $file2);
353 IkiWiki::rcs_commit_staged(message => $message);
355 # XXX we rely on rcs_recentchanges() to be called first!
356 # XXX or else for no cvsps cache to exist yet...
357 # XXX because rcs_diff() doesn't pass -x (as an optimization)
358 @changes = IkiWiki::rcs_recentchanges(3);
359 is_total_number_of_changes(\@changes, 1);
363 my $maxlines = undef;
364 my $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
368 q{unbounded scalar diffs go all the way to 11},
370 my @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
372 $array_diffs[$#array_diffs],
374 q{unbounded array diffs go all the way to 11},
378 $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
382 q{bounded scalar diffs don't go all the way to 11},
384 @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
386 $array_diffs[$#array_diffs],
388 q{bounded array diffs don't go all the way to 11},
393 q{bounded array diffs contain expected maximum number of lines},
396 # can it assume we're under CVS control? or must it check?
399 sub test_rcs_getctime {
400 # can it assume we're under CVS control? or must it check?
401 # given a file, find its creation time, else return 0
402 # first implement in the obvious way
406 sub test_rcs_getmtime {
407 # can it assume we're under CVS control? or must it check?
408 # given a file, find its modification time, else return 0
409 # first implement in the obvious way
413 sub test_rcs_receive {
414 my $description = q{rcs_receive doesn't make sense for CVS};
415 exists $IkiWiki::hooks{rcs}{rcs_receive}
417 : pass($description);
420 sub test_rcs_preprevert {
421 # can it assume we're under CVS control? or must it check?
422 # given a patchset number, return structure describing what'd happen:
423 # - see doc/plugins/write.mdwn:rcs_receive()
424 # don't forget about attachments
427 sub test_rcs_revert {
428 # test rcs_recentchanges() real darn well
429 # extract read-backwards patchset parser from rcs_recentchanges()
430 # recentchanges: given max, return list of changeset/files/etc.
431 # revert: given changeset ID, return list of file/rev/action
433 # can it assume we're under CVS control? or must it check?
434 # given a patchset number, stage the revert for rcs_commit_staged()
435 # if commit succeeds, return undef
436 # else, warn and return error message (really? or just non-undef?)
440 my $test_methods = defined $ENV{TEST_METHOD}
442 : $default_test_methods;
444 _startup($test_methods eq $default_test_methods);
445 _runtests(_get_matching_test_subs($test_methods));
446 _shutdown($test_methods eq $default_test_methods);
452 # INTERNAL SUPPORT ROUTINES
454 sub _plan_for_test_more {
455 my $can_plan = shift;
457 foreach my $program (@required_programs) {
458 my $program_path = `which $program`;
460 return plan(skip_all => "$program not available")
461 unless -x $program_path;
464 foreach my $module (@required_modules) {
465 eval qq{use $module};
466 return plan(skip_all => "$module not available")
470 return plan(skip_all => "can't create $dir: $!")
472 return plan(skip_all => "can't remove $dir: $!")
475 return unless $can_plan;
477 return plan(tests => $total_tests);
480 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
482 use B qw/svref_2object/;
485 my ($coderef, $package) = @_;
486 my $cv = svref_2object($coderef);
487 return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
488 return $cv->GV->STASH->NAME eq $package;
495 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
496 } keys %{"$module\::"};
500 # support for xUnit-style testing, a la Test::Class
503 my $can_plan = shift;
504 _plan_for_test_more($can_plan);
505 _generate_test_config();
509 my $had_plan = shift;
510 done_testing() unless $had_plan;
514 _generate_test_repo();
518 # XXX does srcdir persist between test subs?
519 system "rm -rf $dir";
531 sub _get_matching_test_subs {
534 return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
537 sub _generate_test_config {
538 %config = IkiWiki::defaultconfig();
539 $config{rcs} = "cvs";
540 $config{srcdir} = "$dir/src";
541 $config{cvsrepo} = "$dir/repo";
542 $config{cvspath} = "ikiwiki";
543 IkiWiki::loadplugins();
544 IkiWiki::checkconfig();
547 sub _generate_test_repo {
548 die "can't create $dir: $!"
551 my $cvs = "cvs -d $config{cvsrepo}";
552 my $dn = ">/dev/null";
553 system "$cvs init $dn";
554 system "mkdir $dir/$config{cvspath} $dn";
555 system "cd $dir/$config{cvspath} && "
556 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
557 system "rm -rf $dir/$config{cvspath} $dn";
558 system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
562 my ($file, $message, $contents) = @_;
563 writefile($file, $config{srcdir}, $contents);
564 IkiWiki::rcs_add($file);
575 mkdir($config{srcdir} . "/$dir"),
583 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
585 qq{$file is newly added to CVS},
589 sub is_in_keyword_substitution_mode {
590 my ($file, $mode) = @_;
591 $mode = '(none)' unless defined $mode;
593 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
595 qq{$file is in CVS with expected keyword substitution mode},
599 sub is_total_number_of_changes {
600 my ($changes, $expected_total) = @_;
604 qq{total commits == $expected_total},
608 sub is_most_recent_change {
609 my ($changes, $page, $message) = @_;
611 $changes->[0]{message}[0]{"line"},
613 q{most recent commit's first message line matches},
616 $changes->[0]{pages}[0]{"page"},
618 q{most recent commit's first pagename matches},
623 my ($file, $extension) = @_;
624 $extension = '\..+?' unless defined $extension;
625 $file =~ s|$extension$||g;