Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2017 The LUCI Authors. All rights reserved. | 1 # Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 DEPS = [ | 5 DEPS = [ |
| 6 "context", | 6 'context', |
| 7 "path", | 7 'path', |
| 8 "step", | 8 'raw_io', |
| 9 'step', | |
| 9 ] | 10 ] |
| 10 | 11 |
| 11 | 12 |
| 12 def RunSteps(api): | 13 def RunSteps(api): |
| 14 # 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
| |
| 15 # environment expectations against the runtime enviornment. Other recipes | |
| 16 # should base their expectations against the simulated environment, and not | |
| 17 # touch "os.environ". | |
| 18 import os | |
| 19 orig_environ = os.environ.copy() | |
| 20 try: | |
| 21 key = 'RECIPE_ENGINE_CONTEXT_TEST' | |
| 22 os.environ[key] = 'default' | |
| 23 _RunStepsImpl(api, key) | |
| 24 finally: | |
| 25 os.environ = orig_environ | |
| 26 | |
| 27 | |
| 28 def _RunStepsImpl(api, key): | |
| 13 api.step('no env', ['echo', 'hello']) | 29 api.step('no env', ['echo', 'hello']) |
| 14 | 30 |
| 15 with api.context(env={"SOMETHING": "1"}): | 31 with api.context(env={'SOMETHING': '1'}): |
| 16 api.step('with env', ['echo', 'hello']) | 32 api.step('with env', ['echo', 'hello']) |
| 17 | 33 |
| 18 with api.context(env={"SOMETHING_ELSE": "0"}): | 34 with api.context(env={'SOMETHING_ELSE': '0'}): |
| 19 api.step('with 2 envs', ['echo', 'hello']) | 35 api.step('with 2 envs', ['echo', 'hello']) |
| 20 | 36 |
| 21 with api.context(env={'FOO': 'bar'}): | 37 # The following tests use "expect_step". In simulation mode, this will always |
| 22 api.step('env step', ['bash', '-c', 'echo $FOO']) | 38 # pass. However, when run through "run" or via "unittests/run_test.py", this |
| 39 # will process the real output environment variables and assert that they | |
| 40 # match expectations. | |
| 41 def expect_step(name, exp): | |
| 42 result = api.step( | |
| 43 name, | |
| 44 ['bash', '-c', 'echo -n $'+key], | |
| 45 stdout=api.raw_io.output(), | |
| 46 step_test_data=lambda: api.raw_io.test_api.stream_output(exp), | |
| 47 ) | |
| 48 assert result.stdout == exp, ( | |
| 49 '%r did not equal expected %r' % (result.stdout, exp)) | |
| 23 | 50 |
| 24 base_path = 'foo%s%%(FOO)s%sbaz' % (api.path.pathsep, api.path.pathsep) | 51 expect_step('default', 'default') |
| 25 with api.context(env={'FOO': base_path}): | |
| 26 api.step('env step augmented', ['bash', '-c', 'echo $FOO']) | |
| 27 | 52 |
| 28 pants = api.path['start_dir'].join('pants') | 53 # Can cause envvars to be dropped completely. |
| 29 shirt = api.path['start_dir'].join('shirt') | 54 with api.context(env={key: None}): |
| 30 with api.context(env={'FOO': api.context.Prefix(pants, shirt)}): | 55 expect_step('drop', '') |
| 31 api.step('env step with prefix', ['bash', '-c', 'echo $FOO']) | 56 |
| 57 pants = api.path['start_dir'].join('pants') | |
| 58 shirt = api.path['start_dir'].join('shirt') | |
| 59 with api.context(env={key: 'bar'}): | |
| 60 expect_step('env step', 'bar') | |
| 61 | |
| 62 base_path = api.path.pathsep.join(['foo', '%('+key+')s', 'bar']) | |
| 63 with api.context(env={key: base_path}): | |
| 64 expect_step('env step augmented', | |
| 65 api.path.pathsep.join(['foo', 'default', 'bar'])) | |
| 66 | |
| 67 with api.context(env_prefixes={key: [pants, shirt]}): | |
| 68 expect_step('env step with prefix', | |
| 69 api.path.pathsep.join([str(pants), str(shirt), 'foo', | |
| 70 'default', 'bar'])) | |
| 32 | 71 |
| 33 # Can set the path of default environment variables. | 72 # Can set the path of default environment variables. |
| 34 with api.context(env={'FOO': api.context.Prefix(shirt)}): | 73 with api.context(env_prefixes={key: [shirt]}): |
| 35 api.step('env with default value', | 74 expect_step('env with default value', |
| 36 ['bash', '-c', 'echo $FOO']) | 75 api.path.pathsep.join([str(shirt), 'default'])) |
| 37 | 76 |
| 38 # Can cause envvars to be dropped completely. | 77 # When 'env' is also defined, appends it. |
| 39 with api.context(env={"PYTHONPATH": None}): | 78 with api.context(env={key: 'foo'}): |
| 40 api.step('drop PYTHONPATH', ['bash', '-c', 'set']) | 79 expect_step('env with override value', |
| 80 api.path.pathsep.join([str(shirt), 'foo'])) | |
| 81 | |
| 82 # When "env" is explicitly cleared, does not append. | |
| 83 with api.context(env={key: None}): | |
| 84 expect_step('env with cleared value', str(shirt)) | |
| 85 | |
| 86 # When "env" is explicitly empty, does not append. | |
| 87 with api.context(env={key: ''}): | |
| 88 expect_step('env with empty value', str(shirt)) | |
| 41 | 89 |
| 42 | 90 |
| 43 def GenTests(api): | 91 def GenTests(api): |
| 44 yield api.test('basic') | 92 yield api.test('basic') |
| 45 | 93 |
| OLD | NEW |