summaryrefslogtreecommitdiffstats
path: root/python/mach/mach/test/test_conditions.py
blob: 5775790e692aa9cd2836b9bf450281562de5c768 (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
# 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 pathlib import Path

from buildconfig import topsrcdir
from mozunit import main

from mach.base import MachError
from mach.main import Mach
from mach.registrar import Registrar
from mach.test.conftest import PROVIDER_DIR, TestBase


def _make_populate_context(include_extra_attributes):
    def _populate_context(key=None):
        if key is None:
            return

        attributes = {
            "topdir": topsrcdir,
        }
        if include_extra_attributes:
            attributes["foo"] = True
            attributes["bar"] = False

        try:
            return attributes[key]
        except KeyError:
            raise AttributeError(key)

    return _populate_context


_populate_bare_context = _make_populate_context(False)
_populate_context = _make_populate_context(True)


class TestConditions(TestBase):
    """Tests for conditionally filtering commands."""

    def _run(self, args, context_handler=_populate_bare_context):
        return self._run_mach(
            args, Path("conditions.py"), context_handler=context_handler
        )

    def test_conditions_pass(self):
        """Test that a command which passes its conditions is runnable."""

        self.assertEqual((0, "", ""), self._run(["cmd_condition_true"]))
        self.assertEqual((0, "", ""), self._run(["cmd_foo_ctx"], _populate_context))

    def test_invalid_context_message(self):
        """Test that commands which do not pass all their conditions
        print the proper failure message."""

        def is_bar():
            """Bar must be true"""

        fail_conditions = [is_bar]

        for name in ("cmd_condition_false", "cmd_condition_true_and_false"):
            result, stdout, stderr = self._run([name])
            self.assertEqual(1, result)

            fail_msg = Registrar._condition_failed_message(name, fail_conditions)
            self.assertEqual(fail_msg.rstrip(), stdout.rstrip())

        for name in ("cmd_bar_ctx", "cmd_foobar_ctx"):
            result, stdout, stderr = self._run([name], _populate_context)
            self.assertEqual(1, result)

            fail_msg = Registrar._condition_failed_message(name, fail_conditions)
            self.assertEqual(fail_msg.rstrip(), stdout.rstrip())

    def test_invalid_type(self):
        """Test that a condition which is not callable raises an exception."""

        m = Mach(str(Path.cwd()))
        m.define_category("testing", "Mach unittest", "Testing for mach core", 10)
        self.assertRaises(
            MachError,
            m.load_commands_from_file,
            PROVIDER_DIR / "conditions_invalid.py",
        )

    def test_help_message(self):
        """Test that commands that are not runnable do not show up in help."""

        result, stdout, stderr = self._run(["help"], _populate_context)
        self.assertIn("cmd_condition_true", stdout)
        self.assertNotIn("cmd_condition_false", stdout)
        self.assertNotIn("cmd_condition_true_and_false", stdout)
        self.assertIn("cmd_foo_ctx", stdout)
        self.assertNotIn("cmd_bar_ctx", stdout)
        self.assertNotIn("cmd_foobar_ctx", stdout)


if __name__ == "__main__":
    main()