Files
cbb-refresh-verify-795/tests/test_save_helper_runtime.py
T

226 lines
8.7 KiB
Python

import os
from pathlib import Path
import subprocess
import pytest
from cbb_refresh_verify.runtime_timing import parse_save_progress
@pytest.mark.parametrize('save_ok', [True, False])
@pytest.mark.parametrize('other_document', [True, False])
def test_packaged_vbs_measures_real_save_call_and_preserves_failure_gate(tmp_path, save_ok, other_document):
# Execute the real helper in its target 32-bit host. Only external COM
# object creation is replaced; Save/Validate/Close order and timers are real.
script = Path(__file__).parents[1] / 'src/cbb_refresh_verify/save_verified_block.vbs'
source = script.read_text(encoding='utf-8')
classes = r'''
Class FakePi
Public ProjectFile, ForwardAnnotationStatus, SynchCESStatus, LoadCESStatus, BackAnnotationStatus
Private Sub Class_Initialize
ProjectFile = WScript.Arguments(0)
ForwardAnnotationStatus = 2
SynchCESStatus = 2
LoadCESStatus = 2
BackAnnotationStatus = 2
End Sub
End Class
Class FakeDoc
Public Property Get ProjectIntegration
Set ProjectIntegration = New FakePi
End Property
Public Function Validate(token)
Validate = 17
End Function
Public Function Save()
WScript.Sleep 120
Save = SAVE_RETURN
End Function
Public Function IsSaved()
IsSaved = True
End Function
Public Function Close(saveChanges)
closedDoc = True
Close = True
End Function
End Class
Class FakeApp
Public Property Get ActiveDocument
If closedDoc And Not OTHER_DOCUMENT Then
Set ActiveDocument = Nothing
Else
Set ActiveDocument = New FakeDoc
End If
End Property
Public Sub Quit()
WScript.Echo "TEST_QUIT"
End Sub
End Class
Class FakeLic
Public Function GetToken(value)
GetToken = value
End Function
End Class
'''
source = source.replace('Option Explicit', 'Option Explicit\nDim closedDoc\n'
+ classes.replace('SAVE_RETURN', str(save_ok)).replace('OTHER_DOCUMENT', str(other_document)))
source = source.replace('GetObject(, "MGCPCB.Application")', 'New FakeApp')
source = source.replace('CreateObject("MGCPCBAutomationLicensing.Application")', 'New FakeLic')
harness = tmp_path/'save_harness.vbs'
harness.write_text(source, encoding='ascii')
cscript = Path(os.environ['WINDIR'])/'SysWOW64/cscript.exe'
completed = subprocess.run([str(cscript), '//nologo', str(harness), 'expected.prj'],
capture_output=True, text=True, timeout=10)
parsed = parse_save_progress(completed.stdout)
assert parsed['timings_seconds']['save'] >= .08
if save_ok:
assert completed.returncode == (10 if other_document else 0), completed.stderr
assert 'SAVE_OK' in parsed['markers']
assert 'CLOSE_OK' in parsed['markers']
assert parsed['timings_seconds']['post_save_check'] >= 0
assert parsed['timings_seconds']['close'] >= 0
assert ('TEST_QUIT' in completed.stdout) is (not other_document)
assert ('APP_RETAINED_OTHER_DOCUMENT' in completed.stdout) is other_document
else:
assert completed.returncode == 4, completed.stderr
assert 'SAVE_OK' not in parsed['markers']
assert 'CLOSE_OK' not in parsed['markers']
def run_helper(tmp_path, *, save_body='Save = True', close_body='Close = True',
is_saved='True', gated=False, cancelled=False, transform=None):
source = Path('src/cbb_refresh_verify/save_verified_block.vbs').read_text('utf-8')
classes = '''
Class FakePi
Public ProjectFile, ForwardAnnotationStatus, SynchCESStatus, LoadCESStatus, BackAnnotationStatus
Private Sub Class_Initialize
ProjectFile = WScript.Arguments(0)
ForwardAnnotationStatus = 2
SynchCESStatus = 2
LoadCESStatus = 2
BackAnnotationStatus = 2
End Sub
End Class
Class FakeDoc
Public Name
Private Sub Class_Initialize
Name = "CBB.pcb"
End Sub
Public Property Get ProjectIntegration
Set ProjectIntegration = New FakePi
End Property
Public Function Validate(token)
Validate = 17
End Function
Public Function Save()
WScript.Echo "TEST_SAVE_CALLED"
SAVE_BODY
End Function
Public Function IsSaved()
IsSaved = IS_SAVED_VALUE
End Function
Public Function Close(saveChanges)
CLOSE_BODY
closedDoc = True
End Function
End Class
Class FakeApp
Public Property Get ActiveDocument
If closedDoc Then
Set ActiveDocument = Nothing
Else
Set ActiveDocument = New FakeDoc
End If
End Property
Public Sub Quit()
WScript.Echo "TEST_QUIT"
End Sub
End Class
Class FakeLic
Public Function GetToken(value)
GetToken = value
End Function
End Class
'''.replace('SAVE_BODY', save_body).replace('CLOSE_BODY', close_body).replace('IS_SAVED_VALUE', is_saved)
source = source.replace('Option Explicit', 'Option Explicit\nDim closedDoc\n' + classes)
source = source.replace('GetObject(, "MGCPCB.Application")', 'New FakeApp')
source = source.replace('CreateObject("MGCPCBAutomationLicensing.Application")', 'New FakeLic')
if transform is not None:
source = transform(source)
harness = tmp_path/'facts.vbs'
harness.write_text(source, encoding='ascii')
cancel = tmp_path/'cancel.signal'
if cancelled:
cancel.touch()
args = [str(Path(os.environ['WINDIR'])/'SysWOW64/cscript.exe'), '//nologo',
str(harness), 'expected.prj', 'CBB.pcb', str(cancel)]
if gated:
args.append(str(tmp_path/'start.signal'))
return subprocess.run(args, capture_output=True, text=True, timeout=35)
@pytest.mark.parametrize('body,marker', [('Close = False', 'CLOSE_FAILED'),
('Err.Raise 5, "FakeDoc", "close failed"', 'CLOSE_FAILED')])
def test_close_failure_keeps_successful_save_fact(tmp_path, body, marker):
result = run_helper(tmp_path, close_body=body)
assert 'SAVE_OK' in result.stdout
assert marker in result.stdout
assert 'CLOSE_OK' not in result.stdout
assert result.returncode != 0
def test_save_exception_has_unknown_result_marker(tmp_path):
result = run_helper(tmp_path, save_body='Err.Raise 5, "FakeDoc", "save failed"')
assert 'SAVE_RESULT_UNKNOWN|5|' in result.stdout
assert 'CLOSE_START' not in result.stdout
assert result.returncode != 0
def test_saved_document_postcheck_failure_does_not_discard_save_fact(tmp_path):
result = run_helper(tmp_path, is_saved='False')
assert 'SAVE_OK' in result.stdout
assert 'POST_SAVE_FAILED' in result.stdout
assert 'CLOSE_START' not in result.stdout
def test_cancellation_in_save_allows_identity_checked_saved_close(tmp_path):
result = run_helper(tmp_path, save_body='''
Dim fso, flag
Set fso = CreateObject("Scripting.FileSystemObject")
Set flag = fso.CreateTextFile(WScript.Arguments(2), True)
flag.Close
Save = True
''')
assert 'SAVE_OK' in result.stdout
assert 'CLOSE_OK' in result.stdout
assert 'QUIT_OK' in result.stdout
assert result.returncode == 0, result.stderr
def test_unopened_gate_cancellation_exits_before_com(tmp_path):
result = run_helper(tmp_path, gated=True, cancelled=True)
assert 'CANCELLED_BEFORE_SAVE' in result.stdout
assert 'TEST_SAVE_CALLED' not in result.stdout
assert 'VALIDATE_START' not in result.stdout
assert result.returncode == 9
def test_post_save_identity_failure_names_actual_and_expected_values(tmp_path):
result = run_helper(tmp_path, transform=lambda source:source.replace(
'ProjectFile = WScript.Arguments(0)',
'If afterSave Then\nProjectFile = "other.prj"\nElse\nProjectFile = WScript.Arguments(0)\nEnd If')
.replace('Dim closedDoc', 'Dim closedDoc, afterSave')
.replace('Save = True', 'afterSave = True\nSave = True'))
assert result.returncode == 8, result.stdout + result.stderr
assert 'POST_SAVE_IDENTITY|project=other.prj|expected=expected.prj|document=CBB.pcb|expected_document=CBB.pcb' in result.stdout
assert 'CLOSE_START' not in result.stdout
def test_post_save_transient_unsaved_state_retries_before_closing(tmp_path):
def transform(source):
return source.replace('Dim closedDoc', 'Dim closedDoc, savedCalls').replace(
'IsSaved = True', 'savedCalls = savedCalls + 1\nIsSaved = (savedCalls <> 2)')
result = run_helper(tmp_path, transform=transform)
assert result.returncode == 0, result.stdout + result.stderr
assert 'CLOSE_OK' in result.stdout