blob: d0cd2cf3276a28e4e08534423c52b50ed2d680fb [file] [log] [blame]
Daniel Norman2465fc82022-03-02 12:01:20 -08001#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import os.path
18
19import common
20import merge_target_files
21import merge_utils
22import test_utils
23from merge_target_files import (
24 DEFAULT_FRAMEWORK_ITEM_LIST,
25 DEFAULT_VENDOR_ITEM_LIST,
26 DEFAULT_FRAMEWORK_MISC_INFO_KEYS,
27)
28
29
30class MergeUtilsTest(test_utils.ReleaseToolsTestCase):
31
32 def setUp(self):
33 self.OPTIONS = merge_target_files.OPTIONS
34 self.OPTIONS.framework_item_list = DEFAULT_FRAMEWORK_ITEM_LIST
35 self.OPTIONS.framework_misc_info_keys = DEFAULT_FRAMEWORK_MISC_INFO_KEYS
36 self.OPTIONS.vendor_item_list = DEFAULT_VENDOR_ITEM_LIST
37
38 def test_CopyItems_CopiesItemsMatchingPatterns(self):
39
40 def createEmptyFile(path):
41 if not os.path.exists(os.path.dirname(path)):
42 os.makedirs(os.path.dirname(path))
43 open(path, 'a').close()
44 return path
45
46 def createSymLink(source, dest):
47 os.symlink(source, dest)
48 return dest
49
50 def getRelPaths(start, filepaths):
51 return set(
52 os.path.relpath(path=filepath, start=start) for filepath in filepaths)
53
54 input_dir = common.MakeTempDir()
55 output_dir = common.MakeTempDir()
56 expected_copied_items = []
57 actual_copied_items = []
58 patterns = ['*.cpp', 'subdir/*.txt']
59
60 # Create various files that we expect to get copied because they
61 # match one of the patterns.
62 expected_copied_items.extend([
63 createEmptyFile(os.path.join(input_dir, 'a.cpp')),
64 createEmptyFile(os.path.join(input_dir, 'b.cpp')),
65 createEmptyFile(os.path.join(input_dir, 'subdir', 'c.txt')),
66 createEmptyFile(os.path.join(input_dir, 'subdir', 'd.txt')),
67 createEmptyFile(
68 os.path.join(input_dir, 'subdir', 'subsubdir', 'e.txt')),
69 createSymLink('a.cpp', os.path.join(input_dir, 'a_link.cpp')),
70 ])
71 # Create some more files that we expect to not get copied.
72 createEmptyFile(os.path.join(input_dir, 'a.h'))
73 createEmptyFile(os.path.join(input_dir, 'b.h'))
74 createEmptyFile(os.path.join(input_dir, 'subdir', 'subsubdir', 'f.gif'))
75 createSymLink('a.h', os.path.join(input_dir, 'a_link.h'))
76
77 # Copy items.
78 merge_utils.CopyItems(input_dir, output_dir, patterns)
79
80 # Assert the actual copied items match the ones we expected.
81 for dirpath, _, filenames in os.walk(output_dir):
82 actual_copied_items.extend(
83 os.path.join(dirpath, filename) for filename in filenames)
84 self.assertEqual(
85 getRelPaths(output_dir, actual_copied_items),
86 getRelPaths(input_dir, expected_copied_items))
87 self.assertEqual(
88 os.readlink(os.path.join(output_dir, 'a_link.cpp')), 'a.cpp')
89
90 def test_ValidateConfigLists_ReturnsFalseIfSharedExtractedPartition(self):
91 self.OPTIONS.vendor_item_list = list(DEFAULT_VENDOR_ITEM_LIST)
92 self.OPTIONS.vendor_item_list.append('SYSTEM/my_system_file')
93 self.assertFalse(merge_utils.ValidateConfigLists())
94
95 def test_ValidateConfigLists_ReturnsFalseIfSharedExtractedPartitionImage(
96 self):
97 self.OPTIONS.vendor_item_list = list(DEFAULT_VENDOR_ITEM_LIST)
98 self.OPTIONS.vendor_item_list.append('IMAGES/system.img')
99 self.assertFalse(merge_utils.ValidateConfigLists())
100
101 def test_ValidateConfigLists_ReturnsFalseIfBadSystemMiscInfoKeys(self):
102 for bad_key in ['dynamic_partition_list', 'super_partition_groups']:
103 self.OPTIONS.framework_misc_info_keys = list(
104 DEFAULT_FRAMEWORK_MISC_INFO_KEYS)
105 self.OPTIONS.framework_misc_info_keys.append(bad_key)
106 self.assertFalse(merge_utils.ValidateConfigLists())
107
108 def test_ItemListToPartitionSet(self):
109 item_list = [
110 'META/apexkeys.txt',
111 'META/apkcerts.txt',
112 'META/filesystem_config.txt',
113 'PRODUCT/*',
114 'SYSTEM/*',
115 'SYSTEM_EXT/*',
116 ]
117 partition_set = merge_utils.ItemListToPartitionSet(item_list)
118 self.assertEqual(set(['product', 'system', 'system_ext']), partition_set)