summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/test/test_makeutil.py
blob: 524851bfbd3742612df81aa92f4c26e657d8088b (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
152
153
154
155
156
157
158
159
160
161
162
163
164
# 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

from mozunit import main
from six import StringIO

from mozbuild.makeutil import Makefile, Rule, read_dep_makefile, write_dep_makefile


class TestMakefile(unittest.TestCase):
    def test_rule(self):
        out = StringIO()
        rule = Rule()
        rule.dump(out)
        self.assertEqual(out.getvalue(), "")

        out = StringIO()
        rule.add_targets(["foo", "bar"])
        rule.dump(out)
        self.assertEqual(out.getvalue(), "foo bar:\n")

        out = StringIO()
        rule.add_targets(["baz"])
        rule.add_dependencies(["qux", "hoge", "piyo"])
        rule.dump(out)
        self.assertEqual(out.getvalue(), "foo bar baz: qux hoge piyo\n")

        out = StringIO()
        rule = Rule(["foo", "bar"])
        rule.add_dependencies(["baz"])
        rule.add_commands(["echo $@"])
        rule.add_commands(["$(BAZ) -o $@ $<", "$(TOUCH) $@"])
        rule.dump(out)
        self.assertEqual(
            out.getvalue(),
            "foo bar: baz\n"
            + "\techo $@\n"
            + "\t$(BAZ) -o $@ $<\n"
            + "\t$(TOUCH) $@\n",
        )

        out = StringIO()
        rule = Rule(["foo"])
        rule.add_dependencies(["bar", "foo", "baz"])
        rule.dump(out)
        self.assertEqual(out.getvalue(), "foo: bar baz\n")

        out = StringIO()
        rule.add_targets(["bar"])
        rule.dump(out)
        self.assertEqual(out.getvalue(), "foo bar: baz\n")

        out = StringIO()
        rule.add_targets(["bar"])
        rule.dump(out)
        self.assertEqual(out.getvalue(), "foo bar: baz\n")

        out = StringIO()
        rule.add_dependencies(["bar"])
        rule.dump(out)
        self.assertEqual(out.getvalue(), "foo bar: baz\n")

        out = StringIO()
        rule.add_dependencies(["qux"])
        rule.dump(out)
        self.assertEqual(out.getvalue(), "foo bar: baz qux\n")

        out = StringIO()
        rule.add_dependencies(["qux"])
        rule.dump(out)
        self.assertEqual(out.getvalue(), "foo bar: baz qux\n")

        out = StringIO()
        rule.add_dependencies(["hoge", "hoge"])
        rule.dump(out)
        self.assertEqual(out.getvalue(), "foo bar: baz qux hoge\n")

        out = StringIO()
        rule.add_targets(["fuga", "fuga"])
        rule.dump(out)
        self.assertEqual(out.getvalue(), "foo bar fuga: baz qux hoge\n")

    def test_makefile(self):
        out = StringIO()
        mk = Makefile()
        rule = mk.create_rule(["foo"])
        rule.add_dependencies(["bar", "baz", "qux"])
        rule.add_commands(["echo foo"])
        rule = mk.create_rule().add_targets(["bar", "baz"])
        rule.add_dependencies(["hoge"])
        rule.add_commands(["echo $@"])
        mk.dump(out, removal_guard=False)
        self.assertEqual(
            out.getvalue(),
            "foo: bar baz qux\n" + "\techo foo\n" + "bar baz: hoge\n" + "\techo $@\n",
        )

        out = StringIO()
        mk.dump(out)
        self.assertEqual(
            out.getvalue(),
            "foo: bar baz qux\n"
            + "\techo foo\n"
            + "bar baz: hoge\n"
            + "\techo $@\n"
            + "hoge qux:\n",
        )

    def test_statement(self):
        out = StringIO()
        mk = Makefile()
        mk.create_rule(["foo"]).add_dependencies(["bar"]).add_commands(["echo foo"])
        mk.add_statement("BAR = bar")
        mk.create_rule(["$(BAR)"]).add_commands(["echo $@"])
        mk.dump(out, removal_guard=False)
        self.assertEqual(
            out.getvalue(),
            "foo: bar\n" + "\techo foo\n" + "BAR = bar\n" + "$(BAR):\n" + "\techo $@\n",
        )

    @unittest.skipIf(os.name != "nt", "Test only applicable on Windows.")
    def test_path_normalization(self):
        out = StringIO()
        mk = Makefile()
        rule = mk.create_rule(["c:\\foo"])
        rule.add_dependencies(["c:\\bar", "c:\\baz\\qux"])
        rule.add_commands(["echo c:\\foo"])
        mk.dump(out)
        self.assertEqual(
            out.getvalue(),
            "c:/foo: c:/bar c:/baz/qux\n" + "\techo c:\\foo\n" + "c:/bar c:/baz/qux:\n",
        )

    def test_read_dep_makefile(self):
        input = StringIO(
            os.path.abspath("foo")
            + ": bar\n"
            + "baz qux: \\ \n"
            + "hoge \\\n"
            + "piyo \\\n"
            + "fuga\n"
            + "fuga:\n"
        )
        result = list(read_dep_makefile(input))
        self.assertEqual(len(result), 2)
        self.assertEqual(
            list(result[0].targets()), [os.path.abspath("foo").replace(os.sep, "/")]
        )
        self.assertEqual(list(result[0].dependencies()), ["bar"])
        self.assertEqual(list(result[1].targets()), ["baz", "qux"])
        self.assertEqual(list(result[1].dependencies()), ["hoge", "piyo", "fuga"])

    def test_write_dep_makefile(self):
        out = StringIO()
        write_dep_makefile(out, "target", ["b", "c", "a"])
        self.assertEqual(out.getvalue(), "target: b c a\n" + "a b c:\n")


if __name__ == "__main__":
    main()