def test_read_json_dir_can_ignore_errors( tmp_path: Path, sync_filesystem: SyncFilesystem ) -> None: """It should allow reading all directory files while ignoring any errors.""" Path(tmp_path / "foo.json").write_text("""{ "foo": "hello", "bar": 0 }""") Path(tmp_path / "bar.json").write_text("""{ "foo": "from the",}""") path = PurePosixPath(tmp_path) files: List[Entry[ParsedObj]] = sync_filesystem.read_json_dir( path, ignore_errors=True ) assert files == [Entry(path=path / "foo", contents={"foo": "hello", "bar": 0})]
def test_read_json_dir_reads_multiple_files( tmp_path: Path, sync_filesystem: SyncFilesystem ) -> None: """It should read a all files in a directory.""" Path(tmp_path / "foo.json").write_text("""{ "foo": "hello", "bar": 0 }""") Path(tmp_path / "bar.json").write_text("""{ "foo": "from the", "bar": 1 }""") Path(tmp_path / "baz.json").write_text("""{ "foo": "other side", "bar": 2 }""") path = PurePosixPath(tmp_path) files: List[Entry[ParsedObj]] = sync_filesystem.read_json_dir(path) assert len(files) == 3 assert Entry(path=path / "foo", contents={"foo": "hello", "bar": 0}) in files assert Entry(path=path / "bar", contents={"foo": "from the", "bar": 1}) in files assert Entry(path=path / "baz", contents={"foo": "other side", "bar": 2}) in files
def test_read_json_dir_with_custom_parser( tmp_path: Path, mock_parse_json: MagicMock, sync_filesystem: SyncFilesystem ) -> None: """It should read a all files in a directory.""" Path(tmp_path / "foo.json").write_text("""{ "foo": "hello", "bar": 0 }""") Path(tmp_path / "bar.json").write_text("""{ "foo": "from the", "bar": 1 }""") Path(tmp_path / "baz.json").write_text("""{ "foo": "other side", "bar": 2 }""") mock_parse_json.return_value = {"call me": "maybe"} path = PurePosixPath(tmp_path) files = sync_filesystem.read_json_dir(path, parse_json=mock_parse_json) assert len(files) == 3 assert Entry(path=path / "foo", contents={"call me": "maybe"}) in files assert Entry(path=path / "bar", contents={"call me": "maybe"}) in files assert Entry(path=path / "baz", contents={"call me": "maybe"}) in files mock_parse_json.assert_any_call("""{ "foo": "hello", "bar": 0 }""") mock_parse_json.assert_any_call("""{ "foo": "from the", "bar": 1 }""") mock_parse_json.assert_any_call("""{ "foo": "other side", "bar": 2 }""")