diff --git a/python/scripts/run_tasks_in_changed_packages.py b/python/scripts/run_tasks_in_changed_packages.py index 46e742c3a0c..245cc00d81d 100644 --- a/python/scripts/run_tasks_in_changed_packages.py +++ b/python/scripts/run_tasks_in_changed_packages.py @@ -6,6 +6,7 @@ from pathlib import Path from rich import print + from scripts.task_runner import build_work_items, discover_projects, run_tasks # Tasks that need to run in all packages when core changes (type info propagates) @@ -24,15 +25,23 @@ def get_changed_packages( core_package_changed = False for file_path in changed_files: - # Strip 'python/' prefix if present (when git diff is run from repo root) - file_path_str = str(file_path) - if file_path_str.startswith("python/"): - file_path_str = file_path_str[7:] # Remove 'python/' prefix + path = Path(file_path) # Convert to absolute path if relative - abs_path = Path(file_path_str) - if not abs_path.is_absolute(): - abs_path = workspace_root / file_path_str + if not path.is_absolute(): + # Strip 'python' prefix component if present (when git diff is run from repo root) + parts = path.parts + if parts and parts[0] == "python": + path = Path(*parts[1:]) + elif str(file_path).startswith(("python/", "python\\")): + # Fallback for non-native path separators on POSIX platforms + normalized = str(file_path).replace("\\", "/") + parts = Path(normalized).parts + if parts and parts[0] == "python": + path = Path(*parts[1:]) + abs_path = workspace_root / path + else: + abs_path = path # Check which package this file belongs to for project in projects: @@ -51,6 +60,7 @@ def get_changed_packages( def main() -> None: + """Run task runner CLI for changed packages.""" parser = argparse.ArgumentParser(description="Run task(s) in changed packages, in parallel by default.") parser.add_argument("tasks", nargs="+", help="Task name(s) to run") parser.add_argument("--files", nargs="*", default=None, help="Changed files to determine which packages to run") diff --git a/python/scripts/tests/test_run_tasks_in_changed_packages.py b/python/scripts/tests/test_run_tasks_in_changed_packages.py new file mode 100644 index 00000000000..d1df4662327 --- /dev/null +++ b/python/scripts/tests/test_run_tasks_in_changed_packages.py @@ -0,0 +1,142 @@ +# Copyright (c) Microsoft. All rights reserved. + +"""Unit tests for run_tasks_in_changed_packages.""" + +from pathlib import Path + +from scripts.run_tasks_in_changed_packages import get_changed_packages + + +def test_get_changed_packages_recognizes_repo_relative_paths() -> None: + workspace_root = Path("/repo/python") + projects = [Path("packages/core"), Path("packages/redis")] + + # POSIX spelling with repo prefix + changed, core_changed = get_changed_packages( + projects, + ["python/packages/core/agent_framework/_types.py"], + workspace_root, + ) + assert changed == {Path("packages/core")} + assert core_changed is True + + # Windows native backslash spelling with repo prefix + changed, core_changed = get_changed_packages( + projects, + [r"python\packages\core\agent_framework\_types.py"], + workspace_root, + ) + assert changed == {Path("packages/core")} + assert core_changed is True + + +def test_get_changed_packages_recognizes_workspace_relative_paths() -> None: + workspace_root = Path("/repo/python") + projects = [Path("packages/core"), Path("packages/redis")] + + # Forward slash workspace relative + changed, core_changed = get_changed_packages( + projects, + ["packages/core/agent_framework/_types.py"], + workspace_root, + ) + assert changed == {Path("packages/core")} + assert core_changed is True + + # Windows native backslash workspace relative + changed, core_changed = get_changed_packages( + projects, + [r"packages\core\agent_framework\_types.py"], + workspace_root, + ) + assert changed == {Path("packages/core")} + assert core_changed is True + + +def test_get_changed_packages_recognizes_dot_prefixed_paths() -> None: + workspace_root = Path("/repo/python") + projects = [Path("packages/core")] + + changed, core_changed = get_changed_packages( + projects, + ["./python/packages/core/agent_framework/_types.py"], + workspace_root, + ) + assert changed == {Path("packages/core")} + assert core_changed is True + + changed, core_changed = get_changed_packages( + projects, + [r".\python\packages\core\agent_framework\_types.py"], + workspace_root, + ) + assert changed == {Path("packages/core")} + assert core_changed is True + + +def test_get_changed_packages_non_core_package_flag() -> None: + workspace_root = Path("/repo/python") + projects = [Path("packages/core"), Path("packages/redis")] + + changed, core_changed = get_changed_packages( + projects, + [r"python\packages\redis\agent_framework_redis\_store.py"], + workspace_root, + ) + assert changed == {Path("packages/redis")} + assert core_changed is False + + +def test_get_changed_packages_absolute_paths(tmp_path: Path) -> None: + workspace_root = tmp_path / "python" + project_core = Path("packages/core") + projects = [project_core] + + file_abs = workspace_root / project_core / "agent_framework" / "_types.py" + changed, core_changed = get_changed_packages( + projects, + [str(file_abs)], + workspace_root, + ) + assert changed == {project_core} + assert core_changed is True + + +def test_get_changed_packages_ignores_unrelated_files() -> None: + workspace_root = Path("/repo/python") + projects = [Path("packages/core"), Path("packages/redis")] + + # Files outside workspace packages + unrelated = [ + "README.md", + "python/README.md", + r"python\pyproject.toml", + ".github/workflows/ci.yml", + r"dotnet\packages\Microsoft.Agents.Core\Agent.cs", + ] + changed, core_changed = get_changed_packages( + projects, + unrelated, + workspace_root, + ) + assert changed == set() + assert core_changed is False + + +def test_get_changed_packages_multiple_files_and_deduplication() -> None: + workspace_root = Path("/repo/python") + projects = [Path("packages/core"), Path("packages/redis")] + + files = [ + "python/packages/core/agent_framework/_types.py", + r"python\packages\core\agent_framework\__init__.py", + r"python\packages\redis\agent_framework_redis\_store.py", + "README.md", + ] + changed, core_changed = get_changed_packages( + projects, + files, + workspace_root, + ) + assert changed == {Path("packages/core"), Path("packages/redis")} + assert core_changed is True