]> sipb.mit.edu Git - ikiwiki.git/blob - t/cvs.t
12afc6687ab40abf9baa9612703fd1a379511c37
[ikiwiki.git] / t / cvs.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More; my $total_tests = 57;
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         #   - 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?
33 }
34
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.
42 }
43
44 sub test_cvs_info {
45         # inspect "Repository revision" (used in code)
46         # inspect "Sticky Options" (used in tests to verify existence of "-kb")
47 }
48
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
59 }
60
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         # - but diff doesn't! optimization alert
66         # always pass -b HEAD (configurable like gitmaster_branch?)
67 }
68
69 sub test_cvs_parse_cvsps {
70         # extract method from rcs_recentchanges
71         # document expected changeset format
72         # document expected changeset delimiter
73         # try: cvsps -q -x -p && ls | sort -rn | head -100
74         # - benchmark against current impl (that uses File::ReadBackwards)
75 }
76
77 sub test_cvs_parse_log_accum {
78         # add new, preferred method for rcs_recentchanges to use
79         # teach log_accum to record commits (into transient?)
80         # script cvsps to bootstrap (or replace?) commit history
81         # teach ikiwiki-makerepo to set up log_accum and commit_prep
82         # why are NetBSD commit mails unreliable?
83         # - is it working for CVS commits and failing for web commits?
84 }
85
86 sub test_cvs_is_controlling {
87         # with no args:
88         # - if srcdir is in CVS, return true
89         # - else, return false
90         # with a dir arg:
91         # - if dir is in CVS, return true
92         # - else, return false
93         # with a file arg:
94         # - is there anything that wants the answer? if so, answer
95         # - else, die
96 }
97
98
99 # TESTS FOR GENERAL PLUGIN API CALLS
100
101 sub test_checkconfig {
102         my $default_cvspath = 'ikiwiki';
103
104         undef $config{cvspath}; IkiWiki::checkconfig();
105         is(
106                 $config{cvspath}, $default_cvspath,
107                 q{can provide default cvspath},
108         );
109
110         $config{cvspath} = "/$default_cvspath/"; IkiWiki::checkconfig();
111         is(
112                 $config{cvspath}, $default_cvspath,
113                 q{can set typical cvspath and strip well-meaning slashes},
114         );
115
116         $config{cvspath} = "/$default_cvspath//subdir"; IkiWiki::checkconfig();
117         is(
118                 $config{cvspath}, "$default_cvspath/subdir",
119                 q{can really sanitize cvspath as assumed by rcs_recentchanges},
120         );
121
122         my $default_num_wrappers = @{$config{wrappers}};
123         undef $config{cvs_wrapper}; IkiWiki::checkconfig();
124         is(
125                 @{$config{wrappers}}, $default_num_wrappers,
126                 q{can start with no wrappers configured},
127         );
128
129         $config{cvs_wrapper} = $config{cvsrepo} . "/CVSROOT/post-commit";
130         IkiWiki::checkconfig();
131         is(
132                 @{$config{wrappers}}, ++$default_num_wrappers,
133                 q{can add cvs_wrapper},
134         );
135
136         undef $config{cvs_wrapper};
137         $config{cvspath} = $default_cvspath;
138         IkiWiki::checkconfig();
139 }
140
141 sub test_getsetup {
142         # anything worth testing?
143 }
144
145 sub test_genwrapper {
146         # testable directly? affects rcs_add, but are we exercising this?
147 }
148
149
150 # TESTS FOR VCS PLUGIN API CALLS
151
152 sub test_rcs_update {
153         # can it assume we're under CVS control? or must it check?
154         # anything else worth testing?
155 }
156
157 sub test_rcs_prepedit {
158         # can it assume we're under CVS control? or must it check?
159         # for existing file, returns latest revision in repo
160         # - what's this used for? should it return latest revision in checkout?
161         # for new file, returns empty string
162
163         # netbsd web log says "could not open lock file"
164         # XXX does this work right?
165         # how about with un-added dirs in the srcdir?
166         # how about with cvsps.core lying around?
167 }
168
169 sub test_rcs_commit {
170         # can it assume we're under CVS control? or must it check?
171         # if someone else changed the page since rcs_prepedit was called:
172         # - try to merge into our working copy
173         # - if merge succeeds, proceed to commit
174         # - else, return page content with the conflict markers in it
175         # commit:
176         # - if success, return undef
177         # - else, revert + return content with the conflict markers in it
178         # git.pm receives "session" param -- useful here?
179         # web commits start with "web commit {by,from} "
180         # seeing File::chdir errors on commit?
181
182         # XXX commit can fail due to "could not open lock file"
183 }
184
185 sub test_rcs_commit_staged {
186         # if commit succeeds, return undef
187         # else, warn and return error message (really? or just non-undef?)
188 }
189
190 sub test_rcs_add {
191         my @changes = IkiWiki::rcs_recentchanges(3);
192         is_total_number_of_changes(\@changes, 0);
193
194         my $message = "add a top-level ASCII (non-UTF-8) page via VCS API";
195         my $file = q{test0.mdwn};
196         add_and_commit($file, $message, qq{# \$Id\$\n* some plain ASCII text});
197         is_newly_added($file);
198         is_in_keyword_substitution_mode($file, undef);
199         like(
200                 readfile($config{srcdir} . "/$file"),
201                 qr/^# \$Id: $file,v 1.1 .+\$$/m,
202                 q{can expand RCS Id keyword},
203         );
204         @changes = IkiWiki::rcs_recentchanges(3);
205         is_total_number_of_changes(\@changes, 1);
206         is_most_recent_change(\@changes, stripext($file), $message);
207
208         $message = "add a top-level dir via VCS API";
209         my $dir1 = q{test3};
210         can_mkdir($dir1);
211         IkiWiki::rcs_add($dir1);
212         # XXX test that the wrapper hangs here without our genwrapper()
213         # XXX test that the wrapper doesn't hang here with it
214         @changes = IkiWiki::rcs_recentchanges(3);
215         is_total_number_of_changes(\@changes, 1);       # despite the dir add
216         IkiWiki::rcs_commit(
217                 file => $dir1,
218                 message => $message,
219                 token => "oom",
220         );
221         @changes = IkiWiki::rcs_recentchanges(3);
222         is_total_number_of_changes(\@changes, 1);       # dirs aren't tracked
223
224         $message = "add a non-ASCII (UTF-8) text file in an un-added dir";
225         can_mkdir($_) for (qw(test4 test4/test5));
226         $file = q{test4/test5/test1.mdwn};
227         add_and_commit($file, $message, readfile("t/test1.mdwn"));
228         is_newly_added($file);
229         is_in_keyword_substitution_mode($file, undef);
230         @changes = IkiWiki::rcs_recentchanges(3);
231         is_total_number_of_changes(\@changes, 2);
232         is_most_recent_change(\@changes, stripext($file), $message);
233
234         $message = "add a binary file in an un-added dir, and commit_staged";
235         can_mkdir(q{test6});
236         $file = q{test6/test7.ico};
237         my $bindata_in = readfile("doc/favicon.ico", 1);
238         my $bindata_out = sub { readfile($config{srcdir} . "/$file", 1) };
239         writefile($file, $config{srcdir}, $bindata_in, 1);
240         is(&$bindata_out(), $bindata_in, q{binary files match before commit});
241         IkiWiki::rcs_add($file);
242         IkiWiki::rcs_commit_staged(message => $message);
243         is_newly_added($file);
244         is_in_keyword_substitution_mode($file, q{-kb});
245         is(&$bindata_out(), $bindata_in, q{binary files match after commit});
246         @changes = IkiWiki::rcs_recentchanges(3);
247         is_total_number_of_changes(\@changes, 3);
248         is_most_recent_change(\@changes, $file, $message);
249         ok(
250                 unlink($config{srcdir} . "/$file"),
251                 q{can remove file in order to re-fetch it from repo},
252         );
253         ok(! -e $config{srcdir} . "/$file", q{really removed file});
254         IkiWiki::rcs_update();
255         is(&$bindata_out(), $bindata_in, q{binary files match after re-fetch});
256
257         $message = "add a UTF-8 and a binary file in different dirs";
258         my $file1 = "test8/test9.mdwn";
259         my $file2 = "test10/test11.ico";
260         can_mkdir($_) for (qw(test8 test10));
261         writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
262         writefile($file2, $config{srcdir}, $bindata_in, 1);
263         IkiWiki::rcs_add($_) for ($file1, $file2);
264         IkiWiki::rcs_commit_staged(message => $message);
265         is_newly_added($_) for ($file1, $file2);
266         is_in_keyword_substitution_mode($file1, undef);
267         is_in_keyword_substitution_mode($file2, '-kb');
268         @changes = IkiWiki::rcs_recentchanges(3);
269         is_total_number_of_changes(\@changes, 3);
270         @changes = IkiWiki::rcs_recentchanges(4);
271         is_total_number_of_changes(\@changes, 4);
272         # XXX test for both files in the commit, and no other files
273         is_most_recent_change(\@changes, $file2, $message);
274
275         # prevent web edits from attempting to create .../CVS/foo.mdwn
276         # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
277         # unless your "CVS" is something else and we've made it configurable
278
279         # can it assume we're under CVS control? or must it check?
280
281         # extract method: filetype-guessing
282         # add a binary file, remove it, add a text file by same name, no -kb?
283         # add a text file, remove it, add a binary file by same name, -kb?
284 }
285
286 sub test_rcs_remove {
287         # can it assume we're under CVS control? or must it check?
288         # remove a top-level file
289         # - rcs_commit
290         # - inspect recentchanges: one new change, file removed
291         # remove two files (in different dirs)
292         # - rcs_commit_staged
293         # - inspect recentchanges: one new change, both files removed
294 }
295
296 sub test_rcs_rename {
297         # can it assume we're under CVS control? or must it check?
298         # rename a file in the same dir
299         # - rcs_commit_staged
300         # - inspect recentchanges: one new change, one file removed, one added
301         # rename a file into a different dir
302         # - rcs_commit_staged
303         # - inspect recentchanges: one new change, one file removed, one added
304         # rename a file into a not-yet-existing dir
305         # - rcs_commit_staged
306         # - inspect recentchanges: one new change, one file removed, one added
307         # is it safe to use "mv"? what if $dest is somehow outside the wiki?
308 }
309
310 sub test_rcs_recentchanges {
311         my @changes = IkiWiki::rcs_recentchanges(3);
312         is_total_number_of_changes(\@changes, 0);
313
314         my $message = "Add a page via CVS directly";
315         my $file = q{test2.mdwn};
316         writefile($file, $config{srcdir}, readfile(q{t/test2.mdwn}));
317         system "cd $config{srcdir}"
318                 . " && cvs add $file >/dev/null 2>&1";
319         system "cd $config{srcdir}"
320                 . " && cvs commit -m \"$message\" $file >/dev/null";
321
322         @changes = IkiWiki::rcs_recentchanges(3);
323         is_total_number_of_changes(\@changes, 1);
324         is_most_recent_change(\@changes, stripext($file), $message);
325
326         # CVS commits run ikiwiki once for every committed file (!)
327         # - commit_prep alone should fix this
328         # CVS multi-dir commits show only the first dir in recentchanges
329         # - commit_prep might also fix this?
330         # CVS post-commit hook is amped off to avoid locking against itself
331         # - commit_prep probably doesn't fix this... but maybe?
332         # can it assume we're under CVS control? or must it check?
333         # don't worry whether we're called with a number (we always are)
334         # other rcs tests already inspect much of the returned structure
335         # CVS commits say "cvs" and get the right committer
336         # web commits say "web" and get the right committer
337         # - and don't start with "web commit {by,from} "
338         # "nickname" -- can we ever meaningfully set this?
339
340         # prefer log_accum, then cvsps, else die
341         # run the high-level recentchanges tests 2x (once for each method)
342         # - including in other test subs that check recentchanges?
343 }
344
345 sub test_rcs_diff {
346         my @changes = IkiWiki::rcs_recentchanges(3);
347         is_total_number_of_changes(\@changes, 0);
348
349         my $message = "add a UTF-8 and an ASCII file in different dirs";
350         my $file1 = "rcsdiff1/utf8.mdwn";
351         my $file2 = "rcsdiff2/ascii.mdwn";
352         my $contents2 = ''; $contents2 .= "$_. foo\n" for (1..11);
353         can_mkdir($_) for (qw(rcsdiff1 rcsdiff2));
354         writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
355         writefile($file2, $config{srcdir}, $contents2);
356         IkiWiki::rcs_add($_) for ($file1, $file2);
357         IkiWiki::rcs_commit_staged(message => $message);
358
359         # XXX we rely on rcs_recentchanges() to be called first!
360         # XXX or else for no cvsps cache to exist yet...
361         # XXX because rcs_diff() doesn't pass -x (as an optimization)
362         @changes = IkiWiki::rcs_recentchanges(3);
363         is_total_number_of_changes(\@changes, 1);
364
365         my $changeset = 1;
366
367         my $maxlines = undef;
368         my $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
369         like(
370                 $scalar_diffs,
371                 qr/^\+11\. foo$/m,
372                 q{unbounded scalar diffs go all the way to 11},
373         );
374         my @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
375         is(
376                 $array_diffs[$#array_diffs],
377                 "+11. foo\n",
378                 q{unbounded array diffs go all the way to 11},
379         );
380
381         $maxlines = 8;
382         $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
383         unlike(
384                 $scalar_diffs,
385                 qr/^\+11\. foo$/m,
386                 q{bounded scalar diffs don't go all the way to 11},
387         );
388         @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
389         isnt(
390                 $array_diffs[$#array_diffs],
391                 "+11. foo\n",
392                 q{bounded array diffs don't go all the way to 11},
393         );
394         is(
395                 scalar @array_diffs,
396                 $maxlines,
397                 q{bounded array diffs contain expected maximum number of lines},
398         );
399
400         # can it assume we're under CVS control? or must it check?
401 }
402
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
407         # then cache
408 }
409
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
414         # then cache
415 }
416
417 sub test_rcs_receive {
418         my $description = q{rcs_receive doesn't make sense for CVS};
419         exists $IkiWiki::hooks{rcs}{rcs_receive}
420                 ? fail($description)
421                 : pass($description);
422 }
423
424 sub test_rcs_preprevert {
425         # can it assume we're under CVS control? or must it check?
426         # given a patchset number, return structure describing what'd happen:
427         # - see doc/plugins/write.mdwn:rcs_receive()
428         # don't forget about attachments
429 }
430
431 sub test_rcs_revert {
432         # test rcs_recentchanges() real darn well
433         # extract read-backwards patchset parser from rcs_recentchanges()
434         # recentchanges: given max, return list of changeset/files/etc.
435         # revert: given changeset ID, return list of file/rev/action
436         #
437         # can it assume we're under CVS control? or must it check?
438         # given a patchset number, stage the revert for rcs_commit_staged()
439         # if commit succeeds, return undef
440         # else, warn and return error message (really? or just non-undef?)
441 }
442
443 sub main {
444         my $test_methods = defined $ENV{TEST_METHOD} 
445                          ? $ENV{TEST_METHOD}
446                          : $default_test_methods;
447
448         _startup($test_methods eq $default_test_methods);
449         _runtests(_get_matching_test_subs($test_methods));
450         _shutdown($test_methods eq $default_test_methods);
451 }
452
453 main();
454
455
456 # INTERNAL SUPPORT ROUTINES
457
458 sub _plan_for_test_more {
459         my $can_plan = shift;
460
461         foreach my $program (@required_programs) {
462                 my $program_path = `which $program`;
463                 chomp $program_path;
464                 return plan(skip_all => "$program not available")
465                         unless -x $program_path;
466         }
467
468         foreach my $module (@required_modules) {
469                 eval qq{use $module};
470                 return plan(skip_all => "$module not available")
471                         if $@;
472         }
473
474         return plan(skip_all => "can't create $dir: $!")
475                 unless mkdir($dir);
476         return plan(skip_all => "can't remove $dir: $!")
477                 unless rmdir($dir);
478
479         return unless $can_plan;
480
481         return plan(tests => $total_tests);
482 }
483
484 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
485
486 use B qw/svref_2object/;
487
488 sub in_package {
489         my ($coderef, $package) = @_;
490         my $cv = svref_2object($coderef);
491         return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
492         return $cv->GV->STASH->NAME eq $package;
493 }
494
495 sub list_module {
496         my $module = shift;
497         no strict 'refs';
498         return grep {
499                 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
500         } keys %{"$module\::"};
501 }
502
503
504 # support for xUnit-style testing, a la Test::Class
505
506 sub _startup {
507         my $can_plan = shift;
508         _plan_for_test_more($can_plan);
509         _generate_test_config();
510 }
511
512 sub _shutdown {
513         my $had_plan = shift;
514         done_testing() unless $had_plan;
515 }
516
517 sub _setup {
518         _generate_test_repo();
519 }
520
521 sub _teardown {
522         # XXX does srcdir persist between test subs?
523         system "rm -rf $dir";
524 }
525
526 sub _runtests {
527         my @coderefs = (@_);
528         for (@coderefs) {
529                 _setup();
530                 $_->();
531                 _teardown();
532         }
533 }
534
535 sub _get_matching_test_subs {
536         my $re = shift;
537         no strict 'refs';
538         return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
539 }
540
541 sub _generate_test_config {
542         %config = IkiWiki::defaultconfig();
543         $config{rcs} = "cvs";
544         $config{srcdir} = "$dir/src";
545         $config{cvsrepo} = "$dir/repo";
546         $config{cvspath} = "ikiwiki";
547         IkiWiki::loadplugins();
548         IkiWiki::checkconfig();
549 }
550
551 sub _generate_test_repo {
552         die "can't create $dir: $!"
553                 unless mkdir($dir);
554
555         my $cvs = "cvs -d $config{cvsrepo}";
556         my $dn = ">/dev/null";
557         system "$cvs init $dn";
558         system "mkdir $dir/$config{cvspath} $dn";
559         system "cd $dir/$config{cvspath} && "
560                 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
561         system "rm -rf $dir/$config{cvspath} $dn";
562         system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
563 }
564
565 sub add_and_commit {
566         my ($file, $message, $contents) = @_;
567         writefile($file, $config{srcdir}, $contents);
568         IkiWiki::rcs_add($file);
569         IkiWiki::rcs_commit(
570                 file => $file,
571                 message => $message,
572                 token => "moo",
573         );
574 }
575
576 sub can_mkdir {
577         my $dir = shift;
578         ok(
579                 mkdir($config{srcdir} . "/$dir"),
580                 qq{can mkdir $dir},
581         );
582 }
583
584 sub is_newly_added {
585         my $file = shift;
586         is(
587                 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
588                 '1.1',
589                 qq{$file is newly added to CVS},
590         );
591 }
592
593 sub is_in_keyword_substitution_mode {
594         my ($file, $mode) = @_;
595         $mode = '(none)' unless defined $mode;
596         is(
597                 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
598                 $mode,
599                 qq{$file is in CVS with expected keyword substitution mode},
600         );
601 }
602
603 sub is_total_number_of_changes {
604         my ($changes, $expected_total) = @_;
605         is(
606                 $#{$changes},
607                 $expected_total - 1,
608                 qq{total commits == $expected_total},
609         );
610 }
611
612 sub is_most_recent_change {
613         my ($changes, $page, $message) = @_;
614         is(
615                 $changes->[0]{message}[0]{"line"},
616                 $message,
617                 q{most recent commit's first message line matches},
618         );
619         is(
620                 $changes->[0]{pages}[0]{"page"},
621                 $page,
622                 q{most recent commit's first pagename matches},
623         );
624 }
625
626 sub stripext {
627         my ($file, $extension) = @_;
628         $extension = '\..+?' unless defined $extension;
629         $file =~ s|$extension$||g;
630         return $file;
631 }