4 use Test::More; my $total_tests = 9;
7 my $default_test_methods = '^test_*';
8 my $dir = "/tmp/ikiwiki-test-cvs.$$";
10 # TESTS FOR GENERAL META-BEHAVIOR
12 sub test_web_add_and_commit {
13 my $message = "Add a page via VCS API";
14 writefile('test1.mdwn', $config{srcdir}, readfile("t/test1.mdwn"));
15 IkiWiki::rcs_add("test1.mdwn");
22 my @changes = IkiWiki::rcs_recentchanges(3);
29 $changes[0]{message}[0]{"line"},
31 q{first line of most recent commit message matches},
34 $changes[0]{pages}[0]{"page"},
36 q{first pagename from most recent commit matches},
39 # prevent web edits from attempting to create .../CVS/foo.mdwn
40 # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
41 # unless your "CVS" is something else and we've made it configurable
42 # how much of the web-edit workflow are we actually testing?
43 # because we want to test comments:
44 # - when the first comment for page.mdwn is added, and page/ is
45 # created to hold the comment, page/ isn't added to CVS control,
46 # so the comment isn't either
47 # - side effect for moderated comments: after approval they
48 # show up normally AND are still pending, too
49 # - comments.pm treats rcs_commit_staged() as returning conflicts?
52 sub test_manual_add_and_commit {
53 my $message = "Add a page via CVS directly";
54 writefile('test2.mdwn', $config{srcdir}, readfile("t/test2.mdwn"));
55 system "cd $config{srcdir}"
56 . " && cvs add test2.mdwn >/dev/null 2>&1";
57 system "cd $config{srcdir}"
58 . " && cvs commit -m \"$message\" test2.mdwn >/dev/null";
60 my @changes = IkiWiki::rcs_recentchanges(3);
67 $changes[0]{message}[0]{"line"},
69 q{first line of most recent commit message matches},
72 $changes[0]{pages}[0]{"page"},
74 q{first pagename from most recent commit matches},
77 # CVS commits run ikiwiki once for every committed file (!)
78 # - commit_prep alone should fix this
79 # CVS multi-dir commits show only the first dir in recentchanges
80 # - commit_prep might also fix this?
81 # CVS post-commit hook is amped off to avoid locking against itself
82 # - commit_prep probably doesn't fix this... but maybe?
85 sub test_chdir_magic {
86 # cvs.pm operations are always occurring inside $config{srcdir}
87 # other ikiwiki operations are occurring wherever, and are unaffected
88 # when are we bothering with "local $CWD" and when aren't we?
92 # inspect "Repository revision" (used in code)
93 # inspect "Sticky Options" (used in tests to verify existence of "-kb")
96 sub test_cvs_run_cvs {
97 # extract the stdout-redirect thing
98 # - prove that it silences stdout
99 # - prove that stderr comes through just fine
100 # prove that when cvs exits nonzero (fail), function exits false
101 # prove that when cvs exits zero (success), function exits true
102 # always pass -f, just in case
103 # steal from git.pm: safe_git(), run_or_{die,cry,non}
104 # - open() instead of system()
105 # always call cvs_run_cvs(), don't ever run 'cvs' directly
108 sub test_cvs_run_cvsps {
109 # parameterize command like run_cvs()
110 # expose config vars for e.g. "--cvs-direct -z 30"
111 # always pass -x (unless proven otherwise)
112 # always pass -b HEAD (configurable like gitmaster_branch?)
115 sub test_cvs_parse_cvsps {
116 # extract method from rcs_recentchanges
117 # document expected changeset format
118 # document expected changeset delimiter
119 # try: cvsps -q -x -p && ls | sort -rn | head -100
120 # - benchmark against current impl (that uses File::ReadBackwards)
123 sub test_cvs_parse_log_accum {
124 # add new, preferred method for rcs_recentchanges to use
125 # teach log_accum to record commits (into transient?)
126 # script cvsps to bootstrap (or replace?) commit history
127 # teach ikiwiki-makerepo to set up log_accum and commit_prep
128 # why are NetBSD commit mails unreliable?
129 # - is it working for CVS commits and failing for web commits?
132 sub test_cvs_is_controlling {
134 # - if srcdir is in CVS, return true
135 # - else, return false
137 # - if dir is in CVS, return true
138 # - else, return false
140 # - is there anything that wants the answer? if so, answer
145 # TESTS FOR GENERAL PLUGIN API CALLS
147 sub test_checkconfig {
148 # undef cvspath, expect "ikiwiki"
149 # define cvspath normally, get it back
150 # define cvspath in a subdir, get it back?
151 # define cvspath with extra slashes, get sanitized version back
152 # - yoink test_extra_path_slashes
153 # undef cvs_wrapper, expect $config{wrappers} same size as before
155 my $initial_cvspath = $config{cvspath};
156 $config{cvspath} = "/ikiwiki//";
157 IkiWiki::checkconfig();
161 q{rcs_recentchanges assumes checkconfig has sanitized cvspath},
166 # anything worth testing?
169 sub test_genwrapper {
170 # testable directly? affects rcs_add, but are we exercising this?
174 # TESTS FOR VCS PLUGIN API CALLS
176 sub test_rcs_update {
177 # can it assume we're under CVS control? or must it check?
178 # anything else worth testing?
181 sub test_rcs_prepedit {
182 # can it assume we're under CVS control? or must it check?
183 # for existing file, returns latest revision in repo
184 # - what's this used for? should it return latest revision in checkout?
185 # for new file, returns empty string
188 sub test_rcs_commit {
189 # can it assume we're under CVS control? or must it check?
190 # if someone else changed the page since rcs_prepedit was called:
191 # - try to merge into our working copy
192 # - if merge succeeds, proceed to commit
193 # - else, return page content with the conflict markers in it
195 # - if success, return undef
196 # - else, revert + return content with the conflict markers in it
197 # git.pm receives "session" param -- useful here?
198 # web commits start with "web commit {by,from} "
199 # seeing File::chdir errors on commit?
202 sub test_rcs_commit_staged {
203 # if commit succeeds, return undef
204 # else, warn and return error message (really? or just non-undef?)
209 my $dir2 = "test4/test5";
211 mkdir($config{srcdir} . "/$dir1"),
214 IkiWiki::rcs_add($dir1);
217 message => "shouldn't happen",
221 # can it assume we're under CVS control? or must it check?
222 # add a top-level text file
224 # - inspect recentchanges: new change, no -kb
225 # add a top-level dir
226 # - test mustn't hang (does it hang if we comment out genwrapper?)
227 # - inspect recentchanges: no new change
229 # - reinspect recentchanges: still no new change
230 # add a text file in that dir
231 # - rcs_commit_staged
232 # - inspect recentchanges: new change, no -kb
233 # add a top-level dir + add a binary file in it
234 # - rcs_commit_staged
235 # - inspect recentchanges: new change, yes -kb
236 # add a top-level dir + subdir + add one text and one binary file in it
237 # - rcs_commit_staged
238 # - inspect recentchanges: one new change, two files, one -kb, one not
240 # extract method: filetype-guessing
241 # add a binary file, remove it, add a text file by same name, no -kb?
242 # add a text file, remove it, add a binary file by same name, -kb?
245 sub test_rcs_remove {
246 # can it assume we're under CVS control? or must it check?
247 # remove a top-level file
249 # - inspect recentchanges: one new change, file removed
250 # remove two files (in different dirs)
251 # - rcs_commit_staged
252 # - inspect recentchanges: one new change, both files removed
255 sub test_rcs_rename {
256 # can it assume we're under CVS control? or must it check?
257 # rename a file in the same dir
258 # - rcs_commit_staged
259 # - inspect recentchanges: one new change, one file removed, one added
260 # rename a file into a different dir
261 # - rcs_commit_staged
262 # - inspect recentchanges: one new change, one file removed, one added
263 # rename a file into a not-yet-existing dir
264 # - rcs_commit_staged
265 # - inspect recentchanges: one new change, one file removed, one added
266 # is it safe to use "mv"? what if $dest is somehow outside the wiki?
269 sub test_rcs_recentchanges {
270 # can it assume we're under CVS control? or must it check?
271 # don't worry whether we're called with a number (we always are)
272 # other rcs tests already inspect much of the returned structure
273 # CVS commits say "cvs" and get the right committer
274 # web commits say "web" and get the right committer
275 # - and don't start with "web commit {by,from} "
276 # "nickname" -- can we ever meaningfully set this?
278 # prefer log_accum, then cvsps, else die
279 # run the high-level recentchanges tests 2x (once for each method)
280 # - including in other test subs that check recentchanges?
284 # can it assume we're under CVS control? or must it check?
285 # in list context, return all lines (with \n), up to $maxlines if set
286 # in scalar context, return the whole diff, up to $maxlines if set
289 sub test_rcs_getctime {
290 # can it assume we're under CVS control? or must it check?
291 # given a file, find its creation time, else return 0
292 # first implement in the obvious way
296 sub test_rcs_getmtime {
297 # can it assume we're under CVS control? or must it check?
298 # given a file, find its modification time, else return 0
299 # first implement in the obvious way
303 sub test_rcs_receive {
304 pass(q{rcs_receive doesn't make sense for CVS});
307 sub test_rcs_preprevert {
308 # can it assume we're under CVS control? or must it check?
309 # given a patchset number, return structure describing what'd happen:
310 # - see doc/plugins/write.mdwn:rcs_receive()
311 # don't forget about attachments
314 sub test_rcs_revert {
315 # can it assume we're under CVS control? or must it check?
316 # given a patchset number, stage the revert for rcs_commit_staged()
317 # if commit succeeds, return undef
318 # else, warn and return error message (really? or just non-undef?)
322 my $test_methods = defined $ENV{TEST_METHOD}
324 : $default_test_methods;
326 _startup($test_methods eq $default_test_methods);
327 _runtests(_get_matching_test_subs($test_methods));
328 _shutdown($test_methods eq $default_test_methods);
334 # INTERNAL SUPPORT ROUTINES
336 sub _plan_for_test_more {
337 my $can_plan = shift;
339 foreach my $program (qw(
343 my $program_path = `which $program`;
345 return plan(skip_all => "$program not available")
346 unless -x $program_path;
349 foreach my $module (qw(
356 eval qq{use $module};
357 return plan(skip_all => "$module not available")
361 return plan(skip_all => "can't create $dir: $!")
363 return plan(skip_all => "can't remove $dir: $!")
366 return unless $can_plan;
368 return plan(tests => $total_tests);
371 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
373 use B qw/svref_2object/;
376 my ($coderef, $package) = @_;
377 my $cv = svref_2object($coderef);
378 return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
379 return $cv->GV->STASH->NAME eq $package;
386 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
387 } keys %{"$module\::"};
391 # support for xUnit-style testing, a la Test::Class
394 my $can_plan = shift;
395 _plan_for_test_more($can_plan);
396 _generate_test_config();
400 my $had_plan = shift;
401 done_testing() unless $had_plan;
405 _generate_test_repo();
409 system "rm -rf $dir";
421 sub _get_matching_test_subs {
424 return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
427 sub _generate_test_config {
428 %config = IkiWiki::defaultconfig();
429 $config{rcs} = "cvs";
430 $config{srcdir} = "$dir/src";
431 $config{cvsrepo} = "$dir/repo";
432 $config{cvspath} = "ikiwiki";
433 IkiWiki::loadplugins();
434 IkiWiki::checkconfig();
437 sub _generate_test_repo {
438 die "can't create $dir: $!"
441 my $cvs = "cvs -d $config{cvsrepo}";
442 my $dn = ">/dev/null";
443 system "$cvs init $dn";
444 system "mkdir $dir/$config{cvspath} $dn";
445 system "cd $dir/$config{cvspath} && "
446 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
447 system "rm -rf $dir/$config{cvspath} $dn";
448 system "$cvs co -d $config{srcdir} $config{cvspath} $dn";