mirror_path_test.py [plain text]
__author__ = "Nils Klarlund"
import os
import os.path
import basics
import cache_basics
import mirror_path
import shutil
import tempfile
import unittest
NotCoveredError = basics.NotCoveredError
class MirrorPathTest(unittest.TestCase):
"""We construct a mock-up world of a file system in order to
unittest the DoPath function of mirror_path."""
def setUp(self):
basics.debug_pattern = 3
self.tmp = tempfile.mkdtemp()
caches = cache_basics.SetUpCaches(self.tmp)
self.canonical_path = caches.canonical_path
self.simple_build_stat = caches.simple_build_stat
self.mirror_path = mirror_path.MirrorPath(self.simple_build_stat,
self.canonical_path,
caches.realpath_map,
caches.systemdir_prefix_cache)
self.directories = ['/', '/a', '/link', '/a/link', '/a/b',
'/link/link', '/root']
self.links = ['/a/link', '/link', '/link/link']
self.exists = self.directories + self.links
self.realpaths = {'/' :'/',
'/a' :'/a',
'/a/link' :'/a/b',
'/link' :'/a',
'/link/link':'/a/b'}
def tearDown(self):
shutil.rmtree(self.tmp)
def test_MirrorPath(self):
try:
def isdir(path):
return path in self.directories
def exists(path):
return path in self.exists
def islink(path):
return path in self.links
def realpath(path):
if path.startswith('/root'):
self.fail("Not expected that '%s' startd with '/root'." % path)
return self.realpaths[path]
def makedirs(path):
if path == '/root/a':
self.directories.extend(['/root/a'])
self.exists.extend(['/root/a'])
else:
self.fail("makedirs %s" % path)
def symlink(src, dest):
if not (src, dest) in [ ('/a', '/root/link'),
('/a/b', '/root/a/link') ]:
self.fail("symlink %s %s" % (src, dest))
self.links.append(dest)
self.exists.append(dest)
self.mirror_path.canonical_path.Canonicalize = realpath
isdir_ = os.path.isdir
os.path.isdir = isdir
exists_ = os.path.exists
os.path.exists = exists
islink_ = os.path.islink
os.path.islink = islink
makedirs_ = os.makedirs
os.makedirs = makedirs
symlink_ = os.symlink
os.symlink = symlink
self.mirror_path.DoPath('/a/link', 117, '/root')
self.assertEqual(self.mirror_path.Links(), ['/root/a/link'])
self.assert_(self.simple_build_stat.Lookup('/root/a'))
self.mirror_path.DoPath('/a/link', 117, '/root')
self.assertEqual(self.mirror_path.Links(), ['/root/a/link'])
self.mirror_path.DoPath('/link/link', 117, '/root')
self.assertEqual(self.mirror_path.Links(), ['/root/a/link', '/root/link'])
self.assertEqual(
[ d for d in self.directories if d.startswith('/root') ],
[ '/root', '/root/a' ])
self.assertEqual(
[ d for d in self.links if d.startswith('/root') ],
[ '/root/a/link', '/root/link' ])
self.mirror_path.DoPath('/a/b', 117, '/root')
self.assertEqual(self.mirror_path.Links(), ['/root/a/link', '/root/link'])
self.assertEqual(
[ d for d in self.directories if d.startswith('/root') ],
[ '/root', '/root/a' ])
self.assertEqual(
[ d for d in self.links if d.startswith('/root') ],
[ '/root/a/link', '/root/link' ])
finally:
try:
os.path.isdir = isdir_
os.path.exists = exists_
os.path.islink = islink_
os.makedirs = makedirs_
os.symlink = symlink_
except NameError:
pass
unittest.main()