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