| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2017 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 import os |
| 6 |
| 7 from recipe_engine import recipe_test_api |
| 8 |
| 9 |
| 10 def renderNames(names): |
| 11 def check(name): |
| 12 name = str(name) |
| 13 if '/' in name or '\\' in name: # pragma: no cover |
| 14 raise ValueError('file name contains slash: %r' % name) |
| 15 return name |
| 16 return '\n'.join(sorted(map(check, names))) |
| 17 |
| 18 |
| 19 class FileTestApi(recipe_test_api.RecipeTestApi): |
| 20 def listdir(self, names=(), errno_name=0): |
| 21 """Provides test mock for the `listdir` method. |
| 22 |
| 23 Args: |
| 24 names (iterable[str]) - The list of dir entry names for this `listdir` |
| 25 step to return. |
| 26 errno_name (str|None) - The error name for this step to return, if any. |
| 27 |
| 28 Example: |
| 29 yield (api.test('my_test') |
| 30 + api.step_data('listdir step name', api.file.listdir(['a', 'b', 'c'])) |
| 31 """ |
| 32 return (self.m.raw_io.stream_output(renderNames(names)) |
| 33 + self.errno(errno_name)) |
| 34 |
| 35 def read_text(self, text_content='', errno_name=0): |
| 36 """Provides test mock for the `read_text` method. |
| 37 |
| 38 Args: |
| 39 text_content (str) - The text data for this read_text step to return. |
| 40 errno_name (str|None) - The error name for this step to return, if any. |
| 41 |
| 42 Example: |
| 43 yield (api.test('my_test') |
| 44 + api.step_data('read step name', |
| 45 api.file.read_text('some\nfile\ncontent')) |
| 46 ) |
| 47 """ |
| 48 return (self.m.raw_io.output_text(text_content) |
| 49 + self.errno(errno_name)) |
| 50 |
| 51 def glob_paths(self, names=(), errno_name=0): |
| 52 """Provides test mock for the `glob_paths` method. |
| 53 |
| 54 Args: |
| 55 names (iterable[str]) - The file names for the glob_paths step to return. |
| 56 errno_name (str|None) - The error name for this step to return, if any. |
| 57 |
| 58 Example: |
| 59 yield (api.test('my_test') |
| 60 + api.step_data('glob step name', api.file.glob_paths([ |
| 61 'pattern_path', 'pattern_other_thing' |
| 62 ])) |
| 63 """ |
| 64 return (self.m.raw_io.stream_output(renderNames(names)) |
| 65 + self.errno(errno_name)) |
| 66 |
| 67 def errno(self, errno_name=None): |
| 68 """Provides test mock for any file module method, causing the step to raise |
| 69 a file.Error exception. |
| 70 |
| 71 Args: |
| 72 errno_name (None|str) - The errno error name that the step should raise. |
| 73 This must be e.g. 'EPERM', 'EEXIST', etc. |
| 74 """ |
| 75 data = {'ok': True} |
| 76 if errno_name: |
| 77 data['ok'] = False |
| 78 data['errno_name'] = errno_name |
| 79 # in real operation, this message will come from the underlying OS and |
| 80 # will potentially have descriptive detail. |
| 81 data['message'] = 'file command encountered system error '+errno_name |
| 82 return self.m.json.output(data) |
| OLD | NEW |