]> sipb.mit.edu Git - ikiwiki.git/blob - t/cvs.t
Merge branch 'master' of git://git.ikiwiki.info
[ikiwiki.git] / t / cvs.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More; my $total_tests = 40;
5 use IkiWiki;
6
7 my $default_test_methods = '^test_*';
8 my @required_programs = qw(
9         cvs
10         cvsps
11 );
12 my @required_modules = qw(
13         File::chdir
14         File::MimeInfo
15         Date::Parse
16         File::Temp
17         File::ReadBackwards
18 );
19 my $dir = "/tmp/ikiwiki-test-cvs.$$";
20
21 # TESTS FOR GENERAL META-BEHAVIOR
22
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         # - side effect for moderated comments: after approval they
30         #   show up normally AND are still pending, too
31         # - comments.pm treats rcs_commit_staged() as returning conflicts?
32 }
33
34 sub test_chdir_magic {
35         # cvs.pm operations are always occurring inside $config{srcdir}
36         # other ikiwiki operations are occurring wherever, and are unaffected
37         # when are we bothering with "local $CWD" and when aren't we?
38 }
39
40 sub test_cvs_info {
41         # inspect "Repository revision" (used in code)
42         # inspect "Sticky Options" (used in tests to verify existence of "-kb")
43 }
44
45 sub test_cvs_run_cvs {
46         # extract the stdout-redirect thing
47         # - prove that it silences stdout
48         # - prove that stderr comes through just fine
49         # prove that when cvs exits nonzero (fail), function exits false
50         # prove that when cvs exits zero (success), function exits true
51         # always pass -f, just in case
52         # steal from git.pm: safe_git(), run_or_{die,cry,non}
53         # - open() instead of system()
54         # always call cvs_run_cvs(), don't ever run 'cvs' directly
55 }
56
57 sub test_cvs_run_cvsps {
58         # parameterize command like run_cvs()
59         # expose config vars for e.g. "--cvs-direct -z 30"
60         # always pass -x (unless proven otherwise)
61         # always pass -b HEAD (configurable like gitmaster_branch?)
62 }
63
64 sub test_cvs_parse_cvsps {
65         # extract method from rcs_recentchanges
66         # document expected changeset format
67         # document expected changeset delimiter
68         # try: cvsps -q -x -p && ls | sort -rn | head -100
69         # - benchmark against current impl (that uses File::ReadBackwards)
70 }
71
72 sub test_cvs_parse_log_accum {
73         # add new, preferred method for rcs_recentchanges to use
74         # teach log_accum to record commits (into transient?)
75         # script cvsps to bootstrap (or replace?) commit history
76         # teach ikiwiki-makerepo to set up log_accum and commit_prep
77         # why are NetBSD commit mails unreliable?
78         # - is it working for CVS commits and failing for web commits?
79 }
80
81 sub test_cvs_is_controlling {
82         # with no args:
83         # - if srcdir is in CVS, return true
84         # - else, return false
85         # with a dir arg:
86         # - if dir is in CVS, return true
87         # - else, return false
88         # with a file arg:
89         # - is there anything that wants the answer? if so, answer
90         # - else, die
91 }
92
93
94 # TESTS FOR GENERAL PLUGIN API CALLS
95
96 sub test_checkconfig {
97         # undef cvspath, expect "ikiwiki"
98         # define cvspath normally, get it back
99         # define cvspath in a subdir, get it back?
100         # define cvspath with extra slashes, get sanitized version back
101         # - yoink test_extra_path_slashes
102         # undef cvs_wrapper, expect $config{wrappers} same size as before
103
104         my $initial_cvspath = $config{cvspath};
105         $config{cvspath} = "/ikiwiki//";
106         IkiWiki::checkconfig();
107         is(
108                 $config{cvspath},
109                 $initial_cvspath,
110                 q{rcs_recentchanges assumes checkconfig has sanitized cvspath},
111         );
112 }
113
114 sub test_getsetup {
115         # anything worth testing?
116 }
117
118 sub test_genwrapper {
119         # testable directly? affects rcs_add, but are we exercising this?
120 }
121
122
123 # TESTS FOR VCS PLUGIN API CALLS
124
125 sub test_rcs_update {
126         # can it assume we're under CVS control? or must it check?
127         # anything else worth testing?
128 }
129
130 sub test_rcs_prepedit {
131         # can it assume we're under CVS control? or must it check?
132         # for existing file, returns latest revision in repo
133         # - what's this used for? should it return latest revision in checkout?
134         # for new file, returns empty string
135 }
136
137 sub test_rcs_commit {
138         # can it assume we're under CVS control? or must it check?
139         # if someone else changed the page since rcs_prepedit was called:
140         # - try to merge into our working copy
141         # - if merge succeeds, proceed to commit
142         # - else, return page content with the conflict markers in it
143         # commit:
144         # - if success, return undef
145         # - else, revert + return content with the conflict markers in it
146         # git.pm receives "session" param -- useful here?
147         # web commits start with "web commit {by,from} "
148         # seeing File::chdir errors on commit?
149 }
150
151 sub test_rcs_commit_staged {
152         # if commit succeeds, return undef
153         # else, warn and return error message (really? or just non-undef?)
154 }
155
156 sub test_rcs_add {
157         my $message = "add a top-level ASCII (non-UTF-8) page via VCS API";
158         writefile('test0.mdwn', $config{srcdir}, "* some plain ASCII text");
159         IkiWiki::rcs_add("test0.mdwn");
160         IkiWiki::rcs_commit(
161                 file => "test0.mdwn",
162                 message => $message,
163                 token => "moo",
164         );
165         is_newly_added("test0.mdwn");
166         is_in_keyword_substitution_mode("test0.mdwn", undef);
167         my @changes = IkiWiki::rcs_recentchanges(3);
168         is_total_number_of_changes(\@changes, 1);
169         is_most_recent_change(\@changes, "test0", $message);
170
171         $message = "add a top-level dir via VCS API";
172         my $dir1 = "test3";
173         can_mkdir($dir1);
174         IkiWiki::rcs_add($dir1);
175         # XXX test that the wrapper hangs here without our genwrapper()
176         # XXX test that the wrapper doesn't hang here with it
177         @changes = IkiWiki::rcs_recentchanges(3);
178         is_total_number_of_changes(\@changes, 1);       # despite the dir add
179         IkiWiki::rcs_commit(
180                 file => $dir1,
181                 message => $message,
182                 token => "oom",
183         );
184         @changes = IkiWiki::rcs_recentchanges(3);
185         is_total_number_of_changes(\@changes, 1);       # dirs aren't tracked
186
187         $message = "add a non-ASCII (UTF-8) text file in an un-added dir";
188         my $dir2 = "test4/test5";
189         can_mkdir($_) for ('test4', $dir2);
190         writefile("$dir2/test1.mdwn", $config{srcdir},readfile("t/test1.mdwn"));
191         IkiWiki::rcs_add("$dir2/test1.mdwn");
192         IkiWiki::rcs_commit(
193                 file => "$dir2/test1.mdwn",
194                 message => $message,
195                 token => "omo",
196         );
197         is_newly_added("$dir2/test1.mdwn");
198         is_in_keyword_substitution_mode("$dir2/test1.mdwn", undef);
199         @changes = IkiWiki::rcs_recentchanges(3);
200         is_total_number_of_changes(\@changes, 2);
201         is_most_recent_change(\@changes, "$dir2/test1", $message);
202
203         $message = "add a binary file in an un-added dir, and commit_staged";
204         my $dir3 = "test6";
205         my $file = "$dir3/test7.ico";
206         can_mkdir($dir3);
207         my $bindata_in = readfile("doc/favicon.ico", 1);
208         my $bindata_out = sub { readfile($config{srcdir} . "/$file", 1) };
209         writefile($file, $config{srcdir}, $bindata_in, 1);
210         is(&$bindata_out(), $bindata_in, q{binary files match before commit});
211         IkiWiki::rcs_add($file);
212         IkiWiki::rcs_commit_staged(message => $message);
213         is_newly_added($file);
214         is_in_keyword_substitution_mode($file, q{-kb});
215         is(&$bindata_out(), $bindata_in, q{binary files match after commit});
216         @changes = IkiWiki::rcs_recentchanges(3);
217         is_total_number_of_changes(\@changes, 3);
218         is_most_recent_change(\@changes, $file, $message);
219         ok(
220                 unlink($config{srcdir} . "/$file"),
221                 q{can remove file in order to re-fetch it from repo},
222         );
223         ok(! -e $config{srcdir} . "/$file", q{really removed file});
224         IkiWiki::rcs_update();
225         is(&$bindata_out(), $bindata_in, q{binary files match after re-fetch});
226
227         $message = "add a UTF-8 and a binary file in different dirs";
228         my $file1 = "test8/test9.mdwn";
229         my $file2 = "test10/test11.ico";
230         can_mkdir(qw(test8 test10));
231         writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
232         writefile($file2, $config{srcdir}, $bindata_in, 1);
233         IkiWiki::rcs_add($_) for ($file1, $file2);
234         IkiWiki::rcs_commit_staged(message => $message);
235         is_newly_added($_) for ($file1, $file2);
236         is_in_keyword_substitution_mode($file1, undef);
237         is_in_keyword_substitution_mode($file2, '-kb');
238         @changes = IkiWiki::rcs_recentchanges(3);
239         is_total_number_of_changes(\@changes, 3);
240         @changes = IkiWiki::rcs_recentchanges(4);
241         is_total_number_of_changes(\@changes, 4);
242         # XXX test for both files in the commit, and no other files
243         is_most_recent_change(\@changes, $file2, $message);
244
245         # prevent web edits from attempting to create .../CVS/foo.mdwn
246         # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
247         # unless your "CVS" is something else and we've made it configurable
248
249         # can it assume we're under CVS control? or must it check?
250
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?
254 }
255
256 sub test_rcs_remove {
257         # can it assume we're under CVS control? or must it check?
258         # remove a top-level file
259         # - rcs_commit
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
264 }
265
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?
278 }
279
280 sub test_rcs_recentchanges {
281         my $message = "Add a page via CVS directly";
282         writefile('test2.mdwn', $config{srcdir}, readfile("t/test2.mdwn"));
283         system "cd $config{srcdir}"
284                 . " && cvs add test2.mdwn >/dev/null 2>&1";
285         system "cd $config{srcdir}"
286                 . " && cvs commit -m \"$message\" test2.mdwn >/dev/null";
287
288         my @changes = IkiWiki::rcs_recentchanges(3);
289         is(
290                 $#changes,
291                 0,
292                 q{total commits: 1},
293         );
294         is(
295                 $changes[0]{message}[0]{"line"},
296                 $message,
297                 q{most recent commit's first message line matches},
298         );
299         is(
300                 $changes[0]{pages}[0]{"page"},
301                 "test2",
302                 q{most recent commit's first pagename matches},
303         );
304
305         # CVS commits run ikiwiki once for every committed file (!)
306         # - commit_prep alone should fix this
307         # CVS multi-dir commits show only the first dir in recentchanges
308         # - commit_prep might also fix this?
309         # CVS post-commit hook is amped off to avoid locking against itself
310         # - commit_prep probably doesn't fix this... but maybe?
311         # can it assume we're under CVS control? or must it check?
312         # don't worry whether we're called with a number (we always are)
313         # other rcs tests already inspect much of the returned structure
314         # CVS commits say "cvs" and get the right committer
315         # web commits say "web" and get the right committer
316         # - and don't start with "web commit {by,from} "
317         # "nickname" -- can we ever meaningfully set this?
318
319         # prefer log_accum, then cvsps, else die
320         # run the high-level recentchanges tests 2x (once for each method)
321         # - including in other test subs that check recentchanges?
322 }
323
324 sub test_rcs_diff {
325         # can it assume we're under CVS control? or must it check?
326         # in list context, return all lines (with \n), up to $maxlines if set
327         # in scalar context, return the whole diff, up to $maxlines if set
328 }
329
330 sub test_rcs_getctime {
331         # can it assume we're under CVS control? or must it check?
332         # given a file, find its creation time, else return 0
333         # first implement in the obvious way
334         # then cache
335 }
336
337 sub test_rcs_getmtime {
338         # can it assume we're under CVS control? or must it check?
339         # given a file, find its modification time, else return 0
340         # first implement in the obvious way
341         # then cache
342 }
343
344 sub test_rcs_receive {
345         pass(q{rcs_receive doesn't make sense for CVS});
346 }
347
348 sub test_rcs_preprevert {
349         # can it assume we're under CVS control? or must it check?
350         # given a patchset number, return structure describing what'd happen:
351         # - see doc/plugins/write.mdwn:rcs_receive()
352         # don't forget about attachments
353 }
354
355 sub test_rcs_revert {
356         # can it assume we're under CVS control? or must it check?
357         # given a patchset number, stage the revert for rcs_commit_staged()
358         # if commit succeeds, return undef
359         # else, warn and return error message (really? or just non-undef?)
360 }
361
362 sub main {
363         my $test_methods = defined $ENV{TEST_METHOD} 
364                          ? $ENV{TEST_METHOD}
365                          : $default_test_methods;
366
367         _startup($test_methods eq $default_test_methods);
368         _runtests(_get_matching_test_subs($test_methods));
369         _shutdown($test_methods eq $default_test_methods);
370 }
371
372 main();
373
374
375 # INTERNAL SUPPORT ROUTINES
376
377 sub _plan_for_test_more {
378         my $can_plan = shift;
379
380         foreach my $program (@required_programs) {
381                 my $program_path = `which $program`;
382                 chomp $program_path;
383                 return plan(skip_all => "$program not available")
384                         unless -x $program_path;
385         }
386
387         foreach my $module (@required_modules) {
388                 eval qq{use $module};
389                 return plan(skip_all => "$module not available")
390                         if $@;
391         }
392
393         return plan(skip_all => "can't create $dir: $!")
394                 unless mkdir($dir);
395         return plan(skip_all => "can't remove $dir: $!")
396                 unless rmdir($dir);
397
398         return unless $can_plan;
399
400         return plan(tests => $total_tests);
401 }
402
403 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
404
405 use B qw/svref_2object/;
406
407 sub in_package {
408         my ($coderef, $package) = @_;
409         my $cv = svref_2object($coderef);
410         return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
411         return $cv->GV->STASH->NAME eq $package;
412 }
413
414 sub list_module {
415         my $module = shift;
416         no strict 'refs';
417         return grep {
418                 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
419         } keys %{"$module\::"};
420 }
421
422
423 # support for xUnit-style testing, a la Test::Class
424
425 sub _startup {
426         my $can_plan = shift;
427         _plan_for_test_more($can_plan);
428         _generate_test_config();
429 }
430
431 sub _shutdown {
432         my $had_plan = shift;
433         done_testing() unless $had_plan;
434 }
435
436 sub _setup {
437         _generate_test_repo();
438 }
439
440 sub _teardown {
441         system "rm -rf $dir";
442 }
443
444 sub _runtests {
445         my @coderefs = (@_);
446         for (@coderefs) {
447                 _setup();
448                 $_->();
449                 _teardown();
450         }
451 }
452
453 sub _get_matching_test_subs {
454         my $re = shift;
455         no strict 'refs';
456         return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
457 }
458
459 sub _generate_test_config {
460         %config = IkiWiki::defaultconfig();
461         $config{rcs} = "cvs";
462         $config{srcdir} = "$dir/src";
463         $config{cvsrepo} = "$dir/repo";
464         $config{cvspath} = "ikiwiki";
465         IkiWiki::loadplugins();
466         IkiWiki::checkconfig();
467 }
468
469 sub _generate_test_repo {
470         die "can't create $dir: $!"
471                 unless mkdir($dir);
472
473         my $cvs = "cvs -d $config{cvsrepo}";
474         my $dn = ">/dev/null";
475         system "$cvs init $dn";
476         system "mkdir $dir/$config{cvspath} $dn";
477         system "cd $dir/$config{cvspath} && "
478                 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
479         system "rm -rf $dir/$config{cvspath} $dn";
480         system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
481 }
482
483 sub can_mkdir {
484         my $dir = shift;
485         ok(
486                 mkdir($config{srcdir} . "/$dir"),
487                 qq{can mkdir $dir},
488         );
489 }
490
491 sub is_newly_added {
492         my $file = shift;
493         is(
494                 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
495                 '1.1',
496                 qq{$file is newly added to CVS},
497         );
498 }
499
500 sub is_in_keyword_substitution_mode {
501         my ($file, $mode) = @_;
502         $mode = '(none)' unless defined $mode;
503         is(
504                 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
505                 $mode,
506                 qq{$file is in CVS with expected keyword substitution mode},
507         );
508 }
509
510 sub is_total_number_of_changes {
511         my ($changes, $expected_total) = @_;
512         is(
513                 $#{$changes},
514                 $expected_total - 1,
515                 qq{total commits == $expected_total},
516         );
517 }
518
519 sub is_most_recent_change {
520         my ($changes, $page, $message) = @_;
521         is(
522                 $changes->[0]{message}[0]{"line"},
523                 $message,
524                 q{most recent commit's first message line matches},
525         );
526         is(
527                 $changes->[0]{pages}[0]{"page"},
528                 $page,
529                 q{most recent commit's first pagename matches},
530         );
531 }