115 lines
4.2 KiB
Python
115 lines
4.2 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
from types import ModuleType, SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from dx_productivity.trial_task_center import native_helper
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
class OriginalComponent:
|
|
UID = "original-uid"
|
|
Refdes = "R1"
|
|
|
|
def FindAttribute(self, name):
|
|
value = {"Part Number": "NEW", "Part Name": "OLD_SYMBOL", "Part Label": "OLD_LABEL"}[name]
|
|
return SimpleNamespace(InstanceValue=value, Value=value)
|
|
|
|
|
|
@pytest.mark.parametrize("has_pins", (True, False))
|
|
def test_resume_does_not_infer_native_replacement_from_part_number(monkeypatch, tmp_path: Path, has_pins: bool) -> None:
|
|
modules = {name: ModuleType(name) for name in ("win32api", "win32con", "win32gui", "win32process", "pywinauto")}
|
|
modules["pywinauto"].Desktop = object
|
|
for name, module in modules.items():
|
|
monkeypatch.setitem(sys.modules, name, module)
|
|
component = OriginalComponent()
|
|
promotions = []
|
|
pin = SimpleNamespace(Component=component, Number="1")
|
|
pin._oleobj_ = pin
|
|
view = SimpleNamespace(
|
|
GetName=lambda flag: "A",
|
|
Block=SimpleNamespace(SheetNum=1, PromoteSymbolNumbers=lambda *args: promotions.append(args)),
|
|
Query=lambda *args: [pin] if has_pins else [],
|
|
)
|
|
app = SimpleNamespace(
|
|
ActiveView=view,
|
|
GetProjectData=lambda: SimpleNamespace(GetProjectFilePath=lambda: r"C:\DisposableA\a.prj"),
|
|
)
|
|
snapshot = tmp_path / "snapshot.tsv"
|
|
snapshot.write_text(
|
|
"M\tC:\\DisposableA\\a.prj\tOLD\tNEW\tA:1\n"
|
|
"C\toriginal-uid\tR1\t0\t0\t0\t1\tnet\n",
|
|
encoding="utf-8",
|
|
)
|
|
monkeypatch.setattr(native_helper, "attach", lambda: (app, SimpleNamespace(IVdCmpPin=lambda value: value)))
|
|
monkeypatch.setattr(native_helper, "components_on_sheet", lambda *args: [component])
|
|
monkeypatch.setattr(native_helper, "selection", lambda *args: None)
|
|
exit_code = native_helper.run(snapshot)
|
|
report = json.loads(Path(str(snapshot) + ".native.json").read_text(encoding="utf-8"))
|
|
if has_pins:
|
|
assert exit_code != 0, f"old library identity accepted: {report}"
|
|
else:
|
|
assert promotions == [(1, 1)]
|
|
assert exit_code != 0
|
|
assert report["stage"] != "preflight", f"write API called but mutation state lost: {report}"
|
|
|
|
|
|
def test_native_rollback_rejects_wrong_project_before_undo(tmp_path: Path) -> None:
|
|
source = (ROOT / "integration" / "dxdesigner795" / "QuickPartReplace.vbs").read_text(encoding="ascii")
|
|
declarations = source[:source.index("Set QP_Fso =")]
|
|
routines = source[source.index("Sub QPMain()") :]
|
|
snapshot = tmp_path / "snapshot.tsv"
|
|
snapshot.write_text(
|
|
"M\tC:\\DisposableA\\a.prj\tOLD\tNEW\tA:1\n"
|
|
"C\told-uid\tR1\t0\t0\t0\t1\tnet\n"
|
|
"A\told-uid\tPart Number\tOLD\t\t0\t0\t0\t0\t10\t0\t1\n",
|
|
encoding="utf-8",
|
|
)
|
|
body = r'''
|
|
Set QP_Fso = CreateObject("Scripting.FileSystemObject")
|
|
Set QP_App = New WrongProjectApp
|
|
WScript.Echo "ACTIVE_PROJECT=" & QPProjectPath()
|
|
On Error Resume Next
|
|
QPRollbackNativeReplacement "SNAPSHOT"
|
|
WScript.Echo "ERROR_AFTER_UNDO=" & Err.Description
|
|
Err.Clear
|
|
On Error GoTo 0
|
|
WScript.Quit 0
|
|
Class WrongProjectApp
|
|
Public Function ExecuteCommand(ByVal Command)
|
|
WScript.Echo "WRONG_CONTEXT_COMMAND=" & Command
|
|
ExecuteCommand = True
|
|
End Function
|
|
Public Function GetProjectData()
|
|
Set GetProjectData = Me
|
|
End Function
|
|
Public Function GetProjectFilePath()
|
|
GetProjectFilePath = "C:\UnrelatedB\b.prj"
|
|
End Function
|
|
Public Property Get ActiveView
|
|
Set ActiveView = Me
|
|
End Property
|
|
Public Function Query(ByVal Flags, ByVal Selected)
|
|
Set Query = CreateObject("Scripting.Dictionary")
|
|
End Function
|
|
End Class
|
|
'''.replace("SNAPSHOT", str(snapshot))
|
|
wrapper = tmp_path / "isolated-rollback.vbs"
|
|
wrapper.write_text(declarations + body + routines, encoding="ascii")
|
|
completed = subprocess.run(
|
|
["cscript.exe", "//nologo", str(wrapper)],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=15,
|
|
creationflags=subprocess.CREATE_NO_WINDOW,
|
|
)
|
|
assert completed.returncode == 0, completed.stderr
|
|
assert "WRONG_CONTEXT_COMMAND=undo" not in completed.stdout, completed.stdout
|