1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 from mozunit
import main
9 from six
import StringIO
11 from mozbuild
.makeutil
import Makefile
, Rule
, read_dep_makefile
, write_dep_makefile
14 class TestMakefile(unittest
.TestCase
):
19 self
.assertEqual(out
.getvalue(), "")
22 rule
.add_targets(["foo", "bar"])
24 self
.assertEqual(out
.getvalue(), "foo bar:\n")
27 rule
.add_targets(["baz"])
28 rule
.add_dependencies(["qux", "hoge", "piyo"])
30 self
.assertEqual(out
.getvalue(), "foo bar baz: qux hoge piyo\n")
33 rule
= Rule(["foo", "bar"])
34 rule
.add_dependencies(["baz"])
35 rule
.add_commands(["echo $@"])
36 rule
.add_commands(["$(BAZ) -o $@ $<", "$(TOUCH) $@"])
42 + "\t$(BAZ) -o $@ $<\n"
48 rule
.add_dependencies(["bar", "foo", "baz"])
50 self
.assertEqual(out
.getvalue(), "foo: bar baz\n")
53 rule
.add_targets(["bar"])
55 self
.assertEqual(out
.getvalue(), "foo bar: baz\n")
58 rule
.add_targets(["bar"])
60 self
.assertEqual(out
.getvalue(), "foo bar: baz\n")
63 rule
.add_dependencies(["bar"])
65 self
.assertEqual(out
.getvalue(), "foo bar: baz\n")
68 rule
.add_dependencies(["qux"])
70 self
.assertEqual(out
.getvalue(), "foo bar: baz qux\n")
73 rule
.add_dependencies(["qux"])
75 self
.assertEqual(out
.getvalue(), "foo bar: baz qux\n")
78 rule
.add_dependencies(["hoge", "hoge"])
80 self
.assertEqual(out
.getvalue(), "foo bar: baz qux hoge\n")
83 rule
.add_targets(["fuga", "fuga"])
85 self
.assertEqual(out
.getvalue(), "foo bar fuga: baz qux hoge\n")
87 def test_makefile(self
):
90 rule
= mk
.create_rule(["foo"])
91 rule
.add_dependencies(["bar", "baz", "qux"])
92 rule
.add_commands(["echo foo"])
93 rule
= mk
.create_rule().add_targets(["bar", "baz"])
94 rule
.add_dependencies(["hoge"])
95 rule
.add_commands(["echo $@"])
96 mk
.dump(out
, removal_guard
=False)
99 "foo: bar baz qux\n" + "\techo foo\n" + "bar baz: hoge\n" + "\techo $@\n",
113 def test_statement(self
):
116 mk
.create_rule(["foo"]).add_dependencies(["bar"]).add_commands(["echo foo"])
117 mk
.add_statement("BAR = bar")
118 mk
.create_rule(["$(BAR)"]).add_commands(["echo $@"])
119 mk
.dump(out
, removal_guard
=False)
122 "foo: bar\n" + "\techo foo\n" + "BAR = bar\n" + "$(BAR):\n" + "\techo $@\n",
125 @unittest.skipIf(os
.name
!= "nt", "Test only applicable on Windows.")
126 def test_path_normalization(self
):
129 rule
= mk
.create_rule(["c:\\foo"])
130 rule
.add_dependencies(["c:\\bar", "c:\\baz\\qux"])
131 rule
.add_commands(["echo c:\\foo"])
135 "c:/foo: c:/bar c:/baz/qux\n" + "\techo c:\\foo\n" + "c:/bar c:/baz/qux:\n",
138 def test_read_dep_makefile(self
):
140 os
.path
.abspath("foo")
148 result
= list(read_dep_makefile(input))
149 self
.assertEqual(len(result
), 2)
151 list(result
[0].targets()), [os
.path
.abspath("foo").replace(os
.sep
, "/")]
153 self
.assertEqual(list(result
[0].dependencies()), ["bar"])
154 self
.assertEqual(list(result
[1].targets()), ["baz", "qux"])
155 self
.assertEqual(list(result
[1].dependencies()), ["hoge", "piyo", "fuga"])
157 def test_write_dep_makefile(self
):
159 write_dep_makefile(out
, "target", ["b", "c", "a"])
160 self
.assertEqual(out
.getvalue(), "target: b c a\n" + "a b c:\n")
163 if __name__
== "__main__":