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

Side by Side Diff: recipe_modules/step/example.py

Issue 2875543002: [recipe_modules/step] do not set cwd if it is start_dir. (Closed)
Patch Set: fix take 2 Created 3 years, 7 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
« no previous file with comments | « recipe_modules/step/api.py ('k') | recipe_modules/step/example.expected/basic.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2013 The LUCI Authors. All rights reserved. 1 # Copyright 2013 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 'path', 8 'path',
9 'properties', 9 'properties',
10 'step', 10 'step',
11 ] 11 ]
12 12
13 13
14 RETURN_SCHEMA = config.ReturnSchema( 14 RETURN_SCHEMA = config.ReturnSchema(
15 test_me=config.Single(int) 15 test_me=config.Single(int)
16 ) 16 )
17 17
18 18
19 PROPERTIES = { 19 PROPERTIES = {
20 'bad_return': recipe_api.Property(default=False), 20 'bad_return': recipe_api.Property(default=False),
21 'raise_infra_failure': recipe_api.Property(default=False),
22 'access_invalid_data': recipe_api.Property(default=False), 21 'access_invalid_data': recipe_api.Property(default=False),
23 'timeout': recipe_api.Property(default=0, kind=int), 22 'timeout': recipe_api.Property(default=0, kind=int),
24 } 23 }
25 24
26 25
27 def RunSteps( 26 def RunSteps(api, bad_return, access_invalid_data, timeout):
28 api, bad_return, raise_infra_failure, access_invalid_data, timeout):
29 if bad_return: 27 if bad_return:
30 return RETURN_SCHEMA.new(test_me='this should fail') 28 return RETURN_SCHEMA.new(test_me='this should fail')
31 elif timeout: 29 elif timeout:
32 # Timeout causes the recipe engine to raise an exception if your step takes 30 # Timeout causes the recipe engine to raise an exception if your step takes
33 # longer to run than you allow. Units are seconds. 31 # longer to run than you allow. Units are seconds.
34 if timeout == 1: 32 if timeout == 1:
35 api.step('timeout', ['sleep', '20'], timeout=1) 33 api.step('timeout', ['sleep', '20'], timeout=1)
36 elif timeout == 2: 34 elif timeout == 2:
37 try: 35 try:
38 api.step('caught timeout', ['sleep', '20'], timeout=1) 36 api.step('caught timeout', ['sleep', '20'], timeout=1)
39 except api.step.StepTimeout: 37 except api.step.StepTimeout:
40 return RETURN_SCHEMA(test_me=4) 38 return RETURN_SCHEMA(test_me=4)
41 39
42 40
43 # TODO(martinis) change this 41 # TODO(martinis) change this
44 # The api.step object is directly callable. 42 # The api.step object is directly callable.
45 api.step('hello', ['echo', 'Hello World']) 43 api.step('hello', ['echo', 'Hello World'])
46 api.step('hello', ['echo', 'Why hello, there.']) 44 api.step('hello', ['echo', 'Why hello, there.'])
47 45
46 # You can change the current working directory as well
47 api.step('mk subdir', ['mkdir', 'something'])
48 with api.step.context({'cwd': api.path['start_dir'].join('something')}):
49 api.step('something', ['bash', '-c', 'echo Why hello, there, in a subdir.'])
nodir 2017/05/10 01:52:25 Did you mean "Well"?
iannucci 2017/05/10 01:56:51 Either one works :) https://english.stackexchange.
50
51 # By default, all steps run in 'start_dir', or the cwd of the recipe engine
52 # when the recipe begins. Because of this, setting cwd to start_dir doesn't
53 # show anything in particular in the expectations.
54 with api.step.context({'cwd': api.path['start_dir']}):
55 api.step('start_dir ignored', ['bash', '-c', 'echo what happen'])
56
48 # You can also manipulate various aspects of the step, such as env. 57 # You can also manipulate various aspects of the step, such as env.
49 # These are passed straight through to subprocess.Popen. 58 # These are passed straight through to subprocess.Popen.
50 # Also, abusing bash -c in this way is a TERRIBLE IDEA DON'T DO IT. 59 # Also, abusing bash -c in this way is a TERRIBLE IDEA DON'T DO IT.
51 with api.step.context({'env': {'friend': 'Darth Vader'}}): 60 with api.step.context({'env': {'friend': 'Darth Vader'}}):
52 api.step('goodbye', ['bash', '-c', 'echo Good bye, $friend.']) 61 api.step('goodbye', ['bash', '-c', 'echo Good bye, $friend.'])
53 62
54 # You can modify environment in terms of old environment. Environment 63 # You can modify environment in terms of old environment. Environment
55 # variables are substituted in for expressions of the form %(VARNAME)s. 64 # variables are substituted in for expressions of the form %(VARNAME)s.
56 with api.step.context({'env': {'PATH': api.path.pathsep.join( 65 with api.step.context({'env': {'PATH': api.path.pathsep.join(
57 [str(api.step.package_repo_resource()), '%(PATH)s'])}}): 66 [str(api.step.package_repo_resource()), '%(PATH)s'])}}):
(...skipping 10 matching lines...) Expand all
68 step_result.presentation.logs['the reason'] = ['The reason\nit failed'] 77 step_result.presentation.logs['the reason'] = ['The reason\nit failed']
69 78
70 # Without a command, a step can be used to present some data from the recipe. 79 # Without a command, a step can be used to present some data from the recipe.
71 step_result = api.step('Just print stuff', cmd=None) 80 step_result = api.step('Just print stuff', cmd=None)
72 step_result.presentation.logs['more'] = ['More stuff'] 81 step_result.presentation.logs['more'] = ['More stuff']
73 82
74 try: 83 try:
75 api.step('goodbye', ['echo', 'goodbye']) 84 api.step('goodbye', ['echo', 'goodbye'])
76 # Modifying step_result now would raise an AssertionError. 85 # Modifying step_result now would raise an AssertionError.
77 except api.step.StepFailure: 86 except api.step.StepFailure:
78 # Raising anything besides StepFailure or StepWarning causes the build to go 87 # Raising anything besides StepFailure or StepWarning causes the build to go
79 # purple. 88 # purple.
80 raise ValueError('goodbye must exit 0!') 89 raise ValueError('goodbye must exit 0!')
81 90
82 try: 91 try:
83 api.step('warning', ['echo', 'warning']) 92 api.step('warning', ['echo', 'warning'])
84 except api.step.StepFailure as e: 93 except api.step.StepFailure as e:
85 e.result.presentation.status = api.step.WARNING 94 e.result.presentation.status = api.step.WARNING
86 raise api.step.StepWarning(e.message) 95 raise api.step.StepWarning(e.message)
87 96
88 97
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 ) 159 )
151 160
152 yield ( 161 yield (
153 api.test('invalid_access') + 162 api.test('invalid_access') +
154 api.properties(access_invalid_data=True) + 163 api.properties(access_invalid_data=True) +
155 api.expect_exception('StepDataAttributeError') 164 api.expect_exception('StepDataAttributeError')
156 ) 165 )
157 166
158 yield ( 167 yield (
159 api.test('infra_failure') + 168 api.test('infra_failure') +
160 api.properties(raise_infra_failure=True) +
161 api.step_data('cleanup', retcode=1) 169 api.step_data('cleanup', retcode=1)
162 ) 170 )
163 171
164 yield ( 172 yield (
165 api.test('bad_return') + 173 api.test('bad_return') +
166 api.properties(bad_return=True) + 174 api.properties(bad_return=True) +
167 api.expect_exception('TypeError') 175 api.expect_exception('TypeError')
168 ) 176 )
169 177
170 yield ( 178 yield (
171 api.test('timeout') + 179 api.test('timeout') +
172 api.properties(timeout=1) + 180 api.properties(timeout=1) +
173 api.step_data('timeout', times_out_after=20) 181 api.step_data('timeout', times_out_after=20)
174 ) 182 )
175 183
176 yield ( 184 yield (
177 api.test('catch_timeout') + 185 api.test('catch_timeout') +
178 api.properties(timeout=2) + 186 api.properties(timeout=2) +
179 api.step_data('caught timeout', times_out_after=20) 187 api.step_data('caught timeout', times_out_after=20)
180 ) 188 )
OLDNEW
« no previous file with comments | « recipe_modules/step/api.py ('k') | recipe_modules/step/example.expected/basic.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698