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/.
4
5
6import unittest
7from gecko_taskgraph.util.attributes import (
8    attrmatch,
9    match_run_on_projects,
10)
11from mozunit import main
12
13
14class Attrmatch(unittest.TestCase):
15    def test_trivial_match(self):
16        """Given no conditions, anything matches"""
17        self.assertTrue(attrmatch({}))
18
19    def test_missing_attribute(self):
20        """If a filtering attribute is not present, no match"""
21        self.assertFalse(attrmatch({}, someattr=10))
22
23    def test_literal_attribute(self):
24        """Literal attributes must match exactly"""
25        self.assertTrue(attrmatch({"att": 10}, att=10))
26        self.assertFalse(attrmatch({"att": 10}, att=20))
27
28    def test_set_attribute(self):
29        """Set attributes require set membership"""
30        self.assertTrue(attrmatch({"att": 10}, att={9, 10}))
31        self.assertFalse(attrmatch({"att": 10}, att={19, 20}))
32
33    def test_callable_attribute(self):
34        """Callable attributes are called and any False causes the match to fail"""
35        self.assertTrue(attrmatch({"att": 10}, att=lambda val: True))
36        self.assertFalse(attrmatch({"att": 10}, att=lambda val: False))
37
38        def even(val):
39            return val % 2 == 0
40
41        self.assertTrue(attrmatch({"att": 10}, att=even))
42        self.assertFalse(attrmatch({"att": 11}, att=even))
43
44    def test_all_matches_required(self):
45        """If only one attribute does not match, the result is False"""
46        self.assertFalse(attrmatch({"a": 1}, a=1, b=2, c=3))
47        self.assertFalse(attrmatch({"a": 1, "b": 2}, a=1, b=2, c=3))
48        self.assertTrue(attrmatch({"a": 1, "b": 2, "c": 3}, a=1, b=2, c=3))
49
50
51class MatchRunOnProjects(unittest.TestCase):
52    def test_empty(self):
53        self.assertFalse(match_run_on_projects("birch", []))
54
55    def test_all(self):
56        self.assertTrue(match_run_on_projects("birch", ["all"]))
57        self.assertTrue(match_run_on_projects("larch", ["all"]))
58        self.assertTrue(match_run_on_projects("autoland", ["all"]))
59        self.assertTrue(match_run_on_projects("mozilla-central", ["all"]))
60        self.assertTrue(match_run_on_projects("mozilla-beta", ["all"]))
61        self.assertTrue(match_run_on_projects("mozilla-release", ["all"]))
62
63    def test_release(self):
64        self.assertFalse(match_run_on_projects("birch", ["release"]))
65        self.assertFalse(match_run_on_projects("larch", ["release"]))
66        self.assertFalse(match_run_on_projects("autoland", ["release"]))
67        self.assertTrue(match_run_on_projects("mozilla-central", ["release"]))
68        self.assertTrue(match_run_on_projects("mozilla-beta", ["release"]))
69        self.assertTrue(match_run_on_projects("mozilla-release", ["release"]))
70
71    def test_integration(self):
72        self.assertFalse(match_run_on_projects("birch", ["integration"]))
73        self.assertFalse(match_run_on_projects("larch", ["integration"]))
74        self.assertTrue(match_run_on_projects("autoland", ["integration"]))
75        self.assertFalse(match_run_on_projects("mozilla-central", ["integration"]))
76        self.assertFalse(match_run_on_projects("mozilla-beta", ["integration"]))
77        self.assertFalse(match_run_on_projects("mozilla-integration", ["integration"]))
78
79    def test_combo(self):
80        self.assertTrue(match_run_on_projects("birch", ["release", "birch", "maple"]))
81        self.assertFalse(match_run_on_projects("larch", ["release", "birch", "maple"]))
82        self.assertTrue(match_run_on_projects("maple", ["release", "birch", "maple"]))
83        self.assertFalse(
84            match_run_on_projects("autoland", ["release", "birch", "maple"])
85        )
86        self.assertTrue(
87            match_run_on_projects("mozilla-central", ["release", "birch", "maple"])
88        )
89        self.assertTrue(
90            match_run_on_projects("mozilla-beta", ["release", "birch", "maple"])
91        )
92        self.assertTrue(
93            match_run_on_projects("mozilla-release", ["release", "birch", "maple"])
94        )
95        self.assertTrue(match_run_on_projects("birch", ["birch", "trunk"]))
96
97
98if __name__ == "__main__":
99    main()
100