summaryrefslogtreecommitdiff
path: root/repoze/bfg/tests/test_path.py
blob: 42ab1f8fede44c96154d5155f345e05c222fe739 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import unittest

class TestCallerPath(unittest.TestCase):
    def _callFUT(self, path, level=2, package_globals=None):
        from repoze.bfg.path import caller_path
        return caller_path(path, level, package_globals)

    def test_isabs(self):
        self.assertEqual(self._callFUT('/a/b/c'), '/a/b/c')

    def test_pkgrelative(self):
        import os
        here = os.path.abspath(os.path.dirname(__file__))
        self.assertEqual(self._callFUT('a/b/c'), os.path.join(here, 'a/b/c'))

    def test_memoization_has_bfg_abspath(self):
        import os
        here = os.path.abspath(os.path.dirname(__file__))
        package_globals =  {'__bfg_abspath__':'/foo/bar'}
        self.assertEqual(
            self._callFUT('a/b/c',
                          package_globals=package_globals),
            os.path.join('/foo/bar', 'a/b/c'))

    def test_memoization_success(self):
        import os
        here = os.path.abspath(os.path.dirname(__file__))
        package_globals = {'__name__':'repoze.bfg.tests.test_path'}
        self.assertEqual(
            self._callFUT('a/b/c',
                          package_globals=package_globals),
            os.path.join(here, 'a/b/c'))
        self.assertEqual(package_globals['__bfg_abspath__'], here)
        
    def test_memoization_fail(self):
        import os
        here = os.path.abspath(os.path.dirname(__file__))
        class faildict(dict):
            def __setitem__(self, *arg):
                raise KeyError('name')
        package_globals = faildict({'__name__':'repoze.bfg.tests.test_path'})
        self.assertEqual(
            self._callFUT('a/b/c',
                          package_globals=package_globals),
            os.path.join(here, 'a/b/c'))
        self.failIf('__bfg_abspath__' in package_globals)