Skip to content

Commit

Permalink
Merge pull request #74 from RRosio/config-debug
Browse files Browse the repository at this point in the history
Config error handling updates, testing and config validation
  • Loading branch information
RRosio authored Feb 20, 2025
2 parents 64a1d07 + 4c3cb71 commit 0860388
Show file tree
Hide file tree
Showing 8 changed files with 465 additions and 296 deletions.
3 changes: 3 additions & 0 deletions .pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
markers =
no_setup_config_file_fs: mark test to not use setup_config_file_fs fixture
115 changes: 93 additions & 22 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,87 @@ def setup_tmp_local(tmp_path: Path):
yield [local_root, local_empty_root]


@pytest.fixture(scope="function", autouse=True)
@pytest.fixture(scope="function")
def no_config_permission(tmp_path: Path):
config_dir = tmp_path / "config"
config_dir.mkdir(exist_ok=True)
os.chmod(config_dir, 0o44)

with patch(
"jupyter_fsspec.file_manager.jupyter_config_dir", return_value=str(config_dir)
):
print(f"Patching jupyter_config_dir to: {config_dir}")
yield config_dir

os.chmod(config_dir, 0o755)
config_dir.rmdir()


@pytest.fixture(scope="function")
def malformed_config(tmp_path: Path):
config_dir = tmp_path / "config"
config_dir.mkdir(exist_ok=True)

yaml_content = f"""sources:
- name: "TestSourceAWS"
path: "s3://my-test-bucket/"
kwargs:
anon: false
key: "my-access-key"
secret: "my-secret-key"
client_kwargs:
endpoint_url: "{ENDPOINT_URI}"
"""
yaml_file = config_dir / "jupyter-fsspec.yaml"
yaml_file.write_text(yaml_content)

with patch(
"jupyter_fsspec.file_manager.jupyter_config_dir", return_value=str(config_dir)
):
print(f"Patching jupyter_config_dir to: {config_dir}")


@pytest.fixture(scope="function")
def bad_info_config(tmp_path: Path):
config_dir = tmp_path / "config"
config_dir.mkdir(exist_ok=True)

yaml_content = f"""sources:
- nme: "TestSourceAWS"
path: s3://my-test-bucket/"
kwargs:
anon: false
key: "my-access-key"
secret: "my-secret-key"
client_kwargs:
endpoint_url: "{ENDPOINT_URI}"
"""
yaml_file = config_dir / "jupyter-fsspec.yaml"
yaml_file.write_text(yaml_content)

with patch(
"jupyter_fsspec.file_manager.jupyter_config_dir", return_value=str(config_dir)
):
print(f"Patching jupyter_config_dir to: {config_dir}")


@pytest.fixture(scope="function")
def empty_config(tmp_path: Path):
config_dir = tmp_path / "config"
config_dir.mkdir(exist_ok=True)

yaml_content = """ """
yaml_file = config_dir / "jupyter-fsspec.yaml"
yaml_file.write_text(yaml_content)

with patch(
"jupyter_fsspec.file_manager.jupyter_config_dir", return_value=str(config_dir)
):
print(f"Patching jupyter_config_dir to: {config_dir}")


# TODO: split?
@pytest.fixture(scope="function")
def setup_config_file_fs(tmp_path: Path, setup_tmp_local):
tmp_local = setup_tmp_local[0]
items_tmp_local = list(tmp_local.iterdir())
Expand Down Expand Up @@ -76,36 +156,27 @@ def setup_config_file_fs(tmp_path: Path, setup_tmp_local):


@pytest.fixture(scope="function")
def fs_manager_instance(setup_config_file_fs, s3_client):
async def fs_manager_instance(setup_config_file_fs, s3_client):
fs_manager = setup_config_file_fs
fs_info = fs_manager.get_filesystem_by_protocol("memory")
mem_fs = fs_info["info"]["instance"]
mem_root_path = fs_info["info"]["path"]

if mem_fs:
if mem_fs.exists(f"{mem_root_path}/test_dir"):
mem_fs.rm(f"{mem_root_path}/test_dir", recursive=True)
if mem_fs.exists(f"{mem_root_path}/second_dir"):
mem_fs.rm(f"{mem_root_path}/second_dir", recursive=True)

mem_fs.touch(f"{mem_root_path}/file_in_root.txt")
with mem_fs.open(f"{mem_root_path}/file_in_root.txt", "wb") as f:
f.write("Root file content".encode())

mem_fs.mkdir(f"{mem_root_path}/test_dir", exist_ok=True)
mem_fs.mkdir(f"{mem_root_path}/second_dir", exist_ok=True)
# mem_fs.mkdir(f'{mem_root_path}/second_dir/subdir', exist_ok=True)
mem_fs.touch(f"{mem_root_path}/test_dir/file1.txt")
with mem_fs.open(f"{mem_root_path}/test_dir/file1.txt", "wb") as f:
f.write("Test content".encode())
f.close()
if await mem_fs._exists(f"{mem_root_path}/test_dir"):
await mem_fs._rm(f"{mem_root_path}/test_dir", recursive=True)
if await mem_fs._exists(f"{mem_root_path}/second_dir"):
await mem_fs._rm(f"{mem_root_path}/second_dir", recursive=True)

await mem_fs._pipe(f"{mem_root_path}/file_in_root.txt", b"Root file content")

await mem_fs._mkdir(f"{mem_root_path}/test_dir", exist_ok=True)
await mem_fs._mkdir(f"{mem_root_path}/second_dir", exist_ok=True)

await mem_fs._pipe(f"{mem_root_path}/test_dir/file1.txt", b"Test content")
else:
print("In memory filesystem NOT FOUND")

if mem_fs.exists(f"{mem_root_path}/test_dir/file1.txt"):
mem_fs.info(f"{mem_root_path}/test_dir/file1.txt")
else:
print("File does not exist!")
return fs_manager


Expand Down
4 changes: 4 additions & 0 deletions jupyter_fsspec/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@

class JupyterFsspecException(Exception):
pass


class ConfigFileException(JupyterFsspecException):
pass
Loading

0 comments on commit 0860388

Please sign in to comment.