| OLD | NEW |
| (Empty) |
| 1 # Copyright 2014 The LUCI Authors. All rights reserved. | |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 # that can be found in the LICENSE file. | |
| 4 | |
| 5 DEPS = [ | |
| 6 'path', | |
| 7 'properties', | |
| 8 'python', | |
| 9 'raw_io', | |
| 10 'step', | |
| 11 ] | |
| 12 | |
| 13 | |
| 14 def RunSteps(api): | |
| 15 # Read command's stdout and stderr. | |
| 16 step_result = api.step('echo', ['echo', 'Hello World'], | |
| 17 stdout=api.raw_io.output(), | |
| 18 stderr=api.raw_io.output()) | |
| 19 assert step_result.stdout == 'Hello World\n' | |
| 20 assert step_result.stderr == '' | |
| 21 | |
| 22 # Pass stuff to command's stdin, read it from stdout. | |
| 23 step_result = api.step('cat', ['cat'], | |
| 24 stdin=api.raw_io.input_text(data='hello'), | |
| 25 stdout=api.raw_io.output('out')) | |
| 26 assert step_result.stdout == 'hello' | |
| 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 | |
| 39 # Example of auto-mocking stdout. '\n' appended to mock 'echo' behavior. | |
| 40 step_result = api.step('automock', ['echo', 'huh'], | |
| 41 stdout=api.raw_io.output('out'), | |
| 42 step_test_data=( | |
| 43 lambda: api.raw_io.test_api.stream_output('huh\n'))) | |
| 44 assert step_result.stdout == 'huh\n' | |
| 45 | |
| 46 # Example of auto-mocking stdout + stderr. | |
| 47 step_result = api.step( | |
| 48 'automock (fail)', ['bash', '-c', 'echo blah && echo fail 1>&2'], | |
| 49 stdout=api.raw_io.output('out'), | |
| 50 stderr=api.raw_io.output('err'), | |
| 51 step_test_data=( | |
| 52 lambda: ( | |
| 53 api.raw_io.test_api.stream_output('blah\n') + | |
| 54 api.raw_io.test_api.stream_output('fail\n', 'stderr') | |
| 55 )) | |
| 56 ) | |
| 57 assert step_result.stdout == 'blah\n' | |
| 58 assert step_result.stderr == 'fail\n' | |
| 59 | |
| 60 # leak_to coverage. | |
| 61 step_result = api.step( | |
| 62 'leak stdout', ['echo', 'leaking'], | |
| 63 stdout=api.raw_io.output(leak_to=api.path['tmp_base'].join('out.txt')), | |
| 64 step_test_data=( | |
| 65 lambda: api.raw_io.test_api.stream_output('leaking\n'))) | |
| 66 assert step_result.stdout == 'leaking\n' | |
| 67 | |
| 68 api.step('list temp dir', ['ls', api.raw_io.output_dir()]) | |
| 69 api.step('leak dir', ['ls', api.raw_io.output_dir( | |
| 70 leak_to=api.path['tmp_base'].join('out'))]) | |
| 71 | |
| 72 # Example of overriding default mocked output for a single named output. | |
| 73 step_result = api.python.inline( | |
| 74 'override_default_mock', | |
| 75 """ | |
| 76 import sys | |
| 77 with open(sys.argv[1], 'w') as f: | |
| 78 f.write(%r) | |
| 79 """ % api.properties.get('some_prop', 'good_value'), | |
| 80 args=[api.raw_io.output_text(name='test')], | |
| 81 step_test_data=( | |
| 82 lambda: api.raw_io.test_api.output_text( | |
| 83 'second_bad_value', name='test'))) | |
| 84 assert step_result.raw_io.output_texts['test'] == 'good_value' | |
| 85 assert step_result.raw_io.output_text == 'good_value' | |
| 86 | |
| 87 | |
| 88 def GenTests(api): | |
| 89 # This test shows that you can override a specific placeholder, even with | |
| 90 # default `step_test_data`. However, since this recipe is ACTUALLY run in | |
| 91 # the presubmit, we need to do a trick with properties: | |
| 92 # When run for real, "some_prop" will be "good_value" and pass. | |
| 93 # When run for simulation, we override this property to provide a bad value, | |
| 94 # AND the default step_test_data in RunSteps above ALSO provides another | |
| 95 # bad value, the simulation passes ONLY because of the | |
| 96 # 'override_default_mock' below. | |
| 97 yield (api.test('basic') + | |
| 98 api.properties(some_prop='bad_value') + | |
| 99 api.step_data('echo', | |
| 100 stdout=api.raw_io.output('Hello World\n'), | |
| 101 stderr=api.raw_io.output('')) + | |
| 102 api.step_data('cat', | |
| 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')) + | |
| 108 api.step_data('override_default_mock', | |
| 109 api.raw_io.output_text('good_value', name='test')) | |
| 110 ) | |
| OLD | NEW |