blob: 7e18a3425d575ef2878930de89d7e42633e728e8 [file] [log] [blame]
Daniel Norman6d82fa32019-03-22 17:53:04 -07001#
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
Daniel Normanfdb38812019-04-15 09:47:24 -070019import common
Daniel Norman6d82fa32019-03-22 17:53:04 -070020import test_utils
Daniel Normanfdb38812019-04-15 09:47:24 -070021from merge_target_files import (read_config_list, validate_config_lists,
22 default_system_item_list,
23 default_other_item_list,
24 default_system_misc_info_keys, copy_items)
Daniel Norman6d82fa32019-03-22 17:53:04 -070025
26
27class MergeTargetFilesTest(test_utils.ReleaseToolsTestCase):
28
29 def setUp(self):
30 self.testdata_dir = test_utils.get_testdata_dir()
31
Daniel Normanfdb38812019-04-15 09:47:24 -070032 def test_copy_items_CopiesItemsMatchingPatterns(self):
33
34 def createEmptyFile(path):
35 if not os.path.exists(os.path.dirname(path)):
36 os.makedirs(os.path.dirname(path))
37 open(path, 'a').close()
38 return path
39
40 def createSymLink(source, dest):
41 os.symlink(source, dest)
42 return dest
43
44 def getRelPaths(start, filepaths):
45 return set(
46 os.path.relpath(path=filepath, start=start) for filepath in filepaths)
47
48 input_dir = common.MakeTempDir()
49 output_dir = common.MakeTempDir()
50 expected_copied_items = []
51 actual_copied_items = []
52 patterns = ['*.cpp', 'subdir/*.txt']
53
54 # Create various files that we expect to get copied because they
55 # match one of the patterns.
56 expected_copied_items.extend([
57 createEmptyFile(os.path.join(input_dir, 'a.cpp')),
58 createEmptyFile(os.path.join(input_dir, 'b.cpp')),
59 createEmptyFile(os.path.join(input_dir, 'subdir', 'c.txt')),
60 createEmptyFile(os.path.join(input_dir, 'subdir', 'd.txt')),
61 createEmptyFile(
62 os.path.join(input_dir, 'subdir', 'subsubdir', 'e.txt')),
63 createSymLink('a.cpp', os.path.join(input_dir, 'a_link.cpp')),
64 ])
65 # Create some more files that we expect to not get copied.
66 createEmptyFile(os.path.join(input_dir, 'a.h'))
67 createEmptyFile(os.path.join(input_dir, 'b.h'))
68 createEmptyFile(os.path.join(input_dir, 'subdir', 'subsubdir', 'f.gif'))
69 createSymLink('a.h', os.path.join(input_dir, 'a_link.h'))
70
71 # Copy items.
72 copy_items(input_dir, output_dir, patterns)
73
74 # Assert the actual copied items match the ones we expected.
75 for dirpath, _, filenames in os.walk(output_dir):
76 actual_copied_items.extend(
77 os.path.join(dirpath, filename) for filename in filenames)
78 self.assertEqual(
79 getRelPaths(output_dir, actual_copied_items),
80 getRelPaths(input_dir, expected_copied_items))
81 self.assertEqual(
82 os.readlink(os.path.join(output_dir, 'a_link.cpp')), 'a.cpp')
83
Daniel Norman6d82fa32019-03-22 17:53:04 -070084 def test_read_config_list(self):
85 system_item_list_file = os.path.join(self.testdata_dir,
86 'merge_config_system_item_list')
87 system_item_list = read_config_list(system_item_list_file)
88 expected_system_item_list = [
89 'META/apkcerts.txt',
90 'META/filesystem_config.txt',
91 'META/root_filesystem_config.txt',
92 'META/system_manifest.xml',
93 'META/system_matrix.xml',
94 'META/update_engine_config.txt',
95 'PRODUCT/*',
96 'ROOT/*',
97 'SYSTEM/*',
98 ]
99 self.assertItemsEqual(system_item_list, expected_system_item_list)
100
101 def test_validate_config_lists_ReturnsFalseIfMissingDefaultItem(self):
102 system_item_list = default_system_item_list[:]
103 system_item_list.remove('SYSTEM/*')
104 self.assertFalse(
105 validate_config_lists(system_item_list, default_system_misc_info_keys,
106 default_other_item_list))
107
108 def test_validate_config_lists_ReturnsTrueIfDefaultItemInDifferentList(self):
109 system_item_list = default_system_item_list[:]
110 system_item_list.remove('ROOT/*')
111 other_item_list = default_other_item_list[:]
112 other_item_list.append('ROOT/*')
113 self.assertTrue(
114 validate_config_lists(system_item_list, default_system_misc_info_keys,
115 other_item_list))
116
117 def test_validate_config_lists_ReturnsTrueIfExtraItem(self):
118 system_item_list = default_system_item_list[:]
119 system_item_list.append('MY_NEW_PARTITION/*')
120 self.assertTrue(
121 validate_config_lists(system_item_list, default_system_misc_info_keys,
122 default_other_item_list))
123
124 def test_validate_config_lists_ReturnsFalseIfBadSystemMiscInfoKeys(self):
125 for bad_key in ['dynamic_partition_list', 'super_partition_groups']:
126 system_misc_info_keys = default_system_misc_info_keys[:]
127 system_misc_info_keys.append(bad_key)
128 self.assertFalse(
129 validate_config_lists(default_system_item_list, system_misc_info_keys,
130 default_other_item_list))