def test_read_json_raises_path_does_not_exist_error(
    tmp_path: Path, sync_filesystem: SyncFilesystem
) -> None:
    """It should return a FileNotFoundError if file doesn't exist."""
    path = PurePosixPath(tmp_path / "foo")

    with pytest.raises(PathNotFoundError, match="No such file or directory"):
        sync_filesystem.read_json(path)
def test_read_json_raises_parse_error(
    tmp_path: Path, sync_filesystem: SyncFilesystem
) -> None:
    """It should raise a FileParseError if file is not valid JSON."""
    Path(tmp_path / "foo.json").write_text("""{ "foo": "hello", }""")

    path = PurePosixPath(tmp_path / "foo")

    with pytest.raises(FileParseError, match="Expecting property name"):
        sync_filesystem.read_json(path)
def test_read_json_when_custom_parser_raises(
    tmp_path: Path, mock_parse_json: MagicMock, sync_filesystem: SyncFilesystem
) -> None:
    """It should raise a FileParseError if the custom parser raises."""
    Path(tmp_path / "foo.json").write_text("""{ "foo": "hello", "bar": 42 }""")

    error = Exception("oh no")
    mock_parse_json.side_effect = error
    path = PurePosixPath(tmp_path / "foo")

    with pytest.raises(FileParseError):
        sync_filesystem.read_json(path, parse_json=mock_parse_json)
def test_read_json_loads_json_file_to_dict(
    tmp_path: Path, sync_filesystem: SyncFilesystem
) -> None:
    """It should read a file and load the contents into JSON."""
    Path(tmp_path / "foo.json").write_text("""{ "foo": "hello", "bar": 42 }""")

    path = PurePosixPath(tmp_path / "foo")
    result: ParsedObj = sync_filesystem.read_json(path)

    assert result == {"foo": "hello", "bar": 42}
def test_read_json_with_custom_parser(
    tmp_path: Path, mock_parse_json: MagicMock, sync_filesystem: SyncFilesystem
) -> None:
    """It should read a file and parse into JSON using custom parser."""
    Path(tmp_path / "foo.json").write_text("""{ "this": "is crazy" }""")

    mock_parse_json.return_value = {"call me": "maybe"}
    path = PurePosixPath(tmp_path / "foo")
    result = sync_filesystem.read_json(path, parse_json=mock_parse_json)

    assert result == {"call me": "maybe"}
    mock_parse_json.assert_called_with("""{ "this": "is crazy" }""")