| 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 |
| 13 # This key is set by "//recipe_engine/unittests/run_test.py" to "default". This |
| 14 # causes predictable results when run via "run_test", allowing us to test |
| 15 # correct expression of environment variables in production. |
| 16 _KEY = 'RECIPE_ENGINE_CONTEXT_TEST' |
| 17 |
| 18 |
| 12 def RunSteps(api): | 19 def RunSteps(api): |
| 13 api.step('no env', ['echo', 'hello']) | 20 api.step('no env', ['echo', 'hello']) |
| 14 | 21 |
| 15 with api.context(env={"SOMETHING": "1"}): | 22 with api.context(env={'SOMETHING': '1'}): |
| 16 api.step('with env', ['echo', 'hello']) | 23 api.step('with env', ['echo', 'hello']) |
| 17 | 24 |
| 18 with api.context(env={"SOMETHING_ELSE": "0"}): | 25 with api.context(env={'SOMETHING_ELSE': '0'}): |
| 19 api.step('with 2 envs', ['echo', 'hello']) | 26 api.step('with 2 envs', ['echo', 'hello']) |
| 20 | 27 |
| 21 with api.context(env={'FOO': 'bar'}): | 28 # The following tests use "expect_step". In simulation mode, this will always |
| 22 api.step('env step', ['bash', '-c', 'echo $FOO']) | 29 # pass. However, when run through "run" or via "unittests/run_test.py", this |
| 30 # will process the real output environment variables and assert that they |
| 31 # match expectations. |
| 32 def expect_step(name, exp): |
| 33 result = api.step( |
| 34 name, |
| 35 ['bash', '-c', 'echo -n $'+_KEY], |
| 36 stdout=api.raw_io.output(), |
| 37 step_test_data=lambda: api.raw_io.test_api.stream_output(exp), |
| 38 ) |
| 39 assert result.stdout == exp, ( |
| 40 '%r did not equal expected %r' % (result.stdout, exp)) |
| 23 | 41 |
| 24 base_path = 'foo%s%%(FOO)s%sbaz' % (api.path.pathsep, api.path.pathsep) | 42 expect_step('default', 'default') |
| 25 with api.context(env={'FOO': base_path}): | |
| 26 api.step('env step augmented', ['bash', '-c', 'echo $FOO']) | |
| 27 | 43 |
| 28 pants = api.path['start_dir'].join('pants') | 44 # Can cause envvars to be dropped completely. |
| 29 shirt = api.path['start_dir'].join('shirt') | 45 with api.context(env={_KEY: None}): |
| 30 with api.context(env={'FOO': api.context.Prefix(pants, shirt)}): | 46 expect_step('drop', '') |
| 31 api.step('env step with prefix', ['bash', '-c', 'echo $FOO']) | 47 |
| 48 pants = api.path['start_dir'].join('pants') |
| 49 shirt = api.path['start_dir'].join('shirt') |
| 50 with api.context(env={_KEY: 'bar'}): |
| 51 expect_step('env step', 'bar') |
| 52 |
| 53 base_path = api.path.pathsep.join(['foo', '%('+_KEY+')s', 'bar']) |
| 54 with api.context(env={_KEY: base_path}): |
| 55 expect_step('env step augmented', |
| 56 api.path.pathsep.join(['foo', 'default', 'bar'])) |
| 57 |
| 58 with api.context(env_prefixes={_KEY: [pants, shirt]}): |
| 59 expect_step('env step with prefix', |
| 60 api.path.pathsep.join([str(pants), str(shirt), 'foo', |
| 61 'default', 'bar'])) |
| 32 | 62 |
| 33 # Can set the path of default environment variables. | 63 # Can set the path of default environment variables. |
| 34 with api.context(env={'FOO': api.context.Prefix(shirt)}): | 64 with api.context(env_prefixes={_KEY: [shirt]}): |
| 35 api.step('env with default value', | 65 expect_step('env with default value', |
| 36 ['bash', '-c', 'echo $FOO']) | 66 api.path.pathsep.join([str(shirt), 'default'])) |
| 37 | 67 |
| 38 # Can cause envvars to be dropped completely. | 68 # When 'env' is also defined, appends it. |
| 39 with api.context(env={"PYTHONPATH": None}): | 69 with api.context(env={_KEY: 'foo'}): |
| 40 api.step('drop PYTHONPATH', ['bash', '-c', 'set']) | 70 expect_step('env with override value', |
| 71 api.path.pathsep.join([str(shirt), 'foo'])) |
| 72 |
| 73 # When "env" is explicitly cleared, does not append. |
| 74 with api.context(env={_KEY: None}): |
| 75 expect_step('env with cleared value', str(shirt)) |
| 76 |
| 77 # When "env" is explicitly empty, does not append. |
| 78 with api.context(env={_KEY: ''}): |
| 79 expect_step('env with empty value', str(shirt)) |
| 41 | 80 |
| 42 | 81 |
| 43 def GenTests(api): | 82 def GenTests(api): |
| 44 yield api.test('basic') | 83 yield api.test('basic') |
| 45 | 84 |
| OLD | NEW |