1
0

0.4.27-37-g42980e5.simplify-tests-into-something-more-delarative.patch 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. Subject: Simplify tests into something more delarative
  2. Origin: upstream, commit 0.4.27-37-g42980e5 <https://github.com/ahupp/python-magic/commit/0.4.27-37-g42980e5>
  3. Author: Adam Hupp <adam@hupp.org>
  4. Date: Sun May 26 18:06:37 2024 -0700
  5. --- a/test/python_magic_test.py
  6. +++ b/test/python_magic_test.py
  7. @@ -1,3 +1,5 @@
  8. +from dataclasses import dataclass
  9. +from enum import Enum
  10. import os
  11. import os.path
  12. import shutil
  13. @@ -17,11 +19,140 @@
  14. import magic
  15. +@dataclass
  16. +class TestFile:
  17. + file_name: str
  18. + mime_results: list[str]
  19. + text_results: list[str]
  20. + no_check_elf_results: list[str] | None
  21. + buf_equals_file: bool = True
  22. # magic_descriptor is broken (?) in centos 7, so don't run those tests
  23. SKIP_FROM_DESCRIPTOR = bool(os.environ.get("SKIP_FROM_DESCRIPTOR"))
  24. +COMMON_PLAIN = [
  25. + {},
  26. + {"check_soft": True},
  27. + {"check_soft": False},
  28. + {"check_json": True},
  29. + {"check_json": False},
  30. +]
  31. +
  32. +NO_SOFT = {"check_soft": False}
  33. +
  34. +COMMON_MIME = [{"mime": True, **k} for k in COMMON_PLAIN]
  35. +
  36. +CASES = {
  37. + "magic._pyc_": [
  38. + (COMMON_MIME, [
  39. + "application/octet-stream",
  40. + "text/x-bytecode.python",
  41. + "application/x-bytecode.python",
  42. + ]),
  43. + (COMMON_PLAIN, ["python 2.4 byte-compiled"]),
  44. + (NO_SOFT, ["data"]),
  45. + ],
  46. + "test.pdf": [
  47. + (COMMON_MIME, ["application/pdf"]),
  48. + (COMMON_PLAIN, [
  49. + "PDF document, version 1.2",
  50. + "PDF document, version 1.2, 2 pages",
  51. + "PDF document, version 1.2, 2 page(s)",
  52. + ]),
  53. + (NO_SOFT, ["ASCII text"]),
  54. + ],
  55. + "test.gz": [
  56. + (COMMON_MIME, ["application/gzip", "application/x-gzip"]),
  57. + (COMMON_PLAIN, [
  58. + 'gzip compressed data, was "test", from Unix, last modified: Sun Jun 29 01:32:52 2008',
  59. + 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix',
  60. + 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix, original size 15',
  61. + 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix, original size modulo 2^32 15',
  62. + 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix, truncated',
  63. + ]),
  64. + ({"extension": True}, [
  65. + # some versions return '' for the extensions of a gz file,
  66. + # including w/ the command line. Who knows...
  67. + "gz/tgz/tpz/zabw/svgz/adz/kmy/xcfgz",
  68. + "gz/tgz/tpz/zabw/svgz",
  69. + "",
  70. + "???",
  71. + ]),
  72. + (NO_SOFT, ["data"]),
  73. + ],
  74. + "test.snappy.parquet": [
  75. + (COMMON_MIME, ["application/octet-stream"]),
  76. + (COMMON_PLAIN, ["Apache Parquet", "Par archive data"]),
  77. + (NO_SOFT, ["data"]),
  78. + ],
  79. + "test.json": [
  80. + # TODO: soft, no_json
  81. + (COMMON_MIME, ["application/json"]),
  82. + (COMMON_PLAIN, ["JSON text data"]),
  83. + ({"mime": True, "check_json": False}, [
  84. + "data",
  85. + ]),
  86. + (NO_SOFT, ["JSON text data"])
  87. + ],
  88. + "elf-NetBSD-x86_64-echo": [
  89. + # TODO: soft, no elf
  90. + (COMMON_PLAIN, [
  91. + "ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
  92. + "ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
  93. + ]),
  94. + (COMMON_MIME, [
  95. + "application/x-pie-executable",
  96. + "application/x-sharedlib",
  97. + ]),
  98. + ({"check_elf": False}, [
  99. + "ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
  100. + ]),
  101. + # TODO: sometimes
  102. + # "ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
  103. +
  104. + (NO_SOFT, ["data"]),
  105. + ],
  106. + "test.txt": [
  107. + (COMMON_MIME, ["text/plain"]),
  108. + (COMMON_PLAIN, ["ASCII text"]),
  109. + ({"mime_encoding": True}, [
  110. + "us-ascii",
  111. + ]),
  112. + (NO_SOFT, ["ASCII text"]),
  113. + ],
  114. + "text-iso8859-1.txt": [
  115. + ({"mime_encoding": True}, [
  116. + "iso-8859-1",
  117. + ]),
  118. + ],
  119. + b"\xce\xbb": [
  120. + (COMMON_MIME, ["text/plain"]),
  121. + ],
  122. + "b\xce\xbb".decode("utf-8"): [
  123. + (COMMON_MIME, ["text/plain"]),
  124. + ],
  125. + "name_use.jpg": [
  126. + ({"extension": True}, [
  127. + "jpeg/jpg/jpe/jfif"
  128. + ]),
  129. + ],
  130. + "keep-going.jpg": [
  131. + (COMMON_MIME, [
  132. + "image/jpeg"
  133. + ]),
  134. + ({"mime": True, "keep_going": True}, [
  135. + "image/jpeg\\012- application/octet-stream",
  136. + ])
  137. + ],
  138. + "test.py": [
  139. + (COMMON_MIME, [
  140. + "text/x-python",
  141. + "text/x-script.python",
  142. + ])
  143. + ]
  144. +}
  145. +
  146. class MagicTest(unittest.TestCase):
  147. TESTDATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "testdata"))
  148. @@ -34,26 +165,6 @@
  149. def test_fs_encoding(self):
  150. self.assertEqual("utf-8", sys.getfilesystemencoding().lower())
  151. - def assert_values(self, m, expected_values, buf_equals_file=True):
  152. - for filename, expected_value in expected_values.items():
  153. - try:
  154. - filename = os.path.join(self.TESTDATA_DIR, filename)
  155. - except TypeError:
  156. - filename = os.path.join(self.TESTDATA_DIR.encode("utf-8"), filename)
  157. -
  158. - if type(expected_value) is not tuple:
  159. - expected_value = (expected_value,)
  160. -
  161. - with open(filename, "rb") as f:
  162. - buf_value = m.from_buffer(f.read())
  163. -
  164. - file_value = m.from_file(filename)
  165. -
  166. - if buf_equals_file:
  167. - self.assertEqual(buf_value, file_value)
  168. -
  169. - for value in (buf_value, file_value):
  170. - self.assertIn(value, expected_value)
  171. def test_from_file_str_and_bytes(self):
  172. filename = os.path.join(self.TESTDATA_DIR, "test.pdf")
  173. @@ -63,203 +174,34 @@
  174. "application/pdf", magic.from_file(filename.encode("utf-8"), mime=True)
  175. )
  176. - def test_from_descriptor_str_and_bytes(self):
  177. - if SKIP_FROM_DESCRIPTOR:
  178. - self.skipTest("magic_descriptor is broken in this version of libmagic")
  179. - filename = os.path.join(self.TESTDATA_DIR, "test.pdf")
  180. - with open(filename) as f:
  181. - self.assertEqual(
  182. - "application/pdf", magic.from_descriptor(f.fileno(), mime=True)
  183. - )
  184. - self.assertEqual(
  185. - "application/pdf", magic.from_descriptor(f.fileno(), mime=True)
  186. - )
  187. -
  188. - def test_from_buffer_str_and_bytes(self):
  189. - if SKIP_FROM_DESCRIPTOR:
  190. - self.skipTest("magic_descriptor is broken in this version of libmagic")
  191. - m = magic.Magic(mime=True)
  192. -
  193. - self.assertTrue(
  194. - m.from_buffer('#!/usr/bin/env python\nprint("foo")')
  195. - in ("text/x-python", "text/x-script.python")
  196. - )
  197. - self.assertTrue(
  198. - m.from_buffer(b'#!/usr/bin/env python\nprint("foo")')
  199. - in ("text/x-python", "text/x-script.python")
  200. - )
  201. -
  202. - def test_mime_types(self):
  203. + def test_all_cases(self):
  204. + # TODO:
  205. + # * MAGIC_EXTENSION not supported
  206. + # * keep_going not supported
  207. + # * buffer checks
  208. dest = os.path.join(MagicTest.TESTDATA_DIR, b"\xce\xbb".decode("utf-8"))
  209. shutil.copyfile(os.path.join(MagicTest.TESTDATA_DIR, "lambda"), dest)
  210. + os.environ["TZ"] = "UTC"
  211. try:
  212. - m = magic.Magic(mime=True)
  213. - self.assert_values(
  214. - m,
  215. - {
  216. - "elf-NetBSD-x86_64-echo": (
  217. - "application/x-pie-executable",
  218. - "application/x-sharedlib",
  219. - ),
  220. - "magic._pyc_": (
  221. - "application/octet-stream",
  222. - "text/x-bytecode.python",
  223. - "application/x-bytecode.python",
  224. - ),
  225. - "test.pdf": "application/pdf",
  226. - "test.gz": ("application/gzip", "application/x-gzip"),
  227. - "test.snappy.parquet": "application/octet-stream",
  228. - "text.txt": "text/plain",
  229. - b"\xce\xbb".decode("utf-8"): "text/plain",
  230. - b"\xce\xbb": "text/plain",
  231. - "test.json": "application/json",
  232. - },
  233. - buf_equals_file=False,
  234. - )
  235. - finally:
  236. - os.unlink(dest)
  237. -
  238. - # TODO: Fix this failing test on Ubuntu
  239. - @pytest.mark.skipif(sys.platform == "linux", reason="'JSON data' not found")
  240. - def test_descriptions(self):
  241. - m = magic.Magic()
  242. - os.environ["TZ"] = "UTC" # To get last modified date of test.gz in UTC
  243. - try:
  244. - self.assert_values(
  245. - m,
  246. - {
  247. - "elf-NetBSD-x86_64-echo": (
  248. - "ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
  249. - "ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
  250. - ),
  251. - "magic._pyc_": "python 2.4 byte-compiled",
  252. - "test.pdf": (
  253. - "PDF document, version 1.2",
  254. - "PDF document, version 1.2, 2 pages",
  255. - "PDF document, version 1.2, 2 page(s)",
  256. - ),
  257. - "test.gz": (
  258. - 'gzip compressed data, was "test", from Unix, last '
  259. - "modified: Sun Jun 29 01:32:52 2008",
  260. - 'gzip compressed data, was "test", last modified'
  261. - ": Sun Jun 29 01:32:52 2008, from Unix",
  262. - 'gzip compressed data, was "test", last modified'
  263. - ": Sun Jun 29 01:32:52 2008, from Unix, original size 15",
  264. - 'gzip compressed data, was "test", '
  265. - "last modified: Sun Jun 29 01:32:52 2008, "
  266. - "from Unix, original size modulo 2^32 15",
  267. - 'gzip compressed data, was "test", last modified'
  268. - ": Sun Jun 29 01:32:52 2008, from Unix, truncated",
  269. - ),
  270. - "text.txt": "ASCII text",
  271. - "test.snappy.parquet": ("Apache Parquet", "Par archive data"),
  272. - "test.json": "JSON text data",
  273. - },
  274. - buf_equals_file=False,
  275. - )
  276. + for file_name, cases in CASES:
  277. + filename = os.path.join(self.TESTDATA_DIR, file_name)
  278. + for flags, outputs in cases:
  279. + m = magic.Magic(**flags)
  280. + with open(filename) as f:
  281. + self.assertIn(m.from_descriptor(f.fileno()), outputs)
  282. +
  283. + self.assertIn(m.from_file(filename), outputs)
  284. +
  285. + fname_bytes = filename.encode("utf-8")
  286. + self.assertIn(m.from_file(fname_bytes), outputs)
  287. +
  288. + with open(file_name, "rb") as f:
  289. + buf_result = m.from_buffer(f.read(1024))
  290. + self.assertIn(buf_result, outputs)
  291. finally:
  292. del os.environ["TZ"]
  293. -
  294. - # TODO: Fix this failing test on Ubuntu
  295. - @pytest.mark.skipif(sys.platform == "linux", reason="'JSON data' not found")
  296. - def test_descriptions_no_soft(self):
  297. - m = magic.Magic(check_soft=False)
  298. - self.assert_values(
  299. - m,
  300. - {
  301. - "elf-NetBSD-x86_64-echo": (
  302. - "data",
  303. - "ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
  304. - ),
  305. - "magic._pyc_": "data",
  306. - "test.pdf": "ASCII text",
  307. - "test.gz": "data",
  308. - "text.txt": "ASCII text",
  309. - "test.snappy.parquet": "data",
  310. - "test.json": "JSON text data",
  311. - },
  312. - buf_equals_file=False,
  313. - )
  314. -
  315. - def test_descriptions_no_elf(self):
  316. - m = magic.Magic(check_elf=False)
  317. - self.assert_values(
  318. - m,
  319. - {
  320. - "elf-NetBSD-x86_64-echo": "ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
  321. - },
  322. - buf_equals_file=True,
  323. - )
  324. -
  325. - def test_descriptions_no_json(self):
  326. - m = magic.Magic(check_elf=False)
  327. - self.assert_values(
  328. - m,
  329. - {
  330. - "test.json": "data",
  331. - },
  332. - buf_equals_file=True,
  333. - )
  334. -
  335. - def test_descriptions_no_json_unchanged(self):
  336. - # verify non-json results are unchanged
  337. - m = magic.Magic(check_json=False)
  338. - os.environ["TZ"] = "UTC" # To get last modified date of test.gz in UTC
  339. - try:
  340. - self.assert_values(
  341. - m,
  342. - {
  343. - "elf-NetBSD-x86_64-echo": (
  344. - "ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
  345. - "ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
  346. - ),
  347. - "magic._pyc_": "python 2.4 byte-compiled",
  348. - "test.pdf": (
  349. - "PDF document, version 1.2",
  350. - "PDF document, version 1.2, 2 pages",
  351. - "PDF document, version 1.2, 2 page(s)",
  352. - ),
  353. - "test.gz": (
  354. - 'gzip compressed data, was "test", from Unix, last '
  355. - "modified: Sun Jun 29 01:32:52 2008",
  356. - 'gzip compressed data, was "test", last modified'
  357. - ": Sun Jun 29 01:32:52 2008, from Unix",
  358. - 'gzip compressed data, was "test", last modified'
  359. - ": Sun Jun 29 01:32:52 2008, from Unix, original size 15",
  360. - 'gzip compressed data, was "test", '
  361. - "last modified: Sun Jun 29 01:32:52 2008, "
  362. - "from Unix, original size modulo 2^32 15",
  363. - 'gzip compressed data, was "test", last modified'
  364. - ": Sun Jun 29 01:32:52 2008, from Unix, truncated",
  365. - ),
  366. - "text.txt": "ASCII text",
  367. - "test.snappy.parquet": ("Apache Parquet", "Par archive data"),
  368. - },
  369. - buf_equals_file=False,
  370. - )
  371. - finally:
  372. - del os.environ["TZ"]
  373. -
  374. - def test_extension(self):
  375. - try:
  376. - m = magic.Magic(extension=True)
  377. - self.assert_values(
  378. - m,
  379. - {
  380. - # some versions return '' for the extensions of a gz file,
  381. - # including w/ the command line. Who knows...
  382. - "test.gz": (
  383. - "gz/tgz/tpz/zabw/svgz/adz/kmy/xcfgz",
  384. - "gz/tgz/tpz/zabw/svgz",
  385. - "",
  386. - "???",
  387. - ),
  388. - "name_use.jpg": "jpeg/jpg/jpe/jfif",
  389. - },
  390. - )
  391. - except NotImplementedError:
  392. - self.skipTest("MAGIC_EXTENSION not supported in this version")
  393. + os.unlink(dest)
  394. def test_unicode_result_nonraw(self):
  395. m = magic.Magic(raw=False)
  396. @@ -280,15 +222,6 @@
  397. else:
  398. raise unittest.SkipTest("Magic file doesn't return expected type.")
  399. - def test_mime_encodings(self):
  400. - m = magic.Magic(mime_encoding=True)
  401. - self.assert_values(
  402. - m,
  403. - {
  404. - "text-iso8859-1.txt": "iso-8859-1",
  405. - "text.txt": "us-ascii",
  406. - },
  407. - )
  408. def test_errors(self):
  409. m = magic.Magic()
  410. @@ -300,22 +233,6 @@
  411. finally:
  412. del os.environ["MAGIC"]
  413. - def test_keep_going(self):
  414. - filename = os.path.join(self.TESTDATA_DIR, "keep-going.jpg")
  415. -
  416. - m = magic.Magic(mime=True)
  417. - self.assertEqual(m.from_file(filename), "image/jpeg")
  418. -
  419. - try:
  420. - # this will throw if you have an "old" version of the library
  421. - # I'm otherwise not sure how to query if keep_going is supported
  422. - magic.version()
  423. - m = magic.Magic(mime=True, keep_going=True)
  424. - self.assertEqual(
  425. - m.from_file(filename), "image/jpeg\\012- application/octet-stream"
  426. - )
  427. - except NotImplementedError:
  428. - pass
  429. def test_rethrow(self):
  430. old = magic.magic_buffer