How do I chdir in an exec?

Michael G Schwern schwern at pobox.com
Sun Mar 8 20:21:13 GMT 2009


Let's say I have a layout like this:

ext/Sub-Module/t/foo.t
ext/Other-SubModule/t/bar.t

And I want to run each test in its own ext/$Sub-Module directory.  How can I
do this?

The most straightforward doesn't work...

exec => sub {
    ...derive $sub_module, $command and $file...

    chdir "ext/$sub_module";
    return [$command, $file];
}

because there's no opportunity to chdir back.

I can't use an end-of-scope trigger object because it needs to remain chdir'd
until after the command has been run somewhere inside TAP::Harness.

I can't chdir as part of the command, "cd ext/$sub_module && $command",
because TAP::Harness is looking for a single command not a shell program.

I could remember where I was like this:

  my $Orig_Dir = abs_path;

  ...

  exec => sub {
      ...derive $sub_module, $command and $file...

      chdir $Orig_Dir;
      chdir "ext/$sub_module";
      return "$command $file";
  }

but that seems needlessly complicated.  Finally I can write an exec script
that does the chdir'ing but I've never liked that option as it splits up what
should be straight forward code.

Something that seems easy is possible but complicated.

A simple solution to this problem would seem to be allowing exec (or a new
option) to return a filehandle in place of a command to run.

  exec => sub {
      ...derive $sub_module, $command and $file...

      chdir "ext/$sub_module";
      open my $fh, "-|", $command, $file;
      chdir "../..";

      return $fh;
  }

This gives the user complete control over the running of the test without
having to bail out to a separate script.


-- 
Just call me 'Moron Sugar'.
	http://www.somethingpositive.net/sp05182002.shtml



More information about the tapx-dev mailing list