import threading from pathlib import Path from pypdf import PdfWriter from mentor_pdf_export.exporter import Exporter,output_path from mentor_pdf_export.models import ExportOptions from mentor_pdf_export.scanner import read_project from mentor_pdf_export.workers import BatchWorker def test_pdf_uses_original_project_basename_without_hash(project_factory,tmp_path): project=read_project(project_factory('input','音频_Test')) assert output_path(project,tmp_path/'out').name=='音频_Test.pdf' def test_duplicate_names_export_to_natural_subfolders_without_overwrite(qapp,project_factory,tmp_path): a=read_project(project_factory('design/Work','same')) b=read_project(project_factory('design/Cert','same')) def runner(args,*rest): writer=PdfWriter();writer.add_blank_page(width=500,height=400) with Path(args[args.index('-a')+1]).open('wb') as f:writer.write(f) return 0 worker=BatchWorker([(0,a),(1,b)],tmp_path/'out',ExportOptions(overwrite=True),Exporter(Path('fake.exe'),tmp_path/'runs',runner=runner)) worker.run() assert [r.status for r in worker.results]==['success','success'] assert [Path(r.output).relative_to(tmp_path/'out').as_posix() for r in worker.results]==['Work/same.pdf','Cert/same.pdf'] def test_unique_names_keep_flat_output(qapp,project_factory,tmp_path): from mentor_pdf_export.naming import plan_output_directories a=read_project(project_factory('a','One'));b=read_project(project_factory('b','Two')) assert plan_output_directories([a,b],tmp_path/'out',tmp_path/'runs')==[tmp_path/'out',tmp_path/'out'] def test_derived_subfolder_cannot_enter_any_source_before_first_native_call(qapp,project_factory,tmp_path): a=read_project(project_factory('design/root/Work','same')) b=read_project(project_factory('design/root/Cert','same')) # root is an allowed ancestor, but derived Work/Cert outputs would hit sources. calls=[] worker=BatchWorker([(0,a),(1,b)],tmp_path/'design/root',ExportOptions(),Exporter(Path('fake.exe'),tmp_path/'runs',runner=lambda *args:calls.append(args))) worker.run() assert not calls and not worker.results def test_retry_subset_preserves_collision_folders(qapp,project_factory,tmp_path): a=read_project(project_factory('design/Work','same'));b=read_project(project_factory('design/Cert','same')) def runner(args,*rest): writer=PdfWriter();writer.add_blank_page(width=500,height=400) with Path(args[args.index('-a')+1]).open('wb') as f:writer.write(f) return 0 worker=BatchWorker([(0,a)],tmp_path/'out',ExportOptions(),Exporter(Path('fake.exe'),tmp_path/'runs',runner=runner),all_projects=[a,b]) worker.run() assert Path(worker.results[0].output).relative_to(tmp_path/'out').as_posix()=='Work/same.pdf' def test_junction_aliases_cannot_merge_same_name_outputs(qapp,project_factory,tmp_path): import os,pytest if os.name!='nt':pytest.skip('Windows junction regression') import _winapi a=read_project(project_factory('design/Work','same'));b=read_project(project_factory('design/Cert','same')) output=tmp_path/'out';output.mkdir();shared=tmp_path/'shared';shared.mkdir() _winapi.CreateJunction(str(shared),str(output/'Work')) _winapi.CreateJunction(str(shared),str(output/'Cert')) assert (output/'Work').resolve()==(output/'Cert').resolve()==shared calls=[] def runner(args,*rest): calls.append(args);writer=PdfWriter();writer.add_blank_page(width=500,height=400) with Path(args[args.index('-a')+1]).open('wb') as f:writer.write(f) return 0 worker=BatchWorker([(0,a),(1,b)],output,ExportOptions(overwrite=True),Exporter(Path('fake.exe'),tmp_path/'runs',runner=runner)) worker.run() assert not calls and not worker.results assert not (shared/'same.pdf').exists()