codekingpro/portable-devtools
114k
1"""Tests for distutils.core."""2 3import distutils.core4import io5import os6import sys7from distutils.dist import Distribution8 9import pytest10 11# setup script that uses __file__12setup_using___file__ = """\13 14__file__15 16from distutils.core import setup17setup()18"""19 20setup_prints_cwd = """\21 22import os23print(os.getcwd())24 25from distutils.core import setup26setup()27"""28 29setup_does_nothing = """\30from distutils.core import setup31setup()32"""33 34 35setup_defines_subclass = """\36from distutils.core import setup37from distutils.command.install import install as _install38 39class install(_install):40 sub_commands = _install.sub_commands + ['cmd']41 42setup(cmdclass={'install': install})43"""44 45setup_within_if_main = """\46from distutils.core import setup47 48def main():49 return setup(name="setup_within_if_main")50 51if __name__ == "__main__":52 main()53"""54 55 56@pytest.fixture(autouse=True)57def save_stdout(monkeypatch):58 monkeypatch.setattr(sys, 'stdout', sys.stdout)59 60 61@pytest.fixture62def temp_file(tmp_path):63 return tmp_path / 'file'64 65 66@pytest.mark.usefixtures('save_env')67@pytest.mark.usefixtures('save_argv')68class TestCore:69 def test_run_setup_provides_file(self, temp_file):70 # Make sure the script can use __file__; if that's missing, the test71 # setup.py script will raise NameError.72 temp_file.write_text(setup_using___file__, encoding='utf-8')73 distutils.core.run_setup(temp_file)74 75 def test_run_setup_preserves_sys_argv(self, temp_file):76 # Make sure run_setup does not clobber sys.argv77 argv_copy = sys.argv.copy()78 temp_file.write_text(setup_does_nothing, encoding='utf-8')79 distutils.core.run_setup(temp_file)80 assert sys.argv == argv_copy81 82 def test_run_setup_defines_subclass(self, temp_file):83 # Make sure the script can use __file__; if that's missing, the test84 # setup.py script will raise NameError.85 temp_file.write_text(setup_defines_subclass, encoding='utf-8')86 dist = distutils.core.run_setup(temp_file)87 install = dist.get_command_obj('install')88 assert 'cmd' in install.sub_commands89 90 def test_run_setup_uses_current_dir(self, tmp_path):91 """92 Test that the setup script is run with the current directory93 as its own current directory.94 """95 sys.stdout = io.StringIO()96 cwd = os.getcwd()97 98 # Create a directory and write the setup.py file there:99 setup_py = tmp_path / 'setup.py'100 setup_py.write_text(setup_prints_cwd, encoding='utf-8')101 distutils.core.run_setup(setup_py)102 103 output = sys.stdout.getvalue()104 if output.endswith("\n"):105 output = output[:-1]106 assert cwd == output107 108 def test_run_setup_within_if_main(self, temp_file):109 temp_file.write_text(setup_within_if_main, encoding='utf-8')110 dist = distutils.core.run_setup(temp_file, stop_after="config")111 assert isinstance(dist, Distribution)112 assert dist.get_name() == "setup_within_if_main"113 114 def test_run_commands(self, temp_file):115 sys.argv = ['setup.py', 'build']116 temp_file.write_text(setup_within_if_main, encoding='utf-8')117 dist = distutils.core.run_setup(temp_file, stop_after="commandline")118 assert 'build' not in dist.have_run119 distutils.core.run_commands(dist)120 assert 'build' in dist.have_run121 122 def test_debug_mode(self, capsys, monkeypatch):123 # this covers the code called when DEBUG is set124 sys.argv = ['setup.py', '--name']125 distutils.core.setup(name='bar')126 assert capsys.readouterr().out == 'bar\n'127 monkeypatch.setattr(distutils.core, 'DEBUG', True)128 distutils.core.setup(name='bar')129 wanted = "options (after parsing config files):\n"130 assert capsys.readouterr().out.startswith(wanted)131 