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

Side by Side Diff: recipe_modules/file/test_api.py

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

Powered by Google App Engine
This is Rietveld 408576698