Spandan Das | aacf237 | 2022-05-11 21:46:29 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2022 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import unittest |
| 18 | |
| 19 | from ninja_syntax import Variable, Rule, RuleException, BuildAction, BuildActionException, Pool |
| 20 | |
| 21 | class TestVariable(unittest.TestCase): |
| 22 | |
| 23 | def test_assignment(self): |
| 24 | variable = Variable(name="key", value="value") |
| 25 | self.assertEqual("key = value", next(variable.stream())) |
| 26 | variable = Variable(name="key", value="value with spaces") |
| 27 | self.assertEqual("key = value with spaces", next(variable.stream())) |
| 28 | variable = Variable(name="key", value="$some_other_variable") |
| 29 | self.assertEqual("key = $some_other_variable", next(variable.stream())) |
| 30 | |
| 31 | def test_indentation(self): |
| 32 | variable = Variable(name="key", value="value", indent=0) |
| 33 | self.assertEqual("key = value", next(variable.stream())) |
| 34 | variable = Variable(name="key", value="value", indent=1) |
| 35 | self.assertEqual(" key = value", next(variable.stream())) |
| 36 | |
| 37 | class TestRule(unittest.TestCase): |
| 38 | |
| 39 | def test_rulename_comes_first(self): |
| 40 | rule = Rule(name="myrule") |
| 41 | rule.add_variable("command", "/bin/bash echo") |
| 42 | self.assertEqual("rule myrule", next(rule.stream())) |
| 43 | |
| 44 | def test_command_is_a_required_variable(self): |
| 45 | rule = Rule(name="myrule") |
| 46 | with self.assertRaises(RuleException): |
| 47 | next(rule.stream()) |
| 48 | |
| 49 | def test_bad_rule_variable(self): |
| 50 | rule = Rule(name="myrule") |
| 51 | with self.assertRaises(RuleException): |
| 52 | rule.add_variable(name="unrecognize_rule_variable", value="value") |
| 53 | |
| 54 | def test_rule_variables_are_indented(self): |
| 55 | rule = Rule(name="myrule") |
| 56 | rule.add_variable("command", "/bin/bash echo") |
| 57 | stream = rule.stream() |
| 58 | self.assertEqual("rule myrule", next(stream)) # top-level rule should not be indented |
| 59 | self.assertEqual(" command = /bin/bash echo", next(stream)) |
| 60 | |
| 61 | def test_rule_variables_are_sorted(self): |
| 62 | rule = Rule(name="myrule") |
| 63 | rule.add_variable("description", "Adding description before command") |
| 64 | rule.add_variable("command", "/bin/bash echo") |
| 65 | stream = rule.stream() |
| 66 | self.assertEqual("rule myrule", next(stream)) # rule always comes first |
| 67 | self.assertEqual(" command = /bin/bash echo", next(stream)) |
| 68 | self.assertEqual(" description = Adding description before command", next(stream)) |
| 69 | |
| 70 | class TestBuildAction(unittest.TestCase): |
| 71 | |
| 72 | def test_no_inputs(self): |
| 73 | build = BuildAction(output="out", rule="phony") |
| 74 | stream = build.stream() |
| 75 | self.assertEqual("build out: phony", next(stream)) |
| 76 | # Empty output |
| 77 | build = BuildAction(output="", rule="phony") |
| 78 | with self.assertRaises(BuildActionException): |
| 79 | next(build.stream()) |
| 80 | # Empty rule |
| 81 | build = BuildAction(output="out", rule="") |
| 82 | with self.assertRaises(BuildActionException): |
| 83 | next(build.stream()) |
| 84 | |
| 85 | def test_inputs(self): |
| 86 | build = BuildAction(output="out", rule="cat", inputs=["input1", "input2"]) |
| 87 | self.assertEqual("build out: cat input1 input2", next(build.stream())) |
| 88 | build = BuildAction(output="out", rule="cat", inputs=["input1", "input2"], implicits=["implicits1", "implicits2"], order_only=["order_only1", "order_only2"]) |
| 89 | self.assertEqual("build out: cat input1 input2 | implicits1 implicits2 || order_only1 order_only2", next(build.stream())) |
| 90 | |
| 91 | def test_variables(self): |
| 92 | build = BuildAction(output="out", rule="cat", inputs=["input1", "input2"]) |
| 93 | build.add_variable(name="myvar", value="myval") |
| 94 | stream = build.stream() |
| 95 | next(stream) |
| 96 | self.assertEqual(" myvar = myval", next(stream)) |
| 97 | |
| 98 | class TestPool(unittest.TestCase): |
| 99 | |
| 100 | def test_pool(self): |
| 101 | pool = Pool(name="mypool", depth=10) |
| 102 | stream = pool.stream() |
| 103 | self.assertEqual("pool mypool", next(stream)) |
| 104 | self.assertEqual(" depth = 10", next(stream)) |
| 105 | |
| 106 | if __name__ == "__main__": |
| 107 | unittest.main() |