|
3 | 3 | from pathlib import Path |
4 | 4 | import subprocess |
5 | 5 |
|
| 6 | +import click |
6 | 7 | import pytest |
7 | 8 | from click.testing import CliRunner |
8 | 9 |
|
@@ -107,11 +108,141 @@ def test_configured_project_declarative_flow( |
107 | 108 | monkeypatch: pytest.MonkeyPatch, tmp_path: Path |
108 | 109 | ) -> None: |
109 | 110 | monkeypatch.chdir(tmp_path) |
| 111 | + definition_path = tmp_path / "flow.yaml" |
| 112 | + definition_path.write_text(FLOW_YAML, encoding="utf-8") |
110 | 113 | (tmp_path / "pyproject.toml").write_text( |
111 | 114 | '[tool.crewai]\ntype = "flow"\ndefinition = " flow.yaml "\n', |
112 | 115 | encoding="utf-8", |
113 | 116 | ) |
114 | 117 |
|
115 | 118 | from crewai_cli.run_declarative_flow import configured_project_declarative_flow |
116 | 119 |
|
117 | | - assert configured_project_declarative_flow() == "flow.yaml" |
| 120 | + assert configured_project_declarative_flow() == definition_path.resolve() |
| 121 | + |
| 122 | + |
| 123 | +@pytest.mark.parametrize( |
| 124 | + ("definition", "expected_error"), |
| 125 | + [ |
| 126 | + ("C:/tmp/flow.yaml", "must be relative to the project root"), |
| 127 | + ("~/flow.yaml", "must be a project-local path"), |
| 128 | + ("../flow.yaml", "must resolve inside the project root"), |
| 129 | + ], |
| 130 | +) |
| 131 | +def test_configured_project_declarative_flow_rejects_unsafe_paths( |
| 132 | + monkeypatch: pytest.MonkeyPatch, |
| 133 | + tmp_path: Path, |
| 134 | + definition: str, |
| 135 | + expected_error: str, |
| 136 | +) -> None: |
| 137 | + monkeypatch.chdir(tmp_path) |
| 138 | + (tmp_path / "pyproject.toml").write_text( |
| 139 | + f'[tool.crewai]\ntype = "flow"\ndefinition = "{definition}"\n', |
| 140 | + encoding="utf-8", |
| 141 | + ) |
| 142 | + |
| 143 | + from crewai_cli.run_declarative_flow import configured_project_declarative_flow |
| 144 | + |
| 145 | + with pytest.raises(click.UsageError) as exc_info: |
| 146 | + configured_project_declarative_flow() |
| 147 | + |
| 148 | + assert expected_error in exc_info.value.message |
| 149 | + |
| 150 | + |
| 151 | +def test_configured_project_declarative_flow_allows_normalized_project_path( |
| 152 | + monkeypatch: pytest.MonkeyPatch, |
| 153 | + tmp_path: Path, |
| 154 | +) -> None: |
| 155 | + monkeypatch.chdir(tmp_path) |
| 156 | + definition_path = tmp_path / "flow.yaml" |
| 157 | + definition_path.write_text(FLOW_YAML, encoding="utf-8") |
| 158 | + (tmp_path / "src").mkdir() |
| 159 | + (tmp_path / "pyproject.toml").write_text( |
| 160 | + '[tool.crewai]\ntype = "flow"\ndefinition = "src/../flow.yaml"\n', |
| 161 | + encoding="utf-8", |
| 162 | + ) |
| 163 | + |
| 164 | + from crewai_cli.run_declarative_flow import configured_project_declarative_flow |
| 165 | + |
| 166 | + assert configured_project_declarative_flow() == definition_path.resolve() |
| 167 | + |
| 168 | + |
| 169 | +def test_configured_project_declarative_flow_rejects_absolute_path( |
| 170 | + monkeypatch: pytest.MonkeyPatch, |
| 171 | + tmp_path: Path, |
| 172 | +) -> None: |
| 173 | + monkeypatch.chdir(tmp_path) |
| 174 | + definition = tmp_path / "flow.yaml" |
| 175 | + (tmp_path / "pyproject.toml").write_text( |
| 176 | + f'[tool.crewai]\ntype = "flow"\ndefinition = "{definition.as_posix()}"\n', |
| 177 | + encoding="utf-8", |
| 178 | + ) |
| 179 | + |
| 180 | + from crewai_cli.run_declarative_flow import configured_project_declarative_flow |
| 181 | + |
| 182 | + with pytest.raises(click.UsageError) as exc_info: |
| 183 | + configured_project_declarative_flow() |
| 184 | + |
| 185 | + assert "must be relative to the project root" in exc_info.value.message |
| 186 | + |
| 187 | + |
| 188 | +def test_configured_project_declarative_flow_rejects_symlink_escape( |
| 189 | + monkeypatch: pytest.MonkeyPatch, |
| 190 | + tmp_path: Path, |
| 191 | +) -> None: |
| 192 | + monkeypatch.chdir(tmp_path) |
| 193 | + outside_definition = tmp_path.parent / "outside-flow.yaml" |
| 194 | + outside_definition.write_text(FLOW_YAML, encoding="utf-8") |
| 195 | + link = tmp_path / "flow.yaml" |
| 196 | + try: |
| 197 | + link.symlink_to(outside_definition) |
| 198 | + except (NotImplementedError, OSError) as exc: |
| 199 | + pytest.skip(f"symlinks unavailable: {exc}") |
| 200 | + |
| 201 | + (tmp_path / "pyproject.toml").write_text( |
| 202 | + '[tool.crewai]\ntype = "flow"\ndefinition = "flow.yaml"\n', |
| 203 | + encoding="utf-8", |
| 204 | + ) |
| 205 | + |
| 206 | + from crewai_cli.run_declarative_flow import configured_project_declarative_flow |
| 207 | + |
| 208 | + with pytest.raises(click.UsageError) as exc_info: |
| 209 | + configured_project_declarative_flow() |
| 210 | + |
| 211 | + assert "must resolve inside the project root" in exc_info.value.message |
| 212 | + |
| 213 | + |
| 214 | +def test_configured_project_declarative_flow_rejects_missing_file( |
| 215 | + monkeypatch: pytest.MonkeyPatch, |
| 216 | + tmp_path: Path, |
| 217 | +) -> None: |
| 218 | + monkeypatch.chdir(tmp_path) |
| 219 | + (tmp_path / "pyproject.toml").write_text( |
| 220 | + '[tool.crewai]\ntype = "flow"\ndefinition = "missing-flow.yaml"\n', |
| 221 | + encoding="utf-8", |
| 222 | + ) |
| 223 | + |
| 224 | + from crewai_cli.run_declarative_flow import configured_project_declarative_flow |
| 225 | + |
| 226 | + with pytest.raises(click.UsageError) as exc_info: |
| 227 | + configured_project_declarative_flow() |
| 228 | + |
| 229 | + assert "must point to an existing file" in exc_info.value.message |
| 230 | + |
| 231 | + |
| 232 | +def test_configured_project_declarative_flow_rejects_directory( |
| 233 | + monkeypatch: pytest.MonkeyPatch, |
| 234 | + tmp_path: Path, |
| 235 | +) -> None: |
| 236 | + monkeypatch.chdir(tmp_path) |
| 237 | + (tmp_path / "flow.yaml").mkdir() |
| 238 | + (tmp_path / "pyproject.toml").write_text( |
| 239 | + '[tool.crewai]\ntype = "flow"\ndefinition = "flow.yaml"\n', |
| 240 | + encoding="utf-8", |
| 241 | + ) |
| 242 | + |
| 243 | + from crewai_cli.run_declarative_flow import configured_project_declarative_flow |
| 244 | + |
| 245 | + with pytest.raises(click.UsageError) as exc_info: |
| 246 | + configured_project_declarative_flow() |
| 247 | + |
| 248 | + assert "must point to a regular file" in exc_info.value.message |
0 commit comments