|
8 | 8 |
|
9 | 9 | import pytest |
10 | 10 |
|
11 | | -from . import py38compat as os_helper |
12 | 11 | from distutils.dist import Distribution |
13 | 12 |
|
14 | 13 | # setup script that uses __file__ |
@@ -61,65 +60,63 @@ def save_stdout(monkeypatch): |
61 | 60 | monkeypatch.setattr(sys, 'stdout', sys.stdout) |
62 | 61 |
|
63 | 62 |
|
| 63 | +@pytest.fixture |
| 64 | +def temp_file(tmp_path): |
| 65 | + return tmp_path / 'file' |
| 66 | + |
| 67 | + |
64 | 68 | @pytest.mark.usefixtures('save_env') |
65 | 69 | @pytest.mark.usefixtures('save_argv') |
66 | | -@pytest.mark.usefixtures('cleanup_testfn') |
67 | 70 | class TestCore: |
68 | | - def write_setup(self, text, path=os_helper.TESTFN): |
69 | | - f = open(path, "w") |
70 | | - try: |
71 | | - f.write(text) |
72 | | - finally: |
73 | | - f.close() |
74 | | - return path |
75 | | - |
76 | | - def test_run_setup_provides_file(self): |
| 71 | + def test_run_setup_provides_file(self, temp_file): |
77 | 72 | # Make sure the script can use __file__; if that's missing, the test |
78 | 73 | # setup.py script will raise NameError. |
79 | | - distutils.core.run_setup(self.write_setup(setup_using___file__)) |
| 74 | + temp_file.write_text(setup_using___file__) |
| 75 | + distutils.core.run_setup(temp_file) |
80 | 76 |
|
81 | | - def test_run_setup_preserves_sys_argv(self): |
| 77 | + def test_run_setup_preserves_sys_argv(self, temp_file): |
82 | 78 | # Make sure run_setup does not clobber sys.argv |
83 | 79 | argv_copy = sys.argv.copy() |
84 | | - distutils.core.run_setup(self.write_setup(setup_does_nothing)) |
| 80 | + temp_file.write_text(setup_does_nothing) |
| 81 | + distutils.core.run_setup(temp_file) |
85 | 82 | assert sys.argv == argv_copy |
86 | 83 |
|
87 | | - def test_run_setup_defines_subclass(self): |
| 84 | + def test_run_setup_defines_subclass(self, temp_file): |
88 | 85 | # Make sure the script can use __file__; if that's missing, the test |
89 | 86 | # setup.py script will raise NameError. |
90 | | - dist = distutils.core.run_setup(self.write_setup(setup_defines_subclass)) |
| 87 | + temp_file.write_text(setup_defines_subclass) |
| 88 | + dist = distutils.core.run_setup(temp_file) |
91 | 89 | install = dist.get_command_obj('install') |
92 | 90 | assert 'cmd' in install.sub_commands |
93 | 91 |
|
94 | | - def test_run_setup_uses_current_dir(self): |
95 | | - # This tests that the setup script is run with the current directory |
96 | | - # as its own current directory; this was temporarily broken by a |
97 | | - # previous patch when TESTFN did not use the current directory. |
| 92 | + def test_run_setup_uses_current_dir(self, tmp_path): |
| 93 | + """ |
| 94 | + Test that the setup script is run with the current directory |
| 95 | + as its own current directory. |
| 96 | + """ |
98 | 97 | sys.stdout = io.StringIO() |
99 | 98 | cwd = os.getcwd() |
100 | 99 |
|
101 | 100 | # Create a directory and write the setup.py file there: |
102 | | - os.mkdir(os_helper.TESTFN) |
103 | | - setup_py = os.path.join(os_helper.TESTFN, "setup.py") |
104 | | - distutils.core.run_setup(self.write_setup(setup_prints_cwd, path=setup_py)) |
| 101 | + setup_py = tmp_path / 'setup.py' |
| 102 | + setup_py.write_text(setup_prints_cwd) |
| 103 | + distutils.core.run_setup(setup_py) |
105 | 104 |
|
106 | 105 | output = sys.stdout.getvalue() |
107 | 106 | if output.endswith("\n"): |
108 | 107 | output = output[:-1] |
109 | 108 | assert cwd == output |
110 | 109 |
|
111 | | - def test_run_setup_within_if_main(self): |
112 | | - dist = distutils.core.run_setup( |
113 | | - self.write_setup(setup_within_if_main), stop_after="config" |
114 | | - ) |
| 110 | + def test_run_setup_within_if_main(self, temp_file): |
| 111 | + temp_file.write_text(setup_within_if_main) |
| 112 | + dist = distutils.core.run_setup(temp_file, stop_after="config") |
115 | 113 | assert isinstance(dist, Distribution) |
116 | 114 | assert dist.get_name() == "setup_within_if_main" |
117 | 115 |
|
118 | | - def test_run_commands(self): |
| 116 | + def test_run_commands(self, temp_file): |
119 | 117 | sys.argv = ['setup.py', 'build'] |
120 | | - dist = distutils.core.run_setup( |
121 | | - self.write_setup(setup_within_if_main), stop_after="commandline" |
122 | | - ) |
| 118 | + temp_file.write_text(setup_within_if_main) |
| 119 | + dist = distutils.core.run_setup(temp_file, stop_after="commandline") |
123 | 120 | assert 'build' not in dist.have_run |
124 | 121 | distutils.core.run_commands(dist) |
125 | 122 | assert 'build' in dist.have_run |
|
0 commit comments