]> sipb.mit.edu Git - ikiwiki.git/blob - t/cvs.t
bb6e3e90ac6f44d4801d2ba2a2dc3e87ab7aae95
[ikiwiki.git] / t / cvs.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More; my $total_tests = 9;
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_add_and_commit {
24         my $message = "Add a page via VCS API";
25         writefile('test1.mdwn', $config{srcdir}, readfile("t/test1.mdwn"));
26         IkiWiki::rcs_add("test1.mdwn");
27         IkiWiki::rcs_commit(
28                 file => "test1.mdwn",
29                 message => $message,
30                 token => "moo",
31         );
32
33         my @changes = IkiWiki::rcs_recentchanges(3);
34         is(
35                 $#changes,
36                 0,
37                 q{1 total commit},
38         );
39         is(
40                 $changes[0]{message}[0]{"line"},
41                 $message,
42                 q{first line of most recent commit message matches},
43         );
44         is(
45                 $changes[0]{pages}[0]{"page"},
46                 "test1",
47                 q{first pagename from most recent commit matches},
48         );
49
50         # prevent web edits from attempting to create .../CVS/foo.mdwn
51         # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
52         # unless your "CVS" is something else and we've made it configurable
53         # how much of the web-edit workflow are we actually testing?
54         # because we want to test comments:
55         # - when the first comment for page.mdwn is added, and page/ is
56         #   created to hold the comment, page/ isn't added to CVS control,
57         #   so the comment isn't either
58         # - side effect for moderated comments: after approval they
59         #   show up normally AND are still pending, too
60         # - comments.pm treats rcs_commit_staged() as returning conflicts?
61 }
62
63 sub test_manual_add_and_commit {
64         my $message = "Add a page via CVS directly";
65         writefile('test2.mdwn', $config{srcdir}, readfile("t/test2.mdwn"));
66         system "cd $config{srcdir}"
67                 . " && cvs add test2.mdwn >/dev/null 2>&1";
68         system "cd $config{srcdir}"
69                 . " && cvs commit -m \"$message\" test2.mdwn >/dev/null";
70
71         my @changes = IkiWiki::rcs_recentchanges(3);
72         is(
73                 $#changes,
74                 0,
75                 q{1 total commit},
76         );
77         is(
78                 $changes[0]{message}[0]{"line"},
79                 $message,
80                 q{first line of most recent commit message matches},
81         );
82         is(
83                 $changes[0]{pages}[0]{"page"},
84                 "test2",
85                 q{first pagename from most recent commit matches},
86         );
87
88         # CVS commits run ikiwiki once for every committed file (!)
89         # - commit_prep alone should fix this
90         # CVS multi-dir commits show only the first dir in recentchanges
91         # - commit_prep might also fix this?
92         # CVS post-commit hook is amped off to avoid locking against itself
93         # - commit_prep probably doesn't fix this... but maybe?
94 }
95
96 sub test_chdir_magic {
97         # cvs.pm operations are always occurring inside $config{srcdir}
98         # other ikiwiki operations are occurring wherever, and are unaffected
99         # when are we bothering with "local $CWD" and when aren't we?
100 }
101
102 sub test_cvs_info {
103         # inspect "Repository revision" (used in code)
104         # inspect "Sticky Options" (used in tests to verify existence of "-kb")
105 }
106
107 sub test_cvs_run_cvs {
108         # extract the stdout-redirect thing
109         # - prove that it silences stdout
110         # - prove that stderr comes through just fine
111         # prove that when cvs exits nonzero (fail), function exits false
112         # prove that when cvs exits zero (success), function exits true
113         # always pass -f, just in case
114         # steal from git.pm: safe_git(), run_or_{die,cry,non}
115         # - open() instead of system()
116         # always call cvs_run_cvs(), don't ever run 'cvs' directly
117 }
118
119 sub test_cvs_run_cvsps {
120         # parameterize command like run_cvs()
121         # expose config vars for e.g. "--cvs-direct -z 30"
122         # always pass -x (unless proven otherwise)
123         # always pass -b HEAD (configurable like gitmaster_branch?)
124 }
125
126 sub test_cvs_parse_cvsps {
127         # extract method from rcs_recentchanges
128         # document expected changeset format
129         # document expected changeset delimiter
130         # try: cvsps -q -x -p && ls | sort -rn | head -100
131         # - benchmark against current impl (that uses File::ReadBackwards)
132 }
133
134 sub test_cvs_parse_log_accum {
135         # add new, preferred method for rcs_recentchanges to use
136         # teach log_accum to record commits (into transient?)
137         # script cvsps to bootstrap (or replace?) commit history
138         # teach ikiwiki-makerepo to set up log_accum and commit_prep
139         # why are NetBSD commit mails unreliable?
140         # - is it working for CVS commits and failing for web commits?
141 }
142
143 sub test_cvs_is_controlling {
144         # with no args:
145         # - if srcdir is in CVS, return true
146         # - else, return false
147         # with a dir arg:
148         # - if dir is in CVS, return true
149         # - else, return false
150         # with a file arg:
151         # - is there anything that wants the answer? if so, answer
152         # - else, die
153 }
154
155
156 # TESTS FOR GENERAL PLUGIN API CALLS
157
158 sub test_checkconfig {
159         # undef cvspath, expect "ikiwiki"
160         # define cvspath normally, get it back
161         # define cvspath in a subdir, get it back?
162         # define cvspath with extra slashes, get sanitized version back
163         # - yoink test_extra_path_slashes
164         # undef cvs_wrapper, expect $config{wrappers} same size as before
165
166         my $initial_cvspath = $config{cvspath};
167         $config{cvspath} = "/ikiwiki//";
168         IkiWiki::checkconfig();
169         is(
170                 $config{cvspath},
171                 $initial_cvspath,
172                 q{rcs_recentchanges assumes checkconfig has sanitized cvspath},
173         );
174 }
175
176 sub test_getsetup {
177         # anything worth testing?
178 }
179
180 sub test_genwrapper {
181         # testable directly? affects rcs_add, but are we exercising this?
182 }
183
184
185 # TESTS FOR VCS PLUGIN API CALLS
186
187 sub test_rcs_update {
188         # can it assume we're under CVS control? or must it check?
189         # anything else worth testing?
190 }
191
192 sub test_rcs_prepedit {
193         # can it assume we're under CVS control? or must it check?
194         # for existing file, returns latest revision in repo
195         # - what's this used for? should it return latest revision in checkout?
196         # for new file, returns empty string
197 }
198
199 sub test_rcs_commit {
200         # can it assume we're under CVS control? or must it check?
201         # if someone else changed the page since rcs_prepedit was called:
202         # - try to merge into our working copy
203         # - if merge succeeds, proceed to commit
204         # - else, return page content with the conflict markers in it
205         # commit:
206         # - if success, return undef
207         # - else, revert + return content with the conflict markers in it
208         # git.pm receives "session" param -- useful here?
209         # web commits start with "web commit {by,from} "
210         # seeing File::chdir errors on commit?
211 }
212
213 sub test_rcs_commit_staged {
214         # if commit succeeds, return undef
215         # else, warn and return error message (really? or just non-undef?)
216 }
217
218 sub test_rcs_add {
219         my $dir1 = "test3";
220         my $dir2 = "test4/test5";
221         ok(
222                 mkdir($config{srcdir} . "/$dir1"),
223                 qq{can make $dir1},
224         );
225         IkiWiki::rcs_add($dir1);
226         IkiWiki::rcs_commit(
227                 file => $dir1,
228                 message => "shouldn't happen",
229                 token => "oom",
230         );
231
232         # can it assume we're under CVS control? or must it check?
233         # add a top-level text file
234         # - rcs_commit it
235         # - inspect recentchanges: new change, no -kb
236         # add a top-level dir
237         # - test mustn't hang (does it hang if we comment out genwrapper?)
238         # - inspect recentchanges: no new change
239         # - rcs_commit it
240         # - reinspect recentchanges: still no new change
241         # add a text file in that dir
242         # - rcs_commit_staged
243         # - inspect recentchanges: new change, no -kb
244         # add a top-level dir + add a binary file in it
245         # - rcs_commit_staged
246         # - inspect recentchanges: new change, yes -kb
247         # add a top-level dir + subdir + add one text and one binary file in it
248         # - rcs_commit_staged
249         # - inspect recentchanges: one new change, two files, one -kb, one not
250
251         # extract method: filetype-guessing
252         # add a binary file, remove it, add a text file by same name, no -kb?
253         # add a text file, remove it, add a binary file by same name, -kb?
254 }
255
256 sub test_rcs_remove {
257         # can it assume we're under CVS control? or must it check?
258         # remove a top-level file
259         # - rcs_commit
260         # - inspect recentchanges: one new change, file removed
261         # remove two files (in different dirs)
262         # - rcs_commit_staged
263         # - inspect recentchanges: one new change, both files removed
264 }
265
266 sub test_rcs_rename {
267         # can it assume we're under CVS control? or must it check?
268         # rename a file in the same dir
269         # - rcs_commit_staged
270         # - inspect recentchanges: one new change, one file removed, one added
271         # rename a file into a different dir
272         # - rcs_commit_staged
273         # - inspect recentchanges: one new change, one file removed, one added
274         # rename a file into a not-yet-existing dir
275         # - rcs_commit_staged
276         # - inspect recentchanges: one new change, one file removed, one added
277         # is it safe to use "mv"? what if $dest is somehow outside the wiki?
278 }
279
280 sub test_rcs_recentchanges {
281         # can it assume we're under CVS control? or must it check?
282         # don't worry whether we're called with a number (we always are)
283         # other rcs tests already inspect much of the returned structure
284         # CVS commits say "cvs" and get the right committer
285         # web commits say "web" and get the right committer
286         # - and don't start with "web commit {by,from} "
287         # "nickname" -- can we ever meaningfully set this?
288
289         # prefer log_accum, then cvsps, else die
290         # run the high-level recentchanges tests 2x (once for each method)
291         # - including in other test subs that check recentchanges?
292 }
293
294 sub test_rcs_diff {
295         # can it assume we're under CVS control? or must it check?
296         # in list context, return all lines (with \n), up to $maxlines if set
297         # in scalar context, return the whole diff, up to $maxlines if set
298 }
299
300 sub test_rcs_getctime {
301         # can it assume we're under CVS control? or must it check?
302         # given a file, find its creation time, else return 0
303         # first implement in the obvious way
304         # then cache
305 }
306
307 sub test_rcs_getmtime {
308         # can it assume we're under CVS control? or must it check?
309         # given a file, find its modification time, else return 0
310         # first implement in the obvious way
311         # then cache
312 }
313
314 sub test_rcs_receive {
315         pass(q{rcs_receive doesn't make sense for CVS});
316 }
317
318 sub test_rcs_preprevert {
319         # can it assume we're under CVS control? or must it check?
320         # given a patchset number, return structure describing what'd happen:
321         # - see doc/plugins/write.mdwn:rcs_receive()
322         # don't forget about attachments
323 }
324
325 sub test_rcs_revert {
326         # can it assume we're under CVS control? or must it check?
327         # given a patchset number, stage the revert for rcs_commit_staged()
328         # if commit succeeds, return undef
329         # else, warn and return error message (really? or just non-undef?)
330 }
331
332 sub main {
333         my $test_methods = defined $ENV{TEST_METHOD} 
334                          ? $ENV{TEST_METHOD}
335                          : $default_test_methods;
336
337         _startup($test_methods eq $default_test_methods);
338         _runtests(_get_matching_test_subs($test_methods));
339         _shutdown($test_methods eq $default_test_methods);
340 }
341
342 main();
343
344
345 # INTERNAL SUPPORT ROUTINES
346
347 sub _plan_for_test_more {
348         my $can_plan = shift;
349
350         foreach my $program (@required_programs) {
351                 my $program_path = `which $program`;
352                 chomp $program_path;
353                 return plan(skip_all => "$program not available")
354                         unless -x $program_path;
355         }
356
357         foreach my $module (@required_modules) {
358                 eval qq{use $module};
359                 return plan(skip_all => "$module not available")
360                         if $@;
361         }
362
363         return plan(skip_all => "can't create $dir: $!")
364                 unless mkdir($dir);
365         return plan(skip_all => "can't remove $dir: $!")
366                 unless rmdir($dir);
367
368         return unless $can_plan;
369
370         return plan(tests => $total_tests);
371 }
372
373 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
374
375 use B qw/svref_2object/;
376
377 sub in_package {
378         my ($coderef, $package) = @_;
379         my $cv = svref_2object($coderef);
380         return if not $cv->isa('B::CV') or $cv->GV->isa('B::SPECIAL');
381         return $cv->GV->STASH->NAME eq $package;
382 }
383
384 sub list_module {
385         my $module = shift;
386         no strict 'refs';
387         return grep {
388                 defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
389         } keys %{"$module\::"};
390 }
391
392
393 # support for xUnit-style testing, a la Test::Class
394
395 sub _startup {
396         my $can_plan = shift;
397         _plan_for_test_more($can_plan);
398         _generate_test_config();
399 }
400
401 sub _shutdown {
402         my $had_plan = shift;
403         done_testing() unless $had_plan;
404 }
405
406 sub _setup {
407         _generate_test_repo();
408 }
409
410 sub _teardown {
411         system "rm -rf $dir";
412 }
413
414 sub _runtests {
415         my @coderefs = (@_);
416         for (@coderefs) {
417                 _setup();
418                 $_->();
419                 _teardown();
420         }
421 }
422
423 sub _get_matching_test_subs {
424         my $re = shift;
425         no strict 'refs';
426         return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
427 }
428
429 sub _generate_test_config {
430         %config = IkiWiki::defaultconfig();
431         $config{rcs} = "cvs";
432         $config{srcdir} = "$dir/src";
433         $config{cvsrepo} = "$dir/repo";
434         $config{cvspath} = "ikiwiki";
435         IkiWiki::loadplugins();
436         IkiWiki::checkconfig();
437 }
438
439 sub _generate_test_repo {
440         die "can't create $dir: $!"
441                 unless mkdir($dir);
442
443         my $cvs = "cvs -d $config{cvsrepo}";
444         my $dn = ">/dev/null";
445         system "$cvs init $dn";
446         system "mkdir $dir/$config{cvspath} $dn";
447         system "cd $dir/$config{cvspath} && "
448                 . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
449         system "rm -rf $dir/$config{cvspath} $dn";
450         system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
451 }