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