Chromium Code Reviews| 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 |
| + # environment expectations against the runtime enviornment. Other recipes |
|
iannucci
2017/06/12 19:58:40
you could pass in the expected environment as a pr
dnj
2017/06/13 19:31:21
Good idea. Done, and documented.
|
| + # 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): |