test_kit_path.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import shutil
  3. import sys
  4. import tempfile
  5. import types
  6. import unittest
  7. from pathlib import Path
  8. from unittest.mock import patch
  9. ROOT = Path(__file__).resolve().parents[1]
  10. sys.path.insert(0, str(ROOT / "scripts"))
  11. import audit
  12. import kit_path
  13. sys.modules.setdefault("requests", types.SimpleNamespace())
  14. import rule_check
  15. REAL_NAME = "SKU审核专员-训练校准工具包"
  16. # 中文文件名按 GBK 写入 zip、又被按 cp437 解出来后的样子
  17. GARBLED = REAL_NAME.encode("gbk").decode("cp437")
  18. class KitPathTests(unittest.TestCase):
  19. def setUp(self):
  20. self.tmp = tempfile.TemporaryDirectory()
  21. self.addCleanup(self.tmp.cleanup)
  22. self.assets = os.path.join(self.tmp.name, "assets")
  23. self.cwd = os.path.join(self.tmp.name, "work")
  24. os.makedirs(self.assets)
  25. os.makedirs(self.cwd)
  26. def touch(self, folder, name):
  27. path = os.path.join(folder, name)
  28. open(path, "wb").close()
  29. return path
  30. def find(self):
  31. return kit_path.find(asset_dirs=[self.assets], cwd=self.cwd)
  32. def test_garbled_name_in_assets_is_still_found(self):
  33. expected = self.touch(self.assets, GARBLED + "_v0.9.xlsx")
  34. self.assertEqual(self.find(), expected)
  35. def test_highest_version_wins_numerically(self):
  36. self.touch(self.assets, REAL_NAME + "_v0.9.xlsx")
  37. expected = self.touch(self.assets, GARBLED + "_v0.10.xlsx")
  38. self.assertEqual(self.find(), expected)
  39. def test_lock_files_and_unversioned_sheets_are_ignored(self):
  40. self.touch(self.assets, "~$" + REAL_NAME + "_v0.9.xlsx")
  41. self.touch(self.assets, "notes.xlsx")
  42. self.assertIsNone(self.find())
  43. def test_working_directory_requires_the_full_name(self):
  44. self.touch(self.cwd, "报告_v2.xlsx")
  45. self.assertIsNone(self.find())
  46. expected = self.touch(self.cwd, REAL_NAME + "_v0.9.xlsx")
  47. self.assertEqual(self.find(), expected)
  48. def test_assets_take_priority_over_working_directory(self):
  49. self.touch(self.cwd, REAL_NAME + "_v9.9.xlsx")
  50. expected = self.touch(self.assets, GARBLED + "_v0.9.xlsx")
  51. self.assertEqual(self.find(), expected)
  52. def test_missing_folders_do_not_raise(self):
  53. self.assertIsNone(kit_path.find(asset_dirs=[os.path.join(self.tmp.name, "nope")],
  54. cwd=os.path.join(self.tmp.name, "nope2")))
  55. def test_display_name_survives_undecodable_bytes(self):
  56. raw = (REAL_NAME.encode("gbk") + b"_v0.9.xlsx").decode("utf-8", "surrogateescape")
  57. shown = kit_path.display_name(os.path.join(self.assets, raw))
  58. shown.encode("utf-8")
  59. self.assertTrue(shown.endswith("_v0.9.xlsx"))
  60. def test_both_gate_scripts_load_a_garbled_workbook(self):
  61. real = audit.pick_workbook()
  62. self.assertTrue(real and os.path.exists(real))
  63. garbled = os.path.join(self.assets, GARBLED + "_v0.9.xlsx")
  64. shutil.copyfile(real, garbled)
  65. with patch.object(kit_path, "ASSET_DIRS", [self.assets]), \
  66. patch.object(os, "getcwd", return_value=self.cwd):
  67. self.assertEqual(audit.pick_workbook(), garbled)
  68. self.assertEqual(rule_check._pick_workbook(), garbled)
  69. kit = audit.Kit(audit.pick_workbook())
  70. try:
  71. self.assertEqual(kit.fz06_px(), 800)
  72. finally:
  73. kit.wb.close()
  74. def test_rule_check_reports_the_expected_pattern_when_nothing_is_found(self):
  75. with patch.object(kit_path, "ASSET_DIRS", [self.assets]), \
  76. patch.object(os, "getcwd", return_value=self.cwd):
  77. self.assertIsNone(audit.pick_workbook())
  78. missing = rule_check._pick_workbook()
  79. self.assertIn("_v*.xlsx", missing)
  80. self.assertNotIn("v0.3", missing)
  81. if __name__ == "__main__":
  82. unittest.main()