Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
24 changes: 17 additions & 7 deletions python/scripts/run_tasks_in_changed_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:])
Comment on lines +32 to +41
abs_path = workspace_root / path
else:
abs_path = path

# Check which package this file belongs to
for project in projects:
Expand All @@ -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")
Expand Down
142 changes: 142 additions & 0 deletions python/scripts/tests/test_run_tasks_in_changed_packages.py
Original file line number Diff line number Diff line change
@@ -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
Loading