tfds.testing.MockFs
Stay organized with collections
Save and categorize content based on your preferences.
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
View source
add_file(
path, content=None
) -> None
Add a file, creating all parent directories.
contextmanager
View source
@contextlib.contextmanager
contextmanager() -> Iterator['MockFs']
Activate the mock file system.
mock
View source
@contextlib.contextmanager
mock()
print_tree
View source
print_tree() -> None
read_file
View source
read_file(
path
) -> str
__enter__
View source
__enter__()
__exit__
View source
__exit__(
exc_type, exc_value, traceback
)
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tfds.testing.MockFs\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/datasets/blob/v4.9.3/tensorflow_datasets/testing/test_utils.py#L83-L244) |\n\nThis util wraps mock for the [`tf.io.gfile`](https://www.tensorflow.org/api_docs/python/tf/io/gfile) / `epath.Path` API. \n\n tfds.testing.MockFs()\n\nThis allow to test code which uses absolute paths / GCS path while keeping\ntests hermetic.\n\n#### Usage:\n\n with MockFs() as fs:\n # GCS example\n fs.add_file('gs://bucket/dir/file.txt')\n\n assert tf.io.gfile.glob('gs://bucket/*/file.txt') == [\n 'gs://bucket/dir/file.txt',\n ]\n\n # This also works with absolute paths\n tf.io.gfile.makedirs('/path/to/')\n with tf.io.gfile.GFile('/path/to/file.txt', 'w') as f:\n f.write('Content of file.txt')\n\nInternally, this is done by converting absolute path into local tmp paths:\n\n- `/absolute/path` -\\\u003e `/tmp/mocked_file_system/absolute/path`\n- `gs://path` -\\\u003e `/tmp/mocked_file_system/gs/path`\n\nMethods\n-------\n\n### `add_file`\n\n[View source](https://github.com/tensorflow/datasets/blob/v4.9.3/tensorflow_datasets/testing/test_utils.py#L190-L196) \n\n add_file(\n path, content=None\n ) -\u003e None\n\nAdd a file, creating all parent directories.\n\n### `contextmanager`\n\n[View source](https://github.com/tensorflow/datasets/blob/v4.9.3/tensorflow_datasets/testing/test_utils.py#L124-L128) \n\n @contextlib.contextmanager\n contextmanager() -\u003e Iterator['MockFs']\n\nActivate the mock file system.\n\n### `mock`\n\n[View source](https://github.com/tensorflow/datasets/blob/v4.9.3/tensorflow_datasets/testing/test_utils.py#L130-L138) \n\n @contextlib.contextmanager\n mock()\n\n### `print_tree`\n\n[View source](https://github.com/tensorflow/datasets/blob/v4.9.3/tensorflow_datasets/testing/test_utils.py#L243-L244) \n\n print_tree() -\u003e None\n\n### `read_file`\n\n[View source](https://github.com/tensorflow/datasets/blob/v4.9.3/tensorflow_datasets/testing/test_utils.py#L198-L199) \n\n read_file(\n path\n ) -\u003e str\n\n### `__enter__`\n\n[View source](https://github.com/tensorflow/datasets/blob/v4.9.3/tensorflow_datasets/testing/test_utils.py#L116-L118) \n\n __enter__()\n\n### `__exit__`\n\n[View source](https://github.com/tensorflow/datasets/blob/v4.9.3/tensorflow_datasets/testing/test_utils.py#L120-L122) \n\n __exit__(\n exc_type, exc_value, traceback\n )"]]