Skip to content
Merged
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
46 changes: 39 additions & 7 deletions tests/startup_run_ps1_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
RUN_PS1 = Path(__file__).resolve().parent.parent / "tools" / "startup" / "run.ps1"
DLLS = ["xmem", "d3d12_xs", "AcpHal", "XCRDAPI", "chad", "xapu", "xg_xs", "GameInput"]
STUB_LINES = [f"stub debugger line {n}" for n in range(1, 8)]
STUB_ERROR = "synthetic debugger error"
HEAD_LINES = 5 # run.ps1 prints this many lines of "<LogDir>.out"
# The stub stands in for tools/startup/debug_launch.py: it records its arguments,
# prints seven lines and exits with the code under test.
Expand Down Expand Up @@ -50,6 +51,8 @@
time.sleep(0.05)
for n in range(1, 8):
print("stub debugger line", n)
if {emit_stderr}:
print("synthetic debugger error", file=sys.stderr)
sys.exit({code})
"""
# Stands in for tools/startup/gfx2hlsl.py under -Translate: starts a child process, as the parallel
Expand Down Expand Up @@ -79,7 +82,7 @@
WATCH_CLEANUP = re.compile(
r"if\s*\(\s*\$translator\s+-and\s+-not\s+\$translator\.HasExited\s*\)\s*\{\s*&\s*\{\s*"
r"""\$ErrorActionPreference\s*=\s*['"]Continue['"]\s+&\s+taskkill\.exe\b[^|#\n]*\s/T\b""", re.I)
CAPTURE = re.compile(r"\$(\w+)\s*=\s*\$LASTEXITCODE", re.I)
CAPTURE = re.compile(r"\$(?:script:)?(\w+)\s*=\s*\$LASTEXITCODE", re.I)
EXIT = re.compile(r"exit\s+\$(\w+)", re.I)
HEAD = 'Get-Content "$LogDir.out" -TotalCount 5'
# Without -FakeUser, user APIs hit the host runtime; run.ps1 must warn (docs/startup-runtime.md).
Expand Down Expand Up @@ -146,8 +149,8 @@ def alive(pid):


