Just changed the opening of the file

Previous code worked fine in python 2.7 but generated
"ResourceWarning: Unclosed file" in python 3.4+
Does not change the funtionality of the code at all.
Also updated the unit tests to work with the new code.

Change-Id: If98df5c4905f4b5a22602ba19224f9368fc8129f
This commit is contained in:
Divyansh Acharya 2017-08-17 20:30:56 +00:00
parent cdda299fa9
commit 8ca44080d7
2 changed files with 122 additions and 118 deletions

View File

@ -61,7 +61,8 @@ def path_to_regex(path):
def get_regex_from_whitelist_file(file_path):
lines = []
for line in open(file_path).read().splitlines():
with open(file_path) as white_file:
for line in white_file.read().splitlines():
split_line = line.strip().split('#')
# Before the # is the regex
line_regex = split_line[0].strip()
@ -75,7 +76,7 @@ def construct_regex(blacklist_file, whitelist_file, regex, print_exclude):
if not blacklist_file:
exclude_regex = ''
else:
black_file = open(blacklist_file, 'r')
with open(blacklist_file, 'r') as black_file:
exclude_regex = ''
for line in black_file:
raw_line = line.strip()

View File

@ -12,10 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import io
import mock
import six
from os_testr import regex_builder as os_testr
from os_testr.tests import base
@ -35,74 +34,77 @@ class TestConstructRegex(base.TestCase):
self.assertEqual(result, 'fake_regex')
def test_blacklist_regex_with_comments(self):
blacklist_file = six.StringIO()
with io.StringIO() as blacklist_file:
for i in range(4):
blacklist_file.write('fake_regex_%s # A Comment\n' % i)
blacklist_file.write(u'fake_regex_%s # A Comment\n' % i)
blacklist_file.seek(0)
with mock.patch('six.moves.builtins.open',
return_value=blacklist_file):
result = os_testr.construct_regex('fake_path', None, None, False)
self.assertEqual(
result,
"^((?!fake_regex_3|fake_regex_2|fake_regex_1|fake_regex_0).)*$")
result = os_testr.construct_regex(
'fake_path', None, None, False)
self.assertEqual(result, "^((?!fake_regex_3|fake_regex_2|"
"fake_regex_1|fake_regex_0).)*$")
def test_whitelist_regex_with_comments(self):
whitelist_file = six.StringIO()
with io.StringIO() as whitelist_file:
for i in range(4):
whitelist_file.write('fake_regex_%s # A Comment\n' % i)
whitelist_file.write(u'fake_regex_%s # A Comment\n' % i)
whitelist_file.seek(0)
with mock.patch('six.moves.builtins.open',
return_value=whitelist_file):
result = os_testr.construct_regex(None, 'fake_path', None, False)
result = os_testr.construct_regex(
None, 'fake_path', None, False)
self.assertEqual(
result,
"fake_regex_0|fake_regex_1|fake_regex_2|fake_regex_3")
def test_blacklist_regex_without_comments(self):
blacklist_file = six.StringIO()
with io.StringIO() as blacklist_file:
for i in range(4):
blacklist_file.write('fake_regex_%s\n' % i)
blacklist_file.write(u'fake_regex_%s\n' % i)
blacklist_file.seek(0)
with mock.patch('six.moves.builtins.open',
return_value=blacklist_file):
result = os_testr.construct_regex('fake_path', None, None, False)
self.assertEqual(
result,
"^((?!fake_regex_3|fake_regex_2|fake_regex_1|fake_regex_0).)*$")
result = os_testr.construct_regex(
'fake_path', None, None, False)
self.assertEqual(result, "^((?!fake_regex_3|fake_regex_2|"
"fake_regex_1|fake_regex_0).)*$")
def test_blacklist_regex_with_comments_and_regex(self):
blacklist_file = six.StringIO()
with io.StringIO() as blacklist_file:
for i in range(4):
blacklist_file.write('fake_regex_%s # Comments\n' % i)
blacklist_file.write(u'fake_regex_%s # Comments\n' % i)
blacklist_file.seek(0)
with mock.patch('six.moves.builtins.open',
return_value=blacklist_file):
result = os_testr.construct_regex('fake_path', None,
'fake_regex', False)
expected_regex = ("^((?!fake_regex_3|fake_regex_2|fake_regex_1|"
expected_regex = (
"^((?!fake_regex_3|fake_regex_2|fake_regex_1|"
"fake_regex_0).)*$fake_regex")
self.assertEqual(result, expected_regex)
def test_blacklist_regex_without_comments_and_regex(self):
blacklist_file = six.StringIO()
with io.StringIO() as blacklist_file:
for i in range(4):
blacklist_file.write('fake_regex_%s\n' % i)
blacklist_file.write(u'fake_regex_%s\n' % i)
blacklist_file.seek(0)
with mock.patch('six.moves.builtins.open',
return_value=blacklist_file):
result = os_testr.construct_regex('fake_path', None,
'fake_regex', False)
expected_regex = ("^((?!fake_regex_3|fake_regex_2|fake_regex_1|"
expected_regex = (
"^((?!fake_regex_3|fake_regex_2|fake_regex_1|"
"fake_regex_0).)*$fake_regex")
self.assertEqual(result, expected_regex)
@mock.patch.object(os_testr, 'print_skips')
def test_blacklist_regex_with_comment_print_skips(self, print_mock):
blacklist_file = six.StringIO()
with io.StringIO() as blacklist_file:
for i in range(4):
blacklist_file.write('fake_regex_%s # Comment\n' % i)
blacklist_file.write(u'fake_regex_%s # Comment\n' % i)
blacklist_file.seek(0)
with mock.patch('six.moves.builtins.open',
return_value=blacklist_file):
@ -122,9 +124,9 @@ class TestConstructRegex(base.TestCase):
@mock.patch.object(os_testr, 'print_skips')
def test_blacklist_regex_without_comment_print_skips(self, print_mock):
blacklist_file = six.StringIO()
with io.StringIO() as blacklist_file:
for i in range(4):
blacklist_file.write('fake_regex_%s\n' % i)
blacklist_file.write(u'fake_regex_%s\n' % i)
blacklist_file.seek(0)
with mock.patch('six.moves.builtins.open',
return_value=blacklist_file):
@ -145,20 +147,21 @@ class TestConstructRegex(base.TestCase):
class TestWhitelistFile(base.TestCase):
def test_read_whitelist_file(self):
file_contents = """regex_a
file_contents = u"""regex_a
regex_b"""
whitelist_file = six.StringIO()
with io.StringIO() as whitelist_file:
whitelist_file.write(file_contents)
whitelist_file.seek(0)
with mock.patch('six.moves.builtins.open',
return_value=whitelist_file):
regex = os_testr.get_regex_from_whitelist_file('/path/to/not_used')
regex = os_testr.get_regex_from_whitelist_file(
'/path/to/not_used')
self.assertEqual('regex_a|regex_b', regex)
def test_whitelist_regex_without_comments_and_regex(self):
file_contents = """regex_a
file_contents = u"""regex_a
regex_b"""
whitelist_file = six.StringIO()
with io.StringIO() as whitelist_file:
whitelist_file.write(file_contents)
whitelist_file.seek(0)
with mock.patch('six.moves.builtins.open',