OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 # Copyright 2014 Google Inc. | 3 # Copyright 2014 Google Inc. |
4 # | 4 # |
5 # Use of this source code is governed by a BSD-style license that can be | 5 # Use of this source code is governed by a BSD-style license that can be |
6 # found in the LICENSE file. | 6 # found in the LICENSE file. |
7 | 7 |
8 """ | 8 """ |
9 Test makefile_writer.py | 9 Test makefile_writer.py |
10 """ | 10 """ |
11 | 11 |
12 import argparse | 12 import argparse |
13 import os | 13 import os |
14 import shutil | 14 import shutil |
15 import sys | 15 import sys |
16 import tempfile | 16 import tempfile |
17 import test_variables | 17 import test_variables |
18 import unittest | 18 import unittest |
| 19 import utils |
19 | 20 |
20 sys.path.append(test_variables.GYP_GEN_DIR) | 21 sys.path.append(test_variables.GYP_GEN_DIR) |
21 | 22 |
22 import makefile_writer | 23 import makefile_writer |
23 import vars_dict_lib | 24 import vars_dict_lib |
24 | 25 |
25 EXPECTATIONS_DIR = os.path.join(os.path.dirname(__file__), 'expectations') | |
26 MAKEFILE_NAME = 'Android.mk' | 26 MAKEFILE_NAME = 'Android.mk' |
| 27 REBASELINE_MSG = ('If you\'ve modified makefile_writer.py, run ' |
| 28 '"makefile_writer_tests.py --rebaseline" to rebaseline') |
27 | 29 |
28 def generate_dummy_vars_dict(name): | 30 def generate_dummy_vars_dict(name): |
29 """Create a VarsDict and fill it with dummy entries. | 31 """Create a VarsDict and fill it with dummy entries. |
30 | 32 |
31 Args: | 33 Args: |
32 name: string to be appended to each entry, if not None. | 34 name: string to be appended to each entry, if not None. |
33 | 35 |
34 Returns: | 36 Returns: |
35 A VarsDict with dummy entries. | 37 A VarsDict with dummy entries. |
36 """ | 38 """ |
37 vars_dict = vars_dict_lib.VarsDict() | 39 vars_dict = vars_dict_lib.VarsDict() |
38 for key in vars_dict.keys(): | 40 for key in vars_dict.keys(): |
39 entry = key.lower() | 41 entry = key.lower() |
40 if name: | 42 if name: |
41 entry += '_' + name | 43 entry += '_' + name |
42 vars_dict[key].add(entry) | 44 vars_dict[key].add(entry) |
43 return vars_dict | 45 return vars_dict |
44 | 46 |
| 47 def generate_write_local_vars_params(): |
| 48 """Generator to compute params for write_local_vars tests. |
| 49 |
| 50 Each iteration yields a new tuple: (filename, append, name), specific to a |
| 51 way to call write_local_vars for the tests. |
| 52 |
| 53 Yields: |
| 54 filename: filename corresponding to the expectation file for this |
| 55 combination of params to write_local_vars. |
| 56 append: boolean to pass as append parameter to write_local_vars. |
| 57 name: string to pass as name parameter to write_local_vars. |
| 58 """ |
| 59 for append in [ True, False ]: |
| 60 for name in [ None, 'arm', 'foo' ]: |
| 61 filename = 'write_local_vars' |
| 62 if append: |
| 63 filename += '_append' |
| 64 else: |
| 65 filename += '_no_append' |
| 66 if name: |
| 67 filename += '_' + name |
| 68 else: |
| 69 filename += '_no_name' |
| 70 |
| 71 yield (filename, append, name) |
45 | 72 |
46 def generate_dummy_vars_dict_data(name, condition): | 73 def generate_dummy_vars_dict_data(name, condition): |
47 """Create a dummy VarsDictData. | 74 """Create a dummy VarsDictData. |
48 | 75 |
49 Create a dummy VarsDictData, using the name for both the contained | 76 Create a dummy VarsDictData, using the name for both the contained |
50 VarsDict and the VarsDictData | 77 VarsDict and the VarsDictData |
51 | 78 |
52 Args: | 79 Args: |
53 name: name used by both the returned VarsDictData and its contained | 80 name: name used by both the returned VarsDictData and its contained |
54 VarsDict. | 81 VarsDict. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 114 |
88 def test_write_group_empty(self): | 115 def test_write_group_empty(self): |
89 f = tempfile.TemporaryFile() | 116 f = tempfile.TemporaryFile() |
90 assert f.tell() == 0 | 117 assert f.tell() == 0 |
91 for empty in (None, []): | 118 for empty in (None, []): |
92 for truth in (True, False): | 119 for truth in (True, False): |
93 makefile_writer.write_group(f, 'name', empty, truth) | 120 makefile_writer.write_group(f, 'name', empty, truth) |
94 self.assertEqual(f.tell(), 0) | 121 self.assertEqual(f.tell(), 0) |
95 f.close() | 122 f.close() |
96 | 123 |
97 def __compare_files(self, actual_name, expectation_name, msg=None): | |
98 """Check that two files are identical. | |
99 | |
100 Assert line by line that the files match. | |
101 | |
102 Args: | |
103 actual_name: Full path to the test file. | |
104 expectation_name: Basename of the expectations file within which | |
105 to compare. The file is expected to be in | |
106 platform_tools/android/tests/expectations. | |
107 msg: Message to pass to assertEqual. | |
108 Raises: | |
109 AssertionError: If the files do not match. | |
110 """ | |
111 with open(actual_name, 'r') as result: | |
112 with open(os.path.join(EXPECTATIONS_DIR, | |
113 expectation_name)) as expectation: | |
114 for line in result: | |
115 self.assertEqual(line, expectation.readline(), msg) | |
116 | |
117 def test_write_group(self): | 124 def test_write_group(self): |
118 animals = ('dog', 'cat', 'mouse', 'elephant') | 125 animals = ('dog', 'cat', 'mouse', 'elephant') |
119 fd, filename = tempfile.mkstemp() | 126 fd, filename = tempfile.mkstemp() |
120 with open(filename, 'w') as f: | 127 with open(filename, 'w') as f: |
121 makefile_writer.write_group(f, 'animals', animals, False) | 128 makefile_writer.write_group(f, 'animals', animals, False) |
122 os.close(fd) | 129 os.close(fd) |
123 # Now confirm that it matches expectations | 130 # Now confirm that it matches expectations |
124 self.__compare_files(filename, 'animals.txt') | 131 utils.compare_to_expectation(filename, 'animals.txt', self.assertTrue) |
125 | 132 |
126 with open(filename, 'w') as f: | 133 with open(filename, 'w') as f: |
127 makefile_writer.write_group(f, 'animals_append', animals, True) | 134 makefile_writer.write_group(f, 'animals_append', animals, True) |
128 # Now confirm that it matches expectations | 135 # Now confirm that it matches expectations |
129 self.__compare_files(filename, 'animals_append.txt') | 136 utils.compare_to_expectation(filename, 'animals_append.txt', |
| 137 self.assertTrue) |
130 os.remove(filename) | 138 os.remove(filename) |
131 | 139 |
132 def test_write_local_vars(self): | 140 def test_write_local_vars(self): |
133 vars_dict = generate_dummy_vars_dict(None) | 141 vars_dict = generate_dummy_vars_dict(None) |
| 142 # Compare various ways of calling write_local_vars to expectations. |
| 143 for (filename, append, name) in generate_write_local_vars_params(): |
| 144 fd, outfile = tempfile.mkstemp() |
| 145 with open(outfile, 'w') as f: |
| 146 makefile_writer.write_local_vars(f, vars_dict, append, name) |
| 147 os.close(fd) |
134 | 148 |
135 # Call variations of write_local_vars. | 149 # Compare to the expected file. |
136 for append in [ True, False ]: | 150 utils.compare_to_expectation(outfile, filename, self.assertTrue, |
137 for name in [ None, 'arm', 'foo' ]: | 151 REBASELINE_MSG) |
138 # Now write to a temporary file. | |
139 fd, outfile = tempfile.mkstemp() | |
140 with open(outfile, 'w') as f: | |
141 makefile_writer.write_local_vars(f, vars_dict, append, name) | |
142 os.close(fd) | |
143 | 152 |
144 # Compare to the expected file. | 153 # KNOWN_TARGETS is always a key in the input VarsDict, but it should not |
145 filename = 'write_local_vars' | 154 # be written to the resulting file. |
146 if append: | 155 # Note that this assumes none of our dummy entries is 'KNOWN_TARGETS'. |
147 filename += '_append' | 156 known_targets_name = 'KNOWN_TARGETS' |
148 else: | 157 self.assertEqual(len(vars_dict[known_targets_name]), 1) |
149 filename += '_no_append' | |
150 if name: | |
151 filename += '_' + name | |
152 else: | |
153 filename += '_no_name' | |
154 self.__compare_files(outfile, filename) | |
155 | 158 |
156 # KNOWN_TARGETS is always a part of the input VarsDict, but it should | 159 with open(outfile, 'r') as f: |
157 # not be written to the resulting file. | 160 self.assertNotIn(known_targets_name, f.read()) |
158 # Note that this assumes none of our dummy entries is 'KNOWN_TARGETS'. | 161 os.remove(outfile) |
159 known_targets_name = 'KNOWN_TARGETS' | |
160 self.assertEqual(len(vars_dict[known_targets_name]), 1) | |
161 | |
162 with open(outfile, 'r') as f: | |
163 self.assertNotIn(known_targets_name, f.read()) | |
164 os.remove(outfile) | |
165 | 162 |
166 def test_write_android_mk(self): | 163 def test_write_android_mk(self): |
167 outdir = tempfile.mkdtemp() | 164 outdir = tempfile.mkdtemp() |
168 generate_dummy_makefile(outdir) | 165 generate_dummy_makefile(outdir) |
169 | 166 |
170 self.__compare_files(os.path.join(outdir, MAKEFILE_NAME), MAKEFILE_NAME, | 167 utils.compare_to_expectation(os.path.join(outdir, MAKEFILE_NAME), |
171 'If you\'ve modified makefile_writer.py, run ' + | 168 MAKEFILE_NAME, self.assertTrue, REBASELINE_MSG) |
172 '"makefile_writer_tests.py --rebaseline" ' + | |
173 'to rebaseline') | |
174 | 169 |
175 shutil.rmtree(outdir) | 170 shutil.rmtree(outdir) |
176 | 171 |
177 | 172 |
178 def main(): | 173 def main(): |
179 loader = unittest.TestLoader() | 174 loader = unittest.TestLoader() |
180 suite = loader.loadTestsFromTestCase(MakefileWriterTest) | 175 suite = loader.loadTestsFromTestCase(MakefileWriterTest) |
181 results = unittest.TextTestRunner(verbosity=2).run(suite) | 176 results = unittest.TextTestRunner(verbosity=2).run(suite) |
182 print repr(results) | 177 print repr(results) |
183 if not results.wasSuccessful(): | 178 if not results.wasSuccessful(): |
184 raise Exception('failed one or more unittests') | 179 raise Exception('failed one or more unittests') |
185 | 180 |
186 | 181 |
| 182 def rebaseline(): |
| 183 generate_dummy_makefile(utils.EXPECTATIONS_DIR) |
| 184 |
| 185 vars_dict = generate_dummy_vars_dict(None) |
| 186 for (filename, append, name) in generate_write_local_vars_params(): |
| 187 with open(os.path.join(utils.EXPECTATIONS_DIR, filename), 'w') as f: |
| 188 makefile_writer.write_local_vars(f, vars_dict, append, name) |
| 189 |
| 190 |
187 if __name__ == '__main__': | 191 if __name__ == '__main__': |
188 parser = argparse.ArgumentParser() | 192 parser = argparse.ArgumentParser() |
189 parser.add_argument('-r', '--rebaseline', | 193 parser.add_argument('-r', '--rebaseline', help='Rebaseline expectations.', |
190 help='Rebaseline expectation for Android.mk', | |
191 action='store_true') | 194 action='store_true') |
192 args = parser.parse_args() | 195 args = parser.parse_args() |
193 | 196 |
194 if args.rebaseline: | 197 if args.rebaseline: |
195 generate_dummy_makefile(EXPECTATIONS_DIR) | 198 rebaseline() |
196 else: | 199 else: |
197 main() | 200 main() |
198 | 201 |
OLD | NEW |