4 use Test::More; my $total_tests = 9;
7 my $default_test_methods = '^test_*';
8 my $dir = "/tmp/ikiwiki-test-cvs.$$";
10 sub _plan_for_test_more {
13 foreach my $program (qw(
17 my $program_path = `which $program`;
19 return plan(skip_all => "$program not available")
20 unless -x $program_path;
23 foreach my $module (qw(
31 return plan(skip_all => "$module not available")
35 return plan(skip_all => "can't create $dir: $!")
37 return plan(skip_all => "can't remove $dir: $!")
40 return unless $can_plan;
42 return plan(tests => $total_tests);
46 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
48 use B qw/svref_2object/;
51 my ($coderef, $package) = @_;
52 my $cv = svref_2object($coderef);
53 return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
54 return $cv->GV->STASH->NAME eq $package;
61 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
62 } keys %{"$module\::"};
66 # support for xUnit-style testing, a la Test::Class
70 _plan_for_test_more($can_plan);
71 _generate_test_config();
76 done_testing() unless $had_plan;
80 _generate_test_repo();
96 sub _get_matching_test_subs {
99 return map { \&{*$_} } grep { /$re/ } list_module('main');
102 sub _generate_test_config {
103 %config = IkiWiki::defaultconfig();
104 $config{rcs} = "cvs";
105 $config{srcdir} = "$dir/src";
106 $config{cvsrepo} = "$dir/repo";
107 $config{cvspath} = "ikiwiki";
108 IkiWiki::loadplugins();
109 IkiWiki::checkconfig();
112 sub _generate_test_repo {
113 die "can't create $dir: $!"
116 my $cvs = "cvs -d $config{cvsrepo}";
117 my $dn = ">/dev/null";
118 system "$cvs init $dn";
119 system "mkdir $dir/$config{cvspath} $dn";
120 system "cd $dir/$config{cvspath} && "
121 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
122 system "rm -rf $dir/$config{cvspath} $dn";
123 system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
127 # tests for general meta-behavior:
129 sub test_web_add_and_commit {
130 my $message = "Add a page via VCS API";
131 writefile('test1.mdwn', $config{srcdir}, readfile("t/test1.mdwn"));
132 IkiWiki::rcs_add("test1.mdwn");
134 file => "test1.mdwn",
139 my @changes = IkiWiki::rcs_recentchanges(3);
146 $changes[0]{message}[0]{"line"},
148 q{first line of most recent commit message matches},
151 $changes[0]{pages}[0]{"page"},
153 q{first pagename from most recent commit matches},
156 # prevent web edits from attempting to create .../CVS/foo.mdwn
157 # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
158 # unless your "CVS" is something else and we've made it configurable
159 # how much of the web-edit workflow are we actually testing?
160 # because we want to test comments:
161 # - when the first comment for page.mdwn is added, and page/ is
162 # created to hold the comment, page/ isn't added to CVS control,
163 # so the comment isn't either
164 # - side effect for moderated comments: after approval they
165 # show up normally AND are still pending, too
166 # - comments.pm treats rcs_commit_staged() as returning conflicts?
169 sub test_manual_add_and_commit {
170 my $message = "Add a page via CVS directly";
171 writefile('test2.mdwn', $config{srcdir}, readfile("t/test2.mdwn"));
172 system "cd $config{srcdir}"
173 . " && cvs add test2.mdwn >/dev/null 2>&1";
174 system "cd $config{srcdir}"
175 . " && cvs commit -m \"$message\" test2.mdwn >/dev/null";
177 my @changes = IkiWiki::rcs_recentchanges(3);
184 $changes[0]{message}[0]{"line"},
186 q{first line of most recent commit message matches},
189 $changes[0]{pages}[0]{"page"},
191 q{first pagename from most recent commit matches},
194 # CVS commits run ikiwiki once for every committed file (!)
195 # - commit_prep alone should fix this
196 # CVS multi-dir commits show only the first dir in recentchanges
197 # - commit_prep might also fix this?
198 # CVS post-commit hook is amped off to avoid locking against itself
199 # - commit_prep probably doesn't fix this... but maybe?
202 sub test_chdir_magic {
203 # cvs.pm operations are always occurring inside $config{srcdir}
204 # other ikiwiki operations are occurring wherever, and are unaffected
205 # when are we bothering with "local $CWD" and when aren't we?
209 # tests for VCS API calls:
211 sub test_genwrapper {
212 # testable directly? affects rcs_add, but are we exercising this?
215 sub test_checkconfig {
216 # undef cvspath, expect "ikiwiki"
217 # define cvspath normally, get it back
218 # define cvspath in a subdir, get it back?
219 # define cvspath with extra slashes, get sanitized version back
220 # - yoink test_extra_path_slashes
221 # undef cvs_wrapper, expect $config{wrappers} same size as before
223 my $initial_cvspath = $config{cvspath};
224 $config{cvspath} = "/ikiwiki//";
225 IkiWiki::checkconfig();
229 q{rcs_recentchanges assumes checkconfig has sanitized cvspath},
234 # anything worth testing?
238 # inspect "Repository revision" (used in code)
239 # inspect "Sticky Options" (used in tests to verify existence of "-kb")
242 sub test_cvs_run_cvs {
243 # extract the stdout-redirect thing
244 # - prove that it silences stdout
245 # - prove that stderr comes through just fine
246 # prove that when cvs exits nonzero (fail), function exits false
247 # prove that when cvs exits zero (success), function exits true
248 # always pass -f, just in case
249 # steal from git.pm: safe_git(), run_or_{die,cry,non}
250 # - open() instead of system()
251 # always call cvs_run_cvs(), don't ever run 'cvs' directly
254 sub test_cvs_run_cvsps {
255 # parameterize command like run_cvs()
256 # expose config vars for e.g. "--cvs-direct -z 30"
257 # always pass -x (unless proven otherwise)
258 # always pass -b HEAD (configurable like gitmaster_branch?)
261 sub test_cvs_parse_cvsps {
262 # extract method from rcs_recentchanges
263 # document expected changeset format
264 # document expected changeset delimiter
265 # try: cvsps -q -x -p && ls | sort -rn | head -100
266 # - benchmark against current impl (that uses File::ReadBackwards)
269 sub test_cvs_parse_log_accum {
270 # add new, preferred method for rcs_recentchanges to use
271 # teach log_accum to record commits (into transient?)
272 # script cvsps to bootstrap (or replace?) commit history
273 # teach ikiwiki-makerepo to set up log_accum and commit_prep
274 # why are NetBSD commit mails unreliable?
275 # - is it working for CVS commits and failing for web commits?
278 sub test_cvs_is_controlling {
280 # - if srcdir is in CVS, return true
281 # - else, return false
283 # - if dir is in CVS, return true
284 # - else, return false
286 # - is there anything that wants the answer? if so, answer
290 sub test_rcs_update {
291 # can it assume we're under CVS control? or must it check?
292 # anything else worth testing?
295 sub test_rcs_prepedit {
296 # can it assume we're under CVS control? or must it check?
297 # for existing file, returns latest revision in repo
298 # - what's this used for? should it return latest revision in checkout?
299 # for new file, returns empty string
302 sub test_rcs_commit {
303 # can it assume we're under CVS control? or must it check?
304 # if someone else changed the page since rcs_prepedit was called:
305 # - try to merge into our working copy
306 # - if merge succeeds, proceed to commit
307 # - else, return page content with the conflict markers in it
309 # - if success, return undef
310 # - else, revert + return content with the conflict markers in it
311 # git.pm receives "session" param -- useful here?
312 # web commits start with "web commit {by,from} "
313 # seeing File::chdir errors on commit?
316 sub test_rcs_commit_staged {
317 # if commit succeeds, return undef
318 # else, warn and return error message (really? or just non-undef?)
323 my $dir2 = "test4/test5";
325 mkdir($config{srcdir} . "/$dir1"),
328 IkiWiki::rcs_add($dir1);
331 message => "shouldn't happen",
335 # can it assume we're under CVS control? or must it check?
336 # add a top-level text file
338 # - inspect recentchanges: new change, no -kb
339 # add a top-level dir
340 # - test mustn't hang (does it hang if we comment out genwrapper?)
341 # - inspect recentchanges: no new change
343 # - reinspect recentchanges: still no new change
344 # add a text file in that dir
345 # - rcs_commit_staged
346 # - inspect recentchanges: new change, no -kb
347 # add a top-level dir + add a binary file in it
348 # - rcs_commit_staged
349 # - inspect recentchanges: new change, yes -kb
350 # add a top-level dir + subdir + add one text and one binary file in it
351 # - rcs_commit_staged
352 # - inspect recentchanges: one new change, two files, one -kb, one not
354 # extract method: filetype-guessing
355 # add a binary file, remove it, add a text file by same name, no -kb?
356 # add a text file, remove it, add a binary file by same name, -kb?
359 sub test_rcs_remove {
360 # can it assume we're under CVS control? or must it check?
361 # remove a top-level file
363 # - inspect recentchanges: one new change, file removed
364 # remove two files (in different dirs)
365 # - rcs_commit_staged
366 # - inspect recentchanges: one new change, both files removed
369 sub test_rcs_rename {
370 # can it assume we're under CVS control? or must it check?
371 # rename a file in the same dir
372 # - rcs_commit_staged
373 # - inspect recentchanges: one new change, one file removed, one added
374 # rename a file into a different dir
375 # - rcs_commit_staged
376 # - inspect recentchanges: one new change, one file removed, one added
377 # rename a file into a not-yet-existing dir
378 # - rcs_commit_staged
379 # - inspect recentchanges: one new change, one file removed, one added
380 # is it safe to use "mv"? what if $dest is somehow outside the wiki?
383 sub test_rcs_recentchanges {
384 # can it assume we're under CVS control? or must it check?
385 # don't worry whether we're called with a number (we always are)
386 # other rcs tests already inspect much of the returned structure
387 # CVS commits say "cvs" and get the right committer
388 # web commits say "web" and get the right committer
389 # - and don't start with "web commit {by,from} "
390 # "nickname" -- can we ever meaningfully set this?
392 # prefer log_accum, then cvsps, else die
393 # run the high-level recentchanges tests 2x (once for each method)
394 # - including in other test subs that check recentchanges?
398 # can it assume we're under CVS control? or must it check?
399 # in list context, return all lines (with \n), up to $maxlines if set
400 # in scalar context, return the whole diff, up to $maxlines if set
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 pass(q{rcs_receive doesn't make sense for CVS});
421 sub test_rcs_preprevert {
422 # can it assume we're under CVS control? or must it check?
423 # given a patchset number, return structure describing what'd happen:
424 # - see doc/plugins/write.mdwn:rcs_receive()
425 # don't forget about attachments
428 sub test_rcs_revert {
429 # can it assume we're under CVS control? or must it check?
430 # given a patchset number, stage the revert for rcs_commit_staged()
431 # if commit succeeds, return undef
432 # else, warn and return error message (really? or just non-undef?)
436 my $test_methods = defined $ENV{TEST_METHOD}
438 : $default_test_methods;
440 _startup($test_methods eq $default_test_methods);
441 _runtests(_get_matching_test_subs($test_methods));
442 _shutdown($test_methods eq $default_test_methods);