summaryrefslogtreecommitdiff
path: root/tests/testing/helpers.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/testing/helpers.py')
-rwxr-xr-xtests/testing/helpers.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/testing/helpers.py b/tests/testing/helpers.py
index dcc48904c..757167a4b 100755
--- a/tests/testing/helpers.py
+++ b/tests/testing/helpers.py
@@ -40,6 +40,7 @@
import subprocess
from threading import Timer
import time
+import re
class CallTimeoutException(Exception):
"""Exception that indicates that a process call timed out"""
@@ -105,6 +106,47 @@ class ProcessHelper(subprocess.Popen):
else:
return status, stdout, stderr
+class FileIgnoreList(object):
+ """Helper class to implement file ignore lists.
+
+ This class implements ignore lists using plain string matching and
+ regular expressions. In the simplest use case, rules are created
+ statically upon initialization:
+
+ ignore_list = FileIgnoreList(name=("ignore_me.txt", ), rex=(r".*~", )
+
+ Ignores can be queried using in the same ways as normal Python
+ containers:
+
+ if file_name in ignore_list:
+ print "Ignoring %s" % file_name
+
+
+ New rules can be added at runtime by extending the list in the
+ rules attribute:
+
+ ignore_list.rules.append(FileIgnoreList.simple("bar.txt"))
+ """
+
+ @staticmethod
+ def simple(r):
+ return lambda f: f == r
+
+ @staticmethod
+ def rex(r):
+ re_obj = r if hasattr(r, "search") else re.compile(r)
+ return lambda name: re_obj.search(name)
+
+ def __init__(self, names=(), rex=()):
+ self.rules = [ FileIgnoreList.simple(n) for n in names ] + \
+ [ FileIgnoreList.rex(r) for r in rex ]
+
+ def __contains__(self, name):
+ for rule in self.rules:
+ if rule(name):
+ return True
+ return False
+
if __name__ == "__main__":
# Run internal self tests to ensure that the helpers are working
# properly. The expected output when running this script is
@@ -129,4 +171,20 @@ if __name__ == "__main__":
except CallTimeoutException:
pass
+ ignore_list = FileIgnoreList(
+ names=("ignore.txt", "foo/test.txt"),
+ rex=(r"~$", re.compile("^#")))
+
+ assert "ignore.txt" in ignore_list
+ assert "bar.txt" not in ignore_list
+ assert "foo/test.txt" in ignore_list
+ assert "test.txt" not in ignore_list
+ assert "file1.c~" in ignore_list
+ assert "file1.c" not in ignore_list
+ assert "#foo" in ignore_list
+ assert "foo#" not in ignore_list
+
+ ignore_list.rules.append(FileIgnoreList.simple("bar.txt"))
+ assert "bar.txt" in ignore_list
+
print "SUCCESS!"