4 use Test::More; my $total_tests = 52;
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, q{* some plain ASCII text});
174 is_newly_added($file);
175 is_in_keyword_substitution_mode($file, undef);
176 @changes = IkiWiki::rcs_recentchanges(3);
177 is_total_number_of_changes(\@changes, 1);
178 is_most_recent_change(\@changes, stripext($file), $message);
180 $message = "add a top-level dir via VCS API";
183 IkiWiki::rcs_add($dir1);
184 # XXX test that the wrapper hangs here without our genwrapper()
185 # XXX test that the wrapper doesn't hang here with it
186 @changes = IkiWiki::rcs_recentchanges(3);
187 is_total_number_of_changes(\@changes, 1); # despite the dir add
193 @changes = IkiWiki::rcs_recentchanges(3);
194 is_total_number_of_changes(\@changes, 1); # dirs aren't tracked
196 $message = "add a non-ASCII (UTF-8) text file in an un-added dir";
197 can_mkdir($_) for (qw(test4 test4/test5));
198 $file = q{test4/test5/test1.mdwn};
199 add_and_commit($file, $message, readfile("t/test1.mdwn"));
200 is_newly_added($file);
201 is_in_keyword_substitution_mode($file, undef);
202 @changes = IkiWiki::rcs_recentchanges(3);
203 is_total_number_of_changes(\@changes, 2);
204 is_most_recent_change(\@changes, stripext($file), $message);
206 $message = "add a binary file in an un-added dir, and commit_staged";
208 $file = q{test6/test7.ico};
209 my $bindata_in = readfile("doc/favicon.ico", 1);
210 my $bindata_out = sub { readfile($config{srcdir} . "/$file", 1) };
211 writefile($file, $config{srcdir}, $bindata_in, 1);
212 is(&$bindata_out(), $bindata_in, q{binary files match before commit});
213 IkiWiki::rcs_add($file);
214 IkiWiki::rcs_commit_staged(message => $message);
215 is_newly_added($file);
216 is_in_keyword_substitution_mode($file, q{-kb});
217 is(&$bindata_out(), $bindata_in, q{binary files match after commit});
218 @changes = IkiWiki::rcs_recentchanges(3);
219 is_total_number_of_changes(\@changes, 3);
220 is_most_recent_change(\@changes, $file, $message);
222 unlink($config{srcdir} . "/$file"),
223 q{can remove file in order to re-fetch it from repo},
225 ok(! -e $config{srcdir} . "/$file", q{really removed file});
226 IkiWiki::rcs_update();
227 is(&$bindata_out(), $bindata_in, q{binary files match after re-fetch});
229 $message = "add a UTF-8 and a binary file in different dirs";
230 my $file1 = "test8/test9.mdwn";
231 my $file2 = "test10/test11.ico";
232 can_mkdir($_) for (qw(test8 test10));
233 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
234 writefile($file2, $config{srcdir}, $bindata_in, 1);
235 IkiWiki::rcs_add($_) for ($file1, $file2);
236 IkiWiki::rcs_commit_staged(message => $message);
237 is_newly_added($_) for ($file1, $file2);
238 is_in_keyword_substitution_mode($file1, undef);
239 is_in_keyword_substitution_mode($file2, '-kb');
240 @changes = IkiWiki::rcs_recentchanges(3);
241 is_total_number_of_changes(\@changes, 3);
242 @changes = IkiWiki::rcs_recentchanges(4);
243 is_total_number_of_changes(\@changes, 4);
244 # XXX test for both files in the commit, and no other files
245 is_most_recent_change(\@changes, $file2, $message);
247 # prevent web edits from attempting to create .../CVS/foo.mdwn
248 # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
249 # unless your "CVS" is something else and we've made it configurable
251 # can it assume we're under CVS control? or must it check?
253 # extract method: filetype-guessing
254 # add a binary file, remove it, add a text file by same name, no -kb?
255 # add a text file, remove it, add a binary file by same name, -kb?
258 sub test_rcs_remove {
259 # can it assume we're under CVS control? or must it check?
260 # remove a top-level file
262 # - inspect recentchanges: one new change, file removed
263 # remove two files (in different dirs)
264 # - rcs_commit_staged
265 # - inspect recentchanges: one new change, both files removed
268 sub test_rcs_rename {
269 # can it assume we're under CVS control? or must it check?
270 # rename a file in the same dir
271 # - rcs_commit_staged
272 # - inspect recentchanges: one new change, one file removed, one added
273 # rename a file into a different dir
274 # - rcs_commit_staged
275 # - inspect recentchanges: one new change, one file removed, one added
276 # rename a file into a not-yet-existing dir
277 # - rcs_commit_staged
278 # - inspect recentchanges: one new change, one file removed, one added
279 # is it safe to use "mv"? what if $dest is somehow outside the wiki?
282 sub test_rcs_recentchanges {
283 my @changes = IkiWiki::rcs_recentchanges(3);
284 is_total_number_of_changes(\@changes, 0);
286 my $message = "Add a page via CVS directly";
287 my $file = q{test2.mdwn};
288 writefile($file, $config{srcdir}, readfile(q{t/test2.mdwn}));
289 system "cd $config{srcdir}"
290 . " && cvs add $file >/dev/null 2>&1";
291 system "cd $config{srcdir}"
292 . " && cvs commit -m \"$message\" $file >/dev/null";
294 @changes = IkiWiki::rcs_recentchanges(3);
295 is_total_number_of_changes(\@changes, 1);
296 is_most_recent_change(\@changes, stripext($file), $message);
298 # CVS commits run ikiwiki once for every committed file (!)
299 # - commit_prep alone should fix this
300 # CVS multi-dir commits show only the first dir in recentchanges
301 # - commit_prep might also fix this?
302 # CVS post-commit hook is amped off to avoid locking against itself
303 # - commit_prep probably doesn't fix this... but maybe?
304 # can it assume we're under CVS control? or must it check?
305 # don't worry whether we're called with a number (we always are)
306 # other rcs tests already inspect much of the returned structure
307 # CVS commits say "cvs" and get the right committer
308 # web commits say "web" and get the right committer
309 # - and don't start with "web commit {by,from} "
310 # "nickname" -- can we ever meaningfully set this?
312 # prefer log_accum, then cvsps, else die
313 # run the high-level recentchanges tests 2x (once for each method)
314 # - including in other test subs that check recentchanges?
318 my @changes = IkiWiki::rcs_recentchanges(3);
319 is_total_number_of_changes(\@changes, 0);
321 my $message = "add a UTF-8 and an ASCII file in different dirs";
322 my $file1 = "rcsdiff1/utf8.mdwn";
323 my $file2 = "rcsdiff2/ascii.mdwn";
324 my $contents2 = ''; $contents2 .= "$_. foo\n" for (1..11);
325 can_mkdir($_) for (qw(rcsdiff1 rcsdiff2));
326 writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
327 writefile($file2, $config{srcdir}, $contents2);
328 IkiWiki::rcs_add($_) for ($file1, $file2);
329 IkiWiki::rcs_commit_staged(message => $message);
331 # XXX we rely on rcs_recentchanges() to be called first!
332 # XXX or else for no cvsps cache to exist yet...
333 # XXX because rcs_diff() doesn't pass -x (as an optimization)
334 @changes = IkiWiki::rcs_recentchanges(3);
335 is_total_number_of_changes(\@changes, 1);
339 my $maxlines = undef;
340 my $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
344 q{unbounded scalar diffs go all the way to 11},
346 my @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
348 $array_diffs[$#array_diffs],
350 q{unbounded array diffs go all the way to 11},
354 $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
358 q{bounded scalar diffs don't go all the way to 11},
360 @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
362 $array_diffs[$#array_diffs],
364 q{bounded array diffs don't go all the way to 11},
369 q{bounded array diffs contain expected maximum number of lines},
372 # can it assume we're under CVS control? or must it check?
375 sub test_rcs_getctime {
376 # can it assume we're under CVS control? or must it check?
377 # given a file, find its creation time, else return 0
378 # first implement in the obvious way
382 sub test_rcs_getmtime {
383 # can it assume we're under CVS control? or must it check?
384 # given a file, find its modification time, else return 0
385 # first implement in the obvious way
389 sub test_rcs_receive {
390 my $description = q{rcs_receive doesn't make sense for CVS};
391 exists $IkiWiki::hooks{rcs}{rcs_receive}
393 : pass($description);
396 sub test_rcs_preprevert {
397 # can it assume we're under CVS control? or must it check?
398 # given a patchset number, return structure describing what'd happen:
399 # - see doc/plugins/write.mdwn:rcs_receive()
400 # don't forget about attachments
403 sub test_rcs_revert {
404 # test rcs_recentchanges() real darn well
405 # extract read-backwards patchset parser from rcs_recentchanges()
406 # recentchanges: given max, return list of changeset/files/etc.
407 # revert: given changeset ID, return list of file/rev/action
409 # can it assume we're under CVS control? or must it check?
410 # given a patchset number, stage the revert for rcs_commit_staged()
411 # if commit succeeds, return undef
412 # else, warn and return error message (really? or just non-undef?)
416 my $test_methods = defined $ENV{TEST_METHOD}
418 : $default_test_methods;
420 _startup($test_methods eq $default_test_methods);
421 _runtests(_get_matching_test_subs($test_methods));
422 _shutdown($test_methods eq $default_test_methods);
428 # INTERNAL SUPPORT ROUTINES
430 sub _plan_for_test_more {
431 my $can_plan = shift;
433 foreach my $program (@required_programs) {
434 my $program_path = `which $program`;
436 return plan(skip_all => "$program not available")
437 unless -x $program_path;
440 foreach my $module (@required_modules) {
441 eval qq{use $module};
442 return plan(skip_all => "$module not available")
446 return plan(skip_all => "can't create $dir: $!")
448 return plan(skip_all => "can't remove $dir: $!")
451 return unless $can_plan;
453 return plan(tests => $total_tests);
456 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
458 use B qw/svref_2object/;
461 my ($coderef, $package) = @_;
462 my $cv = svref_2object($coderef);
463 return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
464 return $cv->GV->STASH->NAME eq $package;
471 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
472 } keys %{"$module\::"};
476 # support for xUnit-style testing, a la Test::Class
479 my $can_plan = shift;
480 _plan_for_test_more($can_plan);
481 _generate_test_config();
485 my $had_plan = shift;
486 done_testing() unless $had_plan;
490 _generate_test_repo();
494 # XXX does srcdir persist between test subs?
495 system "rm -rf $dir";
507 sub _get_matching_test_subs {
510 return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
513 sub _generate_test_config {
514 %config = IkiWiki::defaultconfig();
515 $config{rcs} = "cvs";
516 $config{srcdir} = "$dir/src";
517 $config{cvsrepo} = "$dir/repo";
518 $config{cvspath} = "ikiwiki";
519 IkiWiki::loadplugins();
520 IkiWiki::checkconfig();
523 sub _generate_test_repo {
524 die "can't create $dir: $!"
527 my $cvs = "cvs -d $config{cvsrepo}";
528 my $dn = ">/dev/null";
529 system "$cvs init $dn";
530 system "mkdir $dir/$config{cvspath} $dn";
531 system "cd $dir/$config{cvspath} && "
532 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
533 system "rm -rf $dir/$config{cvspath} $dn";
534 system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
538 my ($file, $message, $contents) = @_;
539 writefile($file, $config{srcdir}, $contents);
540 IkiWiki::rcs_add($file);
551 mkdir($config{srcdir} . "/$dir"),
559 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
561 qq{$file is newly added to CVS},
565 sub is_in_keyword_substitution_mode {
566 my ($file, $mode) = @_;
567 $mode = '(none)' unless defined $mode;
569 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
571 qq{$file is in CVS with expected keyword substitution mode},
575 sub is_total_number_of_changes {
576 my ($changes, $expected_total) = @_;
580 qq{total commits == $expected_total},
584 sub is_most_recent_change {
585 my ($changes, $page, $message) = @_;
587 $changes->[0]{message}[0]{"line"},
589 q{most recent commit's first message line matches},
592 $changes->[0]{pages}[0]{"page"},
594 q{most recent commit's first pagename matches},
599 my ($file, $extension) = @_;
600 $extension = '\..+?' unless defined $extension;
601 $file =~ s|$extension$||g;