Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 548c054

Browse files
committed
Stop trying to write into the stdlib during lib2to3 tests (#12331).
This prevents tests from failing when run from a Python installed in a read-only directory.
1 parent ab7c1b3 commit 548c054

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

Lib/lib2to3/tests/test_refactor.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,26 @@ def print_output(self, old_text, new_text, filename, equal):
177177
self.assertEqual(results, expected)
178178

179179
def check_file_refactoring(self, test_file, fixers=_2TO3_FIXERS):
180+
tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor")
181+
self.addCleanup(shutil.rmtree, tmpdir)
182+
# make a copy of the tested file that we can write to
183+
shutil.copy(test_file, tmpdir)
184+
test_file = os.path.join(tmpdir, os.path.basename(test_file))
185+
os.chmod(test_file, 0o644)
186+
180187
def read_file():
181188
with open(test_file, "rb") as fp:
182189
return fp.read()
190+
183191
old_contents = read_file()
184192
rt = self.rt(fixers=fixers)
185193

186194
rt.refactor_file(test_file)
187195
self.assertEqual(old_contents, read_file())
188196

189-
try:
190-
rt.refactor_file(test_file, True)
191-
new_contents = read_file()
192-
self.assertNotEqual(old_contents, new_contents)
193-
finally:
194-
with open(test_file, "wb") as fp:
195-
fp.write(old_contents)
197+
rt.refactor_file(test_file, True)
198+
new_contents = read_file()
199+
self.assertNotEqual(old_contents, new_contents)
196200
return new_contents
197201

198202
def test_refactor_file(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ Tools/Demos
140140
Tests
141141
-----
142142

143+
- Issue #12331: The test suite for lib2to3 can now run from an installed
144+
Python.
145+
143146
- Issue #12626: In regrtest, allow to filter tests using a glob filter
144147
with the ``-m`` (or ``--match``) option. This works with all test cases
145148
using the unittest module. This is useful with long test suites

0 commit comments

Comments
 (0)