Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(list_dir): check permissions and consider workspace filters config #2279

Merged
merged 7 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions antarest/study/storage/explorer_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@ def list_dir(
workspace = get_workspace_from_config(self.config, workspace_name, default_allowed=False)
directory_path = get_folder_from_workspace(workspace, workspace_directory_path)
directories = []
for child in directory_path.iterdir():
if child.is_dir() and not is_study_folder(child) and not should_ignore_folder_for_scan(child):
try:
children = list(directory_path.iterdir())
except PermissionError:
children = [] # we don't want to try to read folders we can't access
for child in children:
if (
child.is_dir()
and not is_study_folder(child)
and not should_ignore_folder_for_scan(child)
and not child.name.startswith((".", "$"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name check for hidden file doesn't work on windows, does it?

Copy link
Contributor Author

@MartinBelthle MartinBelthle Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this code to use the workspace filters to mutualize the code

):
# we don't want to expose the full absolute path on the server
child_rel_path = child.relative_to(workspace.path)
directories.append(NonStudyFolderDTO(path=child_rel_path, workspace=workspace_name, name=child.name))
Expand Down
34 changes: 22 additions & 12 deletions tests/storage/business/test_explorer_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# This file is part of the Antares project.

from pathlib import Path
from unittest.mock import patch

import pytest

Expand All @@ -23,17 +24,9 @@ def build_config(root: Path) -> Config:
return Config(
storage=StorageConfig(
workspaces={
DEFAULT_WORKSPACE_NAME: WorkspaceConfig(path=root / DEFAULT_WORKSPACE_NAME, groups=["toto"]),
"diese": WorkspaceConfig(
path=root / "diese",
groups=["tata"],
filter_out=["to_skip.*"],
),
"test": WorkspaceConfig(
path=root / "test",
groups=["toto"],
filter_out=["to_skip.*"],
),
DEFAULT_WORKSPACE_NAME: WorkspaceConfig(path=root / DEFAULT_WORKSPACE_NAME),
"diese": WorkspaceConfig(path=root / "diese"),
"test": WorkspaceConfig(path=root / "test"),
}
)
)
Expand Down Expand Up @@ -74,6 +67,14 @@ def config_scenario_a(tmp_path: Path) -> Config:
(f / "AW_NO_SCAN").touch()
(f / "study.antares").touch()

d = diese / ".git"
d.mkdir(parents=True)
(d / "config.txt").touch()

d = diese / "$RECYCLE.bin"
d.mkdir(parents=True)
(d / "trash").touch()

config = build_config(tmp_path)

return config
Expand All @@ -84,7 +85,7 @@ def test_list_dir_empty_string(config_scenario_a: Config):
explorer = Explorer(config_scenario_a)
result = explorer.list_dir("diese", "")

assert len(result) == 1
assert len(result) == 1 # we don't want to see the .git folder or the $RECYCLE.BIN
assert result[0] == NonStudyFolderDTO(path=Path("folder"), workspace="diese", name="folder")


Expand All @@ -108,6 +109,15 @@ def test_list_dir_in_empty_folder(config_scenario_a: Config):
assert len(result) == 0


@pytest.mark.unit_test
def test_list_dir_with_permission_error(config_scenario_a: Config):
explorer = Explorer(config_scenario_a)
with patch("os.listdir", side_effect=PermissionError("Permission denied")):
# asserts the endpoint doesn't fail but rather returns an empty list
result = explorer.list_dir("diese", "folder")
assert len(result) == 0


@pytest.mark.unit_test
def test_list_workspaces(tmp_path: Path):
config = build_config(tmp_path)
Expand Down
Loading