summaryrefslogtreecommitdiff
path: root/distutils2/tests/test_database.py
diff options
context:
space:
mode:
author?ric Araujo <merwok@netwok.org>2011-09-24 01:06:28 +0200
committer?ric Araujo <merwok@netwok.org>2011-09-24 01:06:28 +0200
commitd04e4c8c1027310520576e1890061799a0f64f0b (patch)
tree50a302c46be60dc998f981feaac252f04a0b43e9 /distutils2/tests/test_database.py
parentd126c30223b0fabb5f96fe5d2a4eefd701284780 (diff)
downloaddisutils2-d04e4c8c1027310520576e1890061799a0f64f0b.tar.gz
Start a branch to provide Distutils2 for Python 3.
This codebase is compatible with 3.1, 3.2 and 3.3. It was converted with 2to3 and a semi-automated diff/merge with packaging in 3.3 to fix some idioms. We?ve now come full circle from 2.x to 3.x to 2.x to 3.x again :) Starting from now, contributors can make patches for packaging (preferred, as the stdlib?s regrtest is very useful), distutils2 or distutils-python3, and we?ll make patches flow between versions.
Diffstat (limited to 'distutils2/tests/test_database.py')
-rw-r--r--distutils2/tests/test_database.py63
1 files changed, 19 insertions, 44 deletions
diff --git a/distutils2/tests/test_database.py b/distutils2/tests/test_database.py
index e524306..02c72da 100644
--- a/distutils2/tests/test_database.py
+++ b/distutils2/tests/test_database.py
@@ -1,12 +1,10 @@
import os
+import io
import csv
import sys
import shutil
import tempfile
-try:
- from hashlib import md5
-except ImportError:
- from distutils2._backport.hashlib import md5
+from hashlib import md5
from textwrap import dedent
from distutils2.tests.test_util import GlobTestCaseBase
@@ -28,11 +26,8 @@ from distutils2.database import (
def get_hexdigest(filename):
- fp = open(filename, 'rb')
- try:
- checksum = md5(fp.read())
- finally:
- fp.close()
+ with open(filename, 'rb') as file:
+ checksum = md5(file.read())
return checksum.hexdigest()
@@ -43,7 +38,7 @@ def record_pieces(path):
return path, digest, size
-class FakeDistsMixin(object):
+class FakeDistsMixin:
def setUp(self):
super(FakeDistsMixin, self).setUp()
@@ -65,11 +60,11 @@ class FakeDistsMixin(object):
# shutil gives no control over the mode of directories :(
# see http://bugs.python.org/issue1666318
for root, dirs, files in os.walk(self.fake_dists_path):
- os.chmod(root, 0755)
+ os.chmod(root, 0o755)
for f in files:
- os.chmod(os.path.join(root, f), 0644)
+ os.chmod(os.path.join(root, f), 0o644)
for d in dirs:
- os.chmod(os.path.join(root, d), 0755)
+ os.chmod(os.path.join(root, d), 0o755)
class CommonDistributionTests(FakeDistsMixin):
@@ -138,10 +133,9 @@ class TestDistribution(CommonDistributionTests, unittest.TestCase):
for distinfo_dir in self.dirs:
record_file = os.path.join(distinfo_dir, 'RECORD')
- fp = open(record_file, 'w')
- try:
+ with open(record_file, 'w') as file:
record_writer = csv.writer(
- fp, delimiter=',', quoting=csv.QUOTE_NONE,
+ file, delimiter=',', quoting=csv.QUOTE_NONE,
lineterminator='\n')
dist_location = distinfo_dir.replace('.dist-info', '')
@@ -152,12 +146,9 @@ class TestDistribution(CommonDistributionTests, unittest.TestCase):
for file in ('INSTALLER', 'METADATA', 'REQUESTED'):
record_writer.writerow(record_pieces((distinfo_dir, file)))
record_writer.writerow([record_file])
- finally:
- fp.close()
- fp = open(record_file)
- try:
- record_reader = csv.reader(fp, lineterminator='\n')
+ with open(record_file) as file:
+ record_reader = csv.reader(file, lineterminator='\n')
record_data = {}
for row in record_reader:
if row == []:
@@ -165,8 +156,6 @@ class TestDistribution(CommonDistributionTests, unittest.TestCase):
path, md5_, size = (row[:] +
[None for i in range(len(row), 3)])
record_data[path] = md5_, size
- finally:
- fp.close()
self.records[distinfo_dir] = record_data
def test_instantiation(self):
@@ -210,14 +199,11 @@ class TestDistribution(CommonDistributionTests, unittest.TestCase):
]
for distfile in distinfo_files:
- value = dist.get_distinfo_file(distfile)
- try:
- self.assertIsInstance(value, file)
+ with dist.get_distinfo_file(distfile) as value:
+ self.assertIsInstance(value, io.TextIOWrapper)
# Is it the correct file?
self.assertEqual(value.name,
os.path.join(distinfo_dir, distfile))
- finally:
- value.close()
# Test an absolute path that is part of another distributions dist-info
other_distinfo_file = os.path.join(
@@ -640,8 +626,7 @@ class DataFilesTestCase(GlobTestCaseBase):
metadata_path = os.path.join(dist_info, 'METADATA')
resources_path = os.path.join(dist_info, 'RESOURCES')
- fp = open(metadata_path, 'w')
- try:
+ with open(metadata_path, 'w') as fp:
fp.write(dedent("""\
Metadata-Version: 1.2
Name: test
@@ -649,25 +634,18 @@ class DataFilesTestCase(GlobTestCaseBase):
Summary: test
Author: me
"""))
- finally:
- fp.close()
+
test_path = 'test.cfg'
fd, test_resource_path = tempfile.mkstemp()
os.close(fd)
self.addCleanup(os.remove, test_resource_path)
- fp = open(test_resource_path, 'w')
- try:
+ with open(test_resource_path, 'w') as fp:
fp.write('Config')
- finally:
- fp.close()
- fp = open(resources_path, 'w')
- try:
+ with open(resources_path, 'w') as fp:
fp.write('%s,%s' % (test_path, test_resource_path))
- finally:
- fp.close()
# Add fake site-packages to sys.path to retrieve fake dist
self.addCleanup(sys.path.remove, temp_site_packages)
@@ -682,11 +660,8 @@ class DataFilesTestCase(GlobTestCaseBase):
test_resource_path)
self.assertRaises(KeyError, get_file_path, dist_name, 'i-dont-exist')
- fp = get_file(dist_name, test_path)
- try:
+ with get_file(dist_name, test_path) as fp:
self.assertEqual(fp.read(), 'Config')
- finally:
- fp.close()
self.assertRaises(KeyError, get_file, dist_name, 'i-dont-exist')