PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/test/test_zipapp.py

https://gitlab.com/unofficial-mirrors/cpython
Python | 349 lines | 265 code | 39 blank | 45 comment | 12 complexity | c1b4e1819708f17e5f783dee63aecc79 MD5 | raw file
  1. """Test harness for the zipapp module."""
  2. import io
  3. import pathlib
  4. import stat
  5. import sys
  6. import tempfile
  7. import unittest
  8. import zipapp
  9. import zipfile
  10. from unittest.mock import patch
  11. class ZipAppTest(unittest.TestCase):
  12. """Test zipapp module functionality."""
  13. def setUp(self):
  14. tmpdir = tempfile.TemporaryDirectory()
  15. self.addCleanup(tmpdir.cleanup)
  16. self.tmpdir = pathlib.Path(tmpdir.name)
  17. def test_create_archive(self):
  18. # Test packing a directory.
  19. source = self.tmpdir / 'source'
  20. source.mkdir()
  21. (source / '__main__.py').touch()
  22. target = self.tmpdir / 'source.pyz'
  23. zipapp.create_archive(str(source), str(target))
  24. self.assertTrue(target.is_file())
  25. def test_create_archive_with_pathlib(self):
  26. # Test packing a directory using Path objects for source and target.
  27. source = self.tmpdir / 'source'
  28. source.mkdir()
  29. (source / '__main__.py').touch()
  30. target = self.tmpdir / 'source.pyz'
  31. zipapp.create_archive(source, target)
  32. self.assertTrue(target.is_file())
  33. def test_create_archive_with_subdirs(self):
  34. # Test packing a directory includes entries for subdirectories.
  35. source = self.tmpdir / 'source'
  36. source.mkdir()
  37. (source / '__main__.py').touch()
  38. (source / 'foo').mkdir()
  39. (source / 'bar').mkdir()
  40. (source / 'foo' / '__init__.py').touch()
  41. target = io.BytesIO()
  42. zipapp.create_archive(str(source), target)
  43. target.seek(0)
  44. with zipfile.ZipFile(target, 'r') as z:
  45. self.assertIn('foo/', z.namelist())
  46. self.assertIn('bar/', z.namelist())
  47. def test_create_archive_default_target(self):
  48. # Test packing a directory to the default name.
  49. source = self.tmpdir / 'source'
  50. source.mkdir()
  51. (source / '__main__.py').touch()
  52. zipapp.create_archive(str(source))
  53. expected_target = self.tmpdir / 'source.pyz'
  54. self.assertTrue(expected_target.is_file())
  55. def test_no_main(self):
  56. # Test that packing a directory with no __main__.py fails.
  57. source = self.tmpdir / 'source'
  58. source.mkdir()
  59. (source / 'foo.py').touch()
  60. target = self.tmpdir / 'source.pyz'
  61. with self.assertRaises(zipapp.ZipAppError):
  62. zipapp.create_archive(str(source), str(target))
  63. def test_main_and_main_py(self):
  64. # Test that supplying a main argument with __main__.py fails.
  65. source = self.tmpdir / 'source'
  66. source.mkdir()
  67. (source / '__main__.py').touch()
  68. target = self.tmpdir / 'source.pyz'
  69. with self.assertRaises(zipapp.ZipAppError):
  70. zipapp.create_archive(str(source), str(target), main='pkg.mod:fn')
  71. def test_main_written(self):
  72. # Test that the __main__.py is written correctly.
  73. source = self.tmpdir / 'source'
  74. source.mkdir()
  75. (source / 'foo.py').touch()
  76. target = self.tmpdir / 'source.pyz'
  77. zipapp.create_archive(str(source), str(target), main='pkg.mod:fn')
  78. with zipfile.ZipFile(str(target), 'r') as z:
  79. self.assertIn('__main__.py', z.namelist())
  80. self.assertIn(b'pkg.mod.fn()', z.read('__main__.py'))
  81. def test_main_only_written_once(self):
  82. # Test that we don't write multiple __main__.py files.
  83. # The initial implementation had this bug; zip files allow
  84. # multiple entries with the same name
  85. source = self.tmpdir / 'source'
  86. source.mkdir()
  87. # Write 2 files, as the original bug wrote __main__.py
  88. # once for each file written :-(
  89. # See http://bugs.python.org/review/23491/diff/13982/Lib/zipapp.py#newcode67Lib/zipapp.py:67
  90. # (line 67)
  91. (source / 'foo.py').touch()
  92. (source / 'bar.py').touch()
  93. target = self.tmpdir / 'source.pyz'
  94. zipapp.create_archive(str(source), str(target), main='pkg.mod:fn')
  95. with zipfile.ZipFile(str(target), 'r') as z:
  96. self.assertEqual(1, z.namelist().count('__main__.py'))
  97. def test_main_validation(self):
  98. # Test that invalid values for main are rejected.
  99. source = self.tmpdir / 'source'
  100. source.mkdir()
  101. target = self.tmpdir / 'source.pyz'
  102. problems = [
  103. '', 'foo', 'foo:', ':bar', '12:bar', 'a.b.c.:d',
  104. '.a:b', 'a:b.', 'a:.b', 'a:silly name'
  105. ]
  106. for main in problems:
  107. with self.subTest(main=main):
  108. with self.assertRaises(zipapp.ZipAppError):
  109. zipapp.create_archive(str(source), str(target), main=main)
  110. def test_default_no_shebang(self):
  111. # Test that no shebang line is written to the target by default.
  112. source = self.tmpdir / 'source'
  113. source.mkdir()
  114. (source / '__main__.py').touch()
  115. target = self.tmpdir / 'source.pyz'
  116. zipapp.create_archive(str(source), str(target))
  117. with target.open('rb') as f:
  118. self.assertNotEqual(f.read(2), b'#!')
  119. def test_custom_interpreter(self):
  120. # Test that a shebang line with a custom interpreter is written
  121. # correctly.
  122. source = self.tmpdir / 'source'
  123. source.mkdir()
  124. (source / '__main__.py').touch()
  125. target = self.tmpdir / 'source.pyz'
  126. zipapp.create_archive(str(source), str(target), interpreter='python')
  127. with target.open('rb') as f:
  128. self.assertEqual(f.read(2), b'#!')
  129. self.assertEqual(b'python\n', f.readline())
  130. def test_pack_to_fileobj(self):
  131. # Test that we can pack to a file object.
  132. source = self.tmpdir / 'source'
  133. source.mkdir()
  134. (source / '__main__.py').touch()
  135. target = io.BytesIO()
  136. zipapp.create_archive(str(source), target, interpreter='python')
  137. self.assertTrue(target.getvalue().startswith(b'#!python\n'))
  138. def test_read_shebang(self):
  139. # Test that we can read the shebang line correctly.
  140. source = self.tmpdir / 'source'
  141. source.mkdir()
  142. (source / '__main__.py').touch()
  143. target = self.tmpdir / 'source.pyz'
  144. zipapp.create_archive(str(source), str(target), interpreter='python')
  145. self.assertEqual(zipapp.get_interpreter(str(target)), 'python')
  146. def test_read_missing_shebang(self):
  147. # Test that reading the shebang line of a file without one returns None.
  148. source = self.tmpdir / 'source'
  149. source.mkdir()
  150. (source / '__main__.py').touch()
  151. target = self.tmpdir / 'source.pyz'
  152. zipapp.create_archive(str(source), str(target))
  153. self.assertEqual(zipapp.get_interpreter(str(target)), None)
  154. def test_modify_shebang(self):
  155. # Test that we can change the shebang of a file.
  156. source = self.tmpdir / 'source'
  157. source.mkdir()
  158. (source / '__main__.py').touch()
  159. target = self.tmpdir / 'source.pyz'
  160. zipapp.create_archive(str(source), str(target), interpreter='python')
  161. new_target = self.tmpdir / 'changed.pyz'
  162. zipapp.create_archive(str(target), str(new_target), interpreter='python2.7')
  163. self.assertEqual(zipapp.get_interpreter(str(new_target)), 'python2.7')
  164. def test_write_shebang_to_fileobj(self):
  165. # Test that we can change the shebang of a file, writing the result to a
  166. # file object.
  167. source = self.tmpdir / 'source'
  168. source.mkdir()
  169. (source / '__main__.py').touch()
  170. target = self.tmpdir / 'source.pyz'
  171. zipapp.create_archive(str(source), str(target), interpreter='python')
  172. new_target = io.BytesIO()
  173. zipapp.create_archive(str(target), new_target, interpreter='python2.7')
  174. self.assertTrue(new_target.getvalue().startswith(b'#!python2.7\n'))
  175. def test_read_from_pathobj(self):
  176. # Test that we can copy an archive using a pathlib.Path object
  177. # for the source.
  178. source = self.tmpdir / 'source'
  179. source.mkdir()
  180. (source / '__main__.py').touch()
  181. target1 = self.tmpdir / 'target1.pyz'
  182. target2 = self.tmpdir / 'target2.pyz'
  183. zipapp.create_archive(source, target1, interpreter='python')
  184. zipapp.create_archive(target1, target2, interpreter='python2.7')
  185. self.assertEqual(zipapp.get_interpreter(target2), 'python2.7')
  186. def test_read_from_fileobj(self):
  187. # Test that we can copy an archive using an open file object.
  188. source = self.tmpdir / 'source'
  189. source.mkdir()
  190. (source / '__main__.py').touch()
  191. target = self.tmpdir / 'source.pyz'
  192. temp_archive = io.BytesIO()
  193. zipapp.create_archive(str(source), temp_archive, interpreter='python')
  194. new_target = io.BytesIO()
  195. temp_archive.seek(0)
  196. zipapp.create_archive(temp_archive, new_target, interpreter='python2.7')
  197. self.assertTrue(new_target.getvalue().startswith(b'#!python2.7\n'))
  198. def test_remove_shebang(self):
  199. # Test that we can remove the shebang from a file.
  200. source = self.tmpdir / 'source'
  201. source.mkdir()
  202. (source / '__main__.py').touch()
  203. target = self.tmpdir / 'source.pyz'
  204. zipapp.create_archive(str(source), str(target), interpreter='python')
  205. new_target = self.tmpdir / 'changed.pyz'
  206. zipapp.create_archive(str(target), str(new_target), interpreter=None)
  207. self.assertEqual(zipapp.get_interpreter(str(new_target)), None)
  208. def test_content_of_copied_archive(self):
  209. # Test that copying an archive doesn't corrupt it.
  210. source = self.tmpdir / 'source'
  211. source.mkdir()
  212. (source / '__main__.py').touch()
  213. target = io.BytesIO()
  214. zipapp.create_archive(str(source), target, interpreter='python')
  215. new_target = io.BytesIO()
  216. target.seek(0)
  217. zipapp.create_archive(target, new_target, interpreter=None)
  218. new_target.seek(0)
  219. with zipfile.ZipFile(new_target, 'r') as z:
  220. self.assertEqual(set(z.namelist()), {'__main__.py'})
  221. # (Unix only) tests that archives with shebang lines are made executable
  222. @unittest.skipIf(sys.platform == 'win32',
  223. 'Windows does not support an executable bit')
  224. def test_shebang_is_executable(self):
  225. # Test that an archive with a shebang line is made executable.
  226. source = self.tmpdir / 'source'
  227. source.mkdir()
  228. (source / '__main__.py').touch()
  229. target = self.tmpdir / 'source.pyz'
  230. zipapp.create_archive(str(source), str(target), interpreter='python')
  231. self.assertTrue(target.stat().st_mode & stat.S_IEXEC)
  232. @unittest.skipIf(sys.platform == 'win32',
  233. 'Windows does not support an executable bit')
  234. def test_no_shebang_is_not_executable(self):
  235. # Test that an archive with no shebang line is not made executable.
  236. source = self.tmpdir / 'source'
  237. source.mkdir()
  238. (source / '__main__.py').touch()
  239. target = self.tmpdir / 'source.pyz'
  240. zipapp.create_archive(str(source), str(target), interpreter=None)
  241. self.assertFalse(target.stat().st_mode & stat.S_IEXEC)
  242. class ZipAppCmdlineTest(unittest.TestCase):
  243. """Test zipapp module command line API."""
  244. def setUp(self):
  245. tmpdir = tempfile.TemporaryDirectory()
  246. self.addCleanup(tmpdir.cleanup)
  247. self.tmpdir = pathlib.Path(tmpdir.name)
  248. def make_archive(self):
  249. # Test that an archive with no shebang line is not made executable.
  250. source = self.tmpdir / 'source'
  251. source.mkdir()
  252. (source / '__main__.py').touch()
  253. target = self.tmpdir / 'source.pyz'
  254. zipapp.create_archive(source, target)
  255. return target
  256. def test_cmdline_create(self):
  257. # Test the basic command line API.
  258. source = self.tmpdir / 'source'
  259. source.mkdir()
  260. (source / '__main__.py').touch()
  261. args = [str(source)]
  262. zipapp.main(args)
  263. target = source.with_suffix('.pyz')
  264. self.assertTrue(target.is_file())
  265. def test_cmdline_copy(self):
  266. # Test copying an archive.
  267. original = self.make_archive()
  268. target = self.tmpdir / 'target.pyz'
  269. args = [str(original), '-o', str(target)]
  270. zipapp.main(args)
  271. self.assertTrue(target.is_file())
  272. def test_cmdline_copy_inplace(self):
  273. # Test copying an archive in place fails.
  274. original = self.make_archive()
  275. target = self.tmpdir / 'target.pyz'
  276. args = [str(original), '-o', str(original)]
  277. with self.assertRaises(SystemExit) as cm:
  278. zipapp.main(args)
  279. # Program should exit with a non-zero returm code.
  280. self.assertTrue(cm.exception.code)
  281. def test_cmdline_copy_change_main(self):
  282. # Test copying an archive doesn't allow changing __main__.py.
  283. original = self.make_archive()
  284. target = self.tmpdir / 'target.pyz'
  285. args = [str(original), '-o', str(target), '-m', 'foo:bar']
  286. with self.assertRaises(SystemExit) as cm:
  287. zipapp.main(args)
  288. # Program should exit with a non-zero returm code.
  289. self.assertTrue(cm.exception.code)
  290. @patch('sys.stdout', new_callable=io.StringIO)
  291. def test_info_command(self, mock_stdout):
  292. # Test the output of the info command.
  293. target = self.make_archive()
  294. args = [str(target), '--info']
  295. with self.assertRaises(SystemExit) as cm:
  296. zipapp.main(args)
  297. # Program should exit with a zero returm code.
  298. self.assertEqual(cm.exception.code, 0)
  299. self.assertEqual(mock_stdout.getvalue(), "Interpreter: <none>\n")
  300. def test_info_error(self):
  301. # Test the info command fails when the archive does not exist.
  302. target = self.tmpdir / 'dummy.pyz'
  303. args = [str(target), '--info']
  304. with self.assertRaises(SystemExit) as cm:
  305. zipapp.main(args)
  306. # Program should exit with a non-zero returm code.
  307. self.assertTrue(cm.exception.code)
  308. if __name__ == "__main__":
  309. unittest.main()