From b46734734b2129f1c0ee77a8de2553e4d639bddb Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Tue, 5 Jan 2016 11:43:28 -0500 Subject: [PATCH] Support comments in whitelist files We already support comments in blacklist files. we should do the same for whitelist files as well. Change-Id: I5ad0c113cdb04dd1bdaa4d8bfdd1a3ab169fb0af --- os_testr/os_testr.py | 9 ++++++++- os_testr/tests/test_os_testr.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/os_testr/os_testr.py b/os_testr/os_testr.py index 6c5b778..6222bed 100755 --- a/os_testr/os_testr.py +++ b/os_testr/os_testr.py @@ -132,7 +132,14 @@ def path_to_regex(path): def get_regex_from_whitelist_file(file_path): - return '|'.join(open(file_path).read().splitlines()) + 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) + return '|'.join(lines) def construct_regex(blacklist_file, whitelist_file, regex, print_exclude): diff --git a/os_testr/tests/test_os_testr.py b/os_testr/tests/test_os_testr.py index 65fb0a9..8560757 100644 --- a/os_testr/tests/test_os_testr.py +++ b/os_testr/tests/test_os_testr.py @@ -144,6 +144,18 @@ class TestConstructRegex(base.TestCase): 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") + def test_blacklist_regex_without_comments(self): blacklist_file = six.StringIO() for i in range(4):