about summary refs log tree commit homepage
path: root/lib/PublicInbox/TestCommon.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-04-16 16:10:31 -0700
committerEric Wong <e@80x24.org>2021-04-16 22:24:47 -0400
commit315157c2e0c52e9e7b8078ba5c8b79e0b1a3802e (patch)
treeb490b3ba70a579fefc0bcdcac3621ae5e9f3bf4e /lib/PublicInbox/TestCommon.pm
parent604fa817dcd5d430931e6cfb921bd62f4cddc345 (diff)
downloadpublic-inbox-315157c2e0c52e9e7b8078ba5c8b79e0b1a3802e.tar.gz
We use it in t/lei-q-save.t, and were inadvertently writing
to the worktree.

v2: fix -C $DIR with TEST_RUN_MODE=0
Diffstat (limited to 'lib/PublicInbox/TestCommon.pm')
-rw-r--r--lib/PublicInbox/TestCommon.pm10
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm
index d506e4b5..2627871a 100644
--- a/lib/PublicInbox/TestCommon.pm
+++ b/lib/PublicInbox/TestCommon.pm
@@ -290,6 +290,10 @@ sub run_script ($;$$) {
                 # spawn an independent new process, like real-world use cases:
                 require PublicInbox::Spawn;
                 my $cmd = [ key2script($key), @argv ];
+                if (my $d = $opt->{'-C'}) {
+                        $cmd->[0] = File::Spec->rel2abs($cmd->[0]);
+                        $spawn_opt->{'-C'} = $d;
+                }
                 my $pid = PublicInbox::Spawn::spawn($cmd, $env, $spawn_opt);
                 if (defined $pid) {
                         my $r = waitpid($pid, 0) // die "waitpid: $!";
@@ -302,8 +306,14 @@ sub run_script ($;$$) {
                 local %SIG = %SIG;
                 local $0 = join(' ', @$cmd);
                 my $orig_io = _prepare_redirects($fhref);
+                my $cwdfh;
+                if (my $d = $opt->{'-C'}) {
+                        opendir $cwdfh, '.' or die "opendir .: $!";
+                        chdir $d or die "chdir $d: $!";
+                }
                 _run_sub($sub, $key, \@argv);
                 eval { PublicInbox::Inbox::cleanup_task() };
+                die "chdir(restore): $!" if $cwdfh && !chdir($cwdfh);
                 _undo_redirects($orig_io);
                 select STDOUT;
         }