| 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 from recipe_engine import recipe_api, config | 5 from recipe_engine import recipe_api, config |
| 6 | 6 |
| 7 DEPS = [ | 7 DEPS = [ |
| 8 'context', | 8 'context', |
| 9 'path', | 9 'path', |
| 10 'raw_io', |
| 10 'step', | 11 'step', |
| 11 ] | 12 ] |
| 12 | 13 |
| 13 | 14 |
| 14 def RunSteps(api): | 15 def RunSteps(api): |
| 15 api.step('default step', ['bash', '-c', 'echo default!']) | 16 api.step('default step', ['bash', '-c', 'echo default!']) |
| 16 | 17 |
| 17 noop_context = {} | 18 noop_context = {} |
| 18 with api.context(**noop_context): | 19 with api.context(**noop_context): |
| 19 # nothing happens! this is exactly the same as above, but this optimization | 20 # nothing happens! this is exactly the same as above, but this optimization |
| 20 # is helpful when recipes need to calculate contextual values. | 21 # is helpful when recipes need to calculate contextual values. |
| 21 api.step('default step', ['bash', '-c', 'echo default!']) | 22 api.step('default step', ['bash', '-c', 'echo default!']) |
| 22 | 23 |
| 23 # can change cwd | 24 # can change cwd |
| 24 api.step('mk subdir', ['mkdir', '-p', 'subdir']) | 25 api.step('mk subdir', ['mkdir', '-p', 'subdir']) |
| 25 with api.context(cwd=api.path['start_dir'].join('subdir')): | 26 with api.context(cwd=api.path['start_dir'].join('subdir')): |
| 26 api.step('subdir step', ['bash', '-c', 'pwd']) | 27 api.step('subdir step', ['bash', '-c', 'pwd']) |
| 27 api.step('other subdir step', ['bash', '-c', 'echo hi again!']) | 28 api.step('other subdir step', ['bash', '-c', 'echo hi again!']) |
| 28 | 29 |
| 29 # can set envvars, and path prefix. | 30 # can set envvars, and path prefix. |
| 31 pants = api.path['start_dir'].join('pants') |
| 32 shirt = api.path['start_dir'].join('shirt') |
| 30 with api.context(env={'FOO': 'bar'}): | 33 with api.context(env={'FOO': 'bar'}): |
| 31 api.step('env step', ['bash', '-c', 'echo $FOO']) | 34 api.step('env step', ['bash', '-c', 'echo $FOO']) |
| 32 | 35 |
| 33 pants = api.path['start_dir'].join('pants') | 36 with api.context(env_prefixes={'FOO': [pants, shirt]}): |
| 34 shirt = api.path['start_dir'].join('shirt') | 37 expected = api.path.pathsep.join([str(pants), str(shirt), 'bar']) |
| 35 with api.context(env={'FOO': api.context.Prefix(pants, shirt)}): | 38 result = api.step('env step with prefix', |
| 36 api.step('env step with prefix', | 39 ['bash', '-c', 'echo $FOO']) |
| 37 ['bash', '-c', 'echo $FOO']) | 40 |
| 41 # Path prefix won't append empty environment variables. |
| 42 with api.context(env={'FOO': ''}, env_prefixes={'FOO': [pants, shirt]}): |
| 43 expected = api.path.pathsep.join([str(pants), str(shirt)]) |
| 44 result = api.step('env prefixes with empty value', |
| 45 ['bash', '-c', 'echo $FOO']) |
| 38 | 46 |
| 39 # %-formats are errors (for now). Double-% escape them. | 47 # %-formats are errors (for now). Double-% escape them. |
| 40 bad_examples = ['%format', '%s'] | 48 bad_examples = ['%format', '%s'] |
| 41 for example in bad_examples: | 49 for example in bad_examples: |
| 42 try: | 50 try: |
| 43 with api.context(env={'BAD': example}): | 51 with api.context(env={'BAD': example}): |
| 44 assert False # pragma: no cover | 52 assert False # pragma: no cover |
| 45 except ValueError: | 53 except ValueError: |
| 46 pass | 54 pass |
| 47 | 55 |
| 48 # this is fine though: | 56 # this is fine though: |
| 49 with api.context(env={'FINE': '%%format'}): | 57 with api.context(env={'FINE': '%%format'}): |
| 50 pass | 58 pass |
| 51 | 59 |
| 52 # can increment nest level... note that this is a low level api, prefer | 60 # can increment nest level... note that this is a low level api, prefer |
| 53 # api.step.nest instead: | 61 # api.step.nest instead: |
| 54 # YES: | 62 # YES: |
| 55 with api.step.nest('nested'): | 63 with api.step.nest('nested'): |
| 56 api.step('properly indented', ['bash', '-c', 'echo yay!']) | 64 api.step('properly indented', ['bash', '-c', 'echo yay!']) |
| 57 # AVOID: | 65 # AVOID: |
| 58 with api.context(increment_nest_level=True): | 66 with api.context(increment_nest_level=True): |
| 59 api.step('indented with wrong name', ['bash', '-c', 'echo indent?']) | 67 api.step('indented with wrong name', ['bash', '-c', 'echo indent?']) |
| 60 | 68 |
| 61 | 69 |
| 62 def GenTests(api): | 70 def GenTests(api): |
| 63 yield api.test('basic') | 71 yield api.test('basic') |
| OLD | NEW |