test_sdist.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. # -*- coding: utf-8 -*-
  2. """sdist tests"""
  3. import os
  4. import shutil
  5. import sys
  6. import tempfile
  7. import unicodedata
  8. import contextlib
  9. import io
  10. from setuptools.extern import six
  11. from setuptools.extern.six.moves import map
  12. import pytest
  13. import pkg_resources
  14. from setuptools.command.sdist import sdist
  15. from setuptools.command.egg_info import manifest_maker
  16. from setuptools.dist import Distribution
  17. from setuptools.tests import fail_on_ascii
  18. py3_only = pytest.mark.xfail(six.PY2, reason="Test runs on Python 3 only")
  19. SETUP_ATTRS = {
  20. 'name': 'sdist_test',
  21. 'version': '0.0',
  22. 'packages': ['sdist_test'],
  23. 'package_data': {'sdist_test': ['*.txt']}
  24. }
  25. SETUP_PY = """\
  26. from setuptools import setup
  27. setup(**%r)
  28. """ % SETUP_ATTRS
  29. if six.PY3:
  30. LATIN1_FILENAME = 'smörbröd.py'.encode('latin-1')
  31. else:
  32. LATIN1_FILENAME = 'sm\xf6rbr\xf6d.py'
  33. # Cannot use context manager because of Python 2.4
  34. @contextlib.contextmanager
  35. def quiet():
  36. old_stdout, old_stderr = sys.stdout, sys.stderr
  37. sys.stdout, sys.stderr = six.StringIO(), six.StringIO()
  38. try:
  39. yield
  40. finally:
  41. sys.stdout, sys.stderr = old_stdout, old_stderr
  42. # Fake byte literals for Python <= 2.5
  43. def b(s, encoding='utf-8'):
  44. if six.PY3:
  45. return s.encode(encoding)
  46. return s
  47. # Convert to POSIX path
  48. def posix(path):
  49. if six.PY3 and not isinstance(path, str):
  50. return path.replace(os.sep.encode('ascii'), b('/'))
  51. else:
  52. return path.replace(os.sep, '/')
  53. # HFS Plus uses decomposed UTF-8
  54. def decompose(path):
  55. if isinstance(path, six.text_type):
  56. return unicodedata.normalize('NFD', path)
  57. try:
  58. path = path.decode('utf-8')
  59. path = unicodedata.normalize('NFD', path)
  60. path = path.encode('utf-8')
  61. except UnicodeError:
  62. pass # Not UTF-8
  63. return path
  64. def read_all_bytes(filename):
  65. with io.open(filename, 'rb') as fp:
  66. return fp.read()
  67. class TestSdistTest:
  68. def setup_method(self, method):
  69. self.temp_dir = tempfile.mkdtemp()
  70. f = open(os.path.join(self.temp_dir, 'setup.py'), 'w')
  71. f.write(SETUP_PY)
  72. f.close()
  73. # Set up the rest of the test package
  74. test_pkg = os.path.join(self.temp_dir, 'sdist_test')
  75. os.mkdir(test_pkg)
  76. # *.rst was not included in package_data, so c.rst should not be
  77. # automatically added to the manifest when not under version control
  78. for fname in ['__init__.py', 'a.txt', 'b.txt', 'c.rst']:
  79. # Just touch the files; their contents are irrelevant
  80. open(os.path.join(test_pkg, fname), 'w').close()
  81. self.old_cwd = os.getcwd()
  82. os.chdir(self.temp_dir)
  83. def teardown_method(self, method):
  84. os.chdir(self.old_cwd)
  85. shutil.rmtree(self.temp_dir)
  86. def test_package_data_in_sdist(self):
  87. """Regression test for pull request #4: ensures that files listed in
  88. package_data are included in the manifest even if they're not added to
  89. version control.
  90. """
  91. dist = Distribution(SETUP_ATTRS)
  92. dist.script_name = 'setup.py'
  93. cmd = sdist(dist)
  94. cmd.ensure_finalized()
  95. with quiet():
  96. cmd.run()
  97. manifest = cmd.filelist.files
  98. assert os.path.join('sdist_test', 'a.txt') in manifest
  99. assert os.path.join('sdist_test', 'b.txt') in manifest
  100. assert os.path.join('sdist_test', 'c.rst') not in manifest
  101. def test_defaults_case_sensitivity(self):
  102. """
  103. Make sure default files (README.*, etc.) are added in a case-sensitive
  104. way to avoid problems with packages built on Windows.
  105. """
  106. open(os.path.join(self.temp_dir, 'readme.rst'), 'w').close()
  107. open(os.path.join(self.temp_dir, 'SETUP.cfg'), 'w').close()
  108. dist = Distribution(SETUP_ATTRS)
  109. # the extension deliberately capitalized for this test
  110. # to make sure the actual filename (not capitalized) gets added
  111. # to the manifest
  112. dist.script_name = 'setup.PY'
  113. cmd = sdist(dist)
  114. cmd.ensure_finalized()
  115. with quiet():
  116. cmd.run()
  117. # lowercase all names so we can test in a case-insensitive way to make sure the files are not included
  118. manifest = map(lambda x: x.lower(), cmd.filelist.files)
  119. assert 'readme.rst' not in manifest, manifest
  120. assert 'setup.py' not in manifest, manifest
  121. assert 'setup.cfg' not in manifest, manifest
  122. @fail_on_ascii
  123. def test_manifest_is_written_with_utf8_encoding(self):
  124. # Test for #303.
  125. dist = Distribution(SETUP_ATTRS)
  126. dist.script_name = 'setup.py'
  127. mm = manifest_maker(dist)
  128. mm.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt')
  129. os.mkdir('sdist_test.egg-info')
  130. # UTF-8 filename
  131. filename = os.path.join('sdist_test', 'smörbröd.py')
  132. # Must create the file or it will get stripped.
  133. open(filename, 'w').close()
  134. # Add UTF-8 filename and write manifest
  135. with quiet():
  136. mm.run()
  137. mm.filelist.append(filename)
  138. mm.write_manifest()
  139. contents = read_all_bytes(mm.manifest)
  140. # The manifest should be UTF-8 encoded
  141. u_contents = contents.decode('UTF-8')
  142. # The manifest should contain the UTF-8 filename
  143. if six.PY2:
  144. fs_enc = sys.getfilesystemencoding()
  145. filename = filename.decode(fs_enc)
  146. assert posix(filename) in u_contents
  147. @py3_only
  148. @fail_on_ascii
  149. def test_write_manifest_allows_utf8_filenames(self):
  150. # Test for #303.
  151. dist = Distribution(SETUP_ATTRS)
  152. dist.script_name = 'setup.py'
  153. mm = manifest_maker(dist)
  154. mm.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt')
  155. os.mkdir('sdist_test.egg-info')
  156. # UTF-8 filename
  157. filename = os.path.join(b('sdist_test'), b('smörbröd.py'))
  158. # Must touch the file or risk removal
  159. open(filename, "w").close()
  160. # Add filename and write manifest
  161. with quiet():
  162. mm.run()
  163. u_filename = filename.decode('utf-8')
  164. mm.filelist.files.append(u_filename)
  165. # Re-write manifest
  166. mm.write_manifest()
  167. contents = read_all_bytes(mm.manifest)
  168. # The manifest should be UTF-8 encoded
  169. contents.decode('UTF-8')
  170. # The manifest should contain the UTF-8 filename
  171. assert posix(filename) in contents
  172. # The filelist should have been updated as well
  173. assert u_filename in mm.filelist.files
  174. @py3_only
  175. def test_write_manifest_skips_non_utf8_filenames(self):
  176. """
  177. Files that cannot be encoded to UTF-8 (specifically, those that
  178. weren't originally successfully decoded and have surrogate
  179. escapes) should be omitted from the manifest.
  180. See https://bitbucket.org/tarek/distribute/issue/303 for history.
  181. """
  182. dist = Distribution(SETUP_ATTRS)
  183. dist.script_name = 'setup.py'
  184. mm = manifest_maker(dist)
  185. mm.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt')
  186. os.mkdir('sdist_test.egg-info')
  187. # Latin-1 filename
  188. filename = os.path.join(b('sdist_test'), LATIN1_FILENAME)
  189. # Add filename with surrogates and write manifest
  190. with quiet():
  191. mm.run()
  192. u_filename = filename.decode('utf-8', 'surrogateescape')
  193. mm.filelist.append(u_filename)
  194. # Re-write manifest
  195. mm.write_manifest()
  196. contents = read_all_bytes(mm.manifest)
  197. # The manifest should be UTF-8 encoded
  198. contents.decode('UTF-8')
  199. # The Latin-1 filename should have been skipped
  200. assert posix(filename) not in contents
  201. # The filelist should have been updated as well
  202. assert u_filename not in mm.filelist.files
  203. @fail_on_ascii
  204. def test_manifest_is_read_with_utf8_encoding(self):
  205. # Test for #303.
  206. dist = Distribution(SETUP_ATTRS)
  207. dist.script_name = 'setup.py'
  208. cmd = sdist(dist)
  209. cmd.ensure_finalized()
  210. # Create manifest
  211. with quiet():
  212. cmd.run()
  213. # Add UTF-8 filename to manifest
  214. filename = os.path.join(b('sdist_test'), b('smörbröd.py'))
  215. cmd.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt')
  216. manifest = open(cmd.manifest, 'ab')
  217. manifest.write(b('\n') + filename)
  218. manifest.close()
  219. # The file must exist to be included in the filelist
  220. open(filename, 'w').close()
  221. # Re-read manifest
  222. cmd.filelist.files = []
  223. with quiet():
  224. cmd.read_manifest()
  225. # The filelist should contain the UTF-8 filename
  226. if six.PY3:
  227. filename = filename.decode('utf-8')
  228. assert filename in cmd.filelist.files
  229. @py3_only
  230. def test_read_manifest_skips_non_utf8_filenames(self):
  231. # Test for #303.
  232. dist = Distribution(SETUP_ATTRS)
  233. dist.script_name = 'setup.py'
  234. cmd = sdist(dist)
  235. cmd.ensure_finalized()
  236. # Create manifest
  237. with quiet():
  238. cmd.run()
  239. # Add Latin-1 filename to manifest
  240. filename = os.path.join(b('sdist_test'), LATIN1_FILENAME)
  241. cmd.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt')
  242. manifest = open(cmd.manifest, 'ab')
  243. manifest.write(b('\n') + filename)
  244. manifest.close()
  245. # The file must exist to be included in the filelist
  246. open(filename, 'w').close()
  247. # Re-read manifest
  248. cmd.filelist.files = []
  249. with quiet():
  250. cmd.read_manifest()
  251. # The Latin-1 filename should have been skipped
  252. filename = filename.decode('latin-1')
  253. assert filename not in cmd.filelist.files
  254. @fail_on_ascii
  255. def test_sdist_with_utf8_encoded_filename(self):
  256. # Test for #303.
  257. dist = Distribution(SETUP_ATTRS)
  258. dist.script_name = 'setup.py'
  259. cmd = sdist(dist)
  260. cmd.ensure_finalized()
  261. # UTF-8 filename
  262. filename = os.path.join(b('sdist_test'), b('smörbröd.py'))
  263. open(filename, 'w').close()
  264. with quiet():
  265. cmd.run()
  266. if sys.platform == 'darwin':
  267. filename = decompose(filename)
  268. if six.PY3:
  269. fs_enc = sys.getfilesystemencoding()
  270. if sys.platform == 'win32':
  271. if fs_enc == 'cp1252':
  272. # Python 3 mangles the UTF-8 filename
  273. filename = filename.decode('cp1252')
  274. assert filename in cmd.filelist.files
  275. else:
  276. filename = filename.decode('mbcs')
  277. assert filename in cmd.filelist.files
  278. else:
  279. filename = filename.decode('utf-8')
  280. assert filename in cmd.filelist.files
  281. else:
  282. assert filename in cmd.filelist.files
  283. def test_sdist_with_latin1_encoded_filename(self):
  284. # Test for #303.
  285. dist = Distribution(SETUP_ATTRS)
  286. dist.script_name = 'setup.py'
  287. cmd = sdist(dist)
  288. cmd.ensure_finalized()
  289. # Latin-1 filename
  290. filename = os.path.join(b('sdist_test'), LATIN1_FILENAME)
  291. open(filename, 'w').close()
  292. assert os.path.isfile(filename)
  293. with quiet():
  294. cmd.run()
  295. if six.PY3:
  296. # not all windows systems have a default FS encoding of cp1252
  297. if sys.platform == 'win32':
  298. # Latin-1 is similar to Windows-1252 however
  299. # on mbcs filesys it is not in latin-1 encoding
  300. fs_enc = sys.getfilesystemencoding()
  301. if fs_enc == 'mbcs':
  302. filename = filename.decode('mbcs')
  303. else:
  304. filename = filename.decode('latin-1')
  305. assert filename in cmd.filelist.files
  306. else:
  307. # The Latin-1 filename should have been skipped
  308. filename = filename.decode('latin-1')
  309. filename not in cmd.filelist.files
  310. else:
  311. # Under Python 2 there seems to be no decoded string in the
  312. # filelist. However, due to decode and encoding of the
  313. # file name to get utf-8 Manifest the latin1 maybe excluded
  314. try:
  315. # fs_enc should match how one is expect the decoding to
  316. # be proformed for the manifest output.
  317. fs_enc = sys.getfilesystemencoding()
  318. filename.decode(fs_enc)
  319. assert filename in cmd.filelist.files
  320. except UnicodeDecodeError:
  321. filename not in cmd.filelist.files
  322. def test_default_revctrl():
  323. """
  324. When _default_revctrl was removed from the `setuptools.command.sdist`
  325. module in 10.0, it broke some systems which keep an old install of
  326. setuptools (Distribute) around. Those old versions require that the
  327. setuptools package continue to implement that interface, so this
  328. function provides that interface, stubbed. See #320 for details.
  329. This interface must be maintained until Ubuntu 12.04 is no longer
  330. supported (by Setuptools).
  331. """
  332. ep_def = 'svn_cvs = setuptools.command.sdist:_default_revctrl'
  333. ep = pkg_resources.EntryPoint.parse(ep_def)
  334. res = ep.resolve()
  335. assert hasattr(res, '__iter__')