OLD | NEW |
1 # Copyright 2015 The LUCI Authors. All rights reserved. | 1 # Copyright 2015 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 import json | 5 import json |
6 | 6 |
7 from recipe_engine import recipe_test_api | 7 from recipe_engine import recipe_test_api |
8 | 8 |
9 class JsonTestApi(recipe_test_api.RecipeTestApi): | 9 class JsonTestApi(recipe_test_api.RecipeTestApi): |
10 @recipe_test_api.placeholder_step_data | 10 @recipe_test_api.placeholder_step_data |
11 @staticmethod | 11 @staticmethod |
12 def output(data, retcode=None, name=None): | 12 def output(data, retcode=None, name=None): |
| 13 """Supplies placeholder data for a json.output. `data` should be a jsonish |
| 14 python object (e.g. dict, list, str, bool, int, etc). It will be dumped out |
| 15 with json.dumps and the step will be observed to return that dumped value. |
| 16 """ |
13 return json.dumps(data), retcode, name | 17 return json.dumps(data), retcode, name |
14 | 18 |
| 19 def invalid(self, raw_data_str, retcode=None, name=None): |
| 20 """Can be used to supply data for a json.output, except that it takes a raw |
| 21 string rather than a json object.""" |
| 22 ret = recipe_test_api.StepTestData() |
| 23 ret.retcode=retcode |
| 24 placeholder_data = recipe_test_api.PlaceholderTestData( |
| 25 data=raw_data_str, name=name) |
| 26 ret.placeholder_data[(self._module.NAME, 'output', name)] = placeholder_data |
| 27 return ret |
| 28 |
15 def output_stream(self, data, stream='stdout', retcode=None, name=None): | 29 def output_stream(self, data, stream='stdout', retcode=None, name=None): |
16 assert stream in ('stdout', 'stderr') | 30 assert stream in ('stdout', 'stderr') |
17 ret = recipe_test_api.StepTestData() | 31 ret = recipe_test_api.StepTestData() |
18 step_data = self.output(data, retcode=retcode, name=name) | 32 step_data = self.output(data, retcode=retcode, name=name) |
19 setattr(ret, stream, step_data.unwrap_placeholder()) | 33 setattr(ret, stream, step_data.unwrap_placeholder()) |
20 return ret | 34 return ret |
OLD | NEW |