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 # always pass -b HEAD (configurable like gitmaster_branch?)
68 sub test_cvs_parse_cvsps {
69 # extract method from rcs_recentchanges
70 # document expected changeset format
71 # document expected changeset delimiter
72 # try: cvsps -q -x -p && ls | sort -rn | head -100
73 # - benchmark against current impl (that uses File::ReadBackwards)
76 sub test_cvs_parse_log_accum {
77 # add new, preferred method for rcs_recentchanges to use
78 # teach log_accum to record commits (into transient?)
79 # script cvsps to bootstrap (or replace?) commit history
80 # teach ikiwiki-makerepo to set up log_accum and commit_prep
81 # why are NetBSD commit mails unreliable?
82 # - is it working for CVS commits and failing for web commits?
85 sub test_cvs_is_controlling {
87 # - if srcdir is in CVS, return true
88 # - else, return false
90 # - if dir is in CVS, return true
91 # - else, return false
93 # - is there anything that wants the answer? if so, answer
98 # TESTS FOR GENERAL PLUGIN API CALLS
100 sub test_checkconfig {
101 # undef cvspath, expect "ikiwiki"
102 # define cvspath normally, get it back
103 # define cvspath in a subdir, get it back?
104 # define cvspath with extra slashes, get sanitized version back
105 # - yoink test_extra_path_slashes
106 # undef cvs_wrapper, expect $config{wrappers} same size as before
108 my $initial_cvspath = $config{cvspath};
109 $config{cvspath} = "/ikiwiki//";
110 IkiWiki::checkconfig();
114 q{rcs_recentchanges assumes checkconfig has sanitized cvspath},
119 # anything worth testing?
122 sub test_genwrapper {
123 # testable directly? affects rcs_add, but are we exercising this?
127 # TESTS FOR VCS PLUGIN API CALLS
129 sub test_rcs_update {
130 # can it assume we're under CVS control? or must it check?
131 # anything else worth testing?
134 sub test_rcs_prepedit {
135 # can it assume we're under CVS control? or must it check?
136 # for existing file, returns latest revision in repo
137 # - what's this used for? should it return latest revision in checkout?
138 # for new file, returns empty string
140 # netbsd web log says "could not open lock file"
141 # XXX does this work right?
142 # how about with un-added dirs in the srcdir?
143 # how about with cvsps.core lying around?
146 sub test_rcs_commit {
147 # can it assume we're under CVS control? or must it check?
148 # if someone else changed the page since rcs_prepedit was called:
149 # - try to merge into our working copy
150 # - if merge succeeds, proceed to commit
151 # - else, return page content with the conflict markers in it
153 # - if success, return undef
154 # - else, revert + return content with the conflict markers in it
155 # git.pm receives "session" param -- useful here?
156 # web commits start with "web commit {by,from} "
157 # seeing File::chdir errors on commit?
159 # XXX commit can fail due to "could not open lock file"
162 sub test_rcs_commit_staged {
163 # if commit succeeds, return undef
164 # else, warn and return error message (really? or just non-undef?)
168 my @changes = IkiWiki::rcs_recentchanges(3);
169 is_total_number_of_changes(\@changes, 0);
171 my $message = "add a top-level ASCII (non-UTF-8) page via VCS API";
172 my $file = q{test0.mdwn};
173 add_and_commit($file, $message, qq{# \$Id\$\n* some plain ASCII text});
174 is_newly_added($file);
175 is_in_keyword_substitution_mode($file, undef);
177 readfile($config{srcdir} . "/$file"),
178 qr/^# \$Id: $file,v 1.1 .+\$$/m,
179 q{can expand RCS Id keyword},
181 @changes = IkiWiki::rcs_recentchanges(3);
182 is_total_number_of_changes(\@changes, 1);
183 is_most_recent_change(\@changes, stripext($file), $message);
185 $message = "add a top-level dir via VCS API";
188 IkiWiki::rcs_add($dir1);
189 # XXX test that the wrapper hangs here without our genwrapper()
190 # XXX test that the wrapper doesn't hang here with it
191 @changes = IkiWiki::rcs_recentchanges(3);
192 is_total_number_of_changes(\@changes, 1); # despite the dir add
198 @changes = IkiWiki::rcs_recentchanges(3);
199 is_total_number_of_changes(\@changes, 1); # dirs aren't tracked
201 $message = "add a non-ASCII (UTF-8) text file in an un-added dir";
202 can_mkdir($_) for (qw(test4 test4/test5));
203 $file = q{test4/test5/test1.mdwn};
204 add_and_commit($file, $message, readfile("t/test1.mdwn"));
205 is_newly_added($file);
206 is_in_keyword_substitution_mode($file, undef);
207 @changes = IkiWiki::rcs_recentchanges(3);
208 is_total_number_of_changes(\@changes, 2);
209 is_most_recent_change(\@changes, stripext($file), $message);
211 $message = "add a binary file in an un-added dir, and commit_staged";
213 $file = q{test6/test7.ico};
214 my $bindata_in = readfile("doc/favicon.ico", 1);
215 my $bindata_out = sub { readfile($config{srcdir} . "/$file", 1) };
216 writefile($file, $config{srcdir}, $bindata_in, 1);
217 is(&$bindata_out(), $bindata_in, q{binary files match before commit});
218 IkiWiki::rcs_add($file);
219 IkiWiki::rcs_commit_staged(message => $message);
220 is_newly_added($file);
221 is_in_keyword_substitution_mode($file, q{-kb});
222 is(&$bindata_out(), $bindata_in, q{binary files match after commit});
223 @changes = IkiWiki::rcs_recentchanges(3);
224 is_total_number_of_changes(\@changes, 3);
225 is_most_recent_change(\@changes, $file, $message);
227 unlink($config{srcdir} . "/$file"),
228 q{can remove file in order to re-fetch it from repo},
230 ok(! -e $config{srcdir} . "/$file", q{really removed file});
231 IkiWiki::rcs_update();
232 is(&$bindata_out(), $bindata_in, q{binary files match after re-fetch});
234 $message = "add a UTF-8 and a binary file in different dirs";
235 my $file1 = "test8/test9.mdwn";
236 my $file2 = "test10/test11.ico";
237 can_mkdir($_) for (qw(test8 test10));
238 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
239 writefile($file2, $config{srcdir}, $bindata_in, 1);
240 IkiWiki::rcs_add($_) for ($file1, $file2);
241 IkiWiki::rcs_commit_staged(message => $message);
242 is_newly_added($_) for ($file1, $file2);
243 is_in_keyword_substitution_mode($file1, undef);
244 is_in_keyword_substitution_mode($file2, '-kb');
245 @changes = IkiWiki::rcs_recentchanges(3);
246 is_total_number_of_changes(\@changes, 3);
247 @changes = IkiWiki::rcs_recentchanges(4);
248 is_total_number_of_changes(\@changes, 4);
249 # XXX test for both files in the commit, and no other files
250 is_most_recent_change(\@changes, $file2, $message);
252 # prevent web edits from attempting to create .../CVS/foo.mdwn
253 # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
254 # unless your "CVS" is something else and we've made it configurable
256 # can it assume we're under CVS control? or must it check?
258 # extract method: filetype-guessing
259 # add a binary file, remove it, add a text file by same name, no -kb?
260 # add a text file, remove it, add a binary file by same name, -kb?
263 sub test_rcs_remove {
264 # can it assume we're under CVS control? or must it check?
265 # remove a top-level file
267 # - inspect recentchanges: one new change, file removed
268 # remove two files (in different dirs)
269 # - rcs_commit_staged
270 # - inspect recentchanges: one new change, both files removed
273 sub test_rcs_rename {
274 # can it assume we're under CVS control? or must it check?
275 # rename a file in the same dir
276 # - rcs_commit_staged
277 # - inspect recentchanges: one new change, one file removed, one added
278 # rename a file into a different dir
279 # - rcs_commit_staged
280 # - inspect recentchanges: one new change, one file removed, one added
281 # rename a file into a not-yet-existing dir
282 # - rcs_commit_staged
283 # - inspect recentchanges: one new change, one file removed, one added
284 # is it safe to use "mv"? what if $dest is somehow outside the wiki?
287 sub test_rcs_recentchanges {
288 my @changes = IkiWiki::rcs_recentchanges(3);
289 is_total_number_of_changes(\@changes, 0);
291 my $message = "Add a page via CVS directly";
292 my $file = q{test2.mdwn};
293 writefile($file, $config{srcdir}, readfile(q{t/test2.mdwn}));
294 system "cd $config{srcdir}"
295 . " && cvs add $file >/dev/null 2>&1";
296 system "cd $config{srcdir}"
297 . " && cvs commit -m \"$message\" $file >/dev/null";
299 @changes = IkiWiki::rcs_recentchanges(3);
300 is_total_number_of_changes(\@changes, 1);
301 is_most_recent_change(\@changes, stripext($file), $message);
303 # CVS commits run ikiwiki once for every committed file (!)
304 # - commit_prep alone should fix this
305 # CVS multi-dir commits show only the first dir in recentchanges
306 # - commit_prep might also fix this?
307 # CVS post-commit hook is amped off to avoid locking against itself
308 # - commit_prep probably doesn't fix this... but maybe?
309 # can it assume we're under CVS control? or must it check?
310 # don't worry whether we're called with a number (we always are)
311 # other rcs tests already inspect much of the returned structure
312 # CVS commits say "cvs" and get the right committer
313 # web commits say "web" and get the right committer
314 # - and don't start with "web commit {by,from} "
315 # "nickname" -- can we ever meaningfully set this?
317 # prefer log_accum, then cvsps, else die
318 # run the high-level recentchanges tests 2x (once for each method)
319 # - including in other test subs that check recentchanges?
323 my @changes = IkiWiki::rcs_recentchanges(3);
324 is_total_number_of_changes(\@changes, 0);
326 my $message = "add a UTF-8 and an ASCII file in different dirs";
327 my $file1 = "rcsdiff1/utf8.mdwn";
328 my $file2 = "rcsdiff2/ascii.mdwn";
329 my $contents2 = ''; $contents2 .= "$_. foo\n" for (1..11);
330 can_mkdir($_) for (qw(rcsdiff1 rcsdiff2));
331 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
332 writefile($file2, $config{srcdir}, $contents2);
333 IkiWiki::rcs_add($_) for ($file1, $file2);
334 IkiWiki::rcs_commit_staged(message => $message);
336 # XXX we rely on rcs_recentchanges() to be called first!
337 # XXX or else for no cvsps cache to exist yet...
338 # XXX because rcs_diff() doesn't pass -x (as an optimization)
339 @changes = IkiWiki::rcs_recentchanges(3);
340 is_total_number_of_changes(\@changes, 1);
344 my $maxlines = undef;
345 my $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
349 q{unbounded scalar diffs go all the way to 11},
351 my @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
353 $array_diffs[$#array_diffs],
355 q{unbounded array diffs go all the way to 11},
359 $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
363 q{bounded scalar diffs don't go all the way to 11},
365 @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
367 $array_diffs[$#array_diffs],
369 q{bounded array diffs don't go all the way to 11},
374 q{bounded array diffs contain expected maximum number of lines},
377 # can it assume we're under CVS control? or must it check?
380 sub test_rcs_getctime {
381 # can it assume we're under CVS control? or must it check?
382 # given a file, find its creation time, else return 0
383 # first implement in the obvious way
387 sub test_rcs_getmtime {
388 # can it assume we're under CVS control? or must it check?
389 # given a file, find its modification time, else return 0
390 # first implement in the obvious way
394 sub test_rcs_receive {
395 my $description = q{rcs_receive doesn't make sense for CVS};
396 exists $IkiWiki::hooks{rcs}{rcs_receive}
398 : pass($description);
401 sub test_rcs_preprevert {
402 # can it assume we're under CVS control? or must it check?
403 # given a patchset number, return structure describing what'd happen:
404 # - see doc/plugins/write.mdwn:rcs_receive()
405 # don't forget about attachments
408 sub test_rcs_revert {
409 # test rcs_recentchanges() real darn well
410 # extract read-backwards patchset parser from rcs_recentchanges()
411 # recentchanges: given max, return list of changeset/files/etc.
412 # revert: given changeset ID, return list of file/rev/action
414 # can it assume we're under CVS control? or must it check?
415 # given a patchset number, stage the revert for rcs_commit_staged()
416 # if commit succeeds, return undef
417 # else, warn and return error message (really? or just non-undef?)
421 my $test_methods = defined $ENV{TEST_METHOD}
423 : $default_test_methods;
425 _startup($test_methods eq $default_test_methods);
426 _runtests(_get_matching_test_subs($test_methods));
427 _shutdown($test_methods eq $default_test_methods);
433 # INTERNAL SUPPORT ROUTINES
435 sub _plan_for_test_more {
436 my $can_plan = shift;
438 foreach my $program (@required_programs) {
439 my $program_path = `which $program`;
441 return plan(skip_all => "$program not available")
442 unless -x $program_path;
445 foreach my $module (@required_modules) {
446 eval qq{use $module};
447 return plan(skip_all => "$module not available")
451 return plan(skip_all => "can't create $dir: $!")
453 return plan(skip_all => "can't remove $dir: $!")
456 return unless $can_plan;
458 return plan(tests => $total_tests);
461 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
463 use B qw/svref_2object/;
466 my ($coderef, $package) = @_;
467 my $cv = svref_2object($coderef);
468 return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
469 return $cv->GV->STASH->NAME eq $package;
476 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
477 } keys %{"$module\::"};
481 # support for xUnit-style testing, a la Test::Class
484 my $can_plan = shift;
485 _plan_for_test_more($can_plan);
486 _generate_test_config();
490 my $had_plan = shift;
491 done_testing() unless $had_plan;
495 _generate_test_repo();
499 # XXX does srcdir persist between test subs?
500 system "rm -rf $dir";
512 sub _get_matching_test_subs {
515 return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
518 sub _generate_test_config {
519 %config = IkiWiki::defaultconfig();
520 $config{rcs} = "cvs";
521 $config{srcdir} = "$dir/src";
522 $config{cvsrepo} = "$dir/repo";
523 $config{cvspath} = "ikiwiki";
524 IkiWiki::loadplugins();
525 IkiWiki::checkconfig();
528 sub _generate_test_repo {
529 die "can't create $dir: $!"
532 my $cvs = "cvs -d $config{cvsrepo}";
533 my $dn = ">/dev/null";
534 system "$cvs init $dn";
535 system "mkdir $dir/$config{cvspath} $dn";
536 system "cd $dir/$config{cvspath} && "
537 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
538 system "rm -rf $dir/$config{cvspath} $dn";
539 system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
543 my ($file, $message, $contents) = @_;
544 writefile($file, $config{srcdir}, $contents);
545 IkiWiki::rcs_add($file);
556 mkdir($config{srcdir} . "/$dir"),
564 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
566 qq{$file is newly added to CVS},
570 sub is_in_keyword_substitution_mode {
571 my ($file, $mode) = @_;
572 $mode = '(none)' unless defined $mode;
574 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
576 qq{$file is in CVS with expected keyword substitution mode},
580 sub is_total_number_of_changes {
581 my ($changes, $expected_total) = @_;
585 qq{total commits == $expected_total},
589 sub is_most_recent_change {
590 my ($changes, $page, $message) = @_;
592 $changes->[0]{message}[0]{"line"},
594 q{most recent commit's first message line matches},
597 $changes->[0]{pages}[0]{"page"},
599 q{most recent commit's first pagename matches},
604 my ($file, $extension) = @_;
605 $extension = '\..+?' unless defined $extension;
606 $file =~ s|$extension$||g;