Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: recipe_modules/context/examples/full.py

Issue 2933473002: [context] Split "env" values and prefixes. (Closed)
Patch Set: plumb pathsep through so we don't assume OS Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 stdout=api.raw_io.output(),
41 step_test_data=lambda: (
42 api.raw_io.test_api.stream_output(expected)))
43 assert result.stdout.strip() == expected
44
45 # Path prefix won't append empty environment variables.
46 with api.context(env={'FOO': ''}, env_prefixes={'FOO': [pants, shirt]}):
47 expected = api.path.pathsep.join([str(pants), str(shirt)])
48 result = api.step('env prefixes with empty value',
49 ['bash', '-c', 'echo $FOO'],
50 stdout=api.raw_io.output(),
51 step_test_data=lambda: (
52 api.raw_io.test_api.stream_output(expected)))
53 assert result.stdout.strip() == expected
38 54
39 # %-formats are errors (for now). Double-% escape them. 55 # %-formats are errors (for now). Double-% escape them.
40 bad_examples = ['%format', '%s'] 56 bad_examples = ['%format', '%s']
41 for example in bad_examples: 57 for example in bad_examples:
42 try: 58 try:
43 with api.context(env={'BAD': example}): 59 with api.context(env={'BAD': example}):
44 assert False # pragma: no cover 60 assert False # pragma: no cover
45 except ValueError: 61 except ValueError:
46 pass 62 pass
47 63
48 # this is fine though: 64 # this is fine though:
49 with api.context(env={'FINE': '%%format'}): 65 with api.context(env={'FINE': '%%format'}):
50 pass 66 pass
51 67
52 # can increment nest level... note that this is a low level api, prefer 68 # can increment nest level... note that this is a low level api, prefer
53 # api.step.nest instead: 69 # api.step.nest instead:
54 # YES: 70 # YES:
55 with api.step.nest('nested'): 71 with api.step.nest('nested'):
56 api.step('properly indented', ['bash', '-c', 'echo yay!']) 72 api.step('properly indented', ['bash', '-c', 'echo yay!'])
57 # AVOID: 73 # AVOID:
58 with api.context(increment_nest_level=True): 74 with api.context(increment_nest_level=True):
59 api.step('indented with wrong name', ['bash', '-c', 'echo indent?']) 75 api.step('indented with wrong name', ['bash', '-c', 'echo indent?'])
60 76
61 77
62 def GenTests(api): 78 def GenTests(api):
63 yield api.test('basic') 79 yield api.test('basic')
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698