summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozpack/test/test_path.py
blob: daee5df0e4bc39ccb3f9ec6c4d21b80dd0fe76f7 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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/.

import os
import unittest

import mozunit
from mozpack.path import (
    basedir,
    basename,
    commonprefix,
    dirname,
    join,
    match,
    normpath,
    rebase,
    relpath,
    split,
    splitext,
)


class TestPath(unittest.TestCase):
    SEP = os.sep

    def test_relpath(self):
        self.assertEqual(relpath("foo", "foo"), "")
        self.assertEqual(relpath(self.SEP.join(("foo", "bar")), "foo/bar"), "")
        self.assertEqual(relpath(self.SEP.join(("foo", "bar")), "foo"), "bar")
        self.assertEqual(
            relpath(self.SEP.join(("foo", "bar", "baz")), "foo"), "bar/baz"
        )
        self.assertEqual(relpath(self.SEP.join(("foo", "bar")), "foo/bar/baz"), "..")
        self.assertEqual(relpath(self.SEP.join(("foo", "bar")), "foo/baz"), "../bar")
        self.assertEqual(relpath("foo/", "foo"), "")
        self.assertEqual(relpath("foo/bar/", "foo"), "bar")

    def test_join(self):
        self.assertEqual(join("foo", "bar", "baz"), "foo/bar/baz")
        self.assertEqual(join("foo", "", "bar"), "foo/bar")
        self.assertEqual(join("", "foo", "bar"), "foo/bar")
        self.assertEqual(join("", "foo", "/bar"), "/bar")

    def test_normpath(self):
        self.assertEqual(
            normpath(self.SEP.join(("foo", "bar", "baz", "..", "qux"))), "foo/bar/qux"
        )

    def test_dirname(self):
        self.assertEqual(dirname("foo/bar/baz"), "foo/bar")
        self.assertEqual(dirname("foo/bar"), "foo")
        self.assertEqual(dirname("foo"), "")
        self.assertEqual(dirname("foo/bar/"), "foo/bar")

    def test_commonprefix(self):
        self.assertEqual(
            commonprefix(
                [self.SEP.join(("foo", "bar", "baz")), "foo/qux", "foo/baz/qux"]
            ),
            "foo/",
        )
        self.assertEqual(
            commonprefix([self.SEP.join(("foo", "bar", "baz")), "foo/qux", "baz/qux"]),
            "",
        )

    def test_basename(self):
        self.assertEqual(basename("foo/bar/baz"), "baz")
        self.assertEqual(basename("foo/bar"), "bar")
        self.assertEqual(basename("foo"), "foo")
        self.assertEqual(basename("foo/bar/"), "")

    def test_split(self):
        self.assertEqual(
            split(self.SEP.join(("foo", "bar", "baz"))), ["foo", "bar", "baz"]
        )

    def test_splitext(self):
        self.assertEqual(
            splitext(self.SEP.join(("foo", "bar", "baz.qux"))), ("foo/bar/baz", ".qux")
        )

    def test_basedir(self):
        foobarbaz = self.SEP.join(("foo", "bar", "baz"))
        self.assertEqual(basedir(foobarbaz, ["foo", "bar", "baz"]), "foo")
        self.assertEqual(basedir(foobarbaz, ["foo", "foo/bar", "baz"]), "foo/bar")
        self.assertEqual(basedir(foobarbaz, ["foo/bar", "foo", "baz"]), "foo/bar")
        self.assertEqual(basedir(foobarbaz, ["foo", "bar", ""]), "foo")
        self.assertEqual(basedir(foobarbaz, ["bar", "baz", ""]), "")

    def test_match(self):
        self.assertTrue(match("foo", ""))
        self.assertTrue(match("foo/bar/baz.qux", "foo/bar"))
        self.assertTrue(match("foo/bar/baz.qux", "foo"))
        self.assertTrue(match("foo", "*"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/bar/*"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/bar/*"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/bar/*"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/bar/*"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/*/baz.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "*/bar/baz.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "*/*/baz.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "*/*/*"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/*/*"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/*/*.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/b*/*z.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/b*r/ba*z.qux"))
        self.assertFalse(match("foo/bar/baz.qux", "foo/b*z/ba*r.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "**"))
        self.assertTrue(match("foo/bar/baz.qux", "**/baz.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "**/bar/baz.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/**/baz.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/**/*.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "**/foo/bar/baz.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/**/bar/baz.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/**/bar/*.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/**/*.qux"))
        self.assertTrue(match("foo/bar/baz.qux", "**/*.qux"))
        self.assertFalse(match("foo/bar/baz.qux", "**.qux"))
        self.assertFalse(match("foo/bar", "foo/*/bar"))
        self.assertTrue(match("foo/bar/baz.qux", "foo/**/bar/**"))
        self.assertFalse(match("foo/nobar/baz.qux", "foo/**/bar/**"))
        self.assertTrue(match("foo/bar", "foo/**/bar/**"))

    def test_rebase(self):
        self.assertEqual(rebase("foo", "foo/bar", "bar/baz"), "baz")
        self.assertEqual(rebase("foo", "foo", "bar/baz"), "bar/baz")
        self.assertEqual(rebase("foo/bar", "foo", "baz"), "bar/baz")


if os.altsep:

    class TestAltPath(TestPath):
        SEP = os.altsep

    class TestReverseAltPath(TestPath):
        def setUp(self):
            sep = os.sep
            os.sep = os.altsep
            os.altsep = sep

        def tearDown(self):
            self.setUp()

    class TestAltReverseAltPath(TestReverseAltPath):
        SEP = os.altsep


if __name__ == "__main__":
    mozunit.main()