4 use Test::More; my $total_tests = 57;
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, undef);
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, undef);
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, undef);
267 is_in_keyword_substitution_mode($file2, '-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 # prevent web edits from attempting to create .../CVS/foo.mdwn
276 # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
277 # unless your "CVS" is something else and we've made it configurable
279 # can it assume we're under CVS control? or must it check?
281 # extract method: filetype-guessing
282 # add a binary file, remove it, add a text file by same name, no -kb?
283 # add a text file, remove it, add a binary file by same name, -kb?
286 sub test_rcs_remove {
287 # can it assume we're under CVS control? or must it check?
288 # remove a top-level file
290 # - inspect recentchanges: one new change, file removed
291 # remove two files (in different dirs)
292 # - rcs_commit_staged
293 # - inspect recentchanges: one new change, both files removed
296 sub test_rcs_rename {
297 # can it assume we're under CVS control? or must it check?
298 # rename a file in the same dir
299 # - rcs_commit_staged
300 # - inspect recentchanges: one new change, one file removed, one added
301 # rename a file into a different dir
302 # - rcs_commit_staged
303 # - inspect recentchanges: one new change, one file removed, one added
304 # rename a file into a not-yet-existing dir
305 # - rcs_commit_staged
306 # - inspect recentchanges: one new change, one file removed, one added
307 # is it safe to use "mv"? what if $dest is somehow outside the wiki?
310 sub test_rcs_recentchanges {
311 my @changes = IkiWiki::rcs_recentchanges(3);
312 is_total_number_of_changes(\@changes, 0);
314 my $message = "Add a page via CVS directly";
315 my $file = q{test2.mdwn};
316 writefile($file, $config{srcdir}, readfile(q{t/test2.mdwn}));
317 system "cd $config{srcdir}"
318 . " && cvs add $file >/dev/null 2>&1";
319 system "cd $config{srcdir}"
320 . " && cvs commit -m \"$message\" $file >/dev/null";
322 @changes = IkiWiki::rcs_recentchanges(3);
323 is_total_number_of_changes(\@changes, 1);
324 is_most_recent_change(\@changes, stripext($file), $message);
326 # CVS commits run ikiwiki once for every committed file (!)
327 # - commit_prep alone should fix this
328 # CVS multi-dir commits show only the first dir in recentchanges
329 # - commit_prep might also fix this?
330 # CVS post-commit hook is amped off to avoid locking against itself
331 # - commit_prep probably doesn't fix this... but maybe?
332 # can it assume we're under CVS control? or must it check?
333 # don't worry whether we're called with a number (we always are)
334 # other rcs tests already inspect much of the returned structure
335 # CVS commits say "cvs" and get the right committer
336 # web commits say "web" and get the right committer
337 # - and don't start with "web commit {by,from} "
338 # "nickname" -- can we ever meaningfully set this?
340 # prefer log_accum, then cvsps, else die
341 # run the high-level recentchanges tests 2x (once for each method)
342 # - including in other test subs that check recentchanges?
346 my @changes = IkiWiki::rcs_recentchanges(3);
347 is_total_number_of_changes(\@changes, 0);
349 my $message = "add a UTF-8 and an ASCII file in different dirs";
350 my $file1 = "rcsdiff1/utf8.mdwn";
351 my $file2 = "rcsdiff2/ascii.mdwn";
352 my $contents2 = ''; $contents2 .= "$_. foo\n" for (1..11);
353 can_mkdir($_) for (qw(rcsdiff1 rcsdiff2));
354 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
355 writefile($file2, $config{srcdir}, $contents2);
356 IkiWiki::rcs_add($_) for ($file1, $file2);
357 IkiWiki::rcs_commit_staged(message => $message);
359 # XXX we rely on rcs_recentchanges() to be called first!
360 # XXX or else for no cvsps cache to exist yet...
361 # XXX because rcs_diff() doesn't pass -x (as an optimization)
362 @changes = IkiWiki::rcs_recentchanges(3);
363 is_total_number_of_changes(\@changes, 1);
367 my $maxlines = undef;
368 my $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
372 q{unbounded scalar diffs go all the way to 11},
374 my @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
376 $array_diffs[$#array_diffs],
378 q{unbounded array diffs go all the way to 11},
382 $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
386 q{bounded scalar diffs don't go all the way to 11},
388 @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
390 $array_diffs[$#array_diffs],
392 q{bounded array diffs don't go all the way to 11},
397 q{bounded array diffs contain expected maximum number of lines},
400 # can it assume we're under CVS control? or must it check?
403 sub test_rcs_getctime {
404 # can it assume we're under CVS control? or must it check?
405 # given a file, find its creation time, else return 0
406 # first implement in the obvious way
410 sub test_rcs_getmtime {
411 # can it assume we're under CVS control? or must it check?
412 # given a file, find its modification time, else return 0
413 # first implement in the obvious way
417 sub test_rcs_receive {
418 my $description = q{rcs_receive doesn't make sense for CVS};
419 exists $IkiWiki::hooks{rcs}{rcs_receive}
421 : pass($description);
424 sub test_rcs_preprevert {
425 # can it assume we're under CVS control? or must it check?
426 # given a patchset number, return structure describing what'd happen:
427 # - see doc/plugins/write.mdwn:rcs_receive()
428 # don't forget about attachments
431 sub test_rcs_revert {
432 # test rcs_recentchanges() real darn well
433 # extract read-backwards patchset parser from rcs_recentchanges()
434 # recentchanges: given max, return list of changeset/files/etc.
435 # revert: given changeset ID, return list of file/rev/action
437 # can it assume we're under CVS control? or must it check?
438 # given a patchset number, stage the revert for rcs_commit_staged()
439 # if commit succeeds, return undef
440 # else, warn and return error message (really? or just non-undef?)
444 my $test_methods = defined $ENV{TEST_METHOD}
446 : $default_test_methods;
448 _startup($test_methods eq $default_test_methods);
449 _runtests(_get_matching_test_subs($test_methods));
450 _shutdown($test_methods eq $default_test_methods);
456 # INTERNAL SUPPORT ROUTINES
458 sub _plan_for_test_more {
459 my $can_plan = shift;
461 foreach my $program (@required_programs) {
462 my $program_path = `which $program`;
464 return plan(skip_all => "$program not available")
465 unless -x $program_path;
468 foreach my $module (@required_modules) {
469 eval qq{use $module};
470 return plan(skip_all => "$module not available")
474 return plan(skip_all => "can't create $dir: $!")
476 return plan(skip_all => "can't remove $dir: $!")
479 return unless $can_plan;
481 return plan(tests => $total_tests);
484 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
486 use B qw/svref_2object/;
489 my ($coderef, $package) = @_;
490 my $cv = svref_2object($coderef);
491 return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
492 return $cv->GV->STASH->NAME eq $package;
499 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
500 } keys %{"$module\::"};
504 # support for xUnit-style testing, a la Test::Class
507 my $can_plan = shift;
508 _plan_for_test_more($can_plan);
509 _generate_test_config();
513 my $had_plan = shift;
514 done_testing() unless $had_plan;
518 _generate_test_repo();
522 # XXX does srcdir persist between test subs?
523 system "rm -rf $dir";
535 sub _get_matching_test_subs {
538 return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
541 sub _generate_test_config {
542 %config = IkiWiki::defaultconfig();
543 $config{rcs} = "cvs";
544 $config{srcdir} = "$dir/src";
545 $config{cvsrepo} = "$dir/repo";
546 $config{cvspath} = "ikiwiki";
547 IkiWiki::loadplugins();
548 IkiWiki::checkconfig();
551 sub _generate_test_repo {
552 die "can't create $dir: $!"
555 my $cvs = "cvs -d $config{cvsrepo}";
556 my $dn = ">/dev/null";
557 system "$cvs init $dn";
558 system "mkdir $dir/$config{cvspath} $dn";
559 system "cd $dir/$config{cvspath} && "
560 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
561 system "rm -rf $dir/$config{cvspath} $dn";
562 system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
566 my ($file, $message, $contents) = @_;
567 writefile($file, $config{srcdir}, $contents);
568 IkiWiki::rcs_add($file);
579 mkdir($config{srcdir} . "/$dir"),
587 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
589 qq{$file is newly added to CVS},
593 sub is_in_keyword_substitution_mode {
594 my ($file, $mode) = @_;
595 $mode = '(none)' unless defined $mode;
597 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
599 qq{$file is in CVS with expected keyword substitution mode},
603 sub is_total_number_of_changes {
604 my ($changes, $expected_total) = @_;
608 qq{total commits == $expected_total},
612 sub is_most_recent_change {
613 my ($changes, $page, $message) = @_;
615 $changes->[0]{message}[0]{"line"},
617 q{most recent commit's first message line matches},
620 $changes->[0]{pages}[0]{"page"},
622 q{most recent commit's first pagename matches},
627 my ($file, $extension) = @_;
628 $extension = '\..+?' unless defined $extension;
629 $file =~ s|$extension$||g;