summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozfile/tests
diff options
context:
space:
mode:
Diffstat (limited to 'testing/mozbase/mozfile/tests')
-rw-r--r--testing/mozbase/mozfile/tests/files/missing_file_attributes.zipbin0 -> 442 bytes
-rwxr-xr-xtesting/mozbase/mozfile/tests/files/which/baz0
-rwxr-xr-xtesting/mozbase/mozfile/tests/files/which/baz.exe0
-rwxr-xr-xtesting/mozbase/mozfile/tests/files/which/registered/quux.exe0
-rwxr-xr-xtesting/mozbase/mozfile/tests/files/which/unix/baz.exe0
-rw-r--r--testing/mozbase/mozfile/tests/files/which/unix/file0
-rwxr-xr-xtesting/mozbase/mozfile/tests/files/which/unix/foo0
-rwxr-xr-xtesting/mozbase/mozfile/tests/files/which/win/bar0
-rwxr-xr-xtesting/mozbase/mozfile/tests/files/which/win/baz.exe0
-rwxr-xr-xtesting/mozbase/mozfile/tests/files/which/win/foo0
-rwxr-xr-xtesting/mozbase/mozfile/tests/files/which/win/foo.exe0
-rw-r--r--testing/mozbase/mozfile/tests/manifest.ini10
-rw-r--r--testing/mozbase/mozfile/tests/stubs.py56
-rw-r--r--testing/mozbase/mozfile/tests/test_copycontents.py126
-rw-r--r--testing/mozbase/mozfile/tests/test_extract.py152
-rwxr-xr-xtesting/mozbase/mozfile/tests/test_load.py63
-rw-r--r--testing/mozbase/mozfile/tests/test_move_remove.py253
-rw-r--r--testing/mozbase/mozfile/tests/test_tempdir.py44
-rw-r--r--testing/mozbase/mozfile/tests/test_tempfile.py105
-rw-r--r--testing/mozbase/mozfile/tests/test_tree.py30
-rwxr-xr-xtesting/mozbase/mozfile/tests/test_url.py23
-rw-r--r--testing/mozbase/mozfile/tests/test_which.py63
22 files changed, 925 insertions, 0 deletions
diff --git a/testing/mozbase/mozfile/tests/files/missing_file_attributes.zip b/testing/mozbase/mozfile/tests/files/missing_file_attributes.zip
new file mode 100644
index 0000000000..2b5409e89c
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/files/missing_file_attributes.zip
Binary files differ
diff --git a/testing/mozbase/mozfile/tests/files/which/baz b/testing/mozbase/mozfile/tests/files/which/baz
new file mode 100755
index 0000000000..e69de29bb2
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/files/which/baz
diff --git a/testing/mozbase/mozfile/tests/files/which/baz.exe b/testing/mozbase/mozfile/tests/files/which/baz.exe
new file mode 100755
index 0000000000..e69de29bb2
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/files/which/baz.exe
diff --git a/testing/mozbase/mozfile/tests/files/which/registered/quux.exe b/testing/mozbase/mozfile/tests/files/which/registered/quux.exe
new file mode 100755
index 0000000000..e69de29bb2
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/files/which/registered/quux.exe
diff --git a/testing/mozbase/mozfile/tests/files/which/unix/baz.exe b/testing/mozbase/mozfile/tests/files/which/unix/baz.exe
new file mode 100755
index 0000000000..e69de29bb2
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/files/which/unix/baz.exe
diff --git a/testing/mozbase/mozfile/tests/files/which/unix/file b/testing/mozbase/mozfile/tests/files/which/unix/file
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/files/which/unix/file
diff --git a/testing/mozbase/mozfile/tests/files/which/unix/foo b/testing/mozbase/mozfile/tests/files/which/unix/foo
new file mode 100755
index 0000000000..e69de29bb2
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/files/which/unix/foo
diff --git a/testing/mozbase/mozfile/tests/files/which/win/bar b/testing/mozbase/mozfile/tests/files/which/win/bar
new file mode 100755
index 0000000000..e69de29bb2
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/files/which/win/bar
diff --git a/testing/mozbase/mozfile/tests/files/which/win/baz.exe b/testing/mozbase/mozfile/tests/files/which/win/baz.exe
new file mode 100755
index 0000000000..e69de29bb2
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/files/which/win/baz.exe
diff --git a/testing/mozbase/mozfile/tests/files/which/win/foo b/testing/mozbase/mozfile/tests/files/which/win/foo
new file mode 100755
index 0000000000..e69de29bb2
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/files/which/win/foo
diff --git a/testing/mozbase/mozfile/tests/files/which/win/foo.exe b/testing/mozbase/mozfile/tests/files/which/win/foo.exe
new file mode 100755
index 0000000000..e69de29bb2
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/files/which/win/foo.exe
diff --git a/testing/mozbase/mozfile/tests/manifest.ini b/testing/mozbase/mozfile/tests/manifest.ini
new file mode 100644
index 0000000000..258034bef5
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/manifest.ini
@@ -0,0 +1,10 @@
+[DEFAULT]
+subsuite = mozbase
+[test_extract.py]
+[test_load.py]
+[test_move_remove.py]
+[test_tempdir.py]
+[test_tempfile.py]
+[test_tree.py]
+[test_url.py]
+[test_which.py]
diff --git a/testing/mozbase/mozfile/tests/stubs.py b/testing/mozbase/mozfile/tests/stubs.py
new file mode 100644
index 0000000000..3c1bd47207
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/stubs.py
@@ -0,0 +1,56 @@
+import os
+import shutil
+import tempfile
+
+# stub file paths
+files = [
+ ("foo.txt",),
+ (
+ "foo",
+ "bar.txt",
+ ),
+ (
+ "foo",
+ "bar",
+ "fleem.txt",
+ ),
+ (
+ "foobar",
+ "fleem.txt",
+ ),
+ ("bar.txt",),
+ (
+ "nested_tree",
+ "bar",
+ "fleem.txt",
+ ),
+ ("readonly.txt",),
+]
+
+
+def create_empty_stub():
+ tempdir = tempfile.mkdtemp()
+ return tempdir
+
+
+def create_stub(tempdir=None):
+ """create a stub directory"""
+
+ tempdir = tempdir or tempfile.mkdtemp()
+ try:
+ for path in files:
+ fullpath = os.path.join(tempdir, *path)
+ dirname = os.path.dirname(fullpath)
+ if not os.path.exists(dirname):
+ os.makedirs(dirname)
+ contents = path[-1]
+ f = open(fullpath, "w")
+ f.write(contents)
+ f.close()
+ return tempdir
+ except Exception:
+ try:
+ shutil.rmtree(tempdir)
+ except Exception:
+ pass
+ raise
diff --git a/testing/mozbase/mozfile/tests/test_copycontents.py b/testing/mozbase/mozfile/tests/test_copycontents.py
new file mode 100644
index 0000000000..b829d7b3a4
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/test_copycontents.py
@@ -0,0 +1,126 @@
+#!/usr/bin/env python
+
+import os
+import shutil
+import unittest
+
+import mozfile
+import mozunit
+import stubs
+
+
+class MozfileCopyContentsTestCase(unittest.TestCase):
+ """Test our ability to copy the contents of directories"""
+
+ def _directory_is_subset(self, set_, subset_):
+ """
+ Confirm that all the contents of 'subset_' are contained in 'set_'
+ """
+ names = os.listdir(subset_)
+ for name in names:
+ full_set_path = os.path.join(set_, name)
+ full_subset_path = os.path.join(subset_, name)
+ if os.path.isdir(full_subset_path):
+ self.assertTrue(os.path.isdir(full_set_path))
+ self._directory_is_subset(full_set_path, full_subset_path)
+ elif os.path.islink(full_subset_path):
+ self.assertTrue(os.path.islink(full_set_path))
+ else:
+ self.assertTrue(os.stat(full_set_path))
+
+ def _directories_are_equal(self, dir1, dir2):
+ """
+ Confirm that the contents of 'dir1' are the same as 'dir2'
+ """
+ names1 = os.listdir(dir1)
+ names2 = os.listdir(dir2)
+ self.assertTrue(len(names1) == len(names2))
+ for name in names1:
+ self.assertTrue(name in names2)
+ dir1_path = os.path.join(dir1, name)
+ dir2_path = os.path.join(dir2, name)
+ if os.path.isdir(dir1_path):
+ self.assertTrue(os.path.isdir(dir2_path))
+ self._directories_are_equal(dir1_path, dir2_path)
+ elif os.path.islink(dir1_path):
+ self.assertTrue(os.path.islink(dir2_path))
+ else:
+ self.assertTrue(os.stat(dir2_path))
+
+ def test_copy_empty_directory(self):
+ tempdir = stubs.create_empty_stub()
+ dstdir = stubs.create_empty_stub()
+ self.assertTrue(os.path.isdir(tempdir))
+
+ mozfile.copy_contents(tempdir, dstdir)
+ self._directories_are_equal(dstdir, tempdir)
+
+ if os.path.isdir(tempdir):
+ shutil.rmtree(tempdir)
+ if os.path.isdir(dstdir):
+ shutil.rmtree(dstdir)
+
+ def test_copy_full_directory(self):
+ tempdir = stubs.create_stub()
+ dstdir = stubs.create_empty_stub()
+ self.assertTrue(os.path.isdir(tempdir))
+
+ mozfile.copy_contents(tempdir, dstdir)
+ self._directories_are_equal(dstdir, tempdir)
+
+ if os.path.isdir(tempdir):
+ shutil.rmtree(tempdir)
+ if os.path.isdir(dstdir):
+ shutil.rmtree(dstdir)
+
+ def test_copy_full_directory_with_existing_file(self):
+ tempdir = stubs.create_stub()
+ dstdir = stubs.create_empty_stub()
+
+ filename = "i_dont_exist_in_tempdir"
+ f = open(os.path.join(dstdir, filename), "w")
+ f.write("Hello World")
+ f.close()
+
+ self.assertTrue(os.path.isdir(tempdir))
+
+ mozfile.copy_contents(tempdir, dstdir)
+ self._directory_is_subset(dstdir, tempdir)
+ self.assertTrue(os.path.exists(os.path.join(dstdir, filename)))
+
+ if os.path.isdir(tempdir):
+ shutil.rmtree(tempdir)
+ if os.path.isdir(dstdir):
+ shutil.rmtree(dstdir)
+
+ def test_copy_full_directory_with_overlapping_file(self):
+ tempdir = stubs.create_stub()
+ dstdir = stubs.create_empty_stub()
+
+ filename = "i_do_exist_in_tempdir"
+ for d in [tempdir, dstdir]:
+ f = open(os.path.join(d, filename), "w")
+ f.write("Hello " + d)
+ f.close()
+
+ self.assertTrue(os.path.isdir(tempdir))
+ self.assertTrue(os.path.exists(os.path.join(tempdir, filename)))
+ self.assertTrue(os.path.exists(os.path.join(dstdir, filename)))
+
+ line = open(os.path.join(dstdir, filename), "r").readlines()[0]
+ self.assertTrue(line == "Hello " + dstdir)
+
+ mozfile.copy_contents(tempdir, dstdir)
+
+ line = open(os.path.join(dstdir, filename), "r").readlines()[0]
+ self.assertTrue(line == "Hello " + tempdir)
+ self._directories_are_equal(tempdir, dstdir)
+
+ if os.path.isdir(tempdir):
+ shutil.rmtree(tempdir)
+ if os.path.isdir(dstdir):
+ shutil.rmtree(dstdir)
+
+
+if __name__ == "__main__":
+ mozunit.main()
diff --git a/testing/mozbase/mozfile/tests/test_extract.py b/testing/mozbase/mozfile/tests/test_extract.py
new file mode 100644
index 0000000000..c2675d77f7
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/test_extract.py
@@ -0,0 +1,152 @@
+#!/usr/bin/env python
+
+import os
+import tarfile
+import tempfile
+import zipfile
+
+import mozfile
+import mozunit
+import pytest
+import stubs
+
+
+@pytest.fixture
+def ensure_directory_contents():
+ """ensure the directory contents match"""
+
+ def inner(directory):
+ for f in stubs.files:
+ path = os.path.join(directory, *f)
+ exists = os.path.exists(path)
+ if not exists:
+ print("%s does not exist" % (os.path.join(f)))
+ assert exists
+ if exists:
+ contents = open(path).read().strip()
+ assert contents == f[-1]
+
+ return inner
+
+
+@pytest.fixture(scope="module")
+def tarpath(tmpdir_factory):
+ """create a stub tarball for testing"""
+ tmpdir = tmpdir_factory.mktemp("test_extract")
+
+ tempdir = tmpdir.join("stubs").strpath
+ stubs.create_stub(tempdir)
+ filename = tmpdir.join("bundle.tar").strpath
+ archive = tarfile.TarFile(filename, mode="w")
+ for path in stubs.files:
+ archive.add(os.path.join(tempdir, *path), arcname=os.path.join(*path))
+ archive.close()
+
+ assert os.path.exists(filename)
+ return filename
+
+
+@pytest.fixture(scope="module")
+def zippath(tmpdir_factory):
+ """create a stub zipfile for testing"""
+ tmpdir = tmpdir_factory.mktemp("test_extract")
+
+ tempdir = tmpdir.join("stubs").strpath
+ stubs.create_stub(tempdir)
+ filename = tmpdir.join("bundle.zip").strpath
+ archive = zipfile.ZipFile(filename, mode="w")
+ for path in stubs.files:
+ archive.write(os.path.join(tempdir, *path), arcname=os.path.join(*path))
+ archive.close()
+
+ assert os.path.exists(filename)
+ return filename
+
+
+@pytest.fixture(scope="module", params=["tar", "zip"])
+def bundlepath(request, tarpath, zippath):
+ if request.param == "tar":
+ return tarpath
+ else:
+ return zippath
+
+
+def test_extract(tmpdir, bundlepath, ensure_directory_contents):
+ """test extracting a zipfile"""
+ dest = tmpdir.mkdir("dest").strpath
+ mozfile.extract(bundlepath, dest)
+ ensure_directory_contents(dest)
+
+
+def test_extract_zipfile_missing_file_attributes(tmpdir):
+ """if files do not have attributes set the default permissions have to be inherited."""
+ _zipfile = os.path.join(
+ os.path.dirname(__file__), "files", "missing_file_attributes.zip"
+ )
+ assert os.path.exists(_zipfile)
+ dest = tmpdir.mkdir("dest").strpath
+
+ # Get the default file permissions for the user
+ fname = os.path.join(dest, "foo")
+ with open(fname, "w"):
+ pass
+ default_stmode = os.stat(fname).st_mode
+
+ files = mozfile.extract_zip(_zipfile, dest)
+ for filename in files:
+ assert os.stat(os.path.join(dest, filename)).st_mode == default_stmode
+
+
+def test_extract_non_archive(tarpath, zippath):
+ """test the generalized extract function"""
+ # test extracting some non-archive; this should fail
+ fd, filename = tempfile.mkstemp()
+ os.write(fd, b"This is not a zipfile or tarball")
+ os.close(fd)
+ exception = None
+
+ try:
+ dest = tempfile.mkdtemp()
+ mozfile.extract(filename, dest)
+ except Exception as exc:
+ exception = exc
+ finally:
+ os.remove(filename)
+ os.rmdir(dest)
+
+ assert isinstance(exception, Exception)
+
+
+def test_extract_ignore(tmpdir, bundlepath):
+ dest = tmpdir.mkdir("dest").strpath
+ ignore = ("foo", "**/fleem.txt", "read*.txt")
+ mozfile.extract(bundlepath, dest, ignore=ignore)
+
+ assert sorted(os.listdir(dest)) == ["bar.txt", "foo.txt"]
+
+
+def test_tarball_escape(tmpdir):
+ """Ensures that extracting a tarball can't write outside of the intended
+ destination directory.
+ """
+ workdir = tmpdir.mkdir("workdir")
+ os.chdir(workdir)
+
+ # Generate a "malicious" bundle.
+ with open("bad.txt", "w") as fh:
+ fh.write("pwned!")
+
+ def change_name(tarinfo):
+ tarinfo.name = "../" + tarinfo.name
+ return tarinfo
+
+ with tarfile.open("evil.tar", "w:xz") as tar:
+ tar.add("bad.txt", filter=change_name)
+
+ with pytest.raises(RuntimeError):
+ mozfile.extract_tarball("evil.tar", workdir)
+ assert not os.path.exists(tmpdir.join("bad.txt"))
+
+
+if __name__ == "__main__":
+ mozunit.main()
diff --git a/testing/mozbase/mozfile/tests/test_load.py b/testing/mozbase/mozfile/tests/test_load.py
new file mode 100755
index 0000000000..7a3896e33b
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/test_load.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+
+"""
+tests for mozfile.load
+"""
+
+import mozunit
+import pytest
+from mozfile import load
+from wptserve.handlers import handler
+from wptserve.server import WebTestHttpd
+
+
+@pytest.fixture(name="httpd_url")
+def fixture_httpd_url():
+ """Yield a started WebTestHttpd server."""
+
+ @handler
+ def example(request, response):
+ """Example request handler."""
+ body = b"example"
+ return (
+ 200,
+ [("Content-type", "text/plain"), ("Content-length", len(body))],
+ body,
+ )
+
+ httpd = WebTestHttpd(host="127.0.0.1", routes=[("GET", "*", example)])
+
+ httpd.start()
+ yield httpd.get_url()
+ httpd.stop()
+
+
+def test_http(httpd_url):
+ """Test with WebTestHttpd and a http:// URL."""
+ content = load(httpd_url).read()
+ assert content == b"example"
+
+
+@pytest.fixture(name="temporary_file")
+def fixture_temporary_file(tmpdir):
+ """Yield a path to a temporary file."""
+ foobar = tmpdir.join("foobar.txt")
+ foobar.write("hello world")
+
+ yield str(foobar)
+
+ foobar.remove()
+
+
+def test_file_path(temporary_file):
+ """Test loading from a file path."""
+ assert load(temporary_file).read() == "hello world"
+
+
+def test_file_url(temporary_file):
+ """Test loading from a file URL."""
+ assert load("file://%s" % temporary_file).read() == "hello world"
+
+
+if __name__ == "__main__":
+ mozunit.main()
diff --git a/testing/mozbase/mozfile/tests/test_move_remove.py b/testing/mozbase/mozfile/tests/test_move_remove.py
new file mode 100644
index 0000000000..0679c6c3fa
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/test_move_remove.py
@@ -0,0 +1,253 @@
+#!/usr/bin/env python
+
+import errno
+import os
+import shutil
+import stat
+import threading
+import time
+import unittest
+from contextlib import contextmanager
+
+import mozfile
+import mozinfo
+import mozunit
+import stubs
+
+
+def mark_readonly(path):
+ """Removes all write permissions from given file/directory.
+
+ :param path: path of directory/file of which modes must be changed
+ """
+ mode = os.stat(path)[stat.ST_MODE]
+ os.chmod(path, mode & ~stat.S_IWUSR & ~stat.S_IWGRP & ~stat.S_IWOTH)
+
+
+class FileOpenCloseThread(threading.Thread):
+ """Helper thread for asynchronous file handling"""
+
+ def __init__(self, path, delay, delete=False):
+ threading.Thread.__init__(self)
+ self.file_opened = threading.Event()
+ self.delay = delay
+ self.path = path
+ self.delete = delete
+
+ def run(self):
+ with open(self.path):
+ self.file_opened.set()
+ time.sleep(self.delay)
+ if self.delete:
+ try:
+ os.remove(self.path)
+ except Exception:
+ pass
+
+
+@contextmanager
+def wait_file_opened_in_thread(*args, **kwargs):
+ thread = FileOpenCloseThread(*args, **kwargs)
+ thread.start()
+ thread.file_opened.wait()
+ try:
+ yield thread
+ finally:
+ thread.join()
+
+
+class MozfileRemoveTestCase(unittest.TestCase):
+ """Test our ability to remove directories and files"""
+
+ def setUp(self):
+ # Generate a stub
+ self.tempdir = stubs.create_stub()
+
+ def tearDown(self):
+ if os.path.isdir(self.tempdir):
+ shutil.rmtree(self.tempdir)
+
+ def test_remove_directory(self):
+ """Test the removal of a directory"""
+ self.assertTrue(os.path.isdir(self.tempdir))
+ mozfile.remove(self.tempdir)
+ self.assertFalse(os.path.exists(self.tempdir))
+
+ def test_remove_directory_with_open_file(self):
+ """Test removing a directory with an open file"""
+ # Open a file in the generated stub
+ filepath = os.path.join(self.tempdir, *stubs.files[1])
+ f = open(filepath, "w")
+ f.write("foo-bar")
+
+ # keep file open and then try removing the dir-tree
+ if mozinfo.isWin:
+ # On the Windows family WindowsError should be raised.
+ self.assertRaises(OSError, mozfile.remove, self.tempdir)
+ self.assertTrue(os.path.exists(self.tempdir))
+ else:
+ # Folder should be deleted on all other platforms
+ mozfile.remove(self.tempdir)
+ self.assertFalse(os.path.exists(self.tempdir))
+
+ def test_remove_closed_file(self):
+ """Test removing a closed file"""
+ # Open a file in the generated stub
+ filepath = os.path.join(self.tempdir, *stubs.files[1])
+ with open(filepath, "w") as f:
+ f.write("foo-bar")
+
+ # Folder should be deleted on all platforms
+ mozfile.remove(self.tempdir)
+ self.assertFalse(os.path.exists(self.tempdir))
+
+ def test_removing_open_file_with_retry(self):
+ """Test removing a file in use with retry"""
+ filepath = os.path.join(self.tempdir, *stubs.files[1])
+
+ with wait_file_opened_in_thread(filepath, 0.2):
+ # on windows first attempt will fail,
+ # and it will be retried until the thread leave the handle
+ mozfile.remove(filepath)
+
+ # Check deletion was successful
+ self.assertFalse(os.path.exists(filepath))
+
+ def test_removing_already_deleted_file_with_retry(self):
+ """Test removing a meanwhile removed file with retry"""
+ filepath = os.path.join(self.tempdir, *stubs.files[1])
+
+ with wait_file_opened_in_thread(filepath, 0.2, True):
+ # on windows first attempt will fail, and before
+ # the retry the opened file will be deleted in the thread
+ mozfile.remove(filepath)
+
+ # Check deletion was successful
+ self.assertFalse(os.path.exists(filepath))
+
+ def test_remove_readonly_tree(self):
+ """Test removing a read-only directory"""
+
+ dirpath = os.path.join(self.tempdir, "nested_tree")
+ mark_readonly(dirpath)
+
+ # However, mozfile should change write permissions and remove dir.
+ mozfile.remove(dirpath)
+
+ self.assertFalse(os.path.exists(dirpath))
+
+ def test_remove_readonly_file(self):
+ """Test removing read-only files"""
+ filepath = os.path.join(self.tempdir, *stubs.files[1])
+ mark_readonly(filepath)
+
+ # However, mozfile should change write permission and then remove file.
+ mozfile.remove(filepath)
+
+ self.assertFalse(os.path.exists(filepath))
+
+ @unittest.skipIf(mozinfo.isWin, "Symlinks are not supported on Windows")
+ def test_remove_symlink(self):
+ """Test removing a symlink"""
+ file_path = os.path.join(self.tempdir, *stubs.files[1])
+ symlink_path = os.path.join(self.tempdir, "symlink")
+
+ os.symlink(file_path, symlink_path)
+ self.assertTrue(os.path.islink(symlink_path))
+
+ # The linked folder and files should not be deleted
+ mozfile.remove(symlink_path)
+ self.assertFalse(os.path.exists(symlink_path))
+ self.assertTrue(os.path.exists(file_path))
+
+ @unittest.skipIf(mozinfo.isWin, "Symlinks are not supported on Windows")
+ def test_remove_symlink_in_subfolder(self):
+ """Test removing a folder with an contained symlink"""
+ file_path = os.path.join(self.tempdir, *stubs.files[0])
+ dir_path = os.path.dirname(os.path.join(self.tempdir, *stubs.files[1]))
+ symlink_path = os.path.join(dir_path, "symlink")
+
+ os.symlink(file_path, symlink_path)
+ self.assertTrue(os.path.islink(symlink_path))
+
+ # The folder with the contained symlink will be deleted but not the
+ # original linked file
+ mozfile.remove(dir_path)
+ self.assertFalse(os.path.exists(dir_path))
+ self.assertFalse(os.path.exists(symlink_path))
+ self.assertTrue(os.path.exists(file_path))
+
+ @unittest.skipIf(mozinfo.isWin, "Symlinks are not supported on Windows")
+ def test_remove_broken_symlink(self):
+ """Test removing a folder with an contained symlink"""
+ file_path = os.path.join(self.tempdir, "readonly.txt")
+ working_link = os.path.join(self.tempdir, "link_to_readonly.txt")
+ broken_link = os.path.join(self.tempdir, "broken_link")
+ os.symlink(file_path, working_link)
+ os.symlink(os.path.join(self.tempdir, "broken.txt"), broken_link)
+
+ self.assertTrue(os.path.exists(file_path))
+ self.assertTrue(os.path.islink(working_link))
+ self.assertTrue(os.path.islink(broken_link))
+
+ mozfile.remove(working_link)
+ self.assertFalse(os.path.lexists(working_link))
+ self.assertTrue(os.path.exists(file_path))
+
+ mozfile.remove(broken_link)
+ self.assertFalse(os.path.lexists(broken_link))
+
+ @unittest.skipIf(
+ mozinfo.isWin or not os.geteuid(),
+ "Symlinks are not supported on Windows and cannot run test as root",
+ )
+ def test_remove_symlink_for_system_path(self):
+ """Test removing a symlink which points to a system folder"""
+ symlink_path = os.path.join(self.tempdir, "symlink")
+
+ os.symlink(os.path.dirname(self.tempdir), symlink_path)
+ self.assertTrue(os.path.islink(symlink_path))
+
+ # The folder with the contained symlink will be deleted but not the
+ # original linked file
+ mozfile.remove(symlink_path)
+ self.assertFalse(os.path.exists(symlink_path))
+
+ def test_remove_path_that_does_not_exists(self):
+ not_existing_path = os.path.join(self.tempdir, "I_do_not_not_exists")
+ try:
+ mozfile.remove(not_existing_path)
+ except OSError as exc:
+ if exc.errno == errno.ENOENT:
+ self.fail("removing non existing path must not raise error")
+ raise
+
+
+class MozFileMoveTestCase(unittest.TestCase):
+ def setUp(self):
+ # Generate a stub
+ self.tempdir = stubs.create_stub()
+ self.addCleanup(mozfile.rmtree, self.tempdir)
+
+ def test_move_file(self):
+ file_path = os.path.join(self.tempdir, *stubs.files[1])
+ moved_path = file_path + ".moved"
+ self.assertTrue(os.path.isfile(file_path))
+ self.assertFalse(os.path.exists(moved_path))
+ mozfile.move(file_path, moved_path)
+ self.assertFalse(os.path.exists(file_path))
+ self.assertTrue(os.path.isfile(moved_path))
+
+ def test_move_file_with_retry(self):
+ file_path = os.path.join(self.tempdir, *stubs.files[1])
+ moved_path = file_path + ".moved"
+
+ with wait_file_opened_in_thread(file_path, 0.2):
+ # first move attempt should fail on windows and be retried
+ mozfile.move(file_path, moved_path)
+ self.assertFalse(os.path.exists(file_path))
+ self.assertTrue(os.path.isfile(moved_path))
+
+
+if __name__ == "__main__":
+ mozunit.main()
diff --git a/testing/mozbase/mozfile/tests/test_tempdir.py b/testing/mozbase/mozfile/tests/test_tempdir.py
new file mode 100644
index 0000000000..ba16b478b6
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/test_tempdir.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+"""
+tests for mozfile.TemporaryDirectory
+"""
+
+import os
+import unittest
+
+import mozunit
+from mozfile import TemporaryDirectory
+
+
+class TestTemporaryDirectory(unittest.TestCase):
+ def test_removed(self):
+ """ensure that a TemporaryDirectory gets removed"""
+ path = None
+ with TemporaryDirectory() as tmp:
+ path = tmp
+ self.assertTrue(os.path.isdir(tmp))
+ tmpfile = os.path.join(tmp, "a_temp_file")
+ open(tmpfile, "w").write("data")
+ self.assertTrue(os.path.isfile(tmpfile))
+ self.assertFalse(os.path.isdir(path))
+ self.assertFalse(os.path.exists(path))
+
+ def test_exception(self):
+ """ensure that TemporaryDirectory handles exceptions"""
+ path = None
+ with self.assertRaises(Exception):
+ with TemporaryDirectory() as tmp:
+ path = tmp
+ self.assertTrue(os.path.isdir(tmp))
+ raise Exception("oops")
+ self.assertFalse(os.path.isdir(path))
+ self.assertFalse(os.path.exists(path))
+
+
+if __name__ == "__main__":
+ mozunit.main()
diff --git a/testing/mozbase/mozfile/tests/test_tempfile.py b/testing/mozbase/mozfile/tests/test_tempfile.py
new file mode 100644
index 0000000000..3e250d6a76
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/test_tempfile.py
@@ -0,0 +1,105 @@
+#!/usr/bin/env python
+
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+"""
+tests for mozfile.NamedTemporaryFile
+"""
+import os
+import unittest
+
+import mozfile
+import mozunit
+import six
+
+
+class TestNamedTemporaryFile(unittest.TestCase):
+ """test our fix for NamedTemporaryFile"""
+
+ def test_named_temporary_file(self):
+ """Ensure the fix for re-opening a NamedTemporaryFile works
+
+ Refer to https://bugzilla.mozilla.org/show_bug.cgi?id=818777
+ and https://bugzilla.mozilla.org/show_bug.cgi?id=821362
+ """
+
+ test_string = b"A simple test"
+ with mozfile.NamedTemporaryFile() as temp:
+ # Test we can write to file
+ temp.write(test_string)
+ # Forced flush, so that we can read later
+ temp.flush()
+
+ # Test we can open the file again on all platforms
+ self.assertEqual(open(temp.name, "rb").read(), test_string)
+
+ def test_iteration(self):
+ """ensure the line iterator works"""
+
+ # make a file and write to it
+ tf = mozfile.NamedTemporaryFile()
+ notes = [b"doe", b"rae", b"mi"]
+ for note in notes:
+ tf.write(b"%s\n" % note)
+ tf.flush()
+
+ # now read from it
+ tf.seek(0)
+ lines = [line.rstrip(b"\n") for line in tf.readlines()]
+ self.assertEqual(lines, notes)
+
+ # now read from it iteratively
+ lines = []
+ for line in tf:
+ lines.append(line.strip())
+ self.assertEqual(lines, []) # because we did not seek(0)
+ tf.seek(0)
+ lines = []
+ for line in tf:
+ lines.append(line.strip())
+ self.assertEqual(lines, notes)
+
+ def test_delete(self):
+ """ensure ``delete=True/False`` works as expected"""
+
+ # make a deleteable file; ensure it gets cleaned up
+ path = None
+ with mozfile.NamedTemporaryFile(delete=True) as tf:
+ path = tf.name
+ self.assertTrue(isinstance(path, six.string_types))
+ self.assertFalse(os.path.exists(path))
+
+ # it is also deleted when __del__ is called
+ # here we will do so explicitly
+ tf = mozfile.NamedTemporaryFile(delete=True)
+ path = tf.name
+ self.assertTrue(os.path.exists(path))
+ del tf
+ self.assertFalse(os.path.exists(path))
+
+ # Now the same thing but we won't delete the file
+ path = None
+ try:
+ with mozfile.NamedTemporaryFile(delete=False) as tf:
+ path = tf.name
+ self.assertTrue(os.path.exists(path))
+ finally:
+ if path and os.path.exists(path):
+ os.remove(path)
+
+ path = None
+ try:
+ tf = mozfile.NamedTemporaryFile(delete=False)
+ path = tf.name
+ self.assertTrue(os.path.exists(path))
+ del tf
+ self.assertTrue(os.path.exists(path))
+ finally:
+ if path and os.path.exists(path):
+ os.remove(path)
+
+
+if __name__ == "__main__":
+ mozunit.main()
diff --git a/testing/mozbase/mozfile/tests/test_tree.py b/testing/mozbase/mozfile/tests/test_tree.py
new file mode 100644
index 0000000000..eba7d34a6b
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/test_tree.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+# coding=UTF-8
+
+import os
+import shutil
+import tempfile
+import unittest
+
+import mozunit
+from mozfile import tree
+
+
+class TestTree(unittest.TestCase):
+ """Test the tree function."""
+
+ def test_unicode_paths(self):
+ """Test creating tree structure from a Unicode path."""
+ try:
+ tmpdir = tempfile.mkdtemp(suffix=u"tmp🍪")
+ os.mkdir(os.path.join(tmpdir, u"dir🍪"))
+ with open(os.path.join(tmpdir, u"file🍪"), "w") as f:
+ f.write("foo")
+
+ self.assertEqual(u"{}\n├file🍪\n└dir🍪".format(tmpdir), tree(tmpdir))
+ finally:
+ shutil.rmtree(tmpdir)
+
+
+if __name__ == "__main__":
+ mozunit.main()
diff --git a/testing/mozbase/mozfile/tests/test_url.py b/testing/mozbase/mozfile/tests/test_url.py
new file mode 100755
index 0000000000..a19f5f16a8
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/test_url.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+
+"""
+tests for is_url
+"""
+import unittest
+
+import mozunit
+from mozfile import is_url
+
+
+class TestIsUrl(unittest.TestCase):
+ """test the is_url function"""
+
+ def test_is_url(self):
+ self.assertTrue(is_url("http://mozilla.org"))
+ self.assertFalse(is_url("/usr/bin/mozilla.org"))
+ self.assertTrue(is_url("file:///usr/bin/mozilla.org"))
+ self.assertFalse(is_url("c:\foo\bar"))
+
+
+if __name__ == "__main__":
+ mozunit.main()
diff --git a/testing/mozbase/mozfile/tests/test_which.py b/testing/mozbase/mozfile/tests/test_which.py
new file mode 100644
index 0000000000..b02f13ccdf
--- /dev/null
+++ b/testing/mozbase/mozfile/tests/test_which.py
@@ -0,0 +1,63 @@
+# Any copyright is dedicated to the Public Domain.
+# https://creativecommons.org/publicdomain/zero/1.0/
+
+import os
+import sys
+
+import mozunit
+import six
+from mozfile import which
+
+here = os.path.abspath(os.path.dirname(__file__))
+
+
+def test_which(monkeypatch):
+ cwd = os.path.join(here, "files", "which")
+ monkeypatch.chdir(cwd)
+
+ if sys.platform == "win32":
+ if six.PY3:
+ import winreg
+ else:
+ import _winreg as winreg
+ bindir = os.path.join(cwd, "win")
+ monkeypatch.setenv("PATH", bindir)
+ monkeypatch.setattr(winreg, "QueryValue", (lambda k, sk: None))
+
+ assert which("foo.exe").lower() == os.path.join(bindir, "foo.exe").lower()
+ assert which("foo").lower() == os.path.join(bindir, "foo.exe").lower()
+ assert (
+ which("foo", exts=[".FOO", ".BAR"]).lower()
+ == os.path.join(bindir, "foo").lower()
+ )
+ assert os.environ.get("PATHEXT") != [".FOO", ".BAR"]
+ assert which("foo.txt") is None
+
+ assert which("bar").lower() == os.path.join(bindir, "bar").lower()
+ assert which("baz").lower() == os.path.join(cwd, "baz.exe").lower()
+
+ registered_dir = os.path.join(cwd, "registered")
+ quux = os.path.join(registered_dir, "quux.exe").lower()
+
+ def mock_registry(key, subkey):
+ assert subkey == (
+ r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\quux.exe"
+ )
+ return quux
+
+ monkeypatch.setattr(winreg, "QueryValue", mock_registry)
+ assert which("quux").lower() == quux
+ assert which("quux.exe").lower() == quux
+
+ else:
+ bindir = os.path.join(cwd, "unix")
+ monkeypatch.setenv("PATH", bindir)
+ assert which("foo") == os.path.join(bindir, "foo")
+ assert which("baz") is None
+ assert which("baz", exts=[".EXE"]) is None
+ assert "PATHEXT" not in os.environ
+ assert which("file") is None
+
+
+if __name__ == "__main__":
+ mozunit.main()