| OLD | NEW |
| 1 # Copyright 2014 The LUCI Authors. All rights reserved. | 1 # Copyright 2014 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 'path', | 6 'path', |
| 7 'properties', | 7 'properties', |
| 8 'python', | 8 'python', |
| 9 'raw_io', | 9 'raw_io', |
| 10 'step', | 10 'step', |
| 11 ] | 11 ] |
| 12 | 12 |
| 13 | 13 |
| 14 def RunSteps(api): | 14 def RunSteps(api): |
| 15 # Read command's stdout and stderr. | 15 # Read command's stdout and stderr. |
| 16 step_result = api.step('echo', ['echo', 'Hello World'], | 16 step_result = api.step('echo', ['echo', 'Hello World'], |
| 17 stdout=api.raw_io.output(), | 17 stdout=api.raw_io.output(), |
| 18 stderr=api.raw_io.output()) | 18 stderr=api.raw_io.output()) |
| 19 assert step_result.stdout == 'Hello World\n' | 19 assert step_result.stdout == 'Hello World\n' |
| 20 assert step_result.stderr == '' | 20 assert step_result.stderr == '' |
| 21 | 21 |
| 22 # Pass stuff to command's stdin, read it from stdout. | 22 # Pass stuff to command's stdin, read it from stdout. |
| 23 step_result = api.step('cat', ['cat'], | 23 step_result = api.step('cat', ['cat'], |
| 24 stdin=api.raw_io.input(data='hello'), | 24 stdin=api.raw_io.input_text(data='hello'), |
| 25 stdout=api.raw_io.output('out')) | 25 stdout=api.raw_io.output('out')) |
| 26 assert step_result.stdout == 'hello' | 26 assert step_result.stdout == 'hello' |
| 27 | 27 |
| 28 step_result = api.step('cat', ['cat', api.raw_io.input_text(data='hello')], |
| 29 stdout=api.raw_io.output('out')) |
| 30 assert step_result.stdout == 'hello' |
| 31 |
| 32 # \xe2 is not encodable by utf-8 (and has shown up in actual recipe data) |
| 33 # so test that input correctly doesn't try to encode it as utf-8. |
| 34 step_result = api.step('cat', ['cat'], |
| 35 stdin=api.raw_io.input(data='\xe2hello'), |
| 36 stdout=api.raw_io.output()) |
| 37 assert step_result.stdout == '\xe2hello' |
| 38 |
| 28 # Example of auto-mocking stdout. '\n' appended to mock 'echo' behavior. | 39 # Example of auto-mocking stdout. '\n' appended to mock 'echo' behavior. |
| 29 step_result = api.step('automock', ['echo', 'huh'], | 40 step_result = api.step('automock', ['echo', 'huh'], |
| 30 stdout=api.raw_io.output('out'), | 41 stdout=api.raw_io.output('out'), |
| 31 step_test_data=( | 42 step_test_data=( |
| 32 lambda: api.raw_io.test_api.stream_output('huh\n'))) | 43 lambda: api.raw_io.test_api.stream_output('huh\n'))) |
| 33 assert step_result.stdout == 'huh\n' | 44 assert step_result.stdout == 'huh\n' |
| 34 | 45 |
| 35 # Example of auto-mocking stdout + stderr. | 46 # Example of auto-mocking stdout + stderr. |
| 36 step_result = api.step( | 47 step_result = api.step( |
| 37 'automock (fail)', ['bash', '-c', 'echo blah && echo fail 1>&2'], | 48 'automock (fail)', ['bash', '-c', 'echo blah && echo fail 1>&2'], |
| (...skipping 21 matching lines...) Expand all Loading... |
| 59 leak_to=api.path['tmp_base'].join('out'))]) | 70 leak_to=api.path['tmp_base'].join('out'))]) |
| 60 | 71 |
| 61 # Example of overriding default mocked output for a single named output. | 72 # Example of overriding default mocked output for a single named output. |
| 62 step_result = api.python.inline( | 73 step_result = api.python.inline( |
| 63 'override_default_mock', | 74 'override_default_mock', |
| 64 """ | 75 """ |
| 65 import sys | 76 import sys |
| 66 with open(sys.argv[1], 'w') as f: | 77 with open(sys.argv[1], 'w') as f: |
| 67 f.write(%r) | 78 f.write(%r) |
| 68 """ % api.properties.get('some_prop', 'good_value'), | 79 """ % api.properties.get('some_prop', 'good_value'), |
| 69 args=[api.raw_io.output(name='test')], | 80 args=[api.raw_io.output_text(name='test')], |
| 70 step_test_data=( | 81 step_test_data=( |
| 71 lambda: api.raw_io.test_api.output('second_bad_value', name='test'))) | 82 lambda: api.raw_io.test_api.output_text( |
| 72 assert step_result.raw_io.outputs['test'] == 'good_value' | 83 'second_bad_value', name='test'))) |
| 73 assert step_result.raw_io.output == 'good_value' | 84 assert step_result.raw_io.output_texts['test'] == 'good_value' |
| 85 assert step_result.raw_io.output_text == 'good_value' |
| 74 | 86 |
| 75 | 87 |
| 76 def GenTests(api): | 88 def GenTests(api): |
| 77 # This test shows that you can override a specific placeholder, even with | 89 # This test shows that you can override a specific placeholder, even with |
| 78 # default `step_test_data`. However, since this recipe is ACTUALLY run in | 90 # default `step_test_data`. However, since this recipe is ACTUALLY run in |
| 79 # the presubmit, we need to do a trick with properties: | 91 # the presubmit, we need to do a trick with properties: |
| 80 # When run for real, "some_prop" will be "good_value" and pass. | 92 # When run for real, "some_prop" will be "good_value" and pass. |
| 81 # When run for simulation, we override this property to provide a bad value, | 93 # When run for simulation, we override this property to provide a bad value, |
| 82 # AND the default step_test_data in RunSteps above ALSO provides another | 94 # AND the default step_test_data in RunSteps above ALSO provides another |
| 83 # bad value, the simulation passes ONLY because of the | 95 # bad value, the simulation passes ONLY because of the |
| 84 # 'override_default_mock' below. | 96 # 'override_default_mock' below. |
| 85 yield (api.test('basic') + | 97 yield (api.test('basic') + |
| 86 api.properties(some_prop='bad_value') + | 98 api.properties(some_prop='bad_value') + |
| 87 api.step_data('echo', | 99 api.step_data('echo', |
| 88 stdout=api.raw_io.output('Hello World\n'), | 100 stdout=api.raw_io.output('Hello World\n'), |
| 89 stderr=api.raw_io.output('')) + | 101 stderr=api.raw_io.output('')) + |
| 90 api.step_data('cat', | 102 api.step_data('cat', |
| 91 stdout=api.raw_io.output('hello')) + | 103 stdout=api.raw_io.output('hello')) + |
| 104 api.step_data('cat (2)', |
| 105 stdout=api.raw_io.output('hello')) + |
| 106 api.step_data('cat (3)', |
| 107 stdout=api.raw_io.output('\xe2hello')) + |
| 92 api.step_data('override_default_mock', | 108 api.step_data('override_default_mock', |
| 93 api.raw_io.output('good_value', name='test')) | 109 api.raw_io.output_text('good_value', name='test')) |
| 94 ) | 110 ) |
| OLD | NEW |