• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright (C) 2008 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
17import sys
18import unittest
19
20sys.dont_write_bytecode = True
21import lunch
22
23class TestStringMethods(unittest.TestCase):
24
25    def test_find_dirs(self):
26        self.assertEqual([x for x in lunch.FindDirs("test/configs", "multitree_combos")], [
27                    "test/configs/build/make/orchestrator/multitree_combos",
28                    "test/configs/device/aa/bb/multitree_combos",
29                    "test/configs/vendor/aa/bb/multitree_combos"])
30
31    def test_find_file(self):
32        # Finds the one in device first because this is searching from the root,
33        # not using FindNamedConfig.
34        self.assertEqual(lunch.FindFile("test/configs", "v.mcombo"),
35                   "test/configs/device/aa/bb/multitree_combos/v.mcombo")
36
37    def test_find_config_dirs(self):
38        self.assertEqual([x for x in lunch.FindConfigDirs("test/configs")], [
39                    "test/configs/build/make/orchestrator/multitree_combos",
40                    "test/configs/vendor/aa/bb/multitree_combos",
41                    "test/configs/device/aa/bb/multitree_combos"])
42
43    def test_find_named_config(self):
44        # Inside build/orchestrator, overriding device and vendor
45        self.assertEqual(lunch.FindNamedConfig("test/configs", "b"),
46                    "test/configs/build/make/orchestrator/multitree_combos/b.mcombo")
47
48        # Nested dir inside a combo dir
49        self.assertEqual(lunch.FindNamedConfig("test/configs", "nested"),
50                    "test/configs/build/make/orchestrator/multitree_combos/nested/nested.mcombo")
51
52        # Inside vendor, overriding device
53        self.assertEqual(lunch.FindNamedConfig("test/configs", "v"),
54                    "test/configs/vendor/aa/bb/multitree_combos/v.mcombo")
55
56        # Inside device
57        self.assertEqual(lunch.FindNamedConfig("test/configs", "d"),
58                    "test/configs/device/aa/bb/multitree_combos/d.mcombo")
59
60        # Make sure we don't look too deep (for performance)
61        self.assertIsNone(lunch.FindNamedConfig("test/configs", "too_deep"))
62
63
64    def test_choose_config_file(self):
65        # Empty string argument
66        self.assertEqual(lunch.ChooseConfigFromArgs("test/configs", [""]),
67                    (None, None))
68
69        # A PRODUCT-VARIANT name
70        self.assertEqual(lunch.ChooseConfigFromArgs("test/configs", ["v-eng"]),
71                    ("test/configs/vendor/aa/bb/multitree_combos/v.mcombo", "eng"))
72
73        # A PRODUCT-VARIANT name that conflicts with a file
74        self.assertEqual(lunch.ChooseConfigFromArgs("test/configs", ["b-eng"]),
75                    ("test/configs/build/make/orchestrator/multitree_combos/b.mcombo", "eng"))
76
77        # A PRODUCT-VARIANT that doesn't exist
78        self.assertEqual(lunch.ChooseConfigFromArgs("test/configs", ["z-user"]),
79                    (None, None))
80
81        # An explicit file
82        self.assertEqual(lunch.ChooseConfigFromArgs("test/configs",
83                        ["test/configs/build/make/orchestrator/multitree_combos/b.mcombo", "eng"]),
84                    ("test/configs/build/make/orchestrator/multitree_combos/b.mcombo", "eng"))
85
86        # An explicit file that doesn't exist
87        self.assertEqual(lunch.ChooseConfigFromArgs("test/configs",
88                        ["test/configs/doesnt_exist.mcombo", "eng"]),
89                    (None, None))
90
91        # An explicit file without a variant should fail
92        self.assertEqual(lunch.ChooseConfigFromArgs("test/configs",
93                        ["test/configs/build/make/orchestrator/multitree_combos/b.mcombo"]),
94                    ("test/configs/build/make/orchestrator/multitree_combos/b.mcombo", None))
95
96
97    def test_config_cycles(self):
98        # Test that we catch cycles
99        with self.assertRaises(lunch.ConfigException) as context:
100            lunch.LoadConfig("test/configs/parsing/cycles/1.mcombo")
101        self.assertEqual(context.exception.kind, lunch.ConfigException.ERROR_CYCLE)
102
103    def test_config_merge(self):
104        # Test the merge logic
105        self.assertEqual(lunch.LoadConfig("test/configs/parsing/merge/1.mcombo"), {
106                            "in_1": "1",
107                            "in_1_2": "1",
108                            "merged": {"merged_1": "1",
109                                "merged_1_2": "1",
110                                "merged_2": "2",
111                                "merged_2_3": "2",
112                                "merged_3": "3"},
113                            "dict_1": {"a": "b"},
114                            "in_2": "2",
115                            "in_2_3": "2",
116                            "dict_2": {"a": "b"},
117                            "in_3": "3",
118                            "dict_3": {"a": "b"}
119                        })
120
121    def test_list(self):
122        self.assertEqual(sorted(lunch.FindAllLunchable("test/configs")),
123                ["test/configs/build/make/orchestrator/multitree_combos/b.mcombo"])
124
125if __name__ == "__main__":
126    unittest.main()
127
128# vim: sts=4:ts=4:sw=4
129