<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/datasets/blob/v4.9.10/tensorflow_datasets/testing/test_utils.py#L96-L257) |

This util wraps mock for the [`tf.io.gfile`](https://www.tensorflow.org/api_docs/python/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](https://github.com/tensorflow/datasets/blob/v4.9.10/tensorflow_datasets/testing/test_utils.py#L203-L209)

    add_file(
        path, content=None
    ) -> None

Add a file, creating all parent directories.

### `contextmanager`

[View source](https://github.com/tensorflow/datasets/blob/v4.9.10/tensorflow_datasets/testing/test_utils.py#L137-L141)

    @contextlib.contextmanager
    contextmanager() -> Iterator['MockFs']

Activate the mock file system.

### `mock`

[View source](https://github.com/tensorflow/datasets/blob/v4.9.10/tensorflow_datasets/testing/test_utils.py#L143-L151)

    @contextlib.contextmanager
    mock()

### `print_tree`

[View source](https://github.com/tensorflow/datasets/blob/v4.9.10/tensorflow_datasets/testing/test_utils.py#L256-L257)

    print_tree() -> None

### `read_file`

[View source](https://github.com/tensorflow/datasets/blob/v4.9.10/tensorflow_datasets/testing/test_utils.py#L211-L212)

    read_file(
        path
    ) -> str

### `__enter__`

[View source](https://github.com/tensorflow/datasets/blob/v4.9.10/tensorflow_datasets/testing/test_utils.py#L129-L131)

    __enter__()

### `__exit__`

[View source](https://github.com/tensorflow/datasets/blob/v4.9.10/tensorflow_datasets/testing/test_utils.py#L133-L135)

    __exit__(
        exc_type, exc_value, traceback
    )