smoke_test.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """Verify the packaged runtime, including its bundled FFmpeg, from any cwd."""
  2. import json
  3. from pathlib import Path
  4. import subprocess
  5. import tempfile
  6. import traceback
  7. from PIL import Image
  8. import imageio_ffmpeg
  9. from core import FrameConfig, Options, export_frames, inspect_media
  10. def run_self_test(report_path, app_class):
  11. checks = []
  12. app = None
  13. try:
  14. with tempfile.TemporaryDirectory(prefix='animation_tool_') as temp:
  15. root = Path(temp)
  16. images = [Image.new('RGBA', (160, 80), (i * 70, 100, 150, 255)) for i in range(3)]
  17. for image in images:
  18. image.putpixel((0, 0), (0, 0, 0, 0))
  19. gif = root / '测试动画.gif'
  20. images[0].save(gif, save_all=True, append_images=images[1:], duration=100, disposal=2)
  21. info = inspect_media(gif)
  22. if info.count != 3:
  23. raise RuntimeError('GIF frame count mismatch')
  24. options = Options(count=2, prefix='role')
  25. files = export_frames(gif, root / 'gif', info, options,
  26. config=FrameConfig('Role', -51, -85))
  27. data = json.loads((root / 'gif/role.json').read_text(encoding='utf-8'))
  28. if len(files) != 4 or data['HEAD_POS'] != [-65, -5] or data['L']['Cx'] != -109:
  29. raise RuntimeError('Mirrored config or negative HEAD_POS mismatch')
  30. with Image.open(files[0]) as image:
  31. if image.getpixel((0, 0))[3] != 0:
  32. raise RuntimeError('GIF transparency mismatch')
  33. checks.append('GIF, transparency, mirrored offsets and negative HEAD_POS')
  34. files = export_frames(gif, root / 'single', info, options,
  35. config=FrameConfig('Role', -51, -85, mirror=False))
  36. if [p.name for p in files] != ['role_0_0.png', 'role_0_1.png']:
  37. raise RuntimeError('Single-direction naming mismatch')
  38. checks.append('Single-direction PNG and JSON export')
  39. webm = root / '测试视频.webm'
  40. subprocess.run([imageio_ffmpeg.get_ffmpeg_exe(), '-v', 'error', '-f', 'rawvideo',
  41. '-pix_fmt', 'rgb24', '-s', '160x80', '-r', '10', '-i', 'pipe:0',
  42. '-c:v', 'libvpx-vp9', '-lossless', '1', str(webm)],
  43. input=b''.join(im.convert('RGB').tobytes() for im in images),
  44. check=True, capture_output=True,
  45. creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
  46. video_info = inspect_media(webm)
  47. files = export_frames(webm, root / 'webm', video_info, options)
  48. if video_info.count != 3 or len(files) != 2:
  49. raise RuntimeError('Bundled FFmpeg WebM decode mismatch')
  50. checks.append('Bundled FFmpeg WebM encode, decode and PNG export')
  51. app = app_class()
  52. app.withdraw()
  53. app.update()
  54. if not app.mirror_directions.get():
  55. raise RuntimeError('Default mirror setting mismatch')
  56. app.destroy()
  57. app = None
  58. checks.append('Packaged Tk GUI initialization')
  59. result = {'ok': True, 'checks': checks}
  60. except Exception:
  61. result = {'ok': False, 'checks': checks, 'error': traceback.format_exc()}
  62. finally:
  63. if app is not None:
  64. app.destroy()
  65. Path(report_path).write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
  66. return 0 if result['ok'] else 1