110 lines
6.4 KiB
Python
110 lines
6.4 KiB
Python
"""Create task data with the real 1.1.1 EXE, then open its copy in the candidate."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sqlite3
|
|
import subprocess
|
|
import time
|
|
|
|
import win32con
|
|
import win32gui
|
|
|
|
from accept_packaged_ui import wait
|
|
from packaged_window_driver import WindowDriver
|
|
|
|
|
|
def read_tasks(path):
|
|
with sqlite3.connect(path) as db:
|
|
return db.execute('select task_id,name from tasks order by task_id').fetchall()
|
|
|
|
|
|
def read_rules(path):
|
|
with sqlite3.connect(path) as db:
|
|
db.row_factory = sqlite3.Row
|
|
return [dict(row) for row in db.execute('select * from rules order by rule_id')]
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--root', type=Path, required=True)
|
|
parser.add_argument('--resume-created', action='store_true', help='Reuse already-created disposable legacy task/rule fixtures after a driver assertion failed')
|
|
args = parser.parse_args()
|
|
root = args.root.resolve(strict=True)
|
|
repository = Path(__file__).resolve().parents[1]
|
|
legacy = repository / 'dist/DxPartReplace-1.1.1/DxPartReplace/DxPartReplace.exe'
|
|
release = json.loads((repository / 'docs/releases/v1.1.1-build.json').read_text(encoding='utf-8'))
|
|
assert hashlib.sha256(legacy.read_bytes()).hexdigest() == release['artifacts']['exe_sha256']
|
|
old_data, new_data = root / 'v111-created-data', root / 'v111-upgraded-copy'
|
|
env = dict(os.environ, TEMP=str(repository / 'build/test-temp-remediation'),
|
|
TMP=str(repository / 'build/test-temp-remediation'), LOCALAPPDATA=str(root / 'profile'))
|
|
for key in ('PYTHONPATH', 'PYTHONHOME', 'DX_TASK_CENTER_HOME', 'QT_QPA_PLATFORM'):
|
|
env.pop(key, None)
|
|
if args.resume_created:
|
|
assert old_data.is_dir() and not new_data.exists()
|
|
original = read_tasks(old_data / 'task-center.db')
|
|
old_rules = read_rules(old_data / 'task-center.db')
|
|
assert len(original) == 2 and old_rules
|
|
assert all(row['old_part_number'] == 'LEGACY_OLD' and row['new_part_number'] == 'LEGACY_NEW' for row in old_rules)
|
|
else:
|
|
old_data.mkdir(exist_ok=False)
|
|
projects = root / 'upgrade-fixture-projects'
|
|
for name in ('Legacy-A', 'Legacy-B'):
|
|
path = projects / name / 'board.prj'
|
|
path.parent.mkdir(parents=True)
|
|
path.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n', encoding='utf-8')
|
|
env = dict(os.environ, TEMP=str(repository / 'build/test-temp-remediation'),
|
|
TMP=str(repository / 'build/test-temp-remediation'), LOCALAPPDATA=str(root / 'profile'))
|
|
for key in ('PYTHONPATH', 'PYTHONHOME', 'DX_TASK_CENTER_HOME', 'QT_QPA_PLATFORM'):
|
|
env.pop(key, None)
|
|
process = subprocess.Popen([str(legacy), '--data-root', str(old_data)], env=env,
|
|
creationflags=subprocess.CREATE_NO_WINDOW)
|
|
driver = WindowDriver(process.pid, root / 'upgrade-recording')
|
|
window = driver.main()
|
|
try:
|
|
driver.click(driver.find(automation_id='newTaskButton'))
|
|
dialog = wait(lambda: next((w for w in driver.windows() if w.window_text() == '从目录创建工程任务'), None), timeout=15)
|
|
driver.type_text(driver.find(automation_id='taskDirectory', window=dialog), str(projects))
|
|
driver.click(driver.find(automation_id='readTaskProjects', window=dialog))
|
|
create = driver.find('创建任务', kind='Button', window=dialog)
|
|
wait(create.is_enabled, timeout=20)
|
|
driver.click(create)
|
|
original = wait(lambda: (rows if len(rows := read_tasks(old_data / 'task-center.db')) == 2 else None), timeout=20)
|
|
add_rule = wait(lambda: (button if (button := driver.find(automation_id='addMappingButton')).is_enabled() else None), timeout=20)
|
|
driver.click(add_rule)
|
|
rule_dialog = wait(lambda: next((w for w in driver.windows() if w.window_text() == '替换规则'), None), timeout=15)
|
|
fields = sorted(rule_dialog.descendants(control_type='Edit'), key=lambda field: field.rectangle().top)
|
|
assert len(fields) >= 2
|
|
driver.type_text(fields[0], 'LEGACY_OLD')
|
|
driver.type_text(fields[1], 'LEGACY_NEW')
|
|
driver.click(driver.find('确定', kind='Button', window=rule_dialog))
|
|
old_rules = wait(lambda: (rows if len(rows := read_rules(old_data / 'task-center.db')) >= 1 else None), timeout=20)
|
|
assert all(row['old_part_number'] == 'LEGACY_OLD' and row['new_part_number'] == 'LEGACY_NEW' for row in old_rules)
|
|
finally:
|
|
win32gui.PostMessage(window.handle, win32con.WM_CLOSE, 0, 0)
|
|
process.wait(timeout=30)
|
|
before = hashlib.sha256((old_data / 'task-center.db').read_bytes()).hexdigest()
|
|
new_data.mkdir()
|
|
with sqlite3.connect((old_data / 'task-center.db').as_uri() + '?mode=ro', uri=True) as src, sqlite3.connect(new_data / 'task-center.db') as dst:
|
|
src.backup(dst)
|
|
env['QT_QPA_PLATFORM'] = 'offscreen'
|
|
candidate = root / 'extracted/DxPartReplace/DxPartReplace.exe'
|
|
result = subprocess.run([str(candidate), '--smoke-test', '--data-root', str(new_data)],
|
|
env=env, capture_output=True, timeout=90)
|
|
report = dict(legacy_version='1.1.1', task_count=len(original), candidate_exit_code=result.returncode,
|
|
task_identities_preserved=read_tasks(new_data / 'task-center.db') == original,
|
|
legacy_source_unchanged=hashlib.sha256((old_data / 'task-center.db').read_bytes()).hexdigest() == before,
|
|
fixture_is_metadata_only=True, cad_launched=False,
|
|
rules_preserved=all(all(row[key] == before_value for key, before_value in old.items())
|
|
for old, row in zip(old_rules, read_rules(new_data / 'task-center.db'), strict=True)),
|
|
unselected_cells_migrated=all(row['target_cell'] == '' for row in read_rules(new_data / 'task-center.db')),
|
|
rule_count=len(old_rules))
|
|
assert result.returncode == 0 and report['task_identities_preserved'] and report['legacy_source_unchanged'] and report['rules_preserved'] and report['unselected_cells_migrated'], report
|
|
(root / 'v111-upgrade-report.json').write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf-8')
|
|
print(json.dumps(report, ensure_ascii=False, indent=2))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|