Skip to content

Commit 4061f5e

Browse files
committed
개선: 영향 테스트 선택의 facade 팽창을 차단한다
최상위 재수출 package를 전이 경계로 두고 가장 구체적인 테스트 mirror를 선택해 Gov 단일 변경 대상을 tests/gather/gov로 축소한다.
1 parent cf5df6b commit 4061f5e

2 files changed

Lines changed: 108 additions & 12 deletions

File tree

‎tests/architecture/testDartlabGuard.py‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,66 @@ def testGuardIndexReverseClosureKeepsKnownTransitiveChain() -> None:
192192
}
193193

194194

195+
def testGuardIndexReverseClosureStopsAtUnchangedTopPackageFacade() -> None:
196+
"""Regression for #103: 하위 provider 변경이 공개 facade를 통해 전수로 팽창하지 않는다."""
197+
198+
records = [
199+
ModuleRecord("src/dartlab/gather/gov/govApi.py", "dartlab.gather.gov.govApi", "gather", 1.0, ()),
200+
ModuleRecord(
201+
"src/dartlab/gather/gov/__init__.py",
202+
"dartlab.gather.gov",
203+
"gather",
204+
1.0,
205+
(ImportRecord("dartlab.gather.gov.govApi", "gather", 1, True, "static", EAGER_PHASE),),
206+
),
207+
ModuleRecord(
208+
"src/dartlab/gather/__init__.py",
209+
"dartlab.gather",
210+
"gather",
211+
1.0,
212+
(ImportRecord("dartlab.gather.gov", "gather", 1, True, "static", EAGER_PHASE),),
213+
),
214+
ModuleRecord(
215+
"src/dartlab/analysis/unrelated.py",
216+
"dartlab.analysis.unrelated",
217+
"analysis",
218+
2.0,
219+
(ImportRecord("dartlab.gather", "gather", 1, True, "static", EAGER_PHASE),),
220+
),
221+
]
222+
223+
assert reverseImportClosure(records, {"dartlab.gather.gov.govApi"}) == {
224+
"dartlab.gather.gov.govApi",
225+
"dartlab.gather.gov",
226+
"dartlab.gather",
227+
}
228+
229+
230+
def testGuardIndexSelectionUsesDeepestMirrorBelowFacade(tmp_path: Path) -> None:
231+
"""Regression for #103: gov 변경은 tests/gather 전체가 아니라 gov mirror만 고른다."""
232+
(tmp_path / "tests/gather/gov").mkdir(parents=True)
233+
records = [
234+
ModuleRecord("src/dartlab/gather/gov/govApi.py", "dartlab.gather.gov.govApi", "gather", 1.0, ()),
235+
ModuleRecord(
236+
"src/dartlab/gather/gov/__init__.py",
237+
"dartlab.gather.gov",
238+
"gather",
239+
1.0,
240+
(ImportRecord("dartlab.gather.gov.govApi", "gather", 1, True, "static", EAGER_PHASE),),
241+
),
242+
ModuleRecord(
243+
"src/dartlab/gather/__init__.py",
244+
"dartlab.gather",
245+
"gather",
246+
1.0,
247+
(ImportRecord("dartlab.gather.gov", "gather", 1, True, "static", EAGER_PHASE),),
248+
),
249+
]
250+
251+
selected = selectImpactedTestTargets(tmp_path, ["src/dartlab/gather/gov/govApi.py"], records=records)
252+
assert selected["targets"] == ["tests/gather/gov"]
253+
254+
195255
def testGuardIndexTestSelectionUsesMirrorDirectoriesAndFailsClosed(tmp_path: Path) -> None:
196256
"""Regression for #103: 영향 package mirror를 고르고 공용 계약 변경은 전수로 올린다."""
197257
(tmp_path / "tests/analysis").mkdir(parents=True)

‎tests/audit/guard/indexer.py‎

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,12 @@ def reverseImportClosure(records: list[ModuleRecord], changedModules: set[str])
520520
stack = list(changedModules)
521521
while stack:
522522
current = stack.pop()
523+
# ``dartlab.gather`` 같은 최상위 package facade는 서로 무관한 하위 모듈을
524+
# 한데 재수출한다. 하위 변경으로 facade까지 도달한 뒤 다시 모든 facade
525+
# 소비자로 전파하면 단일 provider 변경도 전수 테스트가 된다. facade 자체가
526+
# 변경된 경우에만 그 소비자까지 전파하고, 하위에서 올라온 경우는 경계로 둔다.
527+
if current not in changedModules and _isTopPackageFacade(current):
528+
continue
523529
for record in records:
524530
if record.module in seen:
525531
continue
@@ -570,15 +576,10 @@ def selectImpactedTestTargets(
570576
if changedModules:
571577
indexRecords = records if records is not None else buildIndex(repoRoot)
572578
impactedModules = reverseImportClosure(indexRecords, set(changedModules))
573-
topPackages = {
574-
module.split(".")[1]
575-
for module in impactedModules
576-
if module.startswith("dartlab.") and len(module.split(".")) > 1
577-
}
578-
for package in sorted(topPackages):
579-
mirror = repoRoot / "tests" / package
580-
if mirror.is_dir():
581-
targets.add(mirror.relative_to(repoRoot).as_posix())
579+
for module in sorted(impactedModules):
580+
if _isTopPackageFacade(module) and module not in changedModules:
581+
continue
582+
_addMirrorTarget(repoRoot, targets, module)
582583
targets.update(_testFilesImportingModules(repoRoot, impactedModules))
583584

584585
for path in normalized:
@@ -606,9 +607,10 @@ def selectImpactedTestTargets(
606607
"impactedModules": sorted(impactedModules),
607608
"reason": "unmappedSourceChange",
608609
}
610+
minimalTargets = _minimalTargets(targets)
609611
return {
610612
"mode": "selected" if targets else "skip",
611-
"targets": sorted(targets),
613+
"targets": minimalTargets,
612614
"changedFiles": normalized,
613615
"changedModules": sorted(changedModules),
614616
"impactedModules": sorted(impactedModules),
@@ -628,10 +630,20 @@ def _sourceModuleForPath(path: str) -> str | None:
628630

629631

630632
def _modulePathsOverlap(left: str, right: str) -> bool:
631-
"""package import와 구체 module 변경을 양방향 prefix로 연결한다."""
633+
"""import 경로 ``left``가 변경 module ``right``에 의존하는지 판정한다.
634+
635+
자식 module import는 부모 package 변경의 영향을 받지만, 부모 package import가
636+
모든 자식 module에 의존하는 것은 아니다. 후자는 package ``__init__`` record의
637+
실제 import edge로만 연결해야 공개 facade가 전체 그래프를 합치지 않는다.
638+
"""
632639
if DYNAMIC_UNKNOWN in {left, right}:
633640
return False
634-
return left == right or left.startswith(right + ".") or right.startswith(left + ".")
641+
return left == right or left.startswith(right + ".")
642+
643+
644+
def _isTopPackageFacade(module: str) -> bool:
645+
parts = module.split(".")
646+
return len(parts) == 2 and parts[0] == "dartlab"
635647

636648

637649
def _testFilesImportingModules(repoRoot: Path, impactedModules: set[str]) -> set[str]:
@@ -653,3 +665,27 @@ def _testFilesImportingModules(repoRoot: Path, impactedModules: set[str]) -> set
653665
def _addExistingTarget(repoRoot: Path, targets: set[str], relPath: str) -> None:
654666
if (repoRoot / relPath).exists():
655667
targets.add(relPath)
668+
669+
670+
def _addMirrorTarget(repoRoot: Path, targets: set[str], module: str) -> None:
671+
"""source module에 대응하는 가장 구체적인 기존 테스트 디렉터리를 추가한다."""
672+
parts = module.split(".")
673+
if not parts or parts[0] != "dartlab":
674+
return
675+
relativeParts = parts[1:]
676+
while relativeParts:
677+
candidate = repoRoot.joinpath("tests", *relativeParts)
678+
if candidate.is_dir():
679+
targets.add(candidate.relative_to(repoRoot).as_posix())
680+
return
681+
relativeParts = relativeParts[:-1]
682+
683+
684+
def _minimalTargets(targets: set[str]) -> list[str]:
685+
"""부모 디렉터리가 이미 선택된 자식 파일과 디렉터리를 제거한다."""
686+
kept: list[str] = []
687+
for target in sorted(targets, key=lambda item: (item.count("/"), item)):
688+
if any(target.startswith(parent.rstrip("/") + "/") for parent in kept):
689+
continue
690+
kept.append(target)
691+
return sorted(kept)

0 commit comments

Comments
 (0)