tfds.testing.MockFs

This util wraps mock for the tf.io.gfile / epath.Path API.

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 a file, creating all parent directories.

contextmanager

View source

Activate the mock file system.

mock

View source

View source

read_file

View source

__enter__

View source

__exit__

View source