code: def test_mock_fs(as_path_fn):
      _p = as_path_fn  # pylint: disable=invalid-name
      fs = test_utils.MockFs()
      with fs.mock():
          fs.add_file(_p("/path/to/file1"), "Content of file 1")
          fs.add_file(_p("/path/file.txt"), "Content of file.txt")
  
          # Test `tf.io.gfile.exists`
          assert tf.io.gfile.exists(_p("/path/to/file1"))
          assert tf.io.gfile.exists(_p("/path/to/"))
          assert tf.io.gfile.exists(_p("/path/to"))
          assert tf.io.gfile.exists(_p("/path"))
          assert not tf.io.gfile.exists(_p("/pat"))
          assert not tf.io.gfile.exists(
              _p("/path/to/file1_nonexisting")
          )
          assert not tf.io.gfile.exists(
              _p("/path/to_file1_nonexisting")
          )
  
          # Test `tf.io.gfile.exists` (relative path)
          fs.add_file(_p("relative_path/to/file.txt"), "Content")
          assert tf.io.gfile.exists(_p("relative_path/to/file.txt"))
          assert tf.io.gfile.exists(_p("relative_path/to/"))
          assert tf.io.gfile.exists(_p("relative_path/to"))
          assert tf.io.gfile.exists(_p("relative_path"))
  
          # Test `tf.io.gfile.GFile` (write and read mode)
          with tf.io.gfile.GFile(_p("/path/to/file2"), "w") as f:
              f.write("Content of file 2 (old)")
          assert (
              fs.read_file("/path/to/file2")
              == "Content of file 2 (old)"
          )
          with tf.io.gfile.GFile(_p("/path/to/file2"), "w") as f:
              f.write("Content of file 2 (new)")
          assert (
              fs.read_file("/path/to/file2")
              == "Content of file 2 (new)"
          )
          with tf.io.gfile.GFile(_p("/path/to/file2"), "r") as f:
              assert f.read() == "Content of file 2 (new)"
  
          # Test `tf.io.gfile.rename`
          assert fs.read_file("/path/to/file1") == "Content of file 1"
          tf.io.gfile.rename(
              _p("/path/to/file1"), _p("/path/to/file1_moved")
          )
          assert not tf.io.gfile.exists("/path/to/file1")
          assert (
              fs.read_file("/path/to/file1_moved")
              == "Content of file 1"
          )
  
          # Test `tf.io.gfile.listdir`
          assert set(tf.io.gfile.listdir(_p("/path/to"))) == set(
              tf.io.gfile.listdir(_p("/path/to/"))
          )
          assert set(tf.io.gfile.listdir(_p("/path/to"))) == {
              "file1_moved",
              "file2",
          }
          assert set(tf.io.gfile.listdir(_p("/path"))) == {
              "file.txt",
              "to",
          }
  
          # Test `MockFs.files`
          assert (
              fs.read_file("/path/to/file2")
              == "Content of file 2 (new)"
          )
          assert (
              fs.read_file("/path/to/file1_moved")
              == "Content of file 1"
          )
          assert fs.read_file("/path/file.txt") == "Content of file.txt"
          assert fs.read_file("relative_path/to/file.txt") == "Content"
  
file_path: datasets/tensorflow_datasets/testing/test_utils_test.py
imports: import pathlib
  import pytest
  import tensorflow
  from tensorflow_datasets.testing import test_case
  from tensorflow_datasets.testing import test_utils
label_desc: Use image node_id 1 for calling a global function with example usage: test_mock_fs(as_path_fn) without return types