4 use Test::More; my $total_tests = 9;
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_add_and_commit {
24 my $message = "Add a page via VCS API";
25 writefile('test1.mdwn', $config{srcdir}, readfile("t/test1.mdwn"));
26 IkiWiki::rcs_add("test1.mdwn");
33 my @changes = IkiWiki::rcs_recentchanges(3);
40 $changes[0]{message}[0]{"line"},
42 q{first line of most recent commit message matches},
45 $changes[0]{pages}[0]{"page"},
47 q{first pagename from most recent commit matches},
50 # prevent web edits from attempting to create .../CVS/foo.mdwn
51 # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
52 # unless your "CVS" is something else and we've made it configurable
53 # how much of the web-edit workflow are we actually testing?
54 # because we want to test comments:
55 # - when the first comment for page.mdwn is added, and page/ is
56 # created to hold the comment, page/ isn't added to CVS control,
57 # so the comment isn't either
58 # - side effect for moderated comments: after approval they
59 # show up normally AND are still pending, too
60 # - comments.pm treats rcs_commit_staged() as returning conflicts?
63 sub test_manual_add_and_commit {
64 my $message = "Add a page via CVS directly";
65 writefile('test2.mdwn', $config{srcdir}, readfile("t/test2.mdwn"));
66 system "cd $config{srcdir}"
67 . " && cvs add test2.mdwn >/dev/null 2>&1";
68 system "cd $config{srcdir}"
69 . " && cvs commit -m \"$message\" test2.mdwn >/dev/null";
71 my @changes = IkiWiki::rcs_recentchanges(3);
78 $changes[0]{message}[0]{"line"},
80 q{first line of most recent commit message matches},
83 $changes[0]{pages}[0]{"page"},
85 q{first pagename from most recent commit matches},
88 # CVS commits run ikiwiki once for every committed file (!)
89 # - commit_prep alone should fix this
90 # CVS multi-dir commits show only the first dir in recentchanges
91 # - commit_prep might also fix this?
92 # CVS post-commit hook is amped off to avoid locking against itself
93 # - commit_prep probably doesn't fix this... but maybe?
96 sub test_chdir_magic {
97 # cvs.pm operations are always occurring inside $config{srcdir}
98 # other ikiwiki operations are occurring wherever, and are unaffected
99 # when are we bothering with "local $CWD" and when aren't we?
103 # inspect "Repository revision" (used in code)
104 # inspect "Sticky Options" (used in tests to verify existence of "-kb")
107 sub test_cvs_run_cvs {
108 # extract the stdout-redirect thing
109 # - prove that it silences stdout
110 # - prove that stderr comes through just fine
111 # prove that when cvs exits nonzero (fail), function exits false
112 # prove that when cvs exits zero (success), function exits true
113 # always pass -f, just in case
114 # steal from git.pm: safe_git(), run_or_{die,cry,non}
115 # - open() instead of system()
116 # always call cvs_run_cvs(), don't ever run 'cvs' directly
119 sub test_cvs_run_cvsps {
120 # parameterize command like run_cvs()
121 # expose config vars for e.g. "--cvs-direct -z 30"
122 # always pass -x (unless proven otherwise)
123 # always pass -b HEAD (configurable like gitmaster_branch?)
126 sub test_cvs_parse_cvsps {
127 # extract method from rcs_recentchanges
128 # document expected changeset format
129 # document expected changeset delimiter
130 # try: cvsps -q -x -p && ls | sort -rn | head -100
131 # - benchmark against current impl (that uses File::ReadBackwards)
134 sub test_cvs_parse_log_accum {
135 # add new, preferred method for rcs_recentchanges to use
136 # teach log_accum to record commits (into transient?)
137 # script cvsps to bootstrap (or replace?) commit history
138 # teach ikiwiki-makerepo to set up log_accum and commit_prep
139 # why are NetBSD commit mails unreliable?
140 # - is it working for CVS commits and failing for web commits?
143 sub test_cvs_is_controlling {
145 # - if srcdir is in CVS, return true
146 # - else, return false
148 # - if dir is in CVS, return true
149 # - else, return false
151 # - is there anything that wants the answer? if so, answer
156 # TESTS FOR GENERAL PLUGIN API CALLS
158 sub test_checkconfig {
159 # undef cvspath, expect "ikiwiki"
160 # define cvspath normally, get it back
161 # define cvspath in a subdir, get it back?
162 # define cvspath with extra slashes, get sanitized version back
163 # - yoink test_extra_path_slashes
164 # undef cvs_wrapper, expect $config{wrappers} same size as before
166 my $initial_cvspath = $config{cvspath};
167 $config{cvspath} = "/ikiwiki//";
168 IkiWiki::checkconfig();
172 q{rcs_recentchanges assumes checkconfig has sanitized cvspath},
177 # anything worth testing?
180 sub test_genwrapper {
181 # testable directly? affects rcs_add, but are we exercising this?
185 # TESTS FOR VCS PLUGIN API CALLS
187 sub test_rcs_update {
188 # can it assume we're under CVS control? or must it check?
189 # anything else worth testing?
192 sub test_rcs_prepedit {
193 # can it assume we're under CVS control? or must it check?
194 # for existing file, returns latest revision in repo
195 # - what's this used for? should it return latest revision in checkout?
196 # for new file, returns empty string
199 sub test_rcs_commit {
200 # can it assume we're under CVS control? or must it check?
201 # if someone else changed the page since rcs_prepedit was called:
202 # - try to merge into our working copy
203 # - if merge succeeds, proceed to commit
204 # - else, return page content with the conflict markers in it
206 # - if success, return undef
207 # - else, revert + return content with the conflict markers in it
208 # git.pm receives "session" param -- useful here?
209 # web commits start with "web commit {by,from} "
210 # seeing File::chdir errors on commit?
213 sub test_rcs_commit_staged {
214 # if commit succeeds, return undef
215 # else, warn and return error message (really? or just non-undef?)
220 my $dir2 = "test4/test5";
222 mkdir($config{srcdir} . "/$dir1"),
225 IkiWiki::rcs_add($dir1);
228 message => "shouldn't happen",
232 # can it assume we're under CVS control? or must it check?
233 # add a top-level text file
235 # - inspect recentchanges: new change, no -kb
236 # add a top-level dir
237 # - test mustn't hang (does it hang if we comment out genwrapper?)
238 # - inspect recentchanges: no new change
240 # - reinspect recentchanges: still no new change
241 # add a text file in that dir
242 # - rcs_commit_staged
243 # - inspect recentchanges: new change, no -kb
244 # add a top-level dir + add a binary file in it
245 # - rcs_commit_staged
246 # - inspect recentchanges: new change, yes -kb
247 # add a top-level dir + subdir + add one text and one binary file in it
248 # - rcs_commit_staged
249 # - inspect recentchanges: one new change, two files, one -kb, one not
251 # extract method: filetype-guessing
252 # add a binary file, remove it, add a text file by same name, no -kb?
253 # add a text file, remove it, add a binary file by same name, -kb?
256 sub test_rcs_remove {
257 # can it assume we're under CVS control? or must it check?
258 # remove a top-level file
260 # - inspect recentchanges: one new change, file removed
261 # remove two files (in different dirs)
262 # - rcs_commit_staged
263 # - inspect recentchanges: one new change, both files removed
266 sub test_rcs_rename {
267 # can it assume we're under CVS control? or must it check?
268 # rename a file in the same dir
269 # - rcs_commit_staged
270 # - inspect recentchanges: one new change, one file removed, one added
271 # rename a file into a different dir
272 # - rcs_commit_staged
273 # - inspect recentchanges: one new change, one file removed, one added
274 # rename a file into a not-yet-existing dir
275 # - rcs_commit_staged
276 # - inspect recentchanges: one new change, one file removed, one added
277 # is it safe to use "mv"? what if $dest is somehow outside the wiki?
280 sub test_rcs_recentchanges {
281 # can it assume we're under CVS control? or must it check?
282 # don't worry whether we're called with a number (we always are)
283 # other rcs tests already inspect much of the returned structure
284 # CVS commits say "cvs" and get the right committer
285 # web commits say "web" and get the right committer
286 # - and don't start with "web commit {by,from} "
287 # "nickname" -- can we ever meaningfully set this?
289 # prefer log_accum, then cvsps, else die
290 # run the high-level recentchanges tests 2x (once for each method)
291 # - including in other test subs that check recentchanges?
295 # can it assume we're under CVS control? or must it check?
296 # in list context, return all lines (with \n), up to $maxlines if set
297 # in scalar context, return the whole diff, up to $maxlines if set
300 sub test_rcs_getctime {
301 # can it assume we're under CVS control? or must it check?
302 # given a file, find its creation time, else return 0
303 # first implement in the obvious way
307 sub test_rcs_getmtime {
308 # can it assume we're under CVS control? or must it check?
309 # given a file, find its modification time, else return 0
310 # first implement in the obvious way
314 sub test_rcs_receive {
315 pass(q{rcs_receive doesn't make sense for CVS});
318 sub test_rcs_preprevert {
319 # can it assume we're under CVS control? or must it check?
320 # given a patchset number, return structure describing what'd happen:
321 # - see doc/plugins/write.mdwn:rcs_receive()
322 # don't forget about attachments
325 sub test_rcs_revert {
326 # can it assume we're under CVS control? or must it check?
327 # given a patchset number, stage the revert for rcs_commit_staged()
328 # if commit succeeds, return undef
329 # else, warn and return error message (really? or just non-undef?)
333 my $test_methods = defined $ENV{TEST_METHOD}
335 : $default_test_methods;
337 _startup($test_methods eq $default_test_methods);
338 _runtests(_get_matching_test_subs($test_methods));
339 _shutdown($test_methods eq $default_test_methods);
345 # INTERNAL SUPPORT ROUTINES
347 sub _plan_for_test_more {
348 my $can_plan = shift;
350 foreach my $program (@required_programs) {
351 my $program_path = `which $program`;
353 return plan(skip_all => "$program not available")
354 unless -x $program_path;
357 foreach my $module (@required_modules) {
358 eval qq{use $module};
359 return plan(skip_all => "$module not available")
363 return plan(skip_all => "can't create $dir: $!")
365 return plan(skip_all => "can't remove $dir: $!")
368 return unless $can_plan;
370 return plan(tests => $total_tests);
373 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
375 use B qw/svref_2object/;
378 my ($coderef, $package) = @_;
379 my $cv = svref_2object($coderef);
380 return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
381 return $cv->GV->STASH->NAME eq $package;
388 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
389 } keys %{"$module\::"};
393 # support for xUnit-style testing, a la Test::Class
396 my $can_plan = shift;
397 _plan_for_test_more($can_plan);
398 _generate_test_config();
402 my $had_plan = shift;
403 done_testing() unless $had_plan;
407 _generate_test_repo();
411 system "rm -rf $dir";
423 sub _get_matching_test_subs {
426 return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
429 sub _generate_test_config {
430 %config = IkiWiki::defaultconfig();
431 $config{rcs} = "cvs";
432 $config{srcdir} = "$dir/src";
433 $config{cvsrepo} = "$dir/repo";
434 $config{cvspath} = "ikiwiki";
435 IkiWiki::loadplugins();
436 IkiWiki::checkconfig();
439 sub _generate_test_repo {
440 die "can't create $dir: $!"
443 my $cvs = "cvs -d $config{cvsrepo}";
444 my $dn = ">/dev/null";
445 system "$cvs init $dn";
446 system "mkdir $dir/$config{cvspath} $dn";
447 system "cd $dir/$config{cvspath} && "
448 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
449 system "rm -rf $dir/$config{cvspath} $dn";
450 system "$cvs co -d $config{srcdir} $config{cvspath} $dn";