Set up a mock filesystem for fsspec containing the provided files Example: ```py >>> DummyTestFS = mock_fs(["data/train.txt", "data.test.txt"]) >>> fs = DummyTestFS() >>> assert fsspec.get_filesystem_class("mock").__name__ == "DummyTestFS" >>> assert type(fs).__name__
(file_paths: List[str])
| 522 | |
| 523 | |
| 524 | def mock_fs(file_paths: List[str]): |
| 525 | """ |
| 526 | Set up a mock filesystem for fsspec containing the provided files |
| 527 | |
| 528 | Example: |
| 529 | |
| 530 | ```py |
| 531 | >>> DummyTestFS = mock_fs(["data/train.txt", "data.test.txt"]) |
| 532 | >>> fs = DummyTestFS() |
| 533 | >>> assert fsspec.get_filesystem_class("mock").__name__ == "DummyTestFS" |
| 534 | >>> assert type(fs).__name__ == "DummyTestFS" |
| 535 | >>> print(fs.glob("**")) |
| 536 | ["data", "data/train.txt", "data.test.txt"] |
| 537 | ``` |
| 538 | """ |
| 539 | file_paths = [file_path.split("://")[-1] for file_path in file_paths] |
| 540 | dir_paths = { |
| 541 | "/".join(file_path.split("/")[: i + 1]) for file_path in file_paths for i in range(file_path.count("/")) |
| 542 | } |
| 543 | fs_contents = [{"name": dir_path, "type": "directory"} for dir_path in dir_paths] + [ |
| 544 | {"name": file_path, "type": "file", "size": 10} for file_path in file_paths |
| 545 | ] |
| 546 | |
| 547 | class DummyTestFS(AbstractFileSystem): |
| 548 | protocol = ("mock", "dummy") |
| 549 | _fs_contents = fs_contents |
| 550 | |
| 551 | def ls(self, path, detail=True, refresh=True, **kwargs): |
| 552 | if kwargs.pop("strip_proto", True): |
| 553 | path = self._strip_protocol(path) |
| 554 | |
| 555 | files = not refresh and self._ls_from_cache(path) |
| 556 | if not files: |
| 557 | files = [file for file in self._fs_contents if path == self._parent(file["name"])] |
| 558 | files.sort(key=lambda file: file["name"]) |
| 559 | self.dircache[path.rstrip("/")] = files |
| 560 | |
| 561 | if detail: |
| 562 | return files |
| 563 | return [file["name"] for file in files] |
| 564 | |
| 565 | return DummyTestFS |
| 566 | |
| 567 | |
| 568 | @pytest.mark.parametrize("base_path", ["", "mock://", "my_dir"]) |
no test coverage detected