smoke_samples.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from __future__ import annotations
  2. import argparse
  3. import sys
  4. from pathlib import Path
  5. ROOT = Path(__file__).resolve().parents[1]
  6. if str(ROOT) not in sys.path:
  7. sys.path.insert(0, str(ROOT))
  8. from app.conversion import convert_path_to_markdown
  9. def main() -> int:
  10. parser = argparse.ArgumentParser(
  11. description="Run smoke tests against sample files."
  12. )
  13. parser.add_argument(
  14. "sample_dir",
  15. nargs="?",
  16. default=r"D:\CodeSpace\personal-secretary\Scripts\Word转Markdown\测试文件",
  17. help="Directory containing sample files",
  18. )
  19. parser.add_argument(
  20. "--include-images",
  21. action="store_true",
  22. help="Inline local images when possible",
  23. )
  24. args = parser.parse_args()
  25. sample_dir = Path(args.sample_dir)
  26. failures: list[str] = []
  27. for path in sorted(sample_dir.iterdir()):
  28. if not path.is_file():
  29. continue
  30. try:
  31. markdown = convert_path_to_markdown(
  32. str(path), args.include_images, path.name
  33. )
  34. if not markdown.strip():
  35. raise RuntimeError("Empty markdown output")
  36. print(f"OK {path.name} -> {len(markdown)} chars")
  37. except Exception as exc: # noqa: BLE001
  38. failures.append(f"{path.name}: {exc}")
  39. print(f"FAIL {path.name}: {exc}")
  40. if failures:
  41. print("\nFailures:")
  42. for failure in failures:
  43. print(f"- {failure}")
  44. return 1
  45. return 0
  46. if __name__ == "__main__":
  47. raise SystemExit(main())