diff options
-rw-r--r-- | NEWS | 8 | ||||
-rw-r--r-- | README.yarn | 9 | ||||
-rwxr-xr-x | yarn | 28 | ||||
-rwxr-xr-x | yarn.tests/env.script | 18 |
4 files changed, 62 insertions, 1 deletions
@@ -3,6 +3,14 @@ NEWS for cmdtest This file summarizes changes between releases of cmdtest. +Version 0.10, released UNRELEASED +--------------------------------- + +* Yarn now cleans the environment when it runs shell commands for the + implementation steps. It also sets the `SRCDIR` environment variable + to point at the root of the source tree (the directory where yarn + was invoked from). + Version 0.9, released 2013-07-23 -------------------------------- diff --git a/README.yarn b/README.yarn index 76d57b5..16f7522 100644 --- a/README.yarn +++ b/README.yarn @@ -136,6 +136,15 @@ The following keywords are defined. The test runner creates a temporary directory, whose name is given to the shell code in the `DATADIR` environment variable. + The test runner sets the `SRCDIR` environment variable to the + path to the directory it was invoked from (by convention, the + root of the source tree of the project). + + The test runner removes all other environment variables, except + `TERM`, `USER`, `USERNAME`, `LOGNAME`, `HOME`, and `PATH`. It also + forces `SHELL` set to `/bin/sh`, and `LC_ALL` set to `C`, in order + to have as clean an environment as possible for tests to run in. + The shell commands get invoked with `/bin/sh -eu`, and need to be written accordingly. Be careful about commands that return a non-zero exit code. There will eventually be a library of shell @@ -267,6 +267,31 @@ class YarnRunner(cliapp.Application): return ok + def clean_env(self): + '''Return a clean environment for running tests.''' + + whitelisted = [ + 'TERM', + 'USER', + 'USERNAME', + 'PATH', + 'HOME', + 'LOGNAME', + ] + + hardcoded = { + 'SHELL': '/bin/sh', + 'LC_ALL': 'C', + } + + env = {} + for key in whitelisted: + if key in os.environ: + env[key] = os.environ[key] + for key in hardcoded: + env[key] = hardcoded[key] + return env + def run_step(self, datadir, scenario, step, shell_prelude, report_error): self.info('Running step "%s %s"' % (step.what, step.text)) self.ts['step'] = step @@ -274,8 +299,9 @@ class YarnRunner(cliapp.Application): m = re.match(step.implementation.regexp, step.text) assert m is not None - env = os.environ.copy() + env = self.clean_env() env['DATADIR'] = datadir + env['SRCDIR'] = os.getcwd() for i, match in enumerate(m.groups('')): env['MATCH_%d' % (i+1)] = match diff --git a/yarn.tests/env.script b/yarn.tests/env.script new file mode 100755 index 0000000..2ef35c6 --- /dev/null +++ b/yarn.tests/env.script @@ -0,0 +1,18 @@ +#!/bin/sh + +set -eu + +cat << 'EOF' > "$DATADIR/env.yarn" + SCENARIO check environment + THEN DATADIR is set + AND SRCDIR is set + AND NOTSET is not set + + IMPLEMENTS THEN (\S+) is set + env | grep "^$MATCH_1=" + + IMPLEMENTS THEN (\S+) is not set + ! env | grep "^$MATCH_1=" +EOF + +NOTSET=foo ./run-yarn "$DATADIR/env.yarn" |