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