diff --git a/os_testr/regex_builder.py b/os_testr/regex_builder.py index abc08fc..81a917a 100644 --- a/os_testr/regex_builder.py +++ b/os_testr/regex_builder.py @@ -61,12 +61,13 @@ def path_to_regex(path): def get_regex_from_whitelist_file(file_path): lines = [] - for line in open(file_path).read().splitlines(): - split_line = line.strip().split('#') - # Before the # is the regex - line_regex = split_line[0].strip() - if line_regex: - lines.append(line_regex) + 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() + if line_regex: + lines.append(line_regex) return '|'.join(lines) @@ -75,27 +76,27 @@ def construct_regex(blacklist_file, whitelist_file, regex, print_exclude): if not blacklist_file: exclude_regex = '' else: - black_file = open(blacklist_file, 'r') - exclude_regex = '' - for line in black_file: - raw_line = line.strip() - split_line = raw_line.split('#') - # Before the # is the regex - line_regex = split_line[0].strip() - if len(split_line) > 1: - # After the # is a comment - 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]) + with open(blacklist_file, 'r') as black_file: + exclude_regex = '' + for line in black_file: + raw_line = line.strip() + split_line = raw_line.split('#') + # Before the # is the regex + line_regex = split_line[0].strip() + if len(split_line) > 1: + # After the # is a comment + comment = split_line[1].strip() else: - exclude_regex = line_regex - if exclude_regex: - exclude_regex = "^((?!" + exclude_regex + ").)*$" + comment = '' + if line_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: exclude_regex += regex if whitelist_file: diff --git a/os_testr/tests/test_regex_builder.py b/os_testr/tests/test_regex_builder.py index c78500c..8e00683 100644 --- a/os_testr/tests/test_regex_builder.py +++ b/os_testr/tests/test_regex_builder.py @@ -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,82 +34,85 @@ class TestConstructRegex(base.TestCase): self.assertEqual(result, 'fake_regex') def test_blacklist_regex_with_comments(self): - blacklist_file = six.StringIO() - for i in range(4): - blacklist_file.write('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).)*$") + with io.StringIO() as blacklist_file: + for i in range(4): + 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).)*$") def test_whitelist_regex_with_comments(self): - whitelist_file = six.StringIO() - for i in range(4): - whitelist_file.write('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) - self.assertEqual( - result, - "fake_regex_0|fake_regex_1|fake_regex_2|fake_regex_3") + with io.StringIO() as whitelist_file: + for i in range(4): + 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) + 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() - for i in range(4): - blacklist_file.write('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).)*$") + with io.StringIO() as blacklist_file: + for i in range(4): + 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).)*$") def test_blacklist_regex_with_comments_and_regex(self): - blacklist_file = six.StringIO() - for i in range(4): - blacklist_file.write('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) + with io.StringIO() as blacklist_file: + for i in range(4): + 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|" - "fake_regex_0).)*$fake_regex") - self.assertEqual(result, expected_regex) + 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() - for i in range(4): - blacklist_file.write('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) + with io.StringIO() as blacklist_file: + for i in range(4): + 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|" - "fake_regex_0).)*$fake_regex") - self.assertEqual(result, expected_regex) + 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() - for i in range(4): - blacklist_file.write('fake_regex_%s # 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, True) + with io.StringIO() as blacklist_file: + for i in range(4): + 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): + result = os_testr.construct_regex('fake_path', None, + None, True) - expected_regex = ("^((?!fake_regex_3|fake_regex_2|fake_regex_1|" - "fake_regex_0).)*$") + expected_regex = ("^((?!fake_regex_3|fake_regex_2|fake_regex_1|" + "fake_regex_0).)*$") self.assertEqual(result, expected_regex) calls = print_mock.mock_calls self.assertEqual(len(calls), 4) @@ -122,17 +124,17 @@ 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() - for i in range(4): - blacklist_file.write('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, True) + with io.StringIO() as blacklist_file: + for i in range(4): + 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, True) - expected_regex = ("^((?!fake_regex_3|fake_regex_2|" - "fake_regex_1|fake_regex_0).)*$") + expected_regex = ("^((?!fake_regex_3|fake_regex_2|" + "fake_regex_1|fake_regex_0).)*$") self.assertEqual(result, expected_regex) calls = print_mock.mock_calls self.assertEqual(len(calls), 4) @@ -145,29 +147,30 @@ 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() - 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') - self.assertEqual('regex_a|regex_b', regex) + 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') + 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() - whitelist_file.write(file_contents) - 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) + 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): + result = os_testr.construct_regex(None, 'fake_path', + None, False) - expected_regex = 'regex_a|regex_b' - self.assertEqual(result, expected_regex) + expected_regex = 'regex_a|regex_b' + self.assertEqual(result, expected_regex) class TestGetTestList(base.TestCase):