View source on GitHub |
This util wraps mock for the tf.io.gfile
/ epath.Path
API.
tfds.testing.MockFs()
This allow to test code which uses absolute paths / GCS path while keeping tests hermetic.
Usage:
with MockFs() as fs:
# GCS example
fs.add_file('gs://bucket/dir/file.txt')
assert tf.io.gfile.glob('gs://bucket/*/file.txt') == [
'gs://bucket/dir/file.txt',
]
# This also works with absolute paths
tf.io.gfile.makedirs('/path/to/')
with tf.io.gfile.GFile('/path/to/file.txt', 'w') as f:
f.write('Content of file.txt')
Internally, this is done by converting absolute path into local tmp paths:
/absolute/path
->/tmp/mocked_file_system/absolute/path
gs://path
->/tmp/mocked_file_system/gs/path
Methods
add_file
add_file(
path, content=None
) -> None
Add a file, creating all parent directories.
contextmanager
@contextlib.contextmanager
contextmanager() -> Iterator['MockFs']
Activate the mock file system.
mock
@contextlib.contextmanager
mock()
print_tree
print_tree() -> None
read_file
read_file(
path
) -> str
__enter__
__enter__()
__exit__
__exit__(
exc_type, exc_value, traceback
)