summaryrefslogtreecommitdiff
path: root/test/support/filesystem_dynamic_test_helper.py
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2016-07-23 20:47:26 +0000
committerDimitry Andric <dim@FreeBSD.org>2016-07-23 20:47:26 +0000
commit51072bd6bf79ef2bc6a922079bff57c31c1effbc (patch)
tree91a2effbc9e6f80bdbbf9eb70e06c51ad0867ea0 /test/support/filesystem_dynamic_test_helper.py
parentbb5e33f003797b67974a8893f7f2930fc51b8210 (diff)
Notes
Diffstat (limited to 'test/support/filesystem_dynamic_test_helper.py')
-rw-r--r--test/support/filesystem_dynamic_test_helper.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/test/support/filesystem_dynamic_test_helper.py b/test/support/filesystem_dynamic_test_helper.py
new file mode 100644
index 000000000000..1f48c9527982
--- /dev/null
+++ b/test/support/filesystem_dynamic_test_helper.py
@@ -0,0 +1,85 @@
+import sys
+import os
+import stat
+
+# Ensure that this is being run on a specific platform
+assert sys.platform.startswith('linux') or sys.platform.startswith('darwin') \
+ or sys.platform.startswith('cygwin') or sys.platform.startswith('freebsd')
+
+def env_path():
+ ep = os.environ.get('LIBCXX_FILESYSTEM_DYNAMIC_TEST_ROOT')
+ assert ep is not None
+ ep = os.path.realpath(ep)
+ assert os.path.isdir(ep)
+ return ep
+
+env_path_global = env_path()
+
+# Make sure we don't try and write outside of env_path.
+# All paths used should be sanitized
+def sanitize(p):
+ p = os.path.realpath(p)
+ if os.path.commonprefix([env_path_global, p]):
+ return p
+ assert False
+
+"""
+Some of the tests restrict permissions to induce failures.
+Before we delete the test enviroment, we have to walk it and re-raise the
+permissions.
+"""
+def clean_recursive(root_p):
+ if not os.path.islink(root_p):
+ os.chmod(root_p, 0o777)
+ for ent in os.listdir(root_p):
+ p = os.path.join(root_p, ent)
+ if os.path.islink(p) or not os.path.isdir(p):
+ os.remove(p)
+ else:
+ assert os.path.isdir(p)
+ clean_recursive(p)
+ os.rmdir(p)
+
+
+def init_test_directory(root_p):
+ root_p = sanitize(root_p)
+ assert not os.path.exists(root_p)
+ os.makedirs(root_p)
+
+
+def destroy_test_directory(root_p):
+ root_p = sanitize(root_p)
+ clean_recursive(root_p)
+ os.rmdir(root_p)
+
+
+def create_file(fname, size):
+ with open(sanitize(fname), 'w') as f:
+ f.write('c' * size)
+
+
+def create_dir(dname):
+ os.mkdir(sanitize(dname))
+
+
+def create_symlink(source, link):
+ os.symlink(sanitize(source), sanitize(link))
+
+
+def create_hardlink(source, link):
+ os.link(sanitize(source), sanitize(link))
+
+
+def create_fifo(source):
+ os.mkfifo(sanitize(source))
+
+
+def create_socket(source):
+ mode = 0600|stat.S_IFSOCK
+ os.mknod(sanitize(source), mode)
+
+
+if __name__ == '__main__':
+ command = " ".join(sys.argv[1:])
+ eval(command)
+ sys.exit(0)