test_util.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. """Tests for distutils.util."""
  2. import email
  3. import email.generator
  4. import email.policy
  5. import io
  6. import os
  7. import pathlib
  8. import sys
  9. import sysconfig as stdlib_sysconfig
  10. import unittest.mock as mock
  11. from copy import copy
  12. from distutils import sysconfig, util
  13. from distutils.errors import DistutilsByteCompileError, DistutilsPlatformError
  14. from distutils.util import (
  15. byte_compile,
  16. change_root,
  17. check_environ,
  18. convert_path,
  19. get_host_platform,
  20. get_platform,
  21. grok_environment_error,
  22. rfc822_escape,
  23. split_quoted,
  24. strtobool,
  25. )
  26. import pytest
  27. @pytest.fixture(autouse=True)
  28. def environment(monkeypatch):
  29. monkeypatch.setattr(os, 'name', os.name)
  30. monkeypatch.setattr(sys, 'platform', sys.platform)
  31. monkeypatch.setattr(sys, 'version', sys.version)
  32. monkeypatch.setattr(os, 'sep', os.sep)
  33. monkeypatch.setattr(os.path, 'join', os.path.join)
  34. monkeypatch.setattr(os.path, 'isabs', os.path.isabs)
  35. monkeypatch.setattr(os.path, 'splitdrive', os.path.splitdrive)
  36. monkeypatch.setattr(sysconfig, '_config_vars', copy(sysconfig._config_vars))
  37. @pytest.mark.usefixtures('save_env')
  38. class TestUtil:
  39. def test_get_host_platform(self):
  40. with mock.patch('os.name', 'nt'):
  41. with mock.patch('sys.version', '... [... (ARM64)]'):
  42. assert get_host_platform() == 'win-arm64'
  43. with mock.patch('sys.version', '... [... (ARM)]'):
  44. assert get_host_platform() == 'win-arm32'
  45. with mock.patch('sys.version_info', (3, 9, 0, 'final', 0)):
  46. assert get_host_platform() == stdlib_sysconfig.get_platform()
  47. def test_get_platform(self):
  48. with mock.patch('os.name', 'nt'):
  49. with mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'x86'}):
  50. assert get_platform() == 'win32'
  51. with mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'x64'}):
  52. assert get_platform() == 'win-amd64'
  53. with mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'arm'}):
  54. assert get_platform() == 'win-arm32'
  55. with mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'arm64'}):
  56. assert get_platform() == 'win-arm64'
  57. def test_convert_path(self):
  58. expected = os.sep.join(('', 'home', 'to', 'my', 'stuff'))
  59. assert convert_path('/home/to/my/stuff') == expected
  60. assert convert_path(pathlib.Path('/home/to/my/stuff')) == expected
  61. assert convert_path('.') == os.curdir
  62. def test_change_root(self):
  63. # linux/mac
  64. os.name = 'posix'
  65. def _isabs(path):
  66. return path[0] == '/'
  67. os.path.isabs = _isabs
  68. def _join(*path):
  69. return '/'.join(path)
  70. os.path.join = _join
  71. assert change_root('/root', '/old/its/here') == '/root/old/its/here'
  72. assert change_root('/root', 'its/here') == '/root/its/here'
  73. # windows
  74. os.name = 'nt'
  75. os.sep = '\\'
  76. def _isabs(path):
  77. return path.startswith('c:\\')
  78. os.path.isabs = _isabs
  79. def _splitdrive(path):
  80. if path.startswith('c:'):
  81. return ('', path.replace('c:', ''))
  82. return ('', path)
  83. os.path.splitdrive = _splitdrive
  84. def _join(*path):
  85. return '\\'.join(path)
  86. os.path.join = _join
  87. assert (
  88. change_root('c:\\root', 'c:\\old\\its\\here') == 'c:\\root\\old\\its\\here'
  89. )
  90. assert change_root('c:\\root', 'its\\here') == 'c:\\root\\its\\here'
  91. # BugsBunny os (it's a great os)
  92. os.name = 'BugsBunny'
  93. with pytest.raises(DistutilsPlatformError):
  94. change_root('c:\\root', 'its\\here')
  95. # XXX platforms to be covered: mac
  96. def test_check_environ(self):
  97. util.check_environ.cache_clear()
  98. os.environ.pop('HOME', None)
  99. check_environ()
  100. assert os.environ['PLAT'] == get_platform()
  101. @pytest.mark.skipif("os.name != 'posix'")
  102. def test_check_environ_getpwuid(self):
  103. util.check_environ.cache_clear()
  104. os.environ.pop('HOME', None)
  105. import pwd
  106. # only set pw_dir field, other fields are not used
  107. result = pwd.struct_passwd((
  108. None,
  109. None,
  110. None,
  111. None,
  112. None,
  113. '/home/distutils',
  114. None,
  115. ))
  116. with mock.patch.object(pwd, 'getpwuid', return_value=result):
  117. check_environ()
  118. assert os.environ['HOME'] == '/home/distutils'
  119. util.check_environ.cache_clear()
  120. os.environ.pop('HOME', None)
  121. # bpo-10496: Catch pwd.getpwuid() error
  122. with mock.patch.object(pwd, 'getpwuid', side_effect=KeyError):
  123. check_environ()
  124. assert 'HOME' not in os.environ
  125. def test_split_quoted(self):
  126. assert split_quoted('""one"" "two" \'three\' \\four') == [
  127. 'one',
  128. 'two',
  129. 'three',
  130. 'four',
  131. ]
  132. def test_strtobool(self):
  133. yes = ('y', 'Y', 'yes', 'True', 't', 'true', 'True', 'On', 'on', '1')
  134. no = ('n', 'no', 'f', 'false', 'off', '0', 'Off', 'No', 'N')
  135. for y in yes:
  136. assert strtobool(y)
  137. for n in no:
  138. assert not strtobool(n)
  139. indent = 8 * ' '
  140. @pytest.mark.parametrize(
  141. "given,wanted",
  142. [
  143. # 0x0b, 0x0c, ..., etc are also considered a line break by Python
  144. ("hello\x0b\nworld\n", f"hello\x0b{indent}\n{indent}world\n{indent}"),
  145. ("hello\x1eworld", f"hello\x1e{indent}world"),
  146. ("", ""),
  147. (
  148. "I am a\npoor\nlonesome\nheader\n",
  149. f"I am a\n{indent}poor\n{indent}lonesome\n{indent}header\n{indent}",
  150. ),
  151. ],
  152. )
  153. def test_rfc822_escape(self, given, wanted):
  154. """
  155. We want to ensure a multi-line header parses correctly.
  156. For interoperability, the escaped value should also "round-trip" over
  157. `email.generator.Generator.flatten` and `email.message_from_*`
  158. (see pypa/setuptools#4033).
  159. The main issue is that internally `email.policy.EmailPolicy` uses
  160. `splitlines` which will split on some control chars. If all the new lines
  161. are not prefixed with spaces, the parser will interrupt reading
  162. the current header and produce an incomplete value, while
  163. incorrectly interpreting the rest of the headers as part of the payload.
  164. """
  165. res = rfc822_escape(given)
  166. policy = email.policy.EmailPolicy(
  167. utf8=True,
  168. mangle_from_=False,
  169. max_line_length=0,
  170. )
  171. with io.StringIO() as buffer:
  172. raw = f"header: {res}\nother-header: 42\n\npayload\n"
  173. orig = email.message_from_string(raw)
  174. email.generator.Generator(buffer, policy=policy).flatten(orig)
  175. buffer.seek(0)
  176. regen = email.message_from_file(buffer)
  177. for msg in (orig, regen):
  178. assert msg.get_payload() == "payload\n"
  179. assert msg["other-header"] == "42"
  180. # Generator may replace control chars with `\n`
  181. assert set(msg["header"].splitlines()) == set(res.splitlines())
  182. assert res == wanted
  183. def test_dont_write_bytecode(self):
  184. # makes sure byte_compile raise a DistutilsError
  185. # if sys.dont_write_bytecode is True
  186. old_dont_write_bytecode = sys.dont_write_bytecode
  187. sys.dont_write_bytecode = True
  188. try:
  189. with pytest.raises(DistutilsByteCompileError):
  190. byte_compile([])
  191. finally:
  192. sys.dont_write_bytecode = old_dont_write_bytecode
  193. def test_grok_environment_error(self):
  194. # test obsolete function to ensure backward compat (#4931)
  195. exc = OSError("Unable to find batch file")
  196. msg = grok_environment_error(exc)
  197. assert msg == "error: Unable to find batch file"