from pathlib import Path import os import subprocess import sys import zipfile import pytest ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT / 'tools')) from build_self_extracting_release import build_release from verify_self_extracting_release import verify def test_release_refuses_existing_output_without_deleting_it(tmp_path): source = tmp_path / 'app' source.mkdir() output = tmp_path / 'valuable' output.mkdir() sentinel = output / 'keep.txt' sentinel.write_text('user files') tool = tmp_path / 'tool.exe' tool.write_bytes(b'MZ') with pytest.raises((FileExistsError, ValueError)): build_release(source, output, version='1.2.2', seven_zip=tool, sfx_stub=tool) assert sentinel.read_text() == 'user files' def test_release_rejects_output_inside_source(tmp_path): source = tmp_path / 'app' source.mkdir() tool = tmp_path / 'tool.exe' tool.write_bytes(b'MZ') with pytest.raises(ValueError, match='inside'): build_release(source, source / 'out', version='1.2.2', seven_zip=tool, sfx_stub=tool) assert not (source / 'out').exists() def test_release_refuses_user_data_in_payload(tmp_path): source = tmp_path / 'app' (source / 'data').mkdir(parents=True) (source / 'data' / 'tasks.db').write_bytes(b'private') tool = tmp_path / 'tool.exe' tool.write_bytes(b'MZ') with pytest.raises(ValueError, match='data'): build_release(source, tmp_path / 'out', version='1.2.2', seven_zip=tool, sfx_stub=tool) assert not (tmp_path / 'out').exists() @pytest.mark.skipif(os.name != 'nt' or not os.environ.get('DX_SFX_TEST_TOOLS'), reason='Windows and explicit official 7-Zip tools required') def test_actual_sfx_and_zip_extract_identical_unicode_and_empty_files(tmp_path): tools = Path(os.environ['DX_SFX_TEST_TOOLS']).resolve() source = tmp_path / 'payload' source.mkdir() (source / 'DxPartReplace.exe').write_bytes(b'sample application') (source / '中文 目录').mkdir() (source / '中文 目录' / '空.txt').write_bytes(b'') (source / '中文 目录' / 'file.bin').write_bytes(bytes(range(256)) * 2048) (source / 'licenses').mkdir() original_license=source/'licenses'/'7-Zip-LICENSE.txt' original_license.write_text('original input must remain unchanged') output = tmp_path / 'release' manifest = build_release(source, output, version='1.2.2', seven_zip=tools/'7z.exe', sfx_stub=tools/'7z.sfx', license_path=tools/'License.txt') assert original_license.read_text() == 'original input must remain unchanged' destination = tmp_path / '解压 结果' result = verify(output/'release-manifest-1.2.2.json', destination) assert result['passed'] and result['files'] == 4 with zipfile.ZipFile(output/'DxPartReplace-1.2.2.zip') as archive: assert archive.testzip() is None assert {p.relative_to(destination).as_posix() for p in destination.rglob('*') if p.is_file()} == set(archive.namelist()) assert len(archive.namelist()) == 4 == manifest['file_count'] for name in archive.namelist(): assert (destination/name).read_bytes() == archive.read(name) assert (destination/'DxPartReplace'/'中文 目录'/'file.bin').read_bytes() == bytes(range(256))*2048 with (output/'DxPartReplace-1.2.2.exe').open('ab') as stream: stream.write(b'corrupt download') with pytest.raises(ValueError, match='Artifact checksum'): verify(output/'release-manifest-1.2.2.json', tmp_path/'corrupt-extraction') assert not (tmp_path/'corrupt-extraction').exists()