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

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