summaryrefslogtreecommitdiff
path: root/python/mozbuild/dumbmake/test/test_dumbmake.py
blob: 1172117aaf59e914243bf9e5e72c2705a150df15 (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
# 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/.
from __future__ import unicode_literals

import unittest

from mozunit import (
    main,
)

from dumbmake.dumbmake import (
    add_extra_dependencies,
    all_dependencies,
    dependency_map,
    indentation,
)

class TestDumbmake(unittest.TestCase):
    def test_indentation(self):
        self.assertEqual(indentation(""), 0)
        self.assertEqual(indentation("x"), 0)
        self.assertEqual(indentation(" x"), 1)
        self.assertEqual(indentation("\tx"), 1)
        self.assertEqual(indentation(" \tx"), 2)
        self.assertEqual(indentation("\t x"), 2)
        self.assertEqual(indentation(" x  "), 1)
        self.assertEqual(indentation("\tx\t"), 1)
        self.assertEqual(indentation("  x"), 2)
        self.assertEqual(indentation("    x"), 4)

    def test_dependency_map(self):
        self.assertEqual(dependency_map([]), {})
        self.assertEqual(dependency_map(["a"]), {"a": []})
        self.assertEqual(dependency_map(["a", "b"]), {"a": [], "b": []})
        self.assertEqual(dependency_map(["a", "b", "c"]), {"a": [], "b": [], "c": []})
        # indentation
        self.assertEqual(dependency_map(["a", "\tb", "a", "\tc"]), {"a": [], "b": ["a"], "c": ["a"]})
        self.assertEqual(dependency_map(["a", "\tb", "\t\tc"]), {"a": [], "b": ["a"], "c": ["b", "a"]})
        self.assertEqual(dependency_map(["a", "\tb", "\t\tc", "\td", "\te", "f"]), {"a": [], "b": ["a"], "c": ["b", "a"], "d": ["a"], "e": ["a"], "f": []})
        # irregular indentation
        self.assertEqual(dependency_map(["\ta", "b"]), {"a": [], "b": []})
        self.assertEqual(dependency_map(["a", "\t\t\tb", "\t\tc"]), {"a": [], "b": ["a"], "c": ["a"]})
        self.assertEqual(dependency_map(["a", "\t\tb", "\t\t\tc", "\t\td", "\te", "f"]), {"a": [], "b": ["a"], "c": ["b", "a"], "d": ["a"], "e": ["a"], "f": []})
        # repetitions
        self.assertEqual(dependency_map(["a", "\tb", "a", "\tb"]), {"a": [], "b": ["a"]})
        self.assertEqual(dependency_map(["a", "\tb", "\t\tc", "b", "\td", "\t\te"]), {"a": [], "b": ["a"], "d": ["b"], "e": ["d", "b"], "c": ["b", "a"]})
        # cycles are okay
        self.assertEqual(dependency_map(["a", "\tb", "\t\ta"]), {"a": ["b", "a"], "b": ["a"]})

    def test_all_dependencies(self):
        dm = {"a": [], "b": ["a"], "c": ["b", "a"], "d": ["a"], "e": ["a"], "f": []}
        self.assertEqual(all_dependencies("a", dependency_map=dm), [])
        self.assertEqual(all_dependencies("b", dependency_map=dm), ["a"])
        self.assertEqual(all_dependencies("c", "a", "b", dependency_map=dm), ["b", "a"])
        self.assertEqual(all_dependencies("d", dependency_map=dm), ["a"])
        self.assertEqual(all_dependencies("d", "f", "c", dependency_map=dm), ["b", "a"])
        self.assertEqual(all_dependencies("a", "b", dependency_map=dm), ["a"])
        self.assertEqual(all_dependencies("b", "b", dependency_map=dm), ["a"])

    def test_missing_entry(self):
        # a depends on b, which is missing
        dm = {"a": ["b"]}
        self.assertEqual(all_dependencies("a", dependency_map=dm), ["b"])
        self.assertEqual(all_dependencies("a", "b", dependency_map=dm), ["b"])
        self.assertEqual(all_dependencies("b", dependency_map=dm), [])

    def test_two_dependencies(self):
        dm = {"a": ["c"], "b": ["c"], "c": []}
        # suppose a and b both depend on c.  Then we want to build a and b before c...
        self.assertEqual(all_dependencies("a", "b", dependency_map=dm), ["c"])
        # ... but relative order is preserved.
        self.assertEqual(all_dependencies("b", "a", dependency_map=dm), ["c"])

    def test_nested_dependencies(self):
        # a depends on b depends on c depends on d
        dm = {"a": ["b", "c", "d"], "b": ["c", "d"], "c": ["d"]}
        self.assertEqual(all_dependencies("b", "a", dependency_map=dm), ["b", "c", "d"])
        self.assertEqual(all_dependencies("c", "a", dependency_map=dm), ["b", "c", "d"])

    def test_add_extra_dependencies(self):
        # a depends on b depends on c depends on d
        dm = {"a": ["b", "c", "d"], "b": ["c", "d"], "c": ["d"]}
        # Edge cases.
        self.assertEqual(list(add_extra_dependencies([], dependency_map=dm)),
                         [])
        self.assertEqual(list(add_extra_dependencies([(None, "package")], dependency_map=dm)),
                         [(None, "package")])
        # Easy expansion.
        self.assertEqual(list(add_extra_dependencies([("b", None)], dependency_map=dm)),
                         [("b", None), ("c", None), ("d", None)])
        # Expansion with two groups -- each group is handled independently.
        self.assertEqual(list(add_extra_dependencies([("b", None),
                                                      (None, "package"),
                                                      ("c", None)], dependency_map=dm)),
                         [("b", None), (None, "package"),
                          ("c", None), ("d", None)])
        # Two groups, no duplicate dependencies in each group.
        self.assertEqual(list(add_extra_dependencies([("a", None), ("b", None),
                                                      (None, "package"), (None, "install"),
                                                      ("c", None), ("d", None)], dependency_map=dm)),
                         [("a", None), ("b", None), (None, "package"),
                          (None, "install"), ("c", None), ("d", None)])

if __name__ == '__main__':
    main()