summaryrefslogtreecommitdiff
path: root/tests/common/pathname.py
blob: 00a084b3e9adce769c630bce0ad7e89c561878db (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

from chrysacase import ChrysalideTestCase
from pychrysalide.common import pathname


class TestPathnames(ChrysalideTestCase):
    """TestCase for common.pathname*"""

    @classmethod
    def setUpClass(cls):

        super(TestPathnames, cls).setUpClass()

        cls._tests = [
            {
                'ref'      : '/a/b/d',
                'target'   : '/a/b/e/f',
                'relative' : 'e/f'
            },
            {
                'ref'      : '/a/b/c/d',
                'target'   : '/a/b/f',
                'relative' : '../f'
            },
            {
                'ref'      : '/a/b/c/d',
                'target'   : '/a/b/c/e',
                'relative' : 'e'
            },
            {
                'ref'      : '/a/bb/c/d',
                'target'   : '/a/b/e/f',
                'relative' : '../../b/e/f'
            },
            {
                'ref'      : '/a/b/c/d',
                'target'   : '/a/bb/e/f',
                'relative' : '../../bb/e/f'
            },
            {
                'ref'      : '/a/b/c/d',
                'target'   : '/f',
                'relative' : '../../../f'
            },
            {
                'ref'      : '/z/b/c/d',
                'target'   : '/a/b/e/f',
                'relative' : '../../../a/b/e/f'
            },
            {
                'ref'      : '/a/bbb/c/d',
                'target'   : '/a/bbc/e/f',
                'relative' : '../../bbc/e/f'
            }
        ]


    def testBuildingRelative(self):
        """Build valid relative paths."""

        build_relative = pathname.build_relative_filename

        for tst in self._tests:

            got = build_relative(tst['ref'], tst['target'])

            self.assertEqual(got, tst['relative'])


    def testBuildingAbsolute(self):
        """Build valid absolute paths."""

        build_absolute = pathname.build_absolute_filename

        for tst in self._tests:

            got = build_absolute(tst['ref'], tst['relative'])

            self.assertEqual(got, tst['target'])


    def testBuildingWrongAbsolute(self):
        """Build invalid absolute paths."""

        build_absolute = pathname.build_absolute_filename

        with self.assertRaisesRegex(Exception, 'Relative path is too deep.'):

            got = build_absolute('/a/b', '../../c')


    def testPathnameConstructor(self):
        """Check that no constructor is available for the pathname class."""

        with self.assertRaisesRegex(NotImplementedError, 'Chrysalide does not allow building this kind of object from Python'):

            instance = pathname()


    def testPathnameSamples(self):
        """Play with some path samples."""

        filename = pathname.build_absolute_filename('/tmp/deep', '../myfile')
        self.assertEqual(filename, '/myfile')

        filename = pathname.build_absolute_filename('/tmp/deep/', '../myfile')
        self.assertEqual(filename, '/tmp/myfile')

        filename = pathname.build_relative_filename('/tmp/deep', '/myfile')
        self.assertEqual(filename, '../myfile')