def test_sdist_with_utf8_encoded_filename(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' cmd = sdist(dist) cmd.ensure_finalized() # UTF-8 filename filename = os.path.join(b('sdist_test'), b('smörbröd.py')) open(filename, 'w').close() quiet() try: cmd.run() finally: unquiet() if sys.platform == 'darwin': filename = decompose(filename) if sys.version_info >= (3,): if sys.platform == 'win32': # Python 3 mangles the UTF-8 filename filename = filename.decode('cp1252') self.assertTrue(filename in cmd.filelist.files) else: filename = filename.decode('utf-8') self.assertTrue(filename in cmd.filelist.files) else: self.assertTrue(filename in cmd.filelist.files)
def test_run_ok_custom_executable(self): """ Assert spawn is called with the right parameters """ from setuptools.dist import Distribution dist = Distribution( dict(name='foo', packages=['foo'], use_2to3=True, version='0.0', )) dist.script_name = 'setup.py' from build_commands import GulpCommand cmd = GulpCommand(dist) import tempfile cmd.instance_dir = tempfile.mkdtemp() gulpfile = tempfile.mkstemp(dir=cmd.instance_dir)[1] import os cmd.gulpfile = os.path.basename(gulpfile) cmd.executable = '/tmp/gulp' import mock with mock.patch('build_commands.gulp.find_executable') \ as find_executable: find_executable.return_value = '/tmp/gulp' cmd.finalize_options() spawn_mock = mock.MagicMock() cmd.spawn = spawn_mock import sys old_stdout = sys.stdout try: cmd.run() finally: sys.stdout = old_stdout expected = ['/tmp/gulp', 'build', '--base', cmd.instance_dir, '--gulpfile', gulpfile] spawn_mock.assert_called_once_with(expected)
def test_sdist_with_utf8_encoded_filename(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = "setup.py" cmd = sdist(dist) cmd.ensure_finalized() # UTF-8 filename filename = os.path.join(b("sdist_test"), b("smörbröd.py")) open(filename, "w").close() quiet() try: cmd.run() finally: unquiet() if sys.platform == "darwin": filename = decompose(filename) if sys.version_info >= (3,): if sys.platform == "win32": # Python 3 mangles the UTF-8 filename filename = filename.decode("cp1252") self.assertTrue(filename in cmd.filelist.files) else: filename = filename.decode("utf-8") self.assertTrue(filename in cmd.filelist.files) else: self.assertTrue(filename in cmd.filelist.files)
def test_sdist_with_latin1_encoded_filename(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' cmd = sdist(dist) cmd.ensure_finalized() # Latin-1 filename filename = os.path.join(b('sdist_test'), LATIN1_FILENAME) open(filename, 'w').close() quiet() try: cmd.run() finally: unquiet() if sys.version_info >= (3,): filename = filename.decode('latin-1') if sys.platform == 'win32': # Latin-1 is similar to Windows-1252 self.assertTrue(filename in cmd.filelist.files) else: # The Latin-1 filename should have been skipped self.assertFalse(filename in cmd.filelist.files) else: # No conversion takes place under Python 2 and the file # is included. We shall keep it that way for BBB. self.assertTrue(filename in cmd.filelist.files)
def test_read_manifest_skips_non_utf8_filenames(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' cmd = sdist(dist) cmd.ensure_finalized() # Create manifest with quiet(): cmd.run() # Add Latin-1 filename to manifest filename = os.path.join(b'sdist_test', Filenames.latin_1) cmd.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') manifest = open(cmd.manifest, 'ab') manifest.write(b'\n' + filename) manifest.close() # The file must exist to be included in the filelist open(filename, 'w').close() # Re-read manifest cmd.filelist.files = [] with quiet(): cmd.read_manifest() # The Latin-1 filename should have been skipped filename = filename.decode('latin-1') assert filename not in cmd.filelist.files
def test_manifest_is_written_with_utf8_encoding(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' mm = manifest_maker(dist) mm.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') os.mkdir('sdist_test.egg-info') # UTF-8 filename filename = os.path.join('sdist_test', 'smörbröd.py') # Add UTF-8 filename and write manifest quiet() try: mm.run() mm.filelist.files.append(filename) mm.write_manifest() finally: unquiet() manifest = open(mm.manifest, 'rbU') contents = manifest.read() manifest.close() # The manifest should be UTF-8 encoded try: u_contents = contents.decode('UTF-8') except UnicodeDecodeError, e: self.fail(e)
def test_write_manifest_skips_non_utf8_filenames(self): """ Files that cannot be encoded to UTF-8 (specifically, those that weren't originally successfully decoded and have surrogate escapes) should be omitted from the manifest. See https://bitbucket.org/tarek/distribute/issue/303 for history. """ dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' mm = manifest_maker(dist) mm.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') os.mkdir('sdist_test.egg-info') # Latin-1 filename filename = os.path.join(b'sdist_test', Filenames.latin_1) # Add filename with surrogates and write manifest with quiet(): mm.run() u_filename = filename.decode('utf-8', 'surrogateescape') mm.filelist.append(u_filename) # Re-write manifest mm.write_manifest() contents = read_all_bytes(mm.manifest) # The manifest should be UTF-8 encoded contents.decode('UTF-8') # The Latin-1 filename should have been skipped assert posix(filename) not in contents # The filelist should have been updated as well assert u_filename not in mm.filelist.files
def test_manifest_is_written_with_surrogateescape_error_handler(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' mm = manifest_maker(dist) mm.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') os.mkdir('sdist_test.egg-info') # Latin-1 filename filename = posixpath.join(b('sdist_test'), LATIN1_FILENAME) # Add filename with surrogates and write manifest quiet() try: mm.run() if sys.version_info >= (3,): u = filename.decode('utf-8', 'surrogateescape') mm.filelist.files.append(u) else: mm.filelist.files.append(filename) mm.write_manifest() finally: unquiet() manifest = open(mm.manifest, 'rbU') contents = manifest.read() manifest.close() # The manifest should contain the Latin-1 filename self.assertTrue(filename in contents)
def test_run_ok(self): """ Assert spawn is called with the right parameters """ from setuptools.dist import Distribution dist = Distribution( dict(name='foo', packages=['foo'], use_2to3=True, version='0.0', )) dist.script_name = 'setup.py' from build_commands import BowerCommand cmd = BowerCommand(dist) import tempfile cmd.instance_dir = tempfile.mkdtemp() import mock with mock.patch('build_commands.bower.find_executable') \ as find_executable: find_executable.return_value = '/tmp/bower' cmd.finalize_options() spawn_mock = mock.MagicMock() cmd.spawn = spawn_mock import sys old_stdout = sys.stdout try: cmd.run() finally: sys.stdout = old_stdout expected = ['bower', 'install', cmd.instance_dir] spawn_mock.assert_called_once_with(expected)
def test_local_index(self): # make sure the local index is used # when easy_install looks for installed # packages new_location = tempfile.mkdtemp() target = tempfile.mkdtemp() egg_file = os.path.join(new_location, 'foo-1.0.egg-info') f = open(egg_file, 'w') try: f.write('Name: foo\n') except: f.close() sys.path.append(target) old_ppath = os.environ.get('PYTHONPATH') os.environ['PYTHONPATH'] = ':'.join(sys.path) try: dist = Distribution() dist.script_name = 'setup.py' cmd = easy_install(dist) cmd.install_dir = target cmd.args = ['foo'] cmd.ensure_finalized() cmd.local_index.scan([new_location]) res = cmd.easy_install('foo') self.assertEquals(res.location, new_location) finally: sys.path.remove(target) shutil.rmtree(new_location) shutil.rmtree(target) if old_ppath is not None: os.environ['PYTHONPATH'] = old_ppath else: del os.environ['PYTHONPATH']
def test_write_manifest_skips_non_utf8_filenames(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' mm = manifest_maker(dist) mm.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') os.mkdir('sdist_test.egg-info') # Latin-1 filename filename = os.path.join(b('sdist_test'), LATIN1_FILENAME) # Add filename with surrogates and write manifest quiet() try: mm.run() u_filename = filename.decode('utf-8', 'surrogateescape') mm.filelist.files.append(u_filename) # Re-write manifest mm.write_manifest() finally: unquiet() manifest = open(mm.manifest, 'rbU') contents = manifest.read() manifest.close() # The manifest should be UTF-8 encoded try: contents.decode('UTF-8') except UnicodeDecodeError, e: self.fail(e)
def test_read_manifest_skips_non_utf8_filenames(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = "setup.py" cmd = sdist(dist) cmd.ensure_finalized() # Create manifest with quiet(): cmd.run() # Add Latin-1 filename to manifest filename = os.path.join(b("sdist_test"), LATIN1_FILENAME) cmd.manifest = os.path.join("sdist_test.egg-info", "SOURCES.txt") manifest = open(cmd.manifest, "ab") manifest.write(b("\n") + filename) manifest.close() # The file must exist to be included in the filelist open(filename, "w").close() # Re-read manifest cmd.filelist.files = [] with quiet(): cmd.read_manifest() # The Latin-1 filename should have been skipped filename = filename.decode("latin-1") assert filename not in cmd.filelist.files
def test_sdist_with_utf8_encoded_filename(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = "setup.py" cmd = sdist(dist) cmd.ensure_finalized() # UTF-8 filename filename = os.path.join(b("sdist_test"), b("smörbröd.py")) open(filename, "w").close() with quiet(): cmd.run() if sys.platform == "darwin": filename = decompose(filename) if six.PY3: fs_enc = sys.getfilesystemencoding() if sys.platform == "win32": if fs_enc == "cp1252": # Python 3 mangles the UTF-8 filename filename = filename.decode("cp1252") assert filename in cmd.filelist.files else: filename = filename.decode("mbcs") assert filename in cmd.filelist.files else: filename = filename.decode("utf-8") assert filename in cmd.filelist.files else: assert filename in cmd.filelist.files
def test_sdist_with_utf8_encoded_filename(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' cmd = sdist(dist) cmd.ensure_finalized() filename = os.path.join(b'sdist_test', Filenames.utf_8) open(filename, 'w').close() with quiet(): cmd.run() if sys.platform == 'darwin': filename = decompose(filename) if six.PY3: fs_enc = sys.getfilesystemencoding() if sys.platform == 'win32': if fs_enc == 'cp1252': # Python 3 mangles the UTF-8 filename filename = filename.decode('cp1252') assert filename in cmd.filelist.files else: filename = filename.decode('mbcs') assert filename in cmd.filelist.files else: filename = filename.decode('utf-8') assert filename in cmd.filelist.files else: assert filename in cmd.filelist.files
def test_manifest_is_read_with_utf8_encoding(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = "setup.py" cmd = sdist(dist) cmd.ensure_finalized() # Create manifest with quiet(): cmd.run() # Add UTF-8 filename to manifest filename = os.path.join(b("sdist_test"), b("smörbröd.py")) cmd.manifest = os.path.join("sdist_test.egg-info", "SOURCES.txt") manifest = open(cmd.manifest, "ab") manifest.write(b("\n") + filename) manifest.close() # The file must exist to be included in the filelist open(filename, "w").close() # Re-read manifest cmd.filelist.files = [] with quiet(): cmd.read_manifest() # The filelist should contain the UTF-8 filename if six.PY3: filename = filename.decode("utf-8") assert filename in cmd.filelist.files
def test_develop(self): if sys.version < "2.6" or hasattr(sys, 'real_prefix'): return dist = Distribution( dict(name='foo', packages=['foo'], use_2to3=True, version='0.0', )) dist.script_name = 'setup.py' cmd = develop(dist) cmd.user = 1 cmd.ensure_finalized() cmd.install_dir = site.USER_SITE cmd.user = 1 old_stdout = sys.stdout #sys.stdout = StringIO() try: cmd.run() finally: sys.stdout = old_stdout # let's see if we got our egg link at the right place content = os.listdir(site.USER_SITE) content.sort() self.assertEqual(content, ['easy-install.pth', 'foo.egg-link']) # Check that we are using the right code. path = open(os.path.join(site.USER_SITE, 'foo.egg-link'), 'rt').read().split()[0].strip() init = open(os.path.join(path, 'foo', '__init__.py'), 'rt').read().strip() if sys.version < "3": self.assertEqual(init, 'print "foo"') else: self.assertEqual(init, 'print("foo")')
def test_read_manifest_skips_non_utf8_filenames(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' cmd = sdist(dist) cmd.ensure_finalized() # Create manifest with quiet(): cmd.run() # Add Latin-1 filename to manifest filename = os.path.join(b('sdist_test'), LATIN1_FILENAME) cmd.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') manifest = open(cmd.manifest, 'ab') manifest.write(b('\n') + filename) manifest.close() # The file must exist to be included in the filelist open(filename, 'w').close() # Re-read manifest cmd.filelist.files = [] with quiet(): try: cmd.read_manifest() except UnicodeDecodeError: e = sys.exc_info()[1] self.fail(e) # The Latin-1 filename should have been skipped filename = filename.decode('latin-1') self.assertFalse(filename in cmd.filelist.files)
def test_manifest_is_read_with_surrogateescape_error_handler(self): # Test for #303. # This is hard to test on HFS Plus because it quotes unknown # bytes (see previous test). Furthermore, egg_info.FileList # only appends filenames that os.path.exist. # We therefore write the manifest file by hand and check whether # read_manifest produces a UnicodeDecodeError. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' cmd = sdist(dist) cmd.ensure_finalized() filename = os.path.join(b('sdist_test'), LATIN1_FILENAME) quiet() try: cmd.run() # Add Latin-1 filename to manifest cmd.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') manifest = open(cmd.manifest, 'ab') manifest.write(filename+b('\n')) manifest.close() # Re-read manifest try: cmd.read_manifest() except UnicodeDecodeError, e: self.fail(e) finally: unquiet()
def test_2to3_user_mode(self, test_env): settings = dict( name='foo', packages=['foo'], use_2to3=True, version='0.0', ) dist = Distribution(settings) dist.script_name = 'setup.py' cmd = develop(dist) cmd.user = 1 cmd.ensure_finalized() cmd.install_dir = site.USER_SITE cmd.user = 1 with contexts.quiet(): cmd.run() # let's see if we got our egg link at the right place content = os.listdir(site.USER_SITE) content.sort() assert content == ['easy-install.pth', 'foo.egg-link'] # Check that we are using the right code. fn = os.path.join(site.USER_SITE, 'foo.egg-link') with io.open(fn) as egg_link_file: path = egg_link_file.read().split()[0].strip() fn = os.path.join(path, 'foo', '__init__.py') with io.open(fn) as init_file: init = init_file.read().strip() expected = 'print("foo")' if six.PY3 else 'print "foo"' assert init == expected
def test_manifest_is_read_with_utf8_encoding(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' cmd = sdist(dist) cmd.ensure_finalized() # Create manifest with quiet(): cmd.run() # Add UTF-8 filename to manifest filename = os.path.join(b'sdist_test', Filenames.utf_8) cmd.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') manifest = open(cmd.manifest, 'ab') manifest.write(b'\n' + filename) manifest.close() # The file must exist to be included in the filelist open(filename, 'w').close() # Re-read manifest cmd.filelist.files = [] with quiet(): cmd.read_manifest() # The filelist should contain the UTF-8 filename if six.PY3: filename = filename.decode('utf-8') assert filename in cmd.filelist.files
def __init__(self, *attrs): Distribution.__init__(self, *attrs) self.cmdclass['install'] = uWSGIInstall self.cmdclass['install_lib'] = uWSGIInstallLib self.cmdclass['build_ext'] = uWSGIBuilder if HAS_WHEEL: self.cmdclass['bdist_wheel'] = uWSGIWheel
def test_manifest_is_written_with_utf8_encoding(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' mm = manifest_maker(dist) mm.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') os.mkdir('sdist_test.egg-info') # UTF-8 filename filename = os.path.join('sdist_test', 'smörbröd.py') # Must create the file or it will get stripped. open(filename, 'w').close() # Add UTF-8 filename and write manifest with quiet(): mm.run() mm.filelist.append(filename) mm.write_manifest() contents = read_all_bytes(mm.manifest) # The manifest should be UTF-8 encoded u_contents = contents.decode('UTF-8') # The manifest should contain the UTF-8 filename if six.PY2: fs_enc = sys.getfilesystemencoding() filename = filename.decode(fs_enc) assert posix(filename) in u_contents
def test_defaults_case_sensitivity(self): """ Make sure default files (README.*, etc.) are added in a case-sensitive way to avoid problems with packages built on Windows. """ open(os.path.join(self.temp_dir, 'readme.rst'), 'w').close() open(os.path.join(self.temp_dir, 'SETUP.cfg'), 'w').close() dist = Distribution(SETUP_ATTRS) # the extension deliberately capitalized for this test # to make sure the actual filename (not capitalized) gets added # to the manifest dist.script_name = 'setup.PY' cmd = sdist(dist) cmd.ensure_finalized() with quiet(): cmd.run() # lowercase all names so we can test in a # case-insensitive way to make sure the files # are not included. manifest = map(lambda x: x.lower(), cmd.filelist.files) assert 'readme.rst' not in manifest, manifest assert 'setup.py' not in manifest, manifest assert 'setup.cfg' not in manifest, manifest
def test_write_manifest_allows_utf8_filenames(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' mm = manifest_maker(dist) mm.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') os.mkdir('sdist_test.egg-info') filename = os.path.join(b'sdist_test', Filenames.utf_8) # Must touch the file or risk removal open(filename, "w").close() # Add filename and write manifest with quiet(): mm.run() u_filename = filename.decode('utf-8') mm.filelist.files.append(u_filename) # Re-write manifest mm.write_manifest() contents = read_all_bytes(mm.manifest) # The manifest should be UTF-8 encoded contents.decode('UTF-8') # The manifest should contain the UTF-8 filename assert posix(filename) in contents # The filelist should have been updated as well assert u_filename in mm.filelist.files
def test_tests_are_run_once(capfd): params = dict( name='foo', packages=['dummy'], ) with open('setup.py', 'wt') as f: f.write('from setuptools import setup; setup(\n') for k, v in sorted(params.items()): f.write(' %s=%r,\n' % (k, v)) f.write(')\n') os.makedirs('dummy') with open('dummy/__init__.py', 'wt'): pass with open('dummy/test_dummy.py', 'wt') as f: f.write(DALS( """ from __future__ import print_function import unittest class TestTest(unittest.TestCase): def test_test(self): print('Foo') """)) dist = Distribution(params) dist.script_name = 'setup.py' cmd = test(dist) cmd.ensure_finalized() # The test runner calls sys.exit with contexts.suppress_exceptions(SystemExit): cmd.run() out, err = capfd.readouterr() assert out == 'Foo\n'
def test_test(self): if sys.version < "2.6" or hasattr(sys, 'real_prefix'): return dist = Distribution(dict( name='foo', packages=['name', 'name.space', 'name.space.tests'], namespace_packages=['name'], test_suite='name.space.tests.test_suite', use_2to3=True, )) dist.script_name = 'setup.py' cmd = test(dist) cmd.user = 1 cmd.ensure_finalized() cmd.install_dir = site.USER_SITE cmd.user = 1 old_stdout = sys.stdout sys.stdout = StringIO() try: try: # try/except/finally doesn't work in Python 2.4, so we need nested try-statements. cmd.run() except SystemExit: # The test runner calls sys.exit, stop that making an error. pass finally: sys.stdout = old_stdout
def test_manifest_is_written_with_utf8_encoding(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = "setup.py" mm = manifest_maker(dist) mm.manifest = os.path.join("sdist_test.egg-info", "SOURCES.txt") os.mkdir("sdist_test.egg-info") # UTF-8 filename filename = os.path.join("sdist_test", "smörbröd.py") # Add UTF-8 filename and write manifest quiet() try: mm.run() mm.filelist.files.append(filename) mm.write_manifest() finally: unquiet() manifest = open(mm.manifest, "rbU") contents = manifest.read() manifest.close() # The manifest should be UTF-8 encoded try: u_contents = contents.decode("UTF-8") except UnicodeDecodeError as e: self.fail(e) # The manifest should contain the UTF-8 filename if sys.version_info >= (3,): self.assertTrue(posix(filename) in u_contents) else: self.assertTrue(posix(filename) in contents)
def assert_not_user_site(): # create a finalized easy_install command dist = Distribution() dist.script_name = 'setup.py' cmd = ei.easy_install(dist) cmd.args = ['py'] cmd.ensure_finalized() assert not cmd.user, 'user should not be implied'
def get_dist(tmpdir, kwargs_initial=None, parse=True): kwargs_initial = kwargs_initial or {} with tmpdir.as_cwd(): dist = Distribution(kwargs_initial) dist.script_name = 'setup.py' parse and dist.parse_config_files() yield dist
def test_user_install_not_implied_without_usersite_enabled(self): site.ENABLE_USER_SITE = False # usually enabled #XXX: replace with something meaningfull dist = Distribution() dist.script_name = 'setup.py' cmd = easy_install(dist) cmd.args = ['py'] cmd.initialize_options() assert not cmd.user, 'NOT user should be implied'
def test_unicode_filename_in_sdist(self, sdist_unicode, tmpdir, monkeypatch): """ The install command should execute correctly even if the package has unicode filenames. """ dist = Distribution({'script_args': ['easy_install']}) target = (tmpdir / 'target').ensure_dir() cmd = ei.easy_install( dist, install_dir=str(target), args=['x'], ) monkeypatch.setitem(os.environ, 'PYTHONPATH', str(target)) cmd.ensure_finalized() cmd.easy_install(sdist_unicode)
def test_console_scripts(self, tmpdir): """ Test that console scripts are installed and that they reference only the project by name and not the current version. """ pytest.skip( "TODO: needs a fixture to cause 'develop' " "to be invoked without mutating environment.") settings = dict( name='foo', packages=['foo'], version='0.0', entry_points={ 'console_scripts': [ 'foocmd = foo:foo', ], }, ) dist = Distribution(settings) dist.script_name = 'setup.py' cmd = develop(dist) cmd.ensure_finalized() cmd.install_dir = tmpdir cmd.run()
def test_build(self): dist = Distribution() uut = BuildDbusService(dist) self.assertRaises(DistutilsOptionError, uut.finalize_options) handle, uut.output = tempfile.mkstemp(text=True) uut.finalize_options() uut.run() result = os.read(handle, 1000).decode() self.assertEqual( result, "[D-BUS Service]\nNames=" + Constants.BUS_NAME + "\nExec=coala-dbus")
def test_sdist_with_latin1_encoded_filename(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' cmd = sdist(dist) cmd.ensure_finalized() # Latin-1 filename filename = os.path.join(b('sdist_test'), LATIN1_FILENAME) open(filename, 'w').close() quiet() try: cmd.run() finally: unquiet() # The filelist should contain the Latin-1 filename # (in one representation or other) if sys.platform == 'darwin': filename = hfs_quote(filename) elif sys.version_info >= (3,): filename = filename.decode(sys.getfilesystemencoding(), 'surrogateescape') self.assertTrue(filename in cmd.filelist.files)
def test_manifest_is_read_with_utf8_encoding(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' cmd = sdist(dist) cmd.ensure_finalized() # Create manifest quiet() try: cmd.run() finally: unquiet() # Add UTF-8 filename to manifest filename = os.path.join(b('sdist_test'), b('smörbröd.py')) cmd.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') manifest = open(cmd.manifest, 'ab') manifest.write(b('\n') + filename) manifest.close() # The file must exist to be included in the filelist open(filename, 'w').close() # Re-read manifest cmd.filelist.files = [] quiet() try: cmd.read_manifest() finally: unquiet() # The filelist should contain the UTF-8 filename if sys.version_info >= (3, ): filename = filename.decode('utf-8') self.assertTrue(filename in cmd.filelist.files)
def test_sdist_with_utf8_encoded_filename(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' cmd = sdist(dist) cmd.ensure_finalized() # UTF-8 filename filename = os.path.join(b('sdist_test'), b('smörbröd.py')) open(filename, 'w').close() quiet() try: cmd.run() finally: unquiet() if sys.platform == 'darwin': filename = decompose(filename) if sys.version_info >= (3, ): fs_enc = sys.getfilesystemencoding() if sys.platform == 'win32': if fs_enc == 'cp1252': # Python 3 mangles the UTF-8 filename filename = filename.decode('cp1252') self.assertTrue(filename in cmd.filelist.files) else: filename = filename.decode('mbcs') self.assertTrue(filename in cmd.filelist.files) else: filename = filename.decode('utf-8') self.assertTrue(filename in cmd.filelist.files) else: self.assertTrue(filename in cmd.filelist.files)
def read_configuration(filepath: _Path, find_others=False, ignore_option_errors=False) -> dict: """Read given configuration file and returns options from it as a dict. :param str|unicode filepath: Path to configuration file to get options from. :param bool find_others: Whether to search for other configuration files which could be on in various places. :param bool ignore_option_errors: Whether to silently ignore options, values of which could not be resolved (e.g. due to exceptions in directives such as file:, attr:, etc.). If False exceptions are propagated as expected. :rtype: dict """ from setuptools.dist import Distribution dist = Distribution() filenames = dist.find_config_files() if find_others else [] handlers = _apply(dist, filepath, filenames, ignore_option_errors) return configuration_to_dict(handlers)
def test_manifest_is_written_with_utf8_encoding(self): # Test for #303. dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' mm = manifest_maker(dist) mm.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') os.mkdir('sdist_test.egg-info') # UTF-8 filename filename = os.path.join('sdist_test', 'smörbröd.py') # Add UTF-8 filename and write manifest quiet() try: mm.run() mm.filelist.files.append(filename) mm.write_manifest() finally: unquiet() manifest = open(mm.manifest, 'rbU') contents = manifest.read() manifest.close() # The manifest should be UTF-8 encoded try: u_contents = contents.decode('UTF-8') except UnicodeDecodeError: e = sys.exc_info()[1] self.fail(e) # The manifest should contain the UTF-8 filename if sys.version_info >= (3, ): self.assertTrue(posix(filename) in u_contents) else: self.assertTrue(posix(filename) in contents)
def test_build(self): dist = Distribution() uut = BuildDbusService(dist) self.assertRaises(DistutilsOptionError, uut.finalize_options) with make_temp() as uut.output: uut.finalize_options() uut.run() with open(uut.output) as file: result = file.read(1000) self.assertEqual( result, "[D-BUS Service]\nNames=" + Constants.BUS_NAME + "\nExec=coala-dbus")
def notest_develop_with_setup_requires(self): wanted = ("Could not find suitable distribution for " "Requirement.parse('I-DONT-EXIST')") old_dir = os.getcwd() os.chdir(self.dir) try: try: dist = Distribution({'setup_requires': ['I_DONT_EXIST']}) except DistutilsError, e: error = str(e) if error == wanted: pass finally: os.chdir(old_dir)
def test_write_manifest_skips_non_utf8_filenames(self): """ Files that cannot be encoded to UTF-8 (specifically, those that weren't originally successfully decoded and have surrogate escapes) should be omitted from the manifest. See https://bitbucket.org/tarek/distribute/issue/303 for history. """ dist = Distribution(SETUP_ATTRS) dist.script_name = 'setup.py' mm = manifest_maker(dist) mm.manifest = os.path.join('sdist_test.egg-info', 'SOURCES.txt') os.mkdir('sdist_test.egg-info') # Latin-1 filename filename = os.path.join(b('sdist_test'), LATIN1_FILENAME) # Add filename with surrogates and write manifest with quiet(): mm.run() u_filename = filename.decode('utf-8', 'surrogateescape') mm.filelist.append(u_filename) # Re-write manifest mm.write_manifest() manifest = open(mm.manifest, 'rbU') contents = manifest.read() manifest.close() # The manifest should be UTF-8 encoded contents.decode('UTF-8') # The Latin-1 filename should have been skipped assert posix(filename) not in contents # The filelist should have been updated as well assert u_filename not in mm.filelist.files
def test_win32Definition(self): """ When building on Windows NT, the WIN32 macro will be defined as 1 on the extensions. """ ext = ConditionalExtension( "whatever", ["whatever.c"], define_macros=[("whatever", 2)] ) args = getSetupArgs(extensions=[ext], readme=None) builder = args["cmdclass"]["build_ext"](Distribution()) self.patch(os, "name", "nt") builder.prepare_extensions() self.assertEqual(ext.define_macros, [("whatever", 2), ("WIN32", 1)])
def testCompiler(): if not HAVE_SETUPTOOLS: # silent, we can't test without setuptools return True bitmsghash = Extension( 'bitmsghash', sources=['src/bitmsghash/bitmsghash.cpp'], libraries=['pthread', 'crypto'], ) dist = Distribution() dist.ext_modules = [bitmsghash] cmd = build_ext(dist) cmd.initialize_options() cmd.finalize_options() cmd.force = True try: cmd.run() except CompileError: return False else: fullPath = os.path.join(cmd.build_lib, cmd.get_ext_filename("bitmsghash")) return os.path.isfile(fullPath)
def test_custom_build_py(self): """ Ensure projects defining custom build_py don't break when creating sdists (issue #2849) """ from distutils.command.build_py import build_py as OrigBuildPy using_custom_command_guard = mock.Mock() class CustomBuildPy(OrigBuildPy): """ Some projects have custom commands inheriting from `distutils` """ def get_data_files(self): using_custom_command_guard() return super().get_data_files() setup_attrs = {**SETUP_ATTRS, 'include_package_data': True} assert setup_attrs['package_data'] dist = Distribution(setup_attrs) dist.script_name = 'setup.py' cmd = sdist(dist) cmd.ensure_finalized() # Make sure we use the custom command cmd.cmdclass = {'build_py': CustomBuildPy} cmd.distribution.cmdclass = {'build_py': CustomBuildPy} assert cmd.distribution.get_command_class('build_py') == CustomBuildPy msg = "setuptools instead of distutils" with quiet(), pytest.warns(SetuptoolsDeprecationWarning, match=msg): cmd.run() using_custom_command_guard.assert_called() self.assert_package_data_in_manifest(cmd)
def test_warns_deprecation_when_raising(self): dist = Distribution() cmd = register(dist) cmd.run_command = mock.Mock() cmd.send_metadata = mock.Mock() cmd.send_metadata.side_effect = Exception cmd.announce = mock.Mock() with pytest.raises(Exception): cmd.run() cmd.announce.assert_called_once_with( "WARNING: Registering is deprecated, use twine to upload instead " "(https://pypi.org/p/twine/)", log.WARN)
def test_create_zipfile(self): # Test to make sure zipfile creation handles common cases. # This explicitly includes a folder containing an empty folder. dist = Distribution() cmd = upload_docs(dist) cmd.upload_dir = self.upload_dir zip_file = cmd.create_zipfile() assert zipfile.is_zipfile(zip_file) zip_f = zipfile.ZipFile(zip_file) # woh... assert zip_f.namelist() == ['index.html']
def test_script_install(self, sdist_script, tmpdir, monkeypatch): """ Check scripts are installed. """ dist = Distribution({'script_args': ['easy_install']}) target = (tmpdir / 'target').ensure_dir() cmd = ei.easy_install( dist, install_dir=str(target), args=['x'], ) monkeypatch.setitem(os.environ, 'PYTHONPATH', str(target)) cmd.ensure_finalized() cmd.easy_install(sdist_script) assert (target / 'mypkg_script').exists()
def test_wrap_installers(): called = False def func(): nonlocal called called = True cmd_class = wrap_installers(pre_dist=func, pre_develop=func, post_dist=func, post_develop=func) for name in ['pre_dist', 'pre_develop', 'post_dist', 'post_develop']: cmd_class[name](Distribution()).run() assert called called = False
def get_prefix(): """ Get prefix from either config file or command line :return: str prefix install path """ dist = Distribution() dist.parse_config_files() dist.parse_command_line() try: user_prefix = dist.get_option_dict('install')['prefix'][1] except KeyError: user_prefix = prefix return user_prefix
def test_copy_file(): with ExitStack() as stack: stack.enter_context(patched_cwd()) makedirs_mock = stack.enter_context(patch("os.makedirs")) stack.enter_context(patch("os.path.exists", return_value=False)) copy_mock = stack.enter_context(patch("shutil.copyfile")) dist = Distribution({'name': 'acme.foo'}) cmd = test_egg.test_egg(dist) src = "<src>" dest = "<dest>" cmd._copy_file(src, dest) makedirs_mock.assert_called_once_with(CWD.return_value) copy_mock.assert_called_once_with(src, dest)
def test__wrap_command(): called = False def func(self, cmd): nonlocal called called = True class TestCommand(pkg.BaseCommand): def run(self): pass cmd = pkg._wrap_command(['js'], TestCommand) cmd.run_command = func dist = Distribution() cmd(dist).run() assert called == True
def rust_extensions(dist: Distribution, attr: Literal["rust_extensions"], value: List[RustExtension]) -> None: assert attr == "rust_extensions" has_rust_extensions = len(value) > 0 # Monkey patch has_ext_modules to include Rust extensions; pairs with # patch_distutils_build above. # # has_ext_modules is missing from Distribution typing. orig_has_ext_modules = dist.has_ext_modules # type: ignore[attr-defined] dist.has_ext_modules = lambda: (orig_has_ext_modules( ) or has_rust_extensions) # type: ignore[attr-defined] if has_rust_extensions: patch_distutils_build() add_rust_extension(dist)
def test_build_libraries(self, mock_newer): dist = Distribution() cmd = build_clib(dist) # this will be a long section, just making sure all # exceptions are properly raised libs = [('example', {'sources': 'broken.c'})] with pytest.raises(DistutilsSetupError): cmd.build_libraries(libs) obj_deps = 'some_string' libs = [('example', {'sources': ['source.c'], 'obj_deps': obj_deps})] with pytest.raises(DistutilsSetupError): cmd.build_libraries(libs) obj_deps = {'': ''} libs = [('example', {'sources': ['source.c'], 'obj_deps': obj_deps})] with pytest.raises(DistutilsSetupError): cmd.build_libraries(libs) obj_deps = {'source.c': ''} libs = [('example', {'sources': ['source.c'], 'obj_deps': obj_deps})] with pytest.raises(DistutilsSetupError): cmd.build_libraries(libs) # with that out of the way, let's see if the crude dependency # system works cmd.compiler = mock.MagicMock(spec=cmd.compiler) mock_newer.return_value = ([], []) obj_deps = {'': ('global.h', ), 'example.c': ('example.h', )} libs = [('example', {'sources': ['example.c'], 'obj_deps': obj_deps})] cmd.build_libraries(libs) assert [['example.c', 'global.h', 'example.h']] in mock_newer.call_args[0] assert not cmd.compiler.compile.called assert cmd.compiler.create_static_lib.call_count == 1 # reset the call numbers so we can test again cmd.compiler.reset_mock() mock_newer.return_value = '' # anything as long as it's not ([],[]) cmd.build_libraries(libs) assert cmd.compiler.compile.call_count == 1 assert cmd.compiler.create_static_lib.call_count == 1
def run_commands(self): # pip does not resolve dependencies before building binaries, so unless # packages are installed one-by-one, on old install is used or the build # will simply fail hard. The following is not completely quiet, but at # least a lot less conspicuous. if not is_manylinux(): disabled = set( ('bdist_wheel', 'bdist_egg', 'bdist_wininst', 'bdist_rpm')) for cmd in self.commands: if not cmd in disabled: self.run_command(cmd) else: log.info('Command "%s" is disabled', cmd) cmd_obj = self.get_command_obj(cmd) cmd_obj.get_outputs = lambda: None else: return Distribution.run_commands(self)
def test_view(self): stub_mod_call(self, cli) tmpdir = mkdtemp(self) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['npm', '--view'], name='foo', )) dist.parse_command_line() dist.run_commands() self.assertFalse(exists(join(tmpdir, 'package.json'))) # also log handlers removed. self.assertEqual(len(getLogger('calmjs.cli').handlers), 0) # written to stdout with the correct indentation level. self.assertIn('\n "jquery": "~1.11.0"', sys.stdout.getvalue())
def test_bdist_wininst_warning(distutils_cmd): dist = Distribution(dict( script_name='setup.py', script_args=['bdist_wininst'], name='foo', py_modules=['hi'], )) dist.parse_command_line() with pytest.warns(SetuptoolsDeprecationWarning): dist.run_commands() distutils_cmd.run.assert_called_once()
def pytest_sessionstart(session): """ Build test executables from C/C++ packages """ # build the test executables CMakeTestExtensionBuilder().run() # build the actual modules, so that they can be loaded by python builder = setup.CMakeBuild( Distribution({ 'ext_modules': setup.CMakeExtensionBuilder.extensions, })) # we call finalize_options and set inplace to 1 to set the output directory # to the main package directory builder.finalize_options() builder.inplace = 1 builder.run()
def make_command(CommandClass): dist = Distribution() cmd = CommandClass(dist) for long_opt, short_opt, help_text in cmd.user_options: if long_opt.endswith('='): action = 'store' default = None else: action = 'store_true' default = False long_opt = '--{}'.format(long_opt.rstrip('=')) if short_opt: short_opt = '-{}'.format(short_opt) opts = filter(None, [short_opt, long_opt]) yield (opts, help_text, action, default)
def environ(self, setup_cfg=None, *args, **variables): args = ['green'] + list(args) if setup_cfg is not None: parser = ConfigParser() parser.add_section('green') for k, v in setup_cfg.items(): parser.set('green', k, str(v)) with open('setup.cfg', 'w') as f: parser.write(f) yield Distribution({'script_name': 'setup.py', 'script_args': args or ['green']}) #finally: if os.path.isfile('setup.cfg'): os.remove('setup.cfg')
def test_create_zipfile(self): """ Ensure zipfile creation handles common cases, including a folder containing an empty folder. """ dist = Distribution() cmd = upload_docs(dist) cmd.target_dir = cmd.upload_dir = 'build' with contexts.tempdir() as tmp_dir: tmp_file = os.path.join(tmp_dir, 'foo.zip') zip_file = cmd.create_zipfile(tmp_file) assert zipfile.is_zipfile(tmp_file) with contextlib.closing(zipfile.ZipFile(tmp_file)) as zip_file: assert zip_file.namelist() == ['index.html']