OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 # Copyright 2014 Google Inc. |
| 4 # |
| 5 # Use of this source code is governed by a BSD-style license that can be |
| 6 # found in the LICENSE file. |
| 7 |
| 8 """ |
| 9 Test makefile_writer.py |
| 10 """ |
| 11 |
| 12 import argparse |
| 13 import os |
| 14 import shutil |
| 15 import sys |
| 16 import tempfile |
| 17 import test_variables |
| 18 import unittest |
| 19 |
| 20 sys.path.append(test_variables.GYP_GEN_DIR) |
| 21 |
| 22 import makefile_writer |
| 23 import vars_dict_lib |
| 24 |
| 25 EXPECTATIONS_DIR = os.path.join(os.path.dirname(__file__), 'expectations') |
| 26 MAKEFILE_NAME = 'Android.mk' |
| 27 |
| 28 def generate_dummy_vars_dict(name): |
| 29 """Create a VarsDict and fill it with dummy entries. |
| 30 |
| 31 Args: |
| 32 name: string to be appended to each entry, if not None. |
| 33 |
| 34 Returns: |
| 35 A VarsDict with dummy entries. |
| 36 """ |
| 37 vars_dict = vars_dict_lib.VarsDict() |
| 38 for key in vars_dict.keys(): |
| 39 entry = key.lower() |
| 40 if name: |
| 41 entry += '_' + name |
| 42 vars_dict[key].add(entry) |
| 43 return vars_dict |
| 44 |
| 45 |
| 46 def generate_dummy_vars_dict_data(name, condition): |
| 47 """Create a dummy VarsDictData. |
| 48 |
| 49 Create a dummy VarsDictData, using the name for both the contained |
| 50 VarsDict and the VarsDictData |
| 51 |
| 52 Args: |
| 53 name: name used by both the returned VarsDictData and its contained |
| 54 VarsDict. |
| 55 condition: condition used by the returned VarsDictData. |
| 56 |
| 57 Returns: |
| 58 A VarsDictData with dummy values, using the passed in info. |
| 59 """ |
| 60 vars_dict = generate_dummy_vars_dict(name) |
| 61 |
| 62 return makefile_writer.VarsDictData(vars_dict=vars_dict, name=name, |
| 63 condition=condition) |
| 64 |
| 65 |
| 66 def generate_dummy_makefile(target_dir): |
| 67 """Create a dummy makefile to demonstrate how it works. |
| 68 |
| 69 Use dummy values unrelated to any gyp files. Its output should remain the |
| 70 same unless/until makefile_writer.write_android_mk changes. |
| 71 |
| 72 Args: |
| 73 target_dir: directory in which to write the resulting Android.mk |
| 74 """ |
| 75 common_vars_dict = generate_dummy_vars_dict(None) |
| 76 |
| 77 deviation_params = [('foo', 'COND'), ('bar', None)] |
| 78 deviations = [generate_dummy_vars_dict_data(name, condition) |
| 79 for (name, condition) in deviation_params] |
| 80 |
| 81 makefile_writer.write_android_mk(target_dir=target_dir, |
| 82 common=common_vars_dict, |
| 83 deviations_from_common=deviations) |
| 84 |
| 85 |
| 86 class MakefileWriterTest(unittest.TestCase): |
| 87 |
| 88 def test_write_group_empty(self): |
| 89 f = tempfile.TemporaryFile() |
| 90 assert f.tell() == 0 |
| 91 for empty in (None, []): |
| 92 for truth in (True, False): |
| 93 makefile_writer.write_group(f, 'name', empty, truth) |
| 94 self.assertEqual(f.tell(), 0) |
| 95 f.close() |
| 96 |
| 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): |
| 118 animals = ('dog', 'cat', 'mouse', 'elephant') |
| 119 fd, filename = tempfile.mkstemp() |
| 120 with open(filename, 'w') as f: |
| 121 makefile_writer.write_group(f, 'animals', animals, False) |
| 122 os.close(fd) |
| 123 # Now confirm that it matches expectations |
| 124 self.__compare_files(filename, 'animals.txt') |
| 125 |
| 126 with open(filename, 'w') as f: |
| 127 makefile_writer.write_group(f, 'animals_append', animals, True) |
| 128 # Now confirm that it matches expectations |
| 129 self.__compare_files(filename, 'animals_append.txt') |
| 130 os.remove(filename) |
| 131 |
| 132 def test_write_local_vars(self): |
| 133 vars_dict = generate_dummy_vars_dict(None) |
| 134 |
| 135 # Call variations of write_local_vars. |
| 136 for append in [ True, False ]: |
| 137 for name in [ None, 'arm', 'foo' ]: |
| 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 |
| 144 # Compare to the expected file. |
| 145 filename = 'write_local_vars' |
| 146 if append: |
| 147 filename += '_append' |
| 148 else: |
| 149 filename += '_no_append' |
| 150 if name: |
| 151 filename += '_' + name |
| 152 else: |
| 153 filename += '_no_name' |
| 154 self.__compare_files(outfile, filename) |
| 155 |
| 156 # KNOWN_TARGETS is always a part of the input VarsDict, but it should |
| 157 # not be written to the resulting file. |
| 158 # Note that this assumes none of our dummy entries is 'KNOWN_TARGETS'. |
| 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 |
| 166 def test_write_android_mk(self): |
| 167 outdir = tempfile.mkdtemp() |
| 168 generate_dummy_makefile(outdir) |
| 169 |
| 170 self.__compare_files(os.path.join(outdir, MAKEFILE_NAME), MAKEFILE_NAME, |
| 171 'If you\'ve modified makefile_writer.py, run ' + |
| 172 '"makefile_writer_tests.py --rebaseline" ' + |
| 173 'to rebaseline') |
| 174 |
| 175 shutil.rmtree(outdir) |
| 176 |
| 177 |
| 178 def main(): |
| 179 loader = unittest.TestLoader() |
| 180 suite = loader.loadTestsFromTestCase(MakefileWriterTest) |
| 181 results = unittest.TextTestRunner(verbosity=2).run(suite) |
| 182 print repr(results) |
| 183 if not results.wasSuccessful(): |
| 184 raise Exception('failed one or more unittests') |
| 185 |
| 186 |
| 187 if __name__ == '__main__': |
| 188 parser = argparse.ArgumentParser() |
| 189 parser.add_argument('-r', '--rebaseline', |
| 190 help='Rebaseline expectation for Android.mk', |
| 191 action='store_true') |
| 192 args = parser.parse_args() |
| 193 |
| 194 if args.rebaseline: |
| 195 generate_dummy_makefile(EXPECTATIONS_DIR) |
| 196 else: |
| 197 main() |
| 198 |
OLD | NEW |