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