Spaces:
Running
Running
File size: 5,904 Bytes
e66e95e a64c042 e66e95e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | from pathlib import Path, PurePosixPath
import pytest
from multi_agent_sdlc.system.path_utils import (
normalise_relative_path,
reject_repeated_project_prefix,
resolve_project_path,
)
@pytest.fixture
def project_directory(tmp_path: Path) -> Path:
project_directory = tmp_path / "sandbox" / "terminal-calculator"
project_directory.mkdir(parents=True)
return project_directory
def test_normalise_relative_path_accepts_relative_path() -> None:
result = normalise_relative_path("src/main.py")
assert result == PurePosixPath("src/main.py")
def test_normalise_relative_path_strips_whitespace() -> None:
result = normalise_relative_path(" src/main.py ")
assert result == PurePosixPath("src/main.py")
def test_normalise_relative_path_converts_backslashes() -> None:
result = normalise_relative_path(r"src\services\calculator.py")
assert result == PurePosixPath("src/services/calculator.py")
def test_normalise_relative_path_rejects_empty_path() -> None:
with pytest.raises(
ValueError,
match="Path cannot be empty",
):
normalise_relative_path(" ")
@pytest.mark.parametrize(
"path",
[
"/etc/passwd",
"/tmp/file.py",
"/src/main.py",
],
)
def test_normalise_relative_path_rejects_absolute_path(
path: str,
) -> None:
with pytest.raises(
PermissionError,
match="Absolute paths are not allowed",
):
normalise_relative_path(path)
@pytest.mark.parametrize(
"path",
[
"../main.py",
"../../main.py",
"src/../main.py",
"src/services/../../main.py",
],
)
def test_normalise_relative_path_rejects_parent_traversal(
path: str,
) -> None:
with pytest.raises(
PermissionError,
match="Parent-directory traversal is not allowed",
):
normalise_relative_path(path)
def test_reject_repeated_project_prefix_allows_normal_path(
project_directory: Path,
) -> None:
reject_repeated_project_prefix(
project_directory,
PurePosixPath("src/main.py"),
)
def test_reject_repeated_project_prefix_rejects_project_id(
project_directory: Path,
) -> None:
with pytest.raises(
PermissionError,
match="Do not include the project-directory prefix",
):
reject_repeated_project_prefix(
project_directory,
PurePosixPath("terminal-calculator/src/main.py"),
)
def test_reject_repeated_project_prefix_rejects_sandbox_and_project_id(
project_directory: Path,
) -> None:
with pytest.raises(
PermissionError,
match="Do not include the project-directory prefix",
):
reject_repeated_project_prefix(
project_directory,
PurePosixPath("sandbox/terminal-calculator/src/main.py"),
)
@pytest.mark.parametrize(
"path",
[
"docs/terminal-calculator.md",
"src/sandbox.py",
"sandbox_helpers/utils.py",
],
)
def test_reject_repeated_project_prefix_allows_project_name_outside_prefix(
project_directory: Path,
path: str,
) -> None:
reject_repeated_project_prefix(
project_directory,
PurePosixPath(path),
)
def test_resolve_project_path_returns_resolved_project_path(
project_directory: Path,
) -> None:
result = resolve_project_path(
project_directory,
"src/main.py",
)
assert result == (project_directory / "src/main.py").resolve()
def test_resolve_project_path_normalises_backslashes(
project_directory: Path,
) -> None:
result = resolve_project_path(
project_directory,
r"src\services\calculator.py",
)
assert (
result == (project_directory / "src" / "services" / "calculator.py").resolve()
)
@pytest.mark.parametrize(
"path",
[
"terminal-calculator/src/main.py",
"sandbox/terminal-calculator/src/main.py",
],
)
def test_resolve_project_path_rejects_repeated_project_prefix(
project_directory: Path,
path: str,
) -> None:
with pytest.raises(
PermissionError,
match="Do not include the project-directory prefix",
):
resolve_project_path(
project_directory,
path,
)
def test_resolve_project_path_rejects_parent_traversal(
project_directory: Path,
) -> None:
with pytest.raises(
PermissionError,
match="Parent-directory traversal is not allowed",
):
resolve_project_path(
project_directory,
"../outside.py",
)
def test_resolve_project_path_rejects_absolute_path(
project_directory: Path,
) -> None:
with pytest.raises(
PermissionError,
match="Absolute paths are not allowed",
):
resolve_project_path(
project_directory,
"/tmp/outside.py",
)
def test_resolve_project_path_rejects_symlink_escape(
tmp_path: Path,
project_directory: Path,
) -> None:
outside_directory = tmp_path / "outside"
outside_directory.mkdir()
symlink = project_directory / "external"
symlink.symlink_to(
outside_directory,
target_is_directory=True,
)
with pytest.raises(
PermissionError,
match="Path escapes the project directory",
):
resolve_project_path(
project_directory,
"external/file.py",
)
def test_resolve_project_path_allows_symlink_inside_project(
project_directory: Path,
) -> None:
target_directory = project_directory / "src"
target_directory.mkdir()
symlink = project_directory / "source"
symlink.symlink_to(
target_directory,
target_is_directory=True,
)
result = resolve_project_path(
project_directory,
"source/main.py",
)
assert result == (target_directory / "main.py").resolve()
|