summaryrefslogtreecommitdiff
path: root/distutils2/tests/test_metadata.py
diff options
context:
space:
mode:
Diffstat (limited to 'distutils2/tests/test_metadata.py')
-rw-r--r--distutils2/tests/test_metadata.py64
1 files changed, 21 insertions, 43 deletions
diff --git a/distutils2/tests/test_metadata.py b/distutils2/tests/test_metadata.py
index 600d5ea..96ea73b 100644
--- a/distutils2/tests/test_metadata.py
+++ b/distutils2/tests/test_metadata.py
@@ -1,10 +1,8 @@
-# encoding: utf-8
"""Tests for distutils2.metadata."""
import os
import sys
-import codecs
from textwrap import dedent
-from StringIO import StringIO
+from io import StringIO
from distutils2.errors import (MetadataConflictError, MetadataMissingError,
MetadataUnrecognizedVersionError)
@@ -36,12 +34,8 @@ class MetadataTestCase(LoggingCatcher,
def test_instantiation(self):
PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO')
- f = codecs.open(PKG_INFO, 'r', encoding='utf-8')
- try:
+ with open(PKG_INFO, 'r', encoding='utf-8') as f:
contents = f.read()
- finally:
- f.close()
-
fp = StringIO(contents)
m = Metadata()
@@ -69,11 +63,8 @@ class MetadataTestCase(LoggingCatcher,
def test_metadata_markers(self):
# see if we can be platform-aware
PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO')
- f = codecs.open(PKG_INFO, 'r', encoding='utf-8')
- try:
+ with open(PKG_INFO, 'r', encoding='utf-8') as f:
content = f.read() % sys.platform
- finally:
- f.close()
metadata = Metadata(platform_dependent=True)
metadata.read_file(StringIO(content))
@@ -91,11 +82,8 @@ class MetadataTestCase(LoggingCatcher,
def test_mapping_api(self):
PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO')
- f = codecs.open(PKG_INFO, 'r', encoding='utf-8')
- try:
+ with open(PKG_INFO, 'r', encoding='utf-8') as f:
content = f.read() % sys.platform
- finally:
- f.close()
metadata = Metadata(fileobj=StringIO(content))
self.assertIn('Version', metadata.keys())
self.assertIn('0.5', metadata.values())
@@ -110,6 +98,8 @@ class MetadataTestCase(LoggingCatcher,
metadata.update({'version': '1--2'})
self.assertEqual(len(self.get_logs()), 1)
+ # XXX caveat: the keys method and friends are not 3.x-style views
+ # should be changed or documented
self.assertEqual(list(metadata), metadata.keys())
def test_read_metadata(self):
@@ -142,21 +132,18 @@ class MetadataTestCase(LoggingCatcher,
tmp_dir = self.mkdtemp()
my_file = os.path.join(tmp_dir, 'f')
- metadata = Metadata(mapping={'author': u'Mister Café',
- 'name': u'my.project',
- 'author': u'Café Junior',
- 'summary': u'Café torréfié',
- 'description': u'Héhéhé',
- 'keywords': [u'café', u'coffee']})
+ metadata = Metadata(mapping={'author': 'Mister Café',
+ 'name': 'my.project',
+ 'author': 'Café Junior',
+ 'summary': 'Café torréfié',
+ 'description': 'Héhéhé',
+ 'keywords': ['café', 'coffee']})
metadata.write(my_file)
# the file should use UTF-8
metadata2 = Metadata()
- fp = codecs.open(my_file, encoding='utf-8')
- try:
+ with open(my_file, encoding='utf-8') as fp:
metadata2.read_file(fp)
- finally:
- fp.close()
# XXX when keywords are not defined, metadata will have
# 'Keywords': [] but metadata2 will have 'Keywords': ['']
@@ -164,19 +151,16 @@ class MetadataTestCase(LoggingCatcher,
self.assertEqual(metadata.items(), metadata2.items())
# ASCII also works, it's a subset of UTF-8
- metadata = Metadata(mapping={'author': u'Mister Cafe',
- 'name': u'my.project',
- 'author': u'Cafe Junior',
- 'summary': u'Cafe torrefie',
- 'description': u'Hehehe'})
+ metadata = Metadata(mapping={'author': 'Mister Cafe',
+ 'name': 'my.project',
+ 'author': 'Cafe Junior',
+ 'summary': 'Cafe torrefie',
+ 'description': 'Hehehe'})
metadata.write(my_file)
metadata2 = Metadata()
- fp = codecs.open(my_file, encoding='utf-8')
- try:
+ with open(my_file, encoding='utf-8') as fp:
metadata2.read_file(fp)
- finally:
- fp.close()
def test_metadata_read_write(self):
PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO')
@@ -335,21 +319,15 @@ class MetadataTestCase(LoggingCatcher,
def test_description(self):
PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO')
- f = codecs.open(PKG_INFO, 'r', encoding='utf-8')
- try:
+ with open(PKG_INFO, 'r', encoding='utf-8') as f:
content = f.read() % sys.platform
- finally:
- f.close()
metadata = Metadata()
metadata.read_file(StringIO(content))
# see if we can read the description now
DESC = os.path.join(os.path.dirname(__file__), 'LONG_DESC.txt')
- f = open(DESC)
- try:
+ with open(DESC) as f:
wanted = f.read()
- finally:
- f.close()
self.assertEqual(wanted, metadata['Description'])
# save the file somewhere and make sure we can read it back