95 lines
5.3 KiB
Python
95 lines
5.3 KiB
Python
"""Exercise a relocated release with no system Python environment or PATH."""
|
|
from pathlib import Path
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
ACCEPTANCE = ROOT / ("build/release-acceptance-" + time.strftime("%Y%m%d-%H%M%S"))
|
|
|
|
|
|
def digest(path):
|
|
return hashlib.file_digest(path.open("rb"), "sha256").hexdigest()
|
|
|
|
|
|
def main(package):
|
|
package = Path(package).resolve()
|
|
destination = ACCEPTANCE / "中文 空格 便携工具" / package.name
|
|
if destination.exists():
|
|
raise ValueError("Acceptance destination already exists; preserve previous evidence")
|
|
shutil.copytree(package, destination)
|
|
inputs = ACCEPTANCE / "input"
|
|
fixture_list = json.loads((ROOT / "build/acceptance/inputs.json").read_text(encoding="utf-8"))
|
|
first = inputs / "普通工程"
|
|
second = inputs
|
|
for index in range(6):
|
|
second /= f"长路径目录{index}_" + "中文路径" * 8
|
|
for fixture, target in zip(fixture_list, (first, second)):
|
|
shutil.copytree(Path(fixture["path"]).parent, target)
|
|
prjs = list(inputs.rglob("*.prj"))
|
|
assert len(prjs) == 2 and max(len(str(path)) for path in prjs) > 260
|
|
before = {str(path): digest(path) for path in inputs.rglob("*") if path.is_file()}
|
|
environment = dict(os.environ, PYTHONHOME=r"Z:\invalid-python", PYTHONPATH=r"Z:\invalid-packages",
|
|
PYTHONIOENCODING="utf-8", PATH=os.environ.get("SystemRoot", r"C:\Windows") + r"\System32")
|
|
evidence = {"package": str(package), "relocated": str(destination), "source_path_lengths": [len(str(p)) for p in prjs],
|
|
"environment": {key: environment[key] for key in ("PYTHONHOME", "PYTHONPATH", "PATH")}, "runs": []}
|
|
cmd = str(Path(os.environ.get("SystemRoot", r"C:\Windows")) / "System32/cmd.exe")
|
|
|
|
def run(key, arguments, stdin=None):
|
|
started = time.monotonic()
|
|
command = '""' + str(destination / "导出.cmd") + '" ' + subprocess.list2cmdline(arguments) + '"'
|
|
# cmd parses quotes itself; list2cmdline escaping the /c body breaks its outer quotes.
|
|
process = subprocess.run(subprocess.list2cmdline([cmd]) + " /d /s /c " + command, input=stdin, env=environment,
|
|
cwd=Path(os.environ.get("SystemRoot", r"C:\Windows")) / "Temp",
|
|
capture_output=True, encoding="utf-8", errors="replace", timeout=1200,
|
|
creationflags=subprocess.CREATE_NO_WINDOW)
|
|
(ACCEPTANCE / (key + ".stdout.txt")).write_text(process.stdout, encoding="utf-8")
|
|
(ACCEPTANCE / (key + ".stderr.txt")).write_text(process.stderr, encoding="utf-8")
|
|
evidence["runs"].append({"key": key, "arguments": arguments, "exit_code": process.returncode,
|
|
"seconds": round(time.monotonic() - started, 3)})
|
|
(ACCEPTANCE / "acceptance.json").write_text(json.dumps(evidence, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
print(key, process.returncode, flush=True)
|
|
if process.returncode:
|
|
print(process.stdout, process.stderr, flush=True)
|
|
raise RuntimeError("Acceptance command failed: " + key)
|
|
return process.stdout
|
|
|
|
diagnostic = json.loads(run("check", ["--check", "--json"]))
|
|
assert Path(diagnostic["python"]) == destination / "runtime/python.exe"
|
|
# Drive the actual no-argument wizard: input, output, confirmation, exit menu.
|
|
output = ACCEPTANCE / "导出结果"
|
|
run("wizard-export", [], f"{inputs}\n{output}\nY\n\n")
|
|
batches = sorted(output.glob("批次_*/batch.json"))
|
|
assert len(batches) == 1
|
|
record = json.loads(batches[0].read_text(encoding="utf-8"))
|
|
assert record["exit_code"] == 0 and len(record["projects"]) == 2
|
|
for row in record["projects"]:
|
|
result = row["result"]
|
|
assert row["status"] == "success" and len(result["items"]) == 11
|
|
assert all(item["status"] == "success" for item in result["items"])
|
|
assert result["source_unchanged"] is True
|
|
assert result["snapshot_discovery"]["status"] == "success"
|
|
for item in result["items"]:
|
|
assert item["provenance"]["snapshot"] == row["project"]["snapshot"]
|
|
assert all(Path(path).is_file() for path in item["outputs"])
|
|
run("retry", ["--retry", str(batches[0].parent), "--yes"])
|
|
retry_path = sorted(output.glob("批次_*/batch.json"))[-1]
|
|
retry = json.loads(retry_path.read_text(encoding="utf-8"))
|
|
assert all(item["provenance"]["reused"] for row in retry["projects"] for item in row["result"]["items"])
|
|
assert before == {str(path): digest(path) for path in inputs.rglob("*") if path.is_file()}
|
|
manifest = json.loads((destination / "package-manifest.json").read_text(encoding="utf-8"))
|
|
assert all(digest(destination / name) == info["sha256"] for name, info in manifest["files"].items())
|
|
evidence.update(status="passed", first_batch=str(batches[0].parent), retry_batch=str(retry_path.parent),
|
|
verified_package_files=len(manifest["files"]), source_files_unchanged=len(before),
|
|
projects=2, successful_export_items=22, reused_items=22)
|
|
(ACCEPTANCE / "acceptance.json").write_text(json.dumps(evidence, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
print(json.dumps(evidence, ensure_ascii=False, indent=2), flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv[1])
|