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