Spaces:
Running
Running
Download tests/tools/shared/test_validations.py from pedrorezende/multi-agent-sdlc: direct link, hf CLI and curl.
- Browser
- Download file 11.9 kB
-
https://huggingface.co/spaces/pedrorezende/multi-agent-sdlc/resolve/main/tests/tools/shared/test_validations.py
- Command line
-
hf download hf://spaces/pedrorezende/multi-agent-sdlc/tests/tools/shared/test_validations.py
-
curl -L -o test_validations.py https://huggingface.co/spaces/pedrorezende/multi-agent-sdlc/resolve/main/tests/tools/shared/test_validations.py
11.9 kB
| import pytest | |
| from packaging.requirements import Requirement | |
| from multi_agent_sdlc.tools.shared.validation import ( | |
| create_dependency_validator, | |
| create_entry_point_validator, | |
| create_module_name_validator, | |
| parse_dependency, | |
| validate_application_arguments, | |
| validate_file_content, | |
| validate_project_relative_path, | |
| ) | |
| def test_validate_application_arguments_accepts_valid_arguments( | |
| arguments: list[str], | |
| expected: list[str], | |
| ) -> None: | |
| assert validate_application_arguments(arguments) == expected | |
| def test_validate_application_arguments_rejects_multiline_arguments( | |
| argument: str, | |
| ) -> None: | |
| with pytest.raises( | |
| PermissionError, | |
| match="Multiline application arguments are not allowed", | |
| ): | |
| validate_application_arguments([argument]) | |
| def test_validate_application_arguments_rejects_null_bytes() -> None: | |
| with pytest.raises( | |
| PermissionError, | |
| match="Null bytes are not allowed", | |
| ): | |
| validate_application_arguments(["value\x00other"]) | |
| def test_validate_application_arguments_rejects_non_string_arguments( | |
| argument: object, | |
| ) -> None: | |
| with pytest.raises( | |
| TypeError, | |
| match="Every application argument must be a string", | |
| ): | |
| validate_application_arguments([argument]) # type: ignore[list-item] | |
| def test_validate_application_arguments_preserves_argument_order() -> None: | |
| arguments = ["--first", "one", "--second", "two"] | |
| assert validate_application_arguments(arguments) == arguments | |
| def test_validate_application_arguments_returns_new_list() -> None: | |
| arguments = ["--help"] | |
| result = validate_application_arguments(arguments) | |
| assert result == arguments | |
| assert result is not arguments | |
| def test_validate_project_relative_path_accepts_and_normalises_valid_paths( | |
| path: str, | |
| expected: str, | |
| ) -> None: | |
| assert validate_project_relative_path(path) == expected | |
| def test_validate_project_relative_path_rejects_parent_traversal( | |
| path: str, | |
| ) -> None: | |
| with pytest.raises( | |
| PermissionError, | |
| match="Parent-directory traversal is not allowed", | |
| ): | |
| validate_project_relative_path(path) | |
| def test_validate_project_relative_path_rejects_absolute_paths( | |
| path: str, | |
| ) -> None: | |
| with pytest.raises( | |
| PermissionError, | |
| match="Absolute paths are not allowed", | |
| ): | |
| validate_project_relative_path(path) | |
| def test_validate_file_content_accepts_valid_content( | |
| content: str, | |
| ) -> None: | |
| assert validate_file_content(content) == content | |
| def test_validate_file_content_rejects_null_bytes() -> None: | |
| with pytest.raises( | |
| ValueError, | |
| match="File content cannot contain null bytes", | |
| ): | |
| validate_file_content("before\x00after") | |
| def test_entry_point_validator_accepts_allowed_entry_point() -> None: | |
| validator = create_entry_point_validator( | |
| role="Coder", | |
| prohibited_entry_points={"pytest", "ruff"}, | |
| ) | |
| assert validator("expense-tracker") == "expense-tracker" | |
| def test_entry_point_validator_strips_surrounding_whitespace() -> None: | |
| validator = create_entry_point_validator( | |
| role="Coder", | |
| prohibited_entry_points=set(), | |
| ) | |
| assert validator(" expense-tracker ") == "expense-tracker" | |
| def test_entry_point_validator_rejects_empty_entry_point( | |
| entry_point: str, | |
| ) -> None: | |
| validator = create_entry_point_validator( | |
| role="Coder", | |
| prohibited_entry_points=set(), | |
| ) | |
| with pytest.raises( | |
| ValueError, | |
| match="Entry point cannot be empty", | |
| ): | |
| validator(entry_point) | |
| def test_entry_point_validator_rejects_prohibited_entry_point_case_insensitively( | |
| entry_point: str, | |
| ) -> None: | |
| validator = create_entry_point_validator( | |
| role="Coder", | |
| prohibited_entry_points={"pytest"}, | |
| ) | |
| with pytest.raises( | |
| ValueError, | |
| match="The Coder cannot execute", | |
| ): | |
| validator(entry_point) | |
| def test_module_name_validator_accepts_valid_module_names( | |
| module: str, | |
| expected: str, | |
| ) -> None: | |
| validator = create_module_name_validator( | |
| role="Coder", | |
| prohibited_python_modules=set(), | |
| ) | |
| assert validator(module) == expected | |
| def test_module_name_validator_rejects_invalid_module_names( | |
| module: str, | |
| ) -> None: | |
| validator = create_module_name_validator( | |
| role="Coder", | |
| prohibited_python_modules=set(), | |
| ) | |
| with pytest.raises(ValueError): | |
| validator(module) | |
| def test_module_name_validator_rejects_prohibited_root_module( | |
| module: str, | |
| ) -> None: | |
| validator = create_module_name_validator( | |
| role="Coder", | |
| prohibited_python_modules={"pytest"}, | |
| ) | |
| with pytest.raises( | |
| PermissionError, | |
| match="The Coder cannot execute Python module", | |
| ): | |
| validator(module) | |
| def test_module_name_validator_allows_prohibited_name_as_non_root_module() -> None: | |
| validator = create_module_name_validator( | |
| role="Coder", | |
| prohibited_python_modules={"pytest"}, | |
| ) | |
| assert validator("application.pytest") == "application.pytest" | |
| def test_dependency_validator_accepts_valid_dependencies( | |
| dependency: str, | |
| expected: str, | |
| ) -> None: | |
| validator = create_dependency_validator( | |
| role="Coder", | |
| prohibited_dependencies=set(), | |
| ) | |
| assert validator(dependency) == expected | |
| def test_dependency_validator_rejects_empty_dependency( | |
| dependency: str, | |
| ) -> None: | |
| validator = create_dependency_validator( | |
| role="Coder", | |
| prohibited_dependencies=set(), | |
| ) | |
| with pytest.raises( | |
| ValueError, | |
| match="Dependency specification cannot be empty", | |
| ): | |
| validator(dependency) | |
| def test_dependency_validator_rejects_command_line_options( | |
| dependency: str, | |
| ) -> None: | |
| validator = create_dependency_validator( | |
| role="Coder", | |
| prohibited_dependencies=set(), | |
| ) | |
| with pytest.raises( | |
| PermissionError, | |
| match="Dependency command-line options are not allowed", | |
| ): | |
| validator(dependency) | |
| def test_dependency_validator_rejects_url_dependencies( | |
| dependency: str, | |
| ) -> None: | |
| validator = create_dependency_validator( | |
| role="Coder", | |
| prohibited_dependencies=set(), | |
| ) | |
| with pytest.raises( | |
| PermissionError, | |
| match="Git, URL, SSH, and local-file dependencies are not allowed", | |
| ): | |
| validator(dependency) | |
| def test_dependency_validator_rejects_prohibited_dependency( | |
| dependency: str, | |
| ) -> None: | |
| validator = create_dependency_validator( | |
| role="Coder", | |
| prohibited_dependencies={"pytest"}, | |
| ) | |
| with pytest.raises( | |
| PermissionError, | |
| match="The Coder cannot add dependency", | |
| ): | |
| validator(dependency) | |
| def test_dependency_validator_canonicalizes_prohibited_dependency_names( | |
| dependency: str, | |
| ) -> None: | |
| validator = create_dependency_validator( | |
| role="Coder", | |
| prohibited_dependencies={"my-package"}, | |
| ) | |
| with pytest.raises(PermissionError): | |
| validator(dependency) | |
| def test_dependency_validator_rejects_invalid_requirement( | |
| dependency: str, | |
| ) -> None: | |
| validator = create_dependency_validator( | |
| role="Coder", | |
| prohibited_dependencies=set(), | |
| ) | |
| with pytest.raises( | |
| ValueError, | |
| match="Invalid dependency specification", | |
| ): | |
| validator(dependency) | |
| def test_parse_dependency_returns_requirement( | |
| specification: str, | |
| ) -> None: | |
| result = parse_dependency(specification) | |
| assert isinstance(result, Requirement) | |
| assert result.name == "requests" | |
| def test_parse_dependency_rejects_invalid_requirement( | |
| specification: str, | |
| ) -> None: | |
| with pytest.raises( | |
| ValueError, | |
| match="Invalid dependency specification", | |
| ): | |
| parse_dependency(specification) | |