From fbed44e617669ef028291db599c083d68207671c Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Mon, 23 Jan 2017 18:36:23 -0800 Subject: [PATCH] Fix all "ResourceWarning: unclosed file ..." warnings during tests --- src/icalendar/tests/apple_xlocation_test.py | 3 ++- src/icalendar/tests/test_encoding.py | 3 ++- src/icalendar/tests/test_multiple.py | 7 +++---- src/icalendar/tests/test_recurrence.py | 6 +++--- src/icalendar/tests/test_timezoned.py | 18 +++++++++--------- src/icalendar/tests/test_unit_cal.py | 3 ++- 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/icalendar/tests/apple_xlocation_test.py b/src/icalendar/tests/apple_xlocation_test.py index a58e510..12c0abb 100644 --- a/src/icalendar/tests/apple_xlocation_test.py +++ b/src/icalendar/tests/apple_xlocation_test.py @@ -14,7 +14,8 @@ class TestEncoding(unittest.TestCase): """ try: directory = os.path.dirname(__file__) - data = open(os.path.join(directory, 'x_location.ics'), 'rb').read() + with open(os.path.join(directory, 'x_location.ics'), 'rb') as fp: + data = fp.read() cal = icalendar.Calendar.from_ical(data) for event in cal.walk('vevent'): self.assertEqual(len(event.errors), 1, 'Got too many errors') diff --git a/src/icalendar/tests/test_encoding.py b/src/icalendar/tests/test_encoding.py index 4e2408d..ed9fe45 100644 --- a/src/icalendar/tests/test_encoding.py +++ b/src/icalendar/tests/test_encoding.py @@ -11,7 +11,8 @@ class TestEncoding(unittest.TestCase): def test_create_from_ical(self): directory = os.path.dirname(__file__) - data = open(os.path.join(directory, 'encoding.ics'), 'rb').read() + with open(os.path.join(directory, 'encoding.ics'), 'rb') as fp: + data = fp.read() cal = icalendar.Calendar.from_ical(data) self.assertEqual(cal['prodid'].to_ical().decode('utf-8'), diff --git a/src/icalendar/tests/test_multiple.py b/src/icalendar/tests/test_multiple.py index 05b133d..07dfe26 100644 --- a/src/icalendar/tests/test_multiple.py +++ b/src/icalendar/tests/test_multiple.py @@ -12,10 +12,9 @@ class TestMultiple(unittest.TestCase): def test_multiple(self): directory = os.path.dirname(__file__) - cals = Calendar.from_ical( - open(os.path.join(directory, 'multiple.ics'), 'rb').read(), - multiple=True - ) + with open(os.path.join(directory, 'multiple.ics'), 'rb') as fp: + data = fp.read() + cals = Calendar.from_ical(data, multiple=True) self.assertEqual(len(cals), 2) self.assertSequenceEqual([comp.name for comp in cals[0].walk()], diff --git a/src/icalendar/tests/test_recurrence.py b/src/icalendar/tests/test_recurrence.py index 8ea43ae..6f0c617 100644 --- a/src/icalendar/tests/test_recurrence.py +++ b/src/icalendar/tests/test_recurrence.py @@ -12,9 +12,9 @@ class TestRecurrence(unittest.TestCase): def setUp(self): directory = os.path.dirname(__file__) - self.cal = icalendar.Calendar.from_ical( - open(os.path.join(directory, 'recurrence.ics'), 'rb').read() - ) + with open(os.path.join(directory, 'recurrence.ics'), 'rb') as fp: + data = fp.read() + self.cal = icalendar.Calendar.from_ical(data) def test_recurrence_exdates_one_line(self): first_event = self.cal.walk('vevent')[0] diff --git a/src/icalendar/tests/test_timezoned.py b/src/icalendar/tests/test_timezoned.py index 1bb7a92..a24402b 100644 --- a/src/icalendar/tests/test_timezoned.py +++ b/src/icalendar/tests/test_timezoned.py @@ -12,9 +12,9 @@ class TestTimezoned(unittest.TestCase): def test_create_from_ical(self): directory = os.path.dirname(__file__) - cal = icalendar.Calendar.from_ical( - open(os.path.join(directory, 'timezoned.ics'), 'rb').read() - ) + with open(os.path.join(directory, 'timezoned.ics'), 'rb') as fp: + data = fp.read() + cal = icalendar.Calendar.from_ical(data) self.assertEqual( cal['prodid'].to_ical(), @@ -151,9 +151,9 @@ class TestTimezoneCreation(unittest.TestCase): RFC""" directory = os.path.dirname(__file__) - cal = icalendar.Calendar.from_ical( - open(os.path.join(directory, 'america_new_york.ics'), 'rb').read() - ) + with open(os.path.join(directory, 'america_new_york.ics'), 'rb') as fp: + data = fp.read() + cal = icalendar.Calendar.from_ical(data) tz = cal.walk('VEVENT')[0]['DTSTART'][0].dt.tzinfo self.assertEqual(str(tz), 'custom_America/New_York') @@ -188,9 +188,9 @@ class TestTimezoneCreation(unittest.TestCase): self.maxDiff = None directory = os.path.dirname(__file__) - cal = icalendar.Calendar.from_ical( - open(os.path.join(directory, 'pacific_fiji.ics'), 'rb').read() - ) + with open(os.path.join(directory, 'pacific_fiji.ics'), 'rb') as fp: + data = fp.read() + cal = icalendar.Calendar.from_ical(data) tz = cal.walk('VEVENT')[0]['DTSTART'][0].dt.tzinfo self.assertEqual(str(tz), 'custom_Pacific/Fiji') diff --git a/src/icalendar/tests/test_unit_cal.py b/src/icalendar/tests/test_unit_cal.py index 50e13e6..0cda117 100644 --- a/src/icalendar/tests/test_unit_cal.py +++ b/src/icalendar/tests/test_unit_cal.py @@ -393,7 +393,8 @@ class TestCal(unittest.TestCase): import tempfile import os directory = tempfile.mkdtemp() - open(os.path.join(directory, 'test.ics'), 'wb').write(cal.to_ical()) + with open(os.path.join(directory, 'test.ics'), 'wb') as fp: + fp.write(cal.to_ical()) # Parsing a complete calendar from a string will silently ignore wrong # events but adding the error information to the component's 'errors'