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

Unified 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, 7 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 side-by-side diff with in-line comments
Download patch
Index: recipe_modules/file/test_api.py
diff --git a/recipe_modules/file/test_api.py b/recipe_modules/file/test_api.py
new file mode 100644
index 0000000000000000000000000000000000000000..1dd759578accdec4c2dab6f49e6dd2d7ad8f270a
--- /dev/null
+++ b/recipe_modules/file/test_api.py
@@ -0,0 +1,80 @@
+# Copyright 2017 The LUCI Authors. All rights reserved.
+# Use of this source code is governed under the Apache License, Version 2.0
+# that can be found in the LICENSE file.
+
+import os
+
+from recipe_engine import recipe_test_api
+
+
+class FileTestApi(recipe_test_api.RecipeTestApi):
+ def listdir(self, files=(), errno_name=0):
+ """Provides test mock for the `listdir` method.
+
+ Args:
+ files (iterable[str|Path]) - The list of paths names for this `listdir`
+ step to return.
+ errno_name (str|None) - The error name for this step to return, if any.
+
+ Example:
+ yield (api.test('my_test')
+ + api.step_data('listdir step name', api.file.listdir([
+ '[BASE_PATH]/some/a',
+ '[BASE_PATH]/some/b',
+ '[BASE_PATH]/some/c',
+ ])
+ )
+ """
+ return (self.m.raw_io.stream_output('\n'.join(sorted(map(str, files))))
+ + self.errno(errno_name))
+
+ def read_text(self, text_content='', errno_name=0):
+ """Provides test mock for the `read_text` method.
+
+ Args:
+ text_content (str) - The text data for this read_text step to return.
+ errno_name (str|None) - The error name for this step to return, if any.
+
+ Example:
+ yield (api.test('my_test')
+ + api.step_data('read step name',
+ api.file.read_text('some\nfile\ncontent'))
+ )
+ """
+ return (self.m.raw_io.output_text(text_content)
+ + self.errno(errno_name))
+
+ def glob_paths(self, paths=(), errno_name=0):
+ """Provides test mock for the `glob_paths` method.
+
+ Args:
+ paths (iterable[str|Path]) - The paths for the glob_paths step to return.
+ errno_name (str|None) - The error name for this step to return, if any.
+
+ Example:
+ yield (api.test('my_test')
+ + api.step_data('glob step name', api.file.glob_paths([
+ '[BASE_PATH]/some/pattern_path',
+ '[BASE_PATH]/some/pattern_other_thing',
+ ])
+ )
+ """
+ return (self.m.raw_io.stream_output('\n'.join(map(str, paths)))
+ + self.errno(errno_name))
+
+ def errno(self, errno_name=None):
+ """Provides test mock for any file module method, causing the step to raise
+ a file.Error exception.
+
+ Args:
+ errno_name (None|str) - The errno error name that the step should raise.
+ This must be e.g. 'EPERM', 'EEXIST', etc.
+ """
+ data = {'ok': True}
+ if errno_name:
+ data['ok'] = False
+ data['errno_name'] = errno_name
+ # in real operation, this message will come from the underlying OS and
+ # will potentially have descriptive detail.
+ data['message'] = 'file command encountered system error '+errno_name
+ return self.m.json.output(data)

Powered by Google App Engine
This is Rietveld 408576698