def run_case(shell, script, code, work, translate=False, switches=(), passed=(), fake_user_env=False,
early_exit=False, trace_api_env=None, absolute_build=False, missing_build=False,
native=False, manifest="absent", cache_files=None):
early_exit=False, trace_api_env=None, absolute_build=False, missing_build=False, emit_stderr=False,
missing_python=False, real_log_error=False, native=False, manifest="absent", cache_files=None):
"""Run one copy of the script on a stub that exits with `code`; return what is wrong.

`switches` are extra run.ps1 switches and `passed` the debugger arguments they must add.
Expand All @@ -160,7 +163,11 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(),
startup = root / "tools" / "startup"
startup.mkdir(parents=True)
(startup / "run.ps1").write_bytes(script.encode("utf-8"))
(startup / "debug_launch.py").write_text(STUB.format(code=code, early_exit=early_exit), encoding="utf-8")
if real_log_error:
for name in ("debug_launch.py", "launch.py", "host_cpu.py", "hook_window.py", "window_capture.py"):
shutil.copyfile(RUN_PS1.parent / name, startup / name)
else:
(startup / "debug_launch.py").write_text(STUB.format(code=code, early_exit=early_exit, emit_stderr=emit_stderr), encoding="utf-8")
(startup / "gfx2hlsl.py").write_text(WATCH_STUB.format(early_exit=early_exit), encoding="utf-8")
shutil.copyfile(RUN_PS1.with_name("shader_cache_revision.py"), startup / "shader_cache_revision.py")
if cache_files is not None: # a shader cache left by an earlier run
Expand All @@ -175,6 +182,8 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(),
exe.parent.mkdir()
exe.write_bytes(b"")
data.mkdir()
if real_log_error:
(log / "events.txt").mkdir(parents=True)
if manifest in ("normal", "bracket"):
(data / "MicrosoftGame.config").write_text("<TitleId>1234</TitleId><SizeMB>42</SizeMB>", encoding="utf-8")
if manifest.startswith("bracket"):
Expand All @@ -197,6 +206,8 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(),
# In this tree run.ps1 finds `python` through PATH. This interpreter's directory
# goes first so the stub runs under the Python that runs this check.
env["PATH"] = os.path.dirname(sys.executable) + os.pathsep + env.get("PATH", "")
if missing_python:
env["PATH"] = str(Path(os.environ["SystemRoot"]) / "System32")
build_arg = str(build) if absolute_build else "bld"
if missing_build:
build_arg = str(root / "missing build")
Expand All @@ -223,13 +234,23 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(),
stderr.seek(0)
printed = [line.strip() for line in stdout if line.strip()]
error_text = stderr.read().strip()
if missing_python:
return (["missing Python returned success"] if out.returncode == 0 else []) + (
["missing Python launched debugger"] if (startup / "debugger.json").exists() else [])
if real_log_error:
summary = Path(str(log) + ".out")
text = decode(summary.read_bytes()) if summary.is_file() else ""
return ([f"real debugger log error returned {out.returncode}, expected 1"] if out.returncode != 1 else []) + (
["real debugger log error was not captured in the summary"] if "PermissionError" not in text else [])
found = []
for dll in DLLS:
if not (exe.parent / f"{dll}.dll").is_file():
found.append(f"stand-in {dll}.dll was not copied")
if out.returncode != code:
found.append(f"stub debugger exited {code}, run.ps1 exited {out.returncode}")
want_output = STUB_LINES[:HEAD_LINES]
summary = Path(str(log) + ".out")
summary_lines = [line.strip() for line in decode(summary.read_bytes()).splitlines() if line.strip()] if summary.is_file() else []
want_output = summary_lines[:HEAD_LINES] if emit_stderr else STUB_LINES[:HEAD_LINES]
if cache_files is not None:
# One warning with both revisions and the fixes; PowerShell may wrap it, so compare without whitespace.
flat = re.sub(r"\s", "", " ".join(printed))
Expand All @@ -243,10 +264,12 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(),
if printed != want_output:
found.append(f"stub exit {code}: printed {printed}, expected {want_output} "
f"(stderr: {error_text!r})")
summary = Path(str(log) + ".out")
if not summary.is_file():
found.append(f"stub exit {code}: {summary.name} was not written")
elif [line.strip() for line in decode(summary.read_bytes()).splitlines() if line.strip()] != STUB_LINES:
elif emit_stderr and ([line for line in summary_lines if line in STUB_LINES] != STUB_LINES or
not any(STUB_ERROR in line for line in summary_lines)):
found.append(f"stub exit {code}: {summary.name} does not hold stdout and stderr")
elif not emit_stderr and summary_lines != STUB_LINES:
found.append(f"stub exit {code}: {summary.name} does not hold the debugger's output")
want = [str(exe), str(data), str(log), "--timeout", "9", "--memory-gib", "3",
"--trace-files", "1", "--continue-asserts", "4", *passed]
Expand Down Expand Up @@ -284,6 +307,13 @@ def run_problems(shell, script, work, first_only):
found += run_case(shell, script, 7, work, absolute_build=True)
for manifest in ("normal", "bracket", "bracket_missing"):
found += run_case(shell, script, 0, work, manifest=manifest)
if found and first_only:
return found
for code in (0, 7):
found += run_case(shell, script, code, work, emit_stderr=True)
if found and first_only:
return found
found += run_case(shell, script, 7, work, translate=True, emit_stderr=True)
if found and first_only:
return found
found += run_case(shell, script, 0, work, switches=["-FakeUser"])
Expand Down Expand Up @@ -381,6 +411,8 @@ def shell_failures(shell, script, tmp):
if mutant is None or not run_problems(shell, mutant, tmp, True):
failures.append(f"{name}: mutant '{label}' was not detected")
if shell:
failures += [f"{name}: {item}" for item in run_case(shell, script, 7, tmp, real_log_error=True)]
failures += [f"{name}: {item}" for item in run_case(shell, script, 7, tmp, missing_python=True)]
missing = run_case(shell, script, 7, tmp, missing_build=True)
if not any("debugger [argv" in item and "was None" in item for item in missing):
failures.append(f"{name}: missing build directory did not prevent debugger startup")
Expand Down
11 changes: 8 additions & 3 deletions tools/startup/run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,14 @@ if ($TraceCalls) { $traceArgs = @("--trace-calls", "1") }
$captureArgs = @()
if ($CaptureEvery -ne 0) { $captureArgs = @("--capture-every", "$CaptureEvery") }
try {
& $python -B (Join-Path $PSScriptRoot "debug_launch.py") $Exe $DataDir $LogDir --timeout $Seconds --memory-gib $MemoryGiB `
--trace-files 1 --continue-asserts $ContinueAsserts @traceArgs @captureArgs *> "$LogDir.out"
$debuggerExit = $LASTEXITCODE
# Windows PowerShell 5.1 wraps redirected native stderr as an error record. Keep that
# from aborting the launcher before it can report the debugger's exit code.
& {
$ErrorActionPreference = "Continue"
& $python -B (Join-Path $PSScriptRoot "debug_launch.py") $Exe $DataDir $LogDir --timeout $Seconds --memory-gib $MemoryGiB `
--trace-files 1 --continue-asserts $ContinueAsserts @traceArgs @captureArgs
$script:debuggerExit = $LASTEXITCODE
} *> "$LogDir.out"
} finally {
# taskkill /T needs a live parent PID to reach the watcher's workers.
if ($translator -and -not $translator.HasExited) {
Expand Down
Loading