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:
parent
cdda299fa9
commit
8ca44080d7
|
@ -61,12 +61,13 @@ def path_to_regex(path):
|
||||||
|
|
||||||
def get_regex_from_whitelist_file(file_path):
|
def get_regex_from_whitelist_file(file_path):
|
||||||
lines = []
|
lines = []
|
||||||
for line in open(file_path).read().splitlines():
|
with open(file_path) as white_file:
|
||||||
split_line = line.strip().split('#')
|
for line in white_file.read().splitlines():
|
||||||
# Before the # is the regex
|
split_line = line.strip().split('#')
|
||||||
line_regex = split_line[0].strip()
|
# Before the # is the regex
|
||||||
if line_regex:
|
line_regex = split_line[0].strip()
|
||||||
lines.append(line_regex)
|
if line_regex:
|
||||||
|
lines.append(line_regex)
|
||||||
return '|'.join(lines)
|
return '|'.join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
@ -75,27 +76,27 @@ def construct_regex(blacklist_file, whitelist_file, regex, print_exclude):
|
||||||
if not blacklist_file:
|
if not blacklist_file:
|
||||||
exclude_regex = ''
|
exclude_regex = ''
|
||||||
else:
|
else:
|
||||||
black_file = open(blacklist_file, 'r')
|
with open(blacklist_file, 'r') as black_file:
|
||||||
exclude_regex = ''
|
exclude_regex = ''
|
||||||
for line in black_file:
|
for line in black_file:
|
||||||
raw_line = line.strip()
|
raw_line = line.strip()
|
||||||
split_line = raw_line.split('#')
|
split_line = raw_line.split('#')
|
||||||
# Before the # is the regex
|
# Before the # is the regex
|
||||||
line_regex = split_line[0].strip()
|
line_regex = split_line[0].strip()
|
||||||
if len(split_line) > 1:
|
if len(split_line) > 1:
|
||||||
# After the # is a comment
|
# After the # is a comment
|
||||||
comment = split_line[1].strip()
|
comment = split_line[1].strip()
|
||||||
else:
|
|
||||||
comment = ''
|
|
||||||
if line_regex:
|
|
||||||
if print_exclude:
|
|
||||||
print_skips(line_regex, comment)
|
|
||||||
if exclude_regex:
|
|
||||||
exclude_regex = '|'.join([line_regex, exclude_regex])
|
|
||||||
else:
|
else:
|
||||||
exclude_regex = line_regex
|
comment = ''
|
||||||
if exclude_regex:
|
if line_regex:
|
||||||
exclude_regex = "^((?!" + exclude_regex + ").)*$"
|
if print_exclude:
|
||||||
|
print_skips(line_regex, comment)
|
||||||
|
if exclude_regex:
|
||||||
|
exclude_regex = '|'.join([line_regex, exclude_regex])
|
||||||
|
else:
|
||||||
|
exclude_regex = line_regex
|
||||||
|
if exclude_regex:
|
||||||
|
exclude_regex = "^((?!" + exclude_regex + ").)*$"
|
||||||
if regex:
|
if regex:
|
||||||
exclude_regex += regex
|
exclude_regex += regex
|
||||||
if whitelist_file:
|
if whitelist_file:
|
||||||
|
|
|
@ -12,10 +12,9 @@
|
||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import io
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
import six
|
|
||||||
|
|
||||||
from os_testr import regex_builder as os_testr
|
from os_testr import regex_builder as os_testr
|
||||||
from os_testr.tests import base
|
from os_testr.tests import base
|
||||||
|
|
||||||
|
@ -35,82 +34,85 @@ class TestConstructRegex(base.TestCase):
|
||||||
self.assertEqual(result, 'fake_regex')
|
self.assertEqual(result, 'fake_regex')
|
||||||
|
|
||||||
def test_blacklist_regex_with_comments(self):
|
def test_blacklist_regex_with_comments(self):
|
||||||
blacklist_file = six.StringIO()
|
with io.StringIO() as blacklist_file:
|
||||||
for i in range(4):
|
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)
|
blacklist_file.seek(0)
|
||||||
with mock.patch('six.moves.builtins.open',
|
with mock.patch('six.moves.builtins.open',
|
||||||
return_value=blacklist_file):
|
return_value=blacklist_file):
|
||||||
result = os_testr.construct_regex('fake_path', None, None, False)
|
result = os_testr.construct_regex(
|
||||||
self.assertEqual(
|
'fake_path', None, None, False)
|
||||||
result,
|
self.assertEqual(result, "^((?!fake_regex_3|fake_regex_2|"
|
||||||
"^((?!fake_regex_3|fake_regex_2|fake_regex_1|fake_regex_0).)*$")
|
"fake_regex_1|fake_regex_0).)*$")
|
||||||
|
|
||||||
def test_whitelist_regex_with_comments(self):
|
def test_whitelist_regex_with_comments(self):
|
||||||
whitelist_file = six.StringIO()
|
with io.StringIO() as whitelist_file:
|
||||||
for i in range(4):
|
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)
|
whitelist_file.seek(0)
|
||||||
with mock.patch('six.moves.builtins.open',
|
with mock.patch('six.moves.builtins.open',
|
||||||
return_value=whitelist_file):
|
return_value=whitelist_file):
|
||||||
result = os_testr.construct_regex(None, 'fake_path', None, False)
|
result = os_testr.construct_regex(
|
||||||
self.assertEqual(
|
None, 'fake_path', None, False)
|
||||||
result,
|
self.assertEqual(
|
||||||
"fake_regex_0|fake_regex_1|fake_regex_2|fake_regex_3")
|
result,
|
||||||
|
"fake_regex_0|fake_regex_1|fake_regex_2|fake_regex_3")
|
||||||
|
|
||||||
def test_blacklist_regex_without_comments(self):
|
def test_blacklist_regex_without_comments(self):
|
||||||
blacklist_file = six.StringIO()
|
with io.StringIO() as blacklist_file:
|
||||||
for i in range(4):
|
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)
|
blacklist_file.seek(0)
|
||||||
with mock.patch('six.moves.builtins.open',
|
with mock.patch('six.moves.builtins.open',
|
||||||
return_value=blacklist_file):
|
return_value=blacklist_file):
|
||||||
result = os_testr.construct_regex('fake_path', None, None, False)
|
result = os_testr.construct_regex(
|
||||||
self.assertEqual(
|
'fake_path', None, None, False)
|
||||||
result,
|
self.assertEqual(result, "^((?!fake_regex_3|fake_regex_2|"
|
||||||
"^((?!fake_regex_3|fake_regex_2|fake_regex_1|fake_regex_0).)*$")
|
"fake_regex_1|fake_regex_0).)*$")
|
||||||
|
|
||||||
def test_blacklist_regex_with_comments_and_regex(self):
|
def test_blacklist_regex_with_comments_and_regex(self):
|
||||||
blacklist_file = six.StringIO()
|
with io.StringIO() as blacklist_file:
|
||||||
for i in range(4):
|
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)
|
blacklist_file.seek(0)
|
||||||
with mock.patch('six.moves.builtins.open',
|
with mock.patch('six.moves.builtins.open',
|
||||||
return_value=blacklist_file):
|
return_value=blacklist_file):
|
||||||
result = os_testr.construct_regex('fake_path', None,
|
result = os_testr.construct_regex('fake_path', None,
|
||||||
'fake_regex', False)
|
'fake_regex', False)
|
||||||
|
|
||||||
expected_regex = ("^((?!fake_regex_3|fake_regex_2|fake_regex_1|"
|
expected_regex = (
|
||||||
"fake_regex_0).)*$fake_regex")
|
"^((?!fake_regex_3|fake_regex_2|fake_regex_1|"
|
||||||
self.assertEqual(result, expected_regex)
|
"fake_regex_0).)*$fake_regex")
|
||||||
|
self.assertEqual(result, expected_regex)
|
||||||
|
|
||||||
def test_blacklist_regex_without_comments_and_regex(self):
|
def test_blacklist_regex_without_comments_and_regex(self):
|
||||||
blacklist_file = six.StringIO()
|
with io.StringIO() as blacklist_file:
|
||||||
for i in range(4):
|
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)
|
blacklist_file.seek(0)
|
||||||
with mock.patch('six.moves.builtins.open',
|
with mock.patch('six.moves.builtins.open',
|
||||||
return_value=blacklist_file):
|
return_value=blacklist_file):
|
||||||
result = os_testr.construct_regex('fake_path', None,
|
result = os_testr.construct_regex('fake_path', None,
|
||||||
'fake_regex', False)
|
'fake_regex', False)
|
||||||
|
|
||||||
expected_regex = ("^((?!fake_regex_3|fake_regex_2|fake_regex_1|"
|
expected_regex = (
|
||||||
"fake_regex_0).)*$fake_regex")
|
"^((?!fake_regex_3|fake_regex_2|fake_regex_1|"
|
||||||
self.assertEqual(result, expected_regex)
|
"fake_regex_0).)*$fake_regex")
|
||||||
|
self.assertEqual(result, expected_regex)
|
||||||
|
|
||||||
@mock.patch.object(os_testr, 'print_skips')
|
@mock.patch.object(os_testr, 'print_skips')
|
||||||
def test_blacklist_regex_with_comment_print_skips(self, print_mock):
|
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):
|
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)
|
blacklist_file.seek(0)
|
||||||
with mock.patch('six.moves.builtins.open',
|
with mock.patch('six.moves.builtins.open',
|
||||||
return_value=blacklist_file):
|
return_value=blacklist_file):
|
||||||
result = os_testr.construct_regex('fake_path', None,
|
result = os_testr.construct_regex('fake_path', None,
|
||||||
None, True)
|
None, True)
|
||||||
|
|
||||||
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_0).)*$")
|
||||||
self.assertEqual(result, expected_regex)
|
self.assertEqual(result, expected_regex)
|
||||||
calls = print_mock.mock_calls
|
calls = print_mock.mock_calls
|
||||||
self.assertEqual(len(calls), 4)
|
self.assertEqual(len(calls), 4)
|
||||||
|
@ -122,17 +124,17 @@ class TestConstructRegex(base.TestCase):
|
||||||
|
|
||||||
@mock.patch.object(os_testr, 'print_skips')
|
@mock.patch.object(os_testr, 'print_skips')
|
||||||
def test_blacklist_regex_without_comment_print_skips(self, print_mock):
|
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):
|
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)
|
blacklist_file.seek(0)
|
||||||
with mock.patch('six.moves.builtins.open',
|
with mock.patch('six.moves.builtins.open',
|
||||||
return_value=blacklist_file):
|
return_value=blacklist_file):
|
||||||
result = os_testr.construct_regex('fake_path', None,
|
result = os_testr.construct_regex('fake_path', None,
|
||||||
None, True)
|
None, True)
|
||||||
|
|
||||||
expected_regex = ("^((?!fake_regex_3|fake_regex_2|"
|
expected_regex = ("^((?!fake_regex_3|fake_regex_2|"
|
||||||
"fake_regex_1|fake_regex_0).)*$")
|
"fake_regex_1|fake_regex_0).)*$")
|
||||||
self.assertEqual(result, expected_regex)
|
self.assertEqual(result, expected_regex)
|
||||||
calls = print_mock.mock_calls
|
calls = print_mock.mock_calls
|
||||||
self.assertEqual(len(calls), 4)
|
self.assertEqual(len(calls), 4)
|
||||||
|
@ -145,29 +147,30 @@ class TestConstructRegex(base.TestCase):
|
||||||
|
|
||||||
class TestWhitelistFile(base.TestCase):
|
class TestWhitelistFile(base.TestCase):
|
||||||
def test_read_whitelist_file(self):
|
def test_read_whitelist_file(self):
|
||||||
file_contents = """regex_a
|
file_contents = u"""regex_a
|
||||||
regex_b"""
|
regex_b"""
|
||||||
whitelist_file = six.StringIO()
|
with io.StringIO() as whitelist_file:
|
||||||
whitelist_file.write(file_contents)
|
whitelist_file.write(file_contents)
|
||||||
whitelist_file.seek(0)
|
whitelist_file.seek(0)
|
||||||
with mock.patch('six.moves.builtins.open',
|
with mock.patch('six.moves.builtins.open',
|
||||||
return_value=whitelist_file):
|
return_value=whitelist_file):
|
||||||
regex = os_testr.get_regex_from_whitelist_file('/path/to/not_used')
|
regex = os_testr.get_regex_from_whitelist_file(
|
||||||
self.assertEqual('regex_a|regex_b', regex)
|
'/path/to/not_used')
|
||||||
|
self.assertEqual('regex_a|regex_b', regex)
|
||||||
|
|
||||||
def test_whitelist_regex_without_comments_and_regex(self):
|
def test_whitelist_regex_without_comments_and_regex(self):
|
||||||
file_contents = """regex_a
|
file_contents = u"""regex_a
|
||||||
regex_b"""
|
regex_b"""
|
||||||
whitelist_file = six.StringIO()
|
with io.StringIO() as whitelist_file:
|
||||||
whitelist_file.write(file_contents)
|
whitelist_file.write(file_contents)
|
||||||
whitelist_file.seek(0)
|
whitelist_file.seek(0)
|
||||||
with mock.patch('six.moves.builtins.open',
|
with mock.patch('six.moves.builtins.open',
|
||||||
return_value=whitelist_file):
|
return_value=whitelist_file):
|
||||||
result = os_testr.construct_regex(None, 'fake_path',
|
result = os_testr.construct_regex(None, 'fake_path',
|
||||||
None, False)
|
None, False)
|
||||||
|
|
||||||
expected_regex = 'regex_a|regex_b'
|
expected_regex = 'regex_a|regex_b'
|
||||||
self.assertEqual(result, expected_regex)
|
self.assertEqual(result, expected_regex)
|
||||||
|
|
||||||
|
|
||||||
class TestGetTestList(base.TestCase):
|
class TestGetTestList(base.TestCase):
|
||||||
|
|
Loading…
Reference in New Issue