tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_convert_symlinks.py (4758B)


      1 #!/usr/bin/env python
      2 
      3 # This Source Code Form is subject to the terms of the Mozilla Public
      4 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 # You can obtain one at http://mozilla.org/MPL/2.0/.
      6 
      7 import os
      8 import shutil
      9 import tempfile
     10 import unittest
     11 
     12 import mozunit
     13 from manifestparser import ManifestParser, convert
     14 
     15 
     16 class TestSymlinkConversion(unittest.TestCase):
     17    """
     18    test conversion of a directory tree with symlinks to a manifest structure
     19    """
     20 
     21    def create_stub(self, directory=None):
     22        """stub out a directory with files in it"""
     23 
     24        files = ("foo", "bar", "fleem")
     25        if directory is None:
     26            directory = tempfile.mkdtemp()
     27        for i in files:
     28            open(os.path.join(directory, i), "w").write(i)
     29        subdir = os.path.join(directory, "subdir")
     30        os.mkdir(subdir)
     31        open(os.path.join(subdir, "subfile"), "w").write("baz")
     32        return directory
     33 
     34    def test_relpath(self):
     35        """test convert `relative_to` functionality"""
     36 
     37        oldcwd = os.getcwd()
     38        stub = self.create_stub()
     39        try:
     40            # subdir with in-memory manifest
     41            files = ["../bar", "../fleem", "../foo", "subfile"]
     42            subdir = os.path.join(stub, "subdir")
     43            os.chdir(subdir)
     44            parser = convert([stub], relative_to=".")
     45            self.assertEqual([i["name"] for i in parser.tests], files)
     46        except BaseException:
     47            raise
     48        finally:
     49            shutil.rmtree(stub)
     50            os.chdir(oldcwd)
     51 
     52    @unittest.skipIf(
     53        not hasattr(os, "symlink"), "symlinks unavailable on this platform"
     54    )
     55    def test_relpath_symlink(self):
     56        """
     57        Ensure `relative_to` works in a symlink.
     58        Not available on windows.
     59        """
     60 
     61        oldcwd = os.getcwd()
     62        workspace = tempfile.mkdtemp()
     63        try:
     64            tmpdir = os.path.join(workspace, "directory")
     65            os.makedirs(tmpdir)
     66            linkdir = os.path.join(workspace, "link")
     67            os.symlink(tmpdir, linkdir)
     68            self.create_stub(tmpdir)
     69 
     70            # subdir with in-memory manifest
     71            files = ["../bar", "../fleem", "../foo", "subfile"]
     72            subdir = os.path.join(linkdir, "subdir")
     73            os.chdir(os.path.realpath(subdir))
     74            for directory in (tmpdir, linkdir):
     75                parser = convert([directory], relative_to=".")
     76                self.assertEqual([i["name"] for i in parser.tests], files)
     77        finally:
     78            shutil.rmtree(workspace)
     79            os.chdir(oldcwd)
     80 
     81        # a more complicated example
     82        oldcwd = os.getcwd()
     83        workspace = tempfile.mkdtemp()
     84        try:
     85            tmpdir = os.path.join(workspace, "directory")
     86            os.makedirs(tmpdir)
     87            linkdir = os.path.join(workspace, "link")
     88            os.symlink(tmpdir, linkdir)
     89            self.create_stub(tmpdir)
     90            files = ["../bar", "../fleem", "../foo", "subfile"]
     91            subdir = os.path.join(linkdir, "subdir")
     92            subsubdir = os.path.join(subdir, "sub")
     93            os.makedirs(subsubdir)
     94            linksubdir = os.path.join(linkdir, "linky")
     95            linksubsubdir = os.path.join(subsubdir, "linky")
     96            os.symlink(subdir, linksubdir)
     97            os.symlink(subdir, linksubsubdir)
     98            for dest in (subdir,):
     99                os.chdir(dest)
    100                for directory in (tmpdir, linkdir):
    101                    parser = convert([directory], relative_to=".")
    102                    self.assertEqual([i["name"] for i in parser.tests], files)
    103        finally:
    104            shutil.rmtree(workspace)
    105            os.chdir(oldcwd)
    106 
    107    @unittest.skipIf(
    108        not hasattr(os, "symlink"), "symlinks unavailable on this platform"
    109    )
    110    def test_recursion_symlinks(self):
    111        workspace = tempfile.mkdtemp()
    112        self.addCleanup(shutil.rmtree, workspace)
    113 
    114        # create two dirs
    115        os.makedirs(os.path.join(workspace, "dir1"))
    116        os.makedirs(os.path.join(workspace, "dir2"))
    117 
    118        # create cyclical symlinks
    119        os.symlink(os.path.join("..", "dir1"), os.path.join(workspace, "dir2", "ldir1"))
    120        os.symlink(os.path.join("..", "dir2"), os.path.join(workspace, "dir1", "ldir2"))
    121 
    122        # create one file in each dir
    123        open(os.path.join(workspace, "dir1", "f1.txt"), "a").close()
    124        open(os.path.join(workspace, "dir1", "ldir2", "f2.txt"), "a").close()
    125 
    126        data = []
    127 
    128        def callback(rootdirectory, directory, subdirs, files):
    129            for f in files:
    130                data.append(f)
    131 
    132        ManifestParser._walk_directories([workspace], callback)
    133        self.assertEqual(sorted(data), ["f1.txt", "f2.txt"])
    134 
    135 
    136 if __name__ == "__main__":
    137    mozunit.main()