mirror of
https://github.com/python/cpython.git
synced 2025-09-18 22:50:26 +00:00
use with blocks to make sure files are closed
This commit is contained in:
parent
f24600eb64
commit
3d678e3b12
1 changed files with 12 additions and 18 deletions
|
@ -14,13 +14,11 @@ class FileCompareTestCase(unittest.TestCase):
|
||||||
self.name_diff = support.TESTFN + '-diff'
|
self.name_diff = support.TESTFN + '-diff'
|
||||||
data = 'Contents of file go here.\n'
|
data = 'Contents of file go here.\n'
|
||||||
for name in [self.name, self.name_same, self.name_diff]:
|
for name in [self.name, self.name_same, self.name_diff]:
|
||||||
output = open(name, 'w')
|
with open(name, 'w') as output:
|
||||||
output.write(data)
|
output.write(data)
|
||||||
output.close()
|
|
||||||
|
|
||||||
output = open(self.name_diff, 'a+')
|
with open(self.name_diff, 'a+') as output:
|
||||||
output.write('An extra line.\n')
|
output.write('An extra line.\n')
|
||||||
output.close()
|
|
||||||
self.dir = tempfile.gettempdir()
|
self.dir = tempfile.gettempdir()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
@ -71,13 +69,11 @@ class DirCompareTestCase(unittest.TestCase):
|
||||||
fn = 'FiLe' # Verify case-insensitive comparison
|
fn = 'FiLe' # Verify case-insensitive comparison
|
||||||
else:
|
else:
|
||||||
fn = 'file'
|
fn = 'file'
|
||||||
output = open(os.path.join(dir, fn), 'w')
|
with open(os.path.join(dir, fn), 'w') as output:
|
||||||
output.write(data)
|
output.write(data)
|
||||||
output.close()
|
|
||||||
|
|
||||||
output = open(os.path.join(self.dir_diff, 'file2'), 'w')
|
with open(os.path.join(self.dir_diff, 'file2'), 'w') as output:
|
||||||
output.write('An extra file.\n')
|
output.write('An extra file.\n')
|
||||||
output.close()
|
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
for dir in (self.dir, self.dir_same, self.dir_diff):
|
for dir in (self.dir, self.dir_same, self.dir_diff):
|
||||||
|
@ -104,9 +100,8 @@ class DirCompareTestCase(unittest.TestCase):
|
||||||
"Comparing directory to same fails")
|
"Comparing directory to same fails")
|
||||||
|
|
||||||
# Add different file2
|
# Add different file2
|
||||||
output = open(os.path.join(self.dir, 'file2'), 'w')
|
with open(os.path.join(self.dir, 'file2'), 'w') as output:
|
||||||
output.write('Different contents.\n')
|
output.write('Different contents.\n')
|
||||||
output.close()
|
|
||||||
|
|
||||||
self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same,
|
self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same,
|
||||||
['file', 'file2']) ==
|
['file', 'file2']) ==
|
||||||
|
@ -178,9 +173,8 @@ class DirCompareTestCase(unittest.TestCase):
|
||||||
self._assert_report(d.report, expected_report)
|
self._assert_report(d.report, expected_report)
|
||||||
|
|
||||||
# Add different file2
|
# Add different file2
|
||||||
output = open(os.path.join(self.dir_diff, 'file2'), 'w')
|
with open(os.path.join(self.dir_diff, 'file2'), 'w') as output:
|
||||||
output.write('Different contents.\n')
|
output.write('Different contents.\n')
|
||||||
output.close()
|
|
||||||
d = filecmp.dircmp(self.dir, self.dir_diff)
|
d = filecmp.dircmp(self.dir, self.dir_diff)
|
||||||
self.assertEqual(d.same_files, ['file'])
|
self.assertEqual(d.same_files, ['file'])
|
||||||
self.assertEqual(d.diff_files, ['file2'])
|
self.assertEqual(d.diff_files, ['file2'])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue