Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

phillup (4419)

  (email not shown publicly)

Journal of phillup (4419)

Thursday July 22, 2004
12:50 PM

Testing and Environment Variables

[ #19996 ]

Here is the "problem of the moment":

I'm writing tests for some web modules and I've broken the test down into separate files for testing separate things. Now, this is the backend code for a web app and the whole process is started by logging in. This sets up a session with a SID that is passed back to the user and is used in the future to re-establish the session. Pretty standard stuff.

So, what I want to do is create the session in one test file and run some tests... but, I want to store the SID into the %ENV for the next test to use and re-establish the session.

The kicker seems to be that I'm using Test::Harness to drive the testing and it seems to fork off each test, so changes to %ENV aren't propagated to the next script.

I've also tried setting (and exporting) the environment via the system command using every variant I can think of and I keep getting something along the lines of "export file not found".

Now, I know that export is specific to the bash shell so my first thought was that the test aren't even running in a bash shell, even tho I am. Is this true?

So, I changed my system command to:

system ("/bin/bash export GB_TEST_SID=$sid");

Which I thought would at least not cause the error... and... it still reports:

export GB_TEST_SID=<long number>: No such file or directory

So, how do I set the environment variable in one test script to be read in the next one when Test::Harness is running the test scripts?

Or, more generically... how do I set an environment variable in Perl and have it persist (in the shell environment) after the program exits?

(And... I don't wish to imply that this is the fault of Test::Harness. It seems to be an issue of running each script in a separate child process.)



I wanted to take Perl out of the equation for a moment to confirm it was a child process issue... I did this at a bash prompt (comments inserted):

#show the current env
phillip@prometheus:~$ echo $MY_TEST
# launch a child shell
phillip@prometheus:~$ bash
phillip@prometheus:~$ ps
  PID TTY          TIME CMD
28108 pts/12   00:00:00 bash
28119 pts/12   00:00:00 bash
28124 pts/12   00:00:00 ps
# export and confirm
phillip@prometheus:~$ export MY_TEST="hello world"
phillip@prometheus:~$ echo $MY_TEST
hello world
# launch a child from the child
phillip@prometheus:~$ bash
phillip@prometheus:~$ ps
  PID TTY          TIME CMD
28108 pts/12   00:00:00 bash
28119 pts/12   00:00:00 bash
28125 pts/12   00:00:00 bash
28130 pts/12   00:00:00 ps
# check the environmnet... it works
phillip@prometheus:~$ echo $MY_TEST
hello world
# exit the child of the child
phillip@prometheus:~$ exit
# reconfirm env still set
phillip@prometheus:~$ echo $MY_TEST
hello world
# exit the child... now back in the parent
phillip@prometheus:~$ exit
# and test the env
phillip@prometheus:~$ echo $MY_TEST

It looks to me like there isn't a way to set the environment in a child and then have it present in the next sibling.

So... back to the drawing board.

BTW, I'd still like to know why my system command above returned an error... if anyone knows.


The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • A child can't muck with a higher-level process's environment. It's part of the joy of unix!

    Having sequential siblings mess with environment is usually something like:

    in script0:
      eval `script1`
      eval `script2`

    script1 prints out bash commands like "export FOO=bar" which the running then evaluates into its own environment, leaving it there for script2 to see.

    In brief: the environment is not a good way for children to perform IPC.
  • Module::Build has a feature that helps with this, the notes() method. You can get the Module::Build object inside your tests. This is really handy.
    • That does look handy... thanks!

      I just really wish I'd started with this (and the tests!!) two years ago when I started writing the modules!!

      The customer wants some pretty significant changes and I've convinced them that we need some tests to insure that the functionality doesn't degrade with all of the changes... so... I'm basically backporting tests into the system while trying to provide new features.

      And, the new features are needed soon (of course).


      (OK... enough complaining. Back to work.)