Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Unified Diff: recipe_modules/context/tests/env.py

Issue 2933473002: [context] Split "env" values and prefixes. (Closed)
Patch Set: comments, tests, better definition Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: recipe_modules/context/tests/env.py
diff --git a/recipe_modules/context/tests/env.py b/recipe_modules/context/tests/env.py
index 4ba106097f8af7708ae35cca8a43b71f2dcc39e6..3b0f7dd1a87013748fafd8d69fe8a15f61ef21a9 100644
--- a/recipe_modules/context/tests/env.py
+++ b/recipe_modules/context/tests/env.py
@@ -3,41 +3,89 @@
# that can be found in the LICENSE file.
DEPS = [
- "context",
- "path",
- "step",
+ 'context',
+ 'path',
+ 'raw_io',
+ 'step',
]
def RunSteps(api):
+ # NOTE: This is NOT good practice; we need to import this to set our
dnj 2017/06/10 16:37:33 I'm a bit unhappy about this, but it offers perfec
+ # environment expectations against the runtime enviornment. Other recipes
+ # should base their expectations against the simulated environment, and not
+ # touch "os.environ".
+ import os
+ orig_environ = os.environ.copy()
+ try:
+ key = 'RECIPE_ENGINE_CONTEXT_TEST'
+ os.environ[key] = 'default'
+ _RunStepsImpl(api, key)
+ finally:
+ os.environ = orig_environ
+
+
+def _RunStepsImpl(api, key):
api.step('no env', ['echo', 'hello'])
- with api.context(env={"SOMETHING": "1"}):
+ with api.context(env={'SOMETHING': '1'}):
api.step('with env', ['echo', 'hello'])
- with api.context(env={"SOMETHING_ELSE": "0"}):
+ with api.context(env={'SOMETHING_ELSE': '0'}):
api.step('with 2 envs', ['echo', 'hello'])
- with api.context(env={'FOO': 'bar'}):
- api.step('env step', ['bash', '-c', 'echo $FOO'])
+ # The following tests use "expect_step". In simulation mode, this will always
+ # pass. However, when run through "run" or via "unittests/run_test.py", this
+ # will process the real output environment variables and assert that they
+ # match expectations.
+ def expect_step(name, exp):
+ result = api.step(
+ name,
+ ['bash', '-c', 'echo -n $'+key],
+ stdout=api.raw_io.output(),
+ step_test_data=lambda: api.raw_io.test_api.stream_output(exp),
+ )
+ assert result.stdout == exp, (
+ '%r did not equal expected %r' % (result.stdout, exp))
+
+ expect_step('default', 'default')
+
+ # Can cause envvars to be dropped completely.
+ with api.context(env={key: None}):
+ expect_step('drop', '')
- base_path = 'foo%s%%(FOO)s%sbaz' % (api.path.pathsep, api.path.pathsep)
- with api.context(env={'FOO': base_path}):
- api.step('env step augmented', ['bash', '-c', 'echo $FOO'])
+ pants = api.path['start_dir'].join('pants')
+ shirt = api.path['start_dir'].join('shirt')
+ with api.context(env={key: 'bar'}):
+ expect_step('env step', 'bar')
- pants = api.path['start_dir'].join('pants')
- shirt = api.path['start_dir'].join('shirt')
- with api.context(env={'FOO': api.context.Prefix(pants, shirt)}):
- api.step('env step with prefix', ['bash', '-c', 'echo $FOO'])
+ base_path = api.path.pathsep.join(['foo', '%('+key+')s', 'bar'])
+ with api.context(env={key: base_path}):
+ expect_step('env step augmented',
+ api.path.pathsep.join(['foo', 'default', 'bar']))
+
+ with api.context(env_prefixes={key: [pants, shirt]}):
+ expect_step('env step with prefix',
+ api.path.pathsep.join([str(pants), str(shirt), 'foo',
+ 'default', 'bar']))
# Can set the path of default environment variables.
- with api.context(env={'FOO': api.context.Prefix(shirt)}):
- api.step('env with default value',
- ['bash', '-c', 'echo $FOO'])
+ with api.context(env_prefixes={key: [shirt]}):
+ expect_step('env with default value',
+ api.path.pathsep.join([str(shirt), 'default']))
- # Can cause envvars to be dropped completely.
- with api.context(env={"PYTHONPATH": None}):
- api.step('drop PYTHONPATH', ['bash', '-c', 'set'])
+ # When 'env' is also defined, appends it.
+ with api.context(env={key: 'foo'}):
+ expect_step('env with override value',
+ api.path.pathsep.join([str(shirt), 'foo']))
+
+ # When "env" is explicitly cleared, does not append.
+ with api.context(env={key: None}):
+ expect_step('env with cleared value', str(shirt))
+
+ # When "env" is explicitly empty, does not append.
+ with api.context(env={key: ''}):
+ expect_step('env with empty value', str(shirt))
def GenTests(api):
« no previous file with comments | « recipe_modules/context/examples/full.expected/basic.json ('k') | recipe_modules/context/tests/env.expected/basic.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698