From b30cacdf8da07f40af03f1b26021d8ec4d8b8b4c Mon Sep 17 00:00:00 2001 From: Amitai Schlair Date: Sun, 27 Jan 2013 22:09:57 -0500 Subject: [PATCH] Fix longstanding bug (chdir to nonexistent dirs). In test, set up the post-commit hook for more realism (and bugs!). To make wrappers work in test, set PERL5LIB, and allow the wrappee's path to be overridden. Meta-test that post-commit is really hooked up by verifying that content is getting generated in destdir. About the longstanding bug, which as far as I know was harmless: CVS can't operate outside a srcdir, so we're always setting $CWD. "local $CWD" restores the previous value when we go out of scope. Usually that's correct. But if we're removing the last file from a directory, the post-commit hook will exec in a working directory that's about to not exist (CVS will prune it). The fix: chdir() manually in cvs_runcvs(), so we can selectively not chdir() back. --- IkiWiki/Plugin/cvs.pm | 13 +++++++++- IkiWiki/Wrapper.pm | 3 ++- t/cvs.t | 56 +++++++++++++++++++++++++++++++++++-------- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/IkiWiki/Plugin/cvs.pm b/IkiWiki/Plugin/cvs.pm index 759ea1c23..bccab3159 100644 --- a/IkiWiki/Plugin/cvs.pm +++ b/IkiWiki/Plugin/cvs.pm @@ -502,7 +502,16 @@ sub cvs_runcvs(@) { my @cmd = @_; unshift @cmd, 'cvs', '-Q'; - local $CWD = $config{srcdir}; + # CVS can't operate outside a srcdir, so we're always setting $CWD. + # "local $CWD" restores the previous value when we go out of scope. + # Usually that's correct. But if we're removing the last file from + # a directory, the post-commit hook will exec in a working directory + # that's about to not exist (CVS will prune it). + # + # chdir() manually here, so we can selectively not chdir() back. + + my $oldcwd = $CWD; + chdir($config{srcdir}); eval q{ use IPC::Open3; @@ -530,6 +539,8 @@ sub cvs_runcvs(@) { print STDOUT $cvsout; print STDERR $cvserr; + chdir($oldcwd) if -d $oldcwd; + return ($ret == 0) ? 1 : 0; } diff --git a/IkiWiki/Wrapper.pm b/IkiWiki/Wrapper.pm index 06be36dfc..84b4b5a2f 100644 --- a/IkiWiki/Wrapper.pm +++ b/IkiWiki/Wrapper.pm @@ -28,10 +28,11 @@ sub gen_wrappers () { %config=(%origconfig); } +our $program_to_wrap = $0; sub gen_wrapper () { $config{srcdir}=File::Spec->rel2abs($config{srcdir}); $config{destdir}=File::Spec->rel2abs($config{destdir}); - my $this=File::Spec->rel2abs($0); + my $this=File::Spec->rel2abs($program_to_wrap); if (! -x $this) { error(sprintf(gettext("%s doesn't seem to be executable"), $this)); } diff --git a/t/cvs.t b/t/cvs.t index 2f9056b45..d9b9ec48e 100755 --- a/t/cvs.t +++ b/t/cvs.t @@ -1,7 +1,7 @@ #!/usr/bin/perl use warnings; use strict; -use Test::More; my $total_tests = 69; +use Test::More; my $total_tests = 71; use IkiWiki; my $default_test_methods = '^test_*'; @@ -33,12 +33,7 @@ sub test_web_comments { } 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? - # after commit, presumably only with post-commit hook enabled: - #> Use of chdir('') or chdir(undef) as chdir() is deprecated at - #> /usr/pkg/lib/perl5/vendor_perl/5.14.0/File/chdir.pm line 45. } sub test_cvs_info { @@ -178,7 +173,6 @@ sub test_rcs_commit { # - else, revert + return content with the conflict markers in it # git.pm receives "session" param -- useful here? # web commits start with "web commit {by,from} " - # seeing File::chdir errors on commit? # XXX commit can fail due to "could not open lock file" } @@ -199,9 +193,16 @@ sub test_rcs_add { is_in_keyword_substitution_mode($file, q{-kkv}); like( readfile($config{srcdir} . "/$file"), - qr/^# \$Id: $file,v 1.1 .+\$$/m, + qr/^# \$Id: $file,v 1\.1 .+\$$/m, q{can expand RCS Id keyword}, ); + my $generated_file = $config{destdir} . q{/test0/index.html}; + ok(-e $generated_file, q{post-commit hook generates content}); + like( + readfile($generated_file), + qr/^

\$Id: $file,v 1\.1 .+\$<\/h1>$/m, + q{can htmlize mdwn, including RCS Id}, + ); @changes = IkiWiki::rcs_recentchanges(3); is_total_number_of_changes(\@changes, 1); is_most_recent_change(\@changes, stripext($file), $message); @@ -276,9 +277,9 @@ sub test_rcs_add { $message = "remove the UTF-8 and binary files we just added"; IkiWiki::rcs_remove($_) for ($file1, $file2); IkiWiki::rcs_commit_staged(message => $message); - ok(-d "$config{srcdir}/test8", q{empty dir not pruned (1)}); + ok(! -d "$config{srcdir}/test8", q{empty dir pruned by post-commit}); + ok(! -d "$config{srcdir}/test10", q{empty dir pruned by post-commit}); @changes = IkiWiki::rcs_recentchanges(11); - ok(-d "$config{srcdir}/test8", q{empty dir not pruned (2)}); is_total_number_of_changes(\@changes, 5); # XXX test for both files in the commit, and no other files is_most_recent_change(\@changes, $file2, $message); @@ -531,6 +532,7 @@ sub list_module { sub _startup { my $can_plan = shift; _plan_for_test_more($can_plan); + hook(type => "genwrapper", id => "cvstest", call => \&_wrapper_paths); _generate_test_config(); } @@ -566,8 +568,11 @@ sub _generate_test_config { %config = IkiWiki::defaultconfig(); $config{rcs} = "cvs"; $config{srcdir} = "$dir/src"; + $config{allow_symlinks_before_srcdir} = 1; + $config{destdir} = "$dir/dest"; $config{cvsrepo} = "$dir/repo"; $config{cvspath} = "ikiwiki"; + use Cwd; $config{templatedir} = getcwd() . '/templates'; IkiWiki::loadplugins(); IkiWiki::checkconfig(); } @@ -578,12 +583,39 @@ sub _generate_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"; + + _generate_and_configure_post_commit_hook(); +} + +sub _generate_and_configure_post_commit_hook { + $config{cvs_wrapper} = $config{cvsrepo} . "/CVSROOT/test-post"; + $config{wrapper} = $config{cvs_wrapper}; + + require IkiWiki::Wrapper; + { + no warnings 'once'; + $IkiWiki::program_to_wrap = 'ikiwiki.out'; + # XXX substitute its interpreter to Makefile's $(PERL) + # XXX best solution: do this to all scripts during build + } + IkiWiki::gen_wrapper(); + + my $cvs = "cvs -d $config{cvsrepo}"; + my $dn = ">/dev/null"; + + system "mkdir $config{destdir} $dn"; + system "cd $dir && $cvs co CVSROOT $dn && cd CVSROOT && " . + "echo 'DEFAULT $config{cvsrepo}/CVSROOT/test-post %{sVv} &' " + . " >> loginfo && " + . "$cvs commit -m 'test repo setup' $dn && " + . "cd .. && rm -rf CVSROOT"; } sub add_and_commit { @@ -663,3 +695,7 @@ sub stripext { $file =~ s|$extension$||g; return $file; } + +sub _wrapper_paths { + return qq{newenviron[i++]="PERL5LIB=$ENV{PERL5LIB}";}; +} -- 2.32.0.93.g670b81a890