]> sipb.mit.edu Git - ikiwiki.git/blob - t/cvs.t
5717782a20fbf38a315b33b2706e7ac606c6f42c
[ikiwiki.git] / t / cvs.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More; my $total_tests = 53;
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, qq{# \$Id\$\n* some plain ASCII text});
174         is_newly_added($file);
175         is_in_keyword_substitution_mode($file, undef);
176         like(
177                 readfile($config{srcdir} . "/$file"),
178                 qr/^# \$Id: $file,v 1.1 .+\$$/m,
179                 q{can expand RCS Id keyword},
180         );
181         @changes = IkiWiki::rcs_recentchanges(3);
182         is_total_number_of_changes(\@changes, 1);
183         is_most_recent_change(\@changes, stripext($file), $message);
184
185         $message = "add a top-level dir via VCS API";
186         my $dir1 = q{test3};
187         can_mkdir($dir1);
188         IkiWiki::rcs_add($dir1);
189         # XXX test that the wrapper hangs here without our genwrapper()
190         # XXX test that the wrapper doesn't hang here with it
191         @changes = IkiWiki::rcs_recentchanges(3);
192         is_total_number_of_changes(\@changes, 1);       # despite the dir add
193         IkiWiki::rcs_commit(
194                 file => $dir1,
195                 message => $message,
196                 token => "oom",
197         );
198         @changes = IkiWiki::rcs_recentchanges(3);
199         is_total_number_of_changes(\@changes, 1);       # dirs aren't tracked
200
201         $message = "add a non-ASCII (UTF-8) text file in an un-added dir";
202         can_mkdir($_) for (qw(test4 test4/test5));
203         $file = q{test4/test5/test1.mdwn};
204         add_and_commit($file, $message, readfile("t/test1.mdwn"));
205         is_newly_added($file);
206         is_in_keyword_substitution_mode($file, undef);
207         @changes = IkiWiki::rcs_recentchanges(3);
208         is_total_number_of_changes(\@changes, 2);
209         is_most_recent_change(\@changes, stripext($file), $message);
210
211         $message = "add a binary file in an un-added dir, and commit_staged";
212         can_mkdir(q{test6});
213         $file = q{test6/test7.ico};
214         my $bindata_in = readfile("doc/favicon.ico", 1);
215         my $bindata_out = sub { readfile($config{srcdir} . "/$file", 1) };
216         writefile($file, $config{srcdir}, $bindata_in, 1);
217         is(&$bindata_out(), $bindata_in, q{binary files match before commit});
218         IkiWiki::rcs_add($file);
219         IkiWiki::rcs_commit_staged(message => $message);
220         is_newly_added($file);
221         is_in_keyword_substitution_mode($file, q{-kb});
222         is(&$bindata_out(), $bindata_in, q{binary files match after commit});
223         @changes = IkiWiki::rcs_recentchanges(3);
224         is_total_number_of_changes(\@changes, 3);
225         is_most_recent_change(\@changes, $file, $message);
226         ok(
227                 unlink($config{srcdir} . "/$file"),
228                 q{can remove file in order to re-fetch it from repo},
229         );
230         ok(! -e $config{srcdir} . "/$file", q{really removed file});
231         IkiWiki::rcs_update();
232         is(&$bindata_out(), $bindata_in, q{binary files match after re-fetch});
233
234         $message = "add a UTF-8 and a binary file in different dirs";
235         my $file1 = "test8/test9.mdwn";
236         my $file2 = "test10/test11.ico";
237         can_mkdir($_) for (qw(test8 test10));
238         writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
239         writefile($file2, $config{srcdir}, $bindata_in, 1);
240         IkiWiki::rcs_add($_) for ($file1, $file2);
241         IkiWiki::rcs_commit_staged(message => $message);
242         is_newly_added($_) for ($file1, $file2);
243         is_in_keyword_substitution_mode($file1, undef);
244         is_in_keyword_substitution_mode($file2, '-kb');
245         @changes = IkiWiki::rcs_recentchanges(3);
246         is_total_number_of_changes(\@changes, 3);
247         @changes = IkiWiki::rcs_recentchanges(4);
248         is_total_number_of_changes(\@changes, 4);
249         # XXX test for both files in the commit, and no other files
250         is_most_recent_change(\@changes, $file2, $message);
251
252         # prevent web edits from attempting to create .../CVS/foo.mdwn
253         # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
254         # unless your "CVS" is something else and we've made it configurable
255
256         # can it assume we're under CVS control? or must it check?
257
258         # extract method: filetype-guessing
259         # add a binary file, remove it, add a text file by same name, no -kb?
260         # add a text file, remove it, add a binary file by same name, -kb?
261 }
262
263 sub test_rcs_remove {
264         # can it assume we're under CVS control? or must it check?
265         # remove a top-level file
266         # - rcs_commit
267         # - inspect recentchanges: one new change, file removed
268         # remove two files (in different dirs)
269         # - rcs_commit_staged
270         # - inspect recentchanges: one new change, both files removed
271 }
272
273 sub test_rcs_rename {
274         # can it assume we're under CVS control? or must it check?
275         # rename a file in the same dir
276         # - rcs_commit_staged
277         # - inspect recentchanges: one new change, one file removed, one added
278         # rename a file into a different dir
279         # - rcs_commit_staged
280         # - inspect recentchanges: one new change, one file removed, one added
281         # rename a file into a not-yet-existing dir
282         # - rcs_commit_staged
283         # - inspect recentchanges: one new change, one file removed, one added
284         # is it safe to use "mv"? what if $dest is somehow outside the wiki?
285 }
286
287 sub test_rcs_recentchanges {
288         my @changes = IkiWiki::rcs_recentchanges(3);
289         is_total_number_of_changes(\@changes, 0);
290
291         my $message = "Add a page via CVS directly";
292         my $file = q{test2.mdwn};
293         writefile($file, $config{srcdir}, readfile(q{t/test2.mdwn}));
294         system "cd $config{srcdir}"
295                 . " && cvs add $file >/dev/null 2>&1";
296         system "cd $config{srcdir}"
297                 . " && cvs commit -m \"$message\" $file >/dev/null";
298
299         @changes = IkiWiki::rcs_recentchanges(3);
300         is_total_number_of_changes(\@changes, 1);
301         is_most_recent_change(\@changes, stripext($file), $message);
302
303         # CVS commits run ikiwiki once for every committed file (!)
304         # - commit_prep alone should fix this
305         # CVS multi-dir commits show only the first dir in recentchanges
306         # - commit_prep might also fix this?
307         # CVS post-commit hook is amped off to avoid locking against itself
308         # - commit_prep probably doesn't fix this... but maybe?
309         # can it assume we're under CVS control? or must it check?
310         # don't worry whether we're called with a number (we always are)
311         # other rcs tests already inspect much of the returned structure
312         # CVS commits say "cvs" and get the right committer
313         # web commits say "web" and get the right committer
314         # - and don't start with "web commit {by,from} "
315         # "nickname" -- can we ever meaningfully set this?
316
317         # prefer log_accum, then cvsps, else die
318         # run the high-level recentchanges tests 2x (once for each method)
319         # - including in other test subs that check recentchanges?
320 }
321
322 sub test_rcs_diff {
323         my @changes = IkiWiki::rcs_recentchanges(3);
324         is_total_number_of_changes(\@changes, 0);
325
326         my $message = "add a UTF-8 and an ASCII file in different dirs";
327         my $file1 = "rcsdiff1/utf8.mdwn";
328         my $file2 = "rcsdiff2/ascii.mdwn";
329         my $contents2 = ''; $contents2 .= "$_. foo\n" for (1..11);
330         can_mkdir($_) for (qw(rcsdiff1 rcsdiff2));
331         writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
332         writefile($file2, $config{srcdir}, $contents2);
333         IkiWiki::rcs_add($_) for ($file1, $file2);
334         IkiWiki::rcs_commit_staged(message => $message);
335
336         # XXX we rely on rcs_recentchanges() to be called first!
337         # XXX or else for no cvsps cache to exist yet...
338         # XXX because rcs_diff() doesn't pass -x (as an optimization)
339         @changes = IkiWiki::rcs_recentchanges(3);
340         is_total_number_of_changes(\@changes, 1);
341
342         my $changeset = 1;
343
344         my $maxlines = undef;
345         my $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
346         like(
347                 $scalar_diffs,
348                 qr/^\+11\. foo$/m,
349                 q{unbounded scalar diffs go all the way to 11},
350         );
351         my @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
352         is(
353                 $array_diffs[$#array_diffs],
354                 "+11. foo\n",
355                 q{unbounded array diffs go all the way to 11},
356         );
357
358         $maxlines = 8;
359         $scalar_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
360         unlike(
361                 $scalar_diffs,
362                 qr/^\+11\. foo$/m,
363                 q{bounded scalar diffs don't go all the way to 11},
364         );
365         @array_diffs = IkiWiki::rcs_diff($changeset, $maxlines);
366         isnt(
367                 $array_diffs[$#array_diffs],
368                 "+11. foo\n",
369                 q{bounded array diffs don't go all the way to 11},
370         );
371         is(
372                 scalar @array_diffs,
373                 $maxlines,
374                 q{bounded array diffs contain expected maximum number of lines},
375         );
376
377         # can it assume we're under CVS control? or must it check?
378 }
379
380 sub test_rcs_getctime {
381         # can it assume we're under CVS control? or must it check?
382         # given a file, find its creation time, else return 0
383         # first implement in the obvious way
384         # then cache
385 }
386
387 sub test_rcs_getmtime {
388         # can it assume we're under CVS control? or must it check?
389         # given a file, find its modification time, else return 0
390         # first implement in the obvious way
391         # then cache
392 }
393
394 sub test_rcs_receive {
395         my $description = q{rcs_receive doesn't make sense for CVS};
396         exists $IkiWiki::hooks{rcs}{rcs_receive}
397                 ? fail($description)
398                 : pass($description);
399 }
400
401 sub test_rcs_preprevert {
402         # can it assume we're under CVS control? or must it check?
403         # given a patchset number, return structure describing what'd happen:
404         # - see doc/plugins/write.mdwn:rcs_receive()
405         # don't forget about attachments
406 }
407
408 sub test_rcs_revert {
409         # test rcs_recentchanges() real darn well
410         # extract read-backwards patchset parser from rcs_recentchanges()
411         # recentchanges: given max, return list of changeset/files/etc.
412         # revert: given changeset ID, return list of file/rev/action
413         #
414         # can it assume we're under CVS control? or must it check?
415         # given a patchset number, stage the revert for rcs_commit_staged()
416         # if commit succeeds, return undef
417         # else, warn and return error message (really? or just non-undef?)
418 }
419
420 sub main {
421         my $test_methods = defined $ENV{TEST_METHOD} 
422                          ? $ENV{TEST_METHOD}
423                          : $default_test_methods;
424
425         _startup($test_methods eq $default_test_methods);
426         _runtests(_get_matching_test_subs($test_methods));
427         _shutdown($test_methods eq $default_test_methods);
428 }
429
430 main();
431
432
433 # INTERNAL SUPPORT ROUTINES
434
435 sub _plan_for_test_more {
436         my $can_plan = shift;
437
438         foreach my $program (@required_programs) {
439                 my $program_path = `which $program`;
440                 chomp $program_path;
441                 return plan(skip_all => "$program not available")
442                         unless -x $program_path;
443         }
444
445         foreach my $module (@required_modules) {
446                 eval qq{use $module};
447                 return plan(skip_all => "$module not available")
448                         if $@;
449         }
450
451         return plan(skip_all => "can't create $dir: $!")
452                 unless mkdir($dir);
453         return plan(skip_all => "can't remove $dir: $!")
454                 unless rmdir($dir);
455
456         return unless $can_plan;
457
458         return plan(tests => $total_tests);
459 }
460
461 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
462
463 use B qw/svref_2object/;
464
465 sub in_package {
466         my ($coderef, $package) = @_;
467         my $cv = svref_2object($coderef);
468         return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
469         return $cv->GV->STASH->NAME eq $package;
470 }
471
472 sub list_module {
473         my $module = shift;
474         no strict 'refs';
475         return grep {
476                 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
477         } keys %{"$module\::"};
478 }
479
480
481 # support for xUnit-style testing, a la Test::Class
482
483 sub _startup {
484         my $can_plan = shift;
485         _plan_for_test_more($can_plan);
486         _generate_test_config();
487 }
488
489 sub _shutdown {
490         my $had_plan = shift;
491         done_testing() unless $had_plan;
492 }
493
494 sub _setup {
495         _generate_test_repo();
496 }
497
498 sub _teardown {
499         # XXX does srcdir persist between test subs?
500         system "rm -rf $dir";
501 }
502
503 sub _runtests {
504         my @coderefs = (@_);
505         for (@coderefs) {
506                 _setup();
507                 $_->();
508                 _teardown();
509         }
510 }
511
512 sub _get_matching_test_subs {
513         my $re = shift;
514         no strict 'refs';
515         return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
516 }
517
518 sub _generate_test_config {
519         %config = IkiWiki::defaultconfig();
520         $config{rcs} = "cvs";
521         $config{srcdir} = "$dir/src";
522         $config{cvsrepo} = "$dir/repo";
523         $config{cvspath} = "ikiwiki";
524         IkiWiki::loadplugins();
525         IkiWiki::checkconfig();
526 }
527
528 sub _generate_test_repo {
529         die "can't create $dir: $!"
530                 unless mkdir($dir);
531
532         my $cvs = "cvs -d $config{cvsrepo}";
533         my $dn = ">/dev/null";
534         system "$cvs init $dn";
535         system "mkdir $dir/$config{cvspath} $dn";
536         system "cd $dir/$config{cvspath} && "
537                 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
538         system "rm -rf $dir/$config{cvspath} $dn";
539         system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
540 }
541
542 sub add_and_commit {
543         my ($file, $message, $contents) = @_;
544         writefile($file, $config{srcdir}, $contents);
545         IkiWiki::rcs_add($file);
546         IkiWiki::rcs_commit(
547                 file => $file,
548                 message => $message,
549                 token => "moo",
550         );
551 }
552
553 sub can_mkdir {
554         my $dir = shift;
555         ok(
556                 mkdir($config{srcdir} . "/$dir"),
557                 qq{can mkdir $dir},
558         );
559 }
560
561 sub is_newly_added {
562         my $file = shift;
563         is(
564                 IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
565                 '1.1',
566                 qq{$file is newly added to CVS},
567         );
568 }
569
570 sub is_in_keyword_substitution_mode {
571         my ($file, $mode) = @_;
572         $mode = '(none)' unless defined $mode;
573         is(
574                 IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
575                 $mode,
576                 qq{$file is in CVS with expected keyword substitution mode},
577         );
578 }
579
580 sub is_total_number_of_changes {
581         my ($changes, $expected_total) = @_;
582         is(
583                 $#{$changes},
584                 $expected_total - 1,
585                 qq{total commits == $expected_total},
586         );
587 }
588
589 sub is_most_recent_change {
590         my ($changes, $page, $message) = @_;
591         is(
592                 $changes->[0]{message}[0]{"line"},
593                 $message,
594                 q{most recent commit's first message line matches},
595         );
596         is(
597                 $changes->[0]{pages}[0]{"page"},
598                 $page,
599                 q{most recent commit's first pagename matches},
600         );
601 }
602
603 sub stripext {
604         my ($file, $extension) = @_;
605         $extension = '\..+?' unless defined $extension;
606         $file =~ s|$extension$||g;
607         return $file;
608 }