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 filename = tempfile.mkstemp()[1] | |
epoger
2014/03/26 14:29:48
Looking at http://docs.python.org/2/library/tempfi
scroggo
2014/03/26 16:24:00
I'll trust your link ;). It sounds like my current
| |
120 with open(filename, 'w') as f: | |
121 makefile_writer.write_group(f, 'animals', animals, False) | |
122 # Now confirm that it matches expectations | |
123 self.__compare_files(filename, 'animals.txt') | |
124 | |
125 with open(filename, 'w') as f: | |
126 makefile_writer.write_group(f, 'animals_append', animals, True) | |
127 # Now confirm that it matches expectations | |
128 self.__compare_files(filename, 'animals_append.txt') | |
129 | |
130 def test_write_local_vars(self): | |
131 vars_dict = generate_dummy_vars_dict(None) | |
132 | |
133 # Call variations of write_local_vars. | |
134 for append in [ True, False ]: | |
135 for name in [ None, 'arm', 'foo' ]: | |
136 # Now write to a temporary file. | |
137 outfile = tempfile.mkstemp()[1] | |
138 with open(outfile, 'w') as f: | |
139 makefile_writer.write_local_vars(f, vars_dict, append, name) | |
140 | |
141 # Compare to the expected file. | |
142 filename = 'write_local_vars' | |
143 if append: | |
144 filename += '_append' | |
145 else: | |
146 filename += '_no_append' | |
147 if name: | |
148 filename += '_' + name | |
149 else: | |
150 filename += '_no_name' | |
151 self.__compare_files(outfile, filename) | |
152 | |
153 # KNOWN_TARGETS is always a part of the input VarsDict, but it should | |
154 # not be written to the resulting file. | |
155 # Note that this assumes none of our dummy entries is 'KNOWN_TARGETS'. | |
156 known_targets_name = 'KNOWN_TARGETS' | |
157 self.assertEqual(len(vars_dict[known_targets_name]), 1) | |
158 | |
159 with open(outfile, 'r') as f: | |
160 self.assertNotIn(known_targets_name, f.read()) | |
161 | |
162 def test_write_android_mk(self): | |
163 outdir = tempfile.mkdtemp() | |
164 generate_dummy_makefile(outdir) | |
165 | |
166 self.__compare_files(os.path.join(outdir, MAKEFILE_NAME), MAKEFILE_NAME, | |
167 'If you\'ve modified makefile_writer.py, run ' + | |
168 '"makefile_writer_tests.py --rebaseline" ' + | |
169 'to rebaseline') | |
170 | |
171 shutil.rmtree(outdir) | |
172 | |
173 | |
174 def main(): | |
175 loader = unittest.TestLoader() | |
176 suite = loader.loadTestsFromTestCase(MakefileWriterTest) | |
177 unittest.TextTestRunner(verbosity=2).run(suite) | |
epoger
2014/03/26 14:29:48
I think you need to check for errors in TextTestRu
scroggo
2014/03/26 16:24:00
platform/android/tests/run_all.py does the check y
epoger
2014/03/26 17:22:46
It depends on how each module will be used.
If a
scroggo
2014/03/26 19:18:12
I find the __main__ to be convenient for testing p
| |
178 | |
179 if __name__ == '__main__': | |
epoger
2014/03/26 14:29:48
Will this new test be run automatically by the hou
scroggo
2014/03/26 16:24:00
Yes. It will be run by platform/android/tests/run_
| |
180 parser = argparse.ArgumentParser() | |
181 parser.add_argument('-r', '--rebaseline', | |
182 help='Rebaseline expectation for Android.mk', | |
183 action='store_true') | |
184 args = parser.parse_args() | |
185 | |
186 if args.rebaseline: | |
187 generate_dummy_makefile(EXPECTATIONS_DIR) | |
188 else: | |
189 main() | |
190 | |
OLD | NEW |