]> sipb.mit.edu Git - ikiwiki.git/blobdiff - t/cvs.t
(no commit message)
[ikiwiki.git] / t / cvs.t
diff --git a/t/cvs.t b/t/cvs.t
index a8f1254ef0a79dda0a0ec2d6ac532dbf6f5a83cd..9afd5a7e9061767f8cc0f1f5ad08d97dcc335c98 100755 (executable)
--- a/t/cvs.t
+++ b/t/cvs.t
 #!/usr/bin/perl
 use warnings;
 use strict;
-use Test::More; my $total_tests = 10;
+use Test::More; my $total_tests = 42;
 use IkiWiki;
 
+my $default_test_methods = '^test_*';
+my @required_programs = qw(
+       cvs
+       cvsps
+);
+my @required_modules = qw(
+       File::chdir
+       File::MimeInfo
+       Date::Parse
+       File::Temp
+       File::ReadBackwards
+);
 my $dir = "/tmp/ikiwiki-test-cvs.$$";
 
-sub _plan {
-       my $can_plan = shift;
-
-       my $cvs = `which cvs`; chomp $cvs;
-       my $cvsps = `which cvsps`; chomp $cvsps;
-       return plan(skip_all => 'cvs or cvsps not available')
-               unless -x $cvs && -x $cvsps;
-
-       foreach my $module (qw(File::ReadBackwards File::MimeInfo)) {
-               eval qq{use $module};
-               if ($@) {
-                       return plan(skip_all => "$module not available");
-               }
-       }
-
-       return plan(skip_all => "can't create $dir: $!")
-               unless mkdir($dir);
-
-       return unless $can_plan;
-
-       return plan(tests => $total_tests);
-}
-
-sub _startup {
-       my $can_plan = shift;
-
-       _plan($can_plan);
-       _generate_minimal_config();
-       _create_test_repo();
-}
-
-sub _shutdown {
-       my $had_plan = shift;
-
-       system "rm -rf $dir";
-       done_testing() unless $had_plan;
-}
-
-sub _generate_minimal_config {
-       %config = IkiWiki::defaultconfig();
-       $config{rcs} = "cvs";
-       $config{srcdir} = "$dir/src";
-       $config{cvsrepo} = "$dir/repo";
-       $config{cvspath} = "ikiwiki";
-       IkiWiki::loadplugins();
-       IkiWiki::checkconfig();
-}
-
-sub _create_test_repo {
-       my $cvs = "cvs -d $config{cvsrepo}";
-       my $dn = ">/dev/null";
-       system "$cvs init $dn";
-       system "mkdir $dir/$config{cvspath} $dn";
-       system "cd $dir/$config{cvspath} && "
-               . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
-       system "rm -rf $dir/$config{cvspath} $dn";
-       system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
-}
-
-sub test_web_add_and_commit {
-       my $message = "Added the first page";
-       writefile('test1.mdwn', $config{srcdir}, readfile("t/test1.mdwn"));
-       IkiWiki::rcs_add("test1.mdwn");
-       IkiWiki::rcs_commit(
-               file => "test1.mdwn",
-               message => $message,
-               token => "moo",
-       );
-
-       my @changes = IkiWiki::rcs_recentchanges(3);
-       is(
-               $#changes,
-               0,
-               q{1 total commit},
-       );
-       is(
-               $changes[0]{message}[0]{"line"},
-               $message,
-               q{first line of most recent commit message matches},
-       );
-       is(
-               $changes[0]{pages}[0]{"page"},
-               "test1",
-               q{first pagename from most recent commit matches},
-       );
+# TESTS FOR GENERAL META-BEHAVIOR
 
-       # prevent web edits from attempting to create .../CVS/foo.mdwn
-       # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
-       # unless your "CVS" is something else and we've made it configurable
+sub test_web_comments {
        # how much of the web-edit workflow are we actually testing?
        # because we want to test comments:
        # - when the first comment for page.mdwn is added, and page/ is
@@ -105,76 +31,12 @@ sub test_web_add_and_commit {
        # - comments.pm treats rcs_commit_staged() as returning conflicts?
 }
 
-sub test_manual_add_and_commit {
-       my $message = "Added the second page";
-       writefile('test2.mdwn', $config{srcdir}, readfile("t/test2.mdwn"));
-       system "cd $config{srcdir}"
-               . " && cvs add test2.mdwn >/dev/null 2>&1";
-       system "cd $config{srcdir}"
-               . " && cvs commit -m \"$message\" test2.mdwn >/dev/null";
-
-       my @changes = IkiWiki::rcs_recentchanges(3);
-       is(
-               $#changes,
-               1,
-               q{2 total commits},
-       );
-       is(
-               $changes[0]{message}[0]{"line"},
-               $message,
-               q{first line of most recent commit message matches},
-       );
-       is(
-               $changes[0]{pages}[0]{"page"},
-               "test2",
-               q{first pagename from most recent commit matches},
-       );
-       is(
-               $changes[1]{pages}[0]{"page"},
-               "test1",
-               q{first pagename from second-most-recent commit matches},
-       );
-
-       # CVS commits run ikiwiki once for every committed file (!)
-       # - commit_prep alone should fix this
-       # CVS multi-dir commits show only the first dir in recentchanges
-       # - commit_prep might also fix this?
-       # CVS post-commit hook is amped off to avoid locking against itself
-       # - commit_prep probably doesn't fix this... but maybe?
-}
-
 sub test_chdir_magic {
        # cvs.pm operations are always occurring inside $config{srcdir}
        # other ikiwiki operations are occurring wherever, and are unaffected
        # when are we bothering with "local $CWD" and when aren't we?
 }
 
-sub test_genwrapper {
-       # testable directly? affects rcs_add, but are we exercising this?
-}
-
-sub test_checkconfig {
-       # undef cvspath, expect "ikiwiki"
-       # define cvspath normally, get it back
-       # define cvspath in a subdir, get it back?
-       # define cvspath with extra slashes, get sanitized version back
-       # - yoink test_extra_path_slashes
-       # undef cvs_wrapper, expect $config{wrappers} same size as before
-
-       my $initial_cvspath = $config{cvspath};
-       $config{cvspath} = "/ikiwiki//";
-       IkiWiki::checkconfig();
-       is(
-               $config{cvspath},
-               $initial_cvspath,
-               q{rcs_recentchanges assumes checkconfig has sanitized cvspath},
-       );
-}
-
-sub test_getsetup {
-       # anything worth testing?
-}
-
 sub test_cvs_info {
        # inspect "Repository revision" (used in code)
        # inspect "Sticky Options" (used in tests to verify existence of "-kb")
@@ -228,6 +90,38 @@ sub test_cvs_is_controlling {
        # - else, die
 }
 
+
+# TESTS FOR GENERAL PLUGIN API CALLS
+
+sub test_checkconfig {
+       # undef cvspath, expect "ikiwiki"
+       # define cvspath normally, get it back
+       # define cvspath in a subdir, get it back?
+       # define cvspath with extra slashes, get sanitized version back
+       # - yoink test_extra_path_slashes
+       # undef cvs_wrapper, expect $config{wrappers} same size as before
+
+       my $initial_cvspath = $config{cvspath};
+       $config{cvspath} = "/ikiwiki//";
+       IkiWiki::checkconfig();
+       is(
+               $config{cvspath},
+               $initial_cvspath,
+               q{rcs_recentchanges assumes checkconfig has sanitized cvspath},
+       );
+}
+
+sub test_getsetup {
+       # anything worth testing?
+}
+
+sub test_genwrapper {
+       # testable directly? affects rcs_add, but are we exercising this?
+}
+
+
+# TESTS FOR VCS PLUGIN API CALLS
+
 sub test_rcs_update {
        # can it assume we're under CVS control? or must it check?
        # anything else worth testing?
@@ -260,37 +154,90 @@ sub test_rcs_commit_staged {
 }
 
 sub test_rcs_add {
-       my $dir1 = "test3";
-       my $dir2 = "test4/test5";
-       ok(
-               mkdir($config{srcdir} . "/$dir1"),
-               qq{can make $dir1},
-       );
+       my @changes = IkiWiki::rcs_recentchanges(3);
+       is_total_number_of_changes(\@changes, 0);
+
+       my $message = "add a top-level ASCII (non-UTF-8) page via VCS API";
+       my $file = q{test0.mdwn};
+       add_and_commit($file, $message, q{* some plain ASCII text});
+       is_newly_added($file);
+       is_in_keyword_substitution_mode($file, undef);
+       @changes = IkiWiki::rcs_recentchanges(3);
+       is_total_number_of_changes(\@changes, 1);
+       is_most_recent_change(\@changes, stripext($file), $message);
+
+       $message = "add a top-level dir via VCS API";
+       my $dir1 = q{test3};
+       can_mkdir($dir1);
        IkiWiki::rcs_add($dir1);
+       # XXX test that the wrapper hangs here without our genwrapper()
+       # XXX test that the wrapper doesn't hang here with it
+       @changes = IkiWiki::rcs_recentchanges(3);
+       is_total_number_of_changes(\@changes, 1);       # despite the dir add
        IkiWiki::rcs_commit(
                file => $dir1,
-               message => "shouldn't happen",
+               message => $message,
                token => "oom",
        );
+       @changes = IkiWiki::rcs_recentchanges(3);
+       is_total_number_of_changes(\@changes, 1);       # dirs aren't tracked
+
+       $message = "add a non-ASCII (UTF-8) text file in an un-added dir";
+       can_mkdir($_) for (qw(test4 test4/test5));
+       $file = q{test4/test5/test1.mdwn};
+       add_and_commit($file, $message, readfile("t/test1.mdwn"));
+       is_newly_added($file);
+       is_in_keyword_substitution_mode($file, undef);
+       @changes = IkiWiki::rcs_recentchanges(3);
+       is_total_number_of_changes(\@changes, 2);
+       is_most_recent_change(\@changes, stripext($file), $message);
+
+       $message = "add a binary file in an un-added dir, and commit_staged";
+       can_mkdir(q{test6});
+       $file = q{test6/test7.ico};
+       my $bindata_in = readfile("doc/favicon.ico", 1);
+       my $bindata_out = sub { readfile($config{srcdir} . "/$file", 1) };
+       writefile($file, $config{srcdir}, $bindata_in, 1);
+       is(&$bindata_out(), $bindata_in, q{binary files match before commit});
+       IkiWiki::rcs_add($file);
+       IkiWiki::rcs_commit_staged(message => $message);
+       is_newly_added($file);
+       is_in_keyword_substitution_mode($file, q{-kb});
+       is(&$bindata_out(), $bindata_in, q{binary files match after commit});
+       @changes = IkiWiki::rcs_recentchanges(3);
+       is_total_number_of_changes(\@changes, 3);
+       is_most_recent_change(\@changes, $file, $message);
+       ok(
+               unlink($config{srcdir} . "/$file"),
+               q{can remove file in order to re-fetch it from repo},
+       );
+       ok(! -e $config{srcdir} . "/$file", q{really removed file});
+       IkiWiki::rcs_update();
+       is(&$bindata_out(), $bindata_in, q{binary files match after re-fetch});
+
+       $message = "add a UTF-8 and a binary file in different dirs";
+       my $file1 = "test8/test9.mdwn";
+       my $file2 = "test10/test11.ico";
+       can_mkdir(qw(test8 test10));
+       writefile($file1, $config{srcdir}, readfile('t/test2.mdwn'));
+       writefile($file2, $config{srcdir}, $bindata_in, 1);
+       IkiWiki::rcs_add($_) for ($file1, $file2);
+       IkiWiki::rcs_commit_staged(message => $message);
+       is_newly_added($_) for ($file1, $file2);
+       is_in_keyword_substitution_mode($file1, undef);
+       is_in_keyword_substitution_mode($file2, '-kb');
+       @changes = IkiWiki::rcs_recentchanges(3);
+       is_total_number_of_changes(\@changes, 3);
+       @changes = IkiWiki::rcs_recentchanges(4);
+       is_total_number_of_changes(\@changes, 4);
+       # XXX test for both files in the commit, and no other files
+       is_most_recent_change(\@changes, $file2, $message);
+
+       # prevent web edits from attempting to create .../CVS/foo.mdwn
+       # on case-insensitive filesystems, also prevent .../cvs/foo.mdwn
+       # unless your "CVS" is something else and we've made it configurable
 
        # can it assume we're under CVS control? or must it check?
-       # add a top-level text file
-       # - rcs_commit it
-       # - inspect recentchanges: new change, no -kb
-       # add a top-level dir
-       # - test mustn't hang (does it hang if we comment out genwrapper?)
-       # - inspect recentchanges: no new change
-       # - rcs_commit it
-       # - reinspect recentchanges: still no new change
-       # add a text file in that dir
-       # - rcs_commit_staged
-       # - inspect recentchanges: new change, no -kb
-       # add a top-level dir + add a binary file in it
-       # - rcs_commit_staged
-       # - inspect recentchanges: new change, yes -kb
-       # add a top-level dir + subdir + add one text and one binary file in it
-       # - rcs_commit_staged
-       # - inspect recentchanges: one new change, two files, one -kb, one not
 
        # extract method: filetype-guessing
        # add a binary file, remove it, add a text file by same name, no -kb?
@@ -322,6 +269,27 @@ sub test_rcs_rename {
 }
 
 sub test_rcs_recentchanges {
+       my @changes = IkiWiki::rcs_recentchanges(3);
+       is_total_number_of_changes(\@changes, 0);
+
+       my $message = "Add a page via CVS directly";
+       my $file = q{test2.mdwn};
+       writefile($file, $config{srcdir}, readfile(q{t/test2.mdwn}));
+       system "cd $config{srcdir}"
+               . " && cvs add $file >/dev/null 2>&1";
+       system "cd $config{srcdir}"
+               . " && cvs commit -m \"$message\" $file >/dev/null";
+
+       @changes = IkiWiki::rcs_recentchanges(3);
+       is_total_number_of_changes(\@changes, 1);
+       is_most_recent_change(\@changes, stripext($file), $message);
+
+       # CVS commits run ikiwiki once for every committed file (!)
+       # - commit_prep alone should fix this
+       # CVS multi-dir commits show only the first dir in recentchanges
+       # - commit_prep might also fix this?
+       # CVS post-commit hook is amped off to avoid locking against itself
+       # - commit_prep probably doesn't fix this... but maybe?
        # can it assume we're under CVS control? or must it check?
        # don't worry whether we're called with a number (we always are)
        # other rcs tests already inspect much of the returned structure
@@ -356,7 +324,10 @@ sub test_rcs_getmtime {
 }
 
 sub test_rcs_receive {
-       pass(q{rcs_receive doesn't make sense for CVS});
+       my $description = q{rcs_receive doesn't make sense for CVS};
+       exists $IkiWiki::hooks{rcs}{rcs_receive}
+               ? fail($description)
+               : pass($description);
 }
 
 sub test_rcs_preprevert {
@@ -367,6 +338,11 @@ sub test_rcs_preprevert {
 }
 
 sub test_rcs_revert {
+       # test rcs_recentchanges() real darn well
+       # extract read-backwards patchset parser from rcs_recentchanges()
+       # recentchanges: given max, return list of changeset/files/etc.
+       # revert: given changeset ID, return list of file/rev/action
+       #
        # can it assume we're under CVS control? or must it check?
        # given a patchset number, stage the revert for rcs_commit_staged()
        # if commit succeeds, return undef
@@ -374,25 +350,44 @@ sub test_rcs_revert {
 }
 
 sub main {
-       my $default_test_methods = '^test_*';
        my $test_methods = defined $ENV{TEST_METHOD} 
                         ? $ENV{TEST_METHOD}
                         : $default_test_methods;
 
        _startup($test_methods eq $default_test_methods);
-       $_->() foreach get_test_subs($test_methods);
+       _runtests(_get_matching_test_subs($test_methods));
        _shutdown($test_methods eq $default_test_methods);
 }
 
 main();
 
-################################################################################
-# don't want a dependency on Test::Class; do want a couple of its features
 
-sub get_test_subs {
-       my $re = shift;
-       no strict 'refs';
-       return map { \&{*$_} } grep { /$re/ } list_module('main');
+# INTERNAL SUPPORT ROUTINES
+
+sub _plan_for_test_more {
+       my $can_plan = shift;
+
+       foreach my $program (@required_programs) {
+               my $program_path = `which $program`;
+               chomp $program_path;
+               return plan(skip_all => "$program not available")
+                       unless -x $program_path;
+       }
+
+       foreach my $module (@required_modules) {
+               eval qq{use $module};
+               return plan(skip_all => "$module not available")
+                       if $@;
+       }
+
+       return plan(skip_all => "can't create $dir: $!")
+               unless mkdir($dir);
+       return plan(skip_all => "can't remove $dir: $!")
+               unless rmdir($dir);
+
+       return unless $can_plan;
+
+       return plan(tests => $total_tests);
 }
 
 # http://stackoverflow.com/questions/607282/whats-the-best-way-to-discover-all-subroutines-a-perl-module-has
@@ -413,3 +408,132 @@ sub list_module {
                defined &{"$module\::$_"} and in_package(\&{*$_}, $module)
        } keys %{"$module\::"};
 }
+
+
+# support for xUnit-style testing, a la Test::Class
+
+sub _startup {
+       my $can_plan = shift;
+       _plan_for_test_more($can_plan);
+       _generate_test_config();
+}
+
+sub _shutdown {
+       my $had_plan = shift;
+       done_testing() unless $had_plan;
+}
+
+sub _setup {
+       _generate_test_repo();
+}
+
+sub _teardown {
+       system "rm -rf $dir";
+}
+
+sub _runtests {
+       my @coderefs = (@_);
+       for (@coderefs) {
+               _setup();
+               $_->();
+               _teardown();
+       }
+}
+
+sub _get_matching_test_subs {
+       my $re = shift;
+       no strict 'refs';
+       return map { \&{*$_} } grep { /$re/ } sort(list_module('main'));
+}
+
+sub _generate_test_config {
+       %config = IkiWiki::defaultconfig();
+       $config{rcs} = "cvs";
+       $config{srcdir} = "$dir/src";
+       $config{cvsrepo} = "$dir/repo";
+       $config{cvspath} = "ikiwiki";
+       IkiWiki::loadplugins();
+       IkiWiki::checkconfig();
+}
+
+sub _generate_test_repo {
+       die "can't create $dir: $!"
+               unless mkdir($dir);
+
+       my $cvs = "cvs -d $config{cvsrepo}";
+       my $dn = ">/dev/null";
+       system "$cvs init $dn";
+       system "mkdir $dir/$config{cvspath} $dn";
+       system "cd $dir/$config{cvspath} && "
+               . "$cvs import -m import $config{cvspath} VENDOR RELEASE $dn";
+       system "rm -rf $dir/$config{cvspath} $dn";
+       system "$cvs co -d $config{srcdir} $config{cvspath} $dn";
+}
+
+sub add_and_commit {
+       my ($file, $message, $contents) = @_;
+       writefile($file, $config{srcdir}, $contents);
+       IkiWiki::rcs_add($file);
+       IkiWiki::rcs_commit(
+               file => $file,
+               message => $message,
+               token => "moo",
+       );
+}
+
+sub can_mkdir {
+       my $dir = shift;
+       ok(
+               mkdir($config{srcdir} . "/$dir"),
+               qq{can mkdir $dir},
+       );
+}
+
+sub is_newly_added {
+       my $file = shift;
+       is(
+               IkiWiki::Plugin::cvs::cvs_info("Repository revision", $file),
+               '1.1',
+               qq{$file is newly added to CVS},
+       );
+}
+
+sub is_in_keyword_substitution_mode {
+       my ($file, $mode) = @_;
+       $mode = '(none)' unless defined $mode;
+       is(
+               IkiWiki::Plugin::cvs::cvs_info("Sticky Options", $file),
+               $mode,
+               qq{$file is in CVS with expected keyword substitution mode},
+       );
+}
+
+sub is_total_number_of_changes {
+       my ($changes, $expected_total) = @_;
+       is(
+               $#{$changes},
+               $expected_total - 1,
+               qq{total commits == $expected_total},
+       );
+}
+
+sub is_most_recent_change {
+       my ($changes, $page, $message) = @_;
+       is(
+               $changes->[0]{message}[0]{"line"},
+               $message,
+               q{most recent commit's first message line matches},
+       );
+       is(
+               $changes->[0]{pages}[0]{"page"},
+               $page,
+               q{most recent commit's first pagename matches},
+       );
+}
+
+sub stripext {
+       my ($file, $extension) = @_;
+       $extension = '\..+?' unless defined $extension;
+       $file =~ s|$extension$||g;
+       return $file;
+}