From 8443dc250bb58091c435db2b29e2029c434d1750 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 31 Jul 2013 18:37:33 +0100 Subject: Set SRCDIR and clean environment --- yarn | 28 +++++++++++++++++++++++++++- yarn.tests/env.script | 18 ++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100755 yarn.tests/env.script diff --git a/yarn b/yarn index 288e23b..6fbd63a 100755 --- a/yarn +++ b/yarn @@ -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" -- cgit v1.2.1 From 2399b141f96604818c3a05b1e7e2cc1a8505f3b8 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 31 Jul 2013 18:38:48 +0100 Subject: Update NEWS about SRCDIR, clean env --- NEWS | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/NEWS b/NEWS index ca4d551..4951f4c 100644 --- a/NEWS +++ b/NEWS @@ -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 -------------------------------- -- cgit v1.2.1 From 8e8b0139119f102b08283c528ddbae70728bc188 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 31 Jul 2013 18:40:03 +0100 Subject: Add SRCDIR to the README --- README.yarn | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.yarn b/README.yarn index 76d57b5..7006a66 100644 --- a/README.yarn +++ b/README.yarn @@ -136,6 +136,10 @@ 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 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 -- cgit v1.2.1 From 02fe4bfce7b93c54e7e0e1a68a822dcd7764b30f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 31 Jul 2013 18:41:45 +0100 Subject: Add to README mention of environment cleanup --- README.yarn | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.yarn b/README.yarn index 7006a66..16f7522 100644 --- a/README.yarn +++ b/README.yarn @@ -140,6 +140,11 @@ The following keywords are defined. 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 -- cgit v1.2.1