3 # Copyright 2014 Google Inc.
5 # Use of this source code is governed by a BSD-style license that can be
6 # found in the LICENSE file.
9 Test makefile_writer.py
21 sys.path.append(test_variables.GYP_GEN_DIR)
23 import makefile_writer
24 import tool_makefile_writer
27 MAKEFILE_NAME = test_variables.ANDROID_MK
28 REBASELINE_MSG = ('If you\'ve modified makefile_writer.py, run '
29 '"makefile_writer_tests.py --rebaseline" to rebaseline')
32 def generate_dummy_vars_dict(name):
33 """Create a VarsDict and fill it with dummy entries.
36 name: string to be appended to each entry, if not None.
39 A VarsDict with dummy entries.
41 vars_dict = vars_dict_lib.VarsDict()
42 for key in vars_dict.keys():
46 vars_dict[key].add(entry)
49 def generate_write_local_vars_params():
50 """Generator to compute params for write_local_vars tests.
52 Each iteration yields a new tuple: (filename, append, name), specific to a
53 way to call write_local_vars for the tests.
56 filename: filename corresponding to the expectation file for this
57 combination of params to write_local_vars.
58 append: boolean to pass as append parameter to write_local_vars.
59 name: string to pass as name parameter to write_local_vars.
61 for append in [ True, False ]:
62 for name in [ None, 'arm', 'foo' ]:
63 filename = 'write_local_vars'
67 filename += '_no_append'
69 filename += '_' + name
71 filename += '_no_name'
73 yield (filename, append, name)
75 def generate_dummy_vars_dict_data(name, condition):
76 """Create a dummy VarsDictData.
78 Create a dummy VarsDictData, using the name for both the contained
79 VarsDict and the VarsDictData
82 name: name used by both the returned VarsDictData and its contained
84 condition: condition used by the returned VarsDictData.
87 A VarsDictData with dummy values, using the passed in info.
89 vars_dict = generate_dummy_vars_dict(name)
91 return makefile_writer.VarsDictData(vars_dict=vars_dict, name=name,
95 def generate_dummy_makefile(target_dir):
96 """Create a dummy makefile to demonstrate how it works.
98 Use dummy values unrelated to any gyp files. Its output should remain the
99 same unless/until makefile_writer.write_android_mk changes.
102 target_dir: directory in which to write the resulting Android.mk
104 common_vars_dict = generate_dummy_vars_dict(None)
106 deviation_params = [('foo', 'COND'), ('bar', None)]
107 deviations = [generate_dummy_vars_dict_data(name, condition)
108 for (name, condition) in deviation_params]
110 makefile_writer.write_android_mk(target_dir=target_dir,
111 common=common_vars_dict,
112 deviations_from_common=deviations)
114 def generate_dummy_tool_makefile(target_dir):
115 """Create a dummy makefile for a tool.
118 target_dir: directory in which to write the resulting Android.mk
120 vars_dict = generate_dummy_vars_dict(None)
121 tool_makefile_writer.write_tool_android_mk(target_dir=target_dir,
125 class MakefileWriterTest(unittest.TestCase):
127 def test_write_group_empty(self):
128 f = tempfile.TemporaryFile()
130 for empty in (None, []):
131 for truth in (True, False):
132 makefile_writer.write_group(f, 'name', empty, truth)
133 self.assertEqual(f.tell(), 0)
136 def test_write_group(self):
137 animals = ('dog', 'cat', 'mouse', 'elephant')
138 fd, filename = tempfile.mkstemp()
139 with open(filename, 'w') as f:
140 makefile_writer.write_group(f, 'animals', animals, False)
142 # Now confirm that it matches expectations
143 utils.compare_to_expectation(filename, 'animals.txt', self.assertTrue)
145 with open(filename, 'w') as f:
146 makefile_writer.write_group(f, 'animals_append', animals, True)
147 # Now confirm that it matches expectations
148 utils.compare_to_expectation(filename, 'animals_append.txt',
152 def test_write_local_vars(self):
153 vars_dict = generate_dummy_vars_dict(None)
154 # Compare various ways of calling write_local_vars to expectations.
155 for (filename, append, name) in generate_write_local_vars_params():
156 fd, outfile = tempfile.mkstemp()
157 with open(outfile, 'w') as f:
158 makefile_writer.write_local_vars(f, vars_dict, append, name)
161 # Compare to the expected file.
162 utils.compare_to_expectation(outfile, filename, self.assertTrue,
165 # KNOWN_TARGETS is always a key in the input VarsDict, but it should not
166 # be written to the resulting file.
167 # Note that this assumes none of our dummy entries is 'KNOWN_TARGETS'.
168 known_targets_name = 'KNOWN_TARGETS'
169 self.assertEqual(len(vars_dict[known_targets_name]), 1)
171 with open(outfile, 'r') as f:
172 self.assertNotIn(known_targets_name, f.read())
175 def test_write_android_mk(self):
176 outdir = tempfile.mkdtemp()
177 generate_dummy_makefile(outdir)
179 utils.compare_to_expectation(os.path.join(outdir, MAKEFILE_NAME),
180 MAKEFILE_NAME, self.assertTrue, REBASELINE_MSG)
182 shutil.rmtree(outdir)
184 def test_tool_writer(self):
185 outdir = tempfile.mkdtemp()
186 tool_dir = os.path.join(outdir, TOOL_DIR)
188 generate_dummy_tool_makefile(tool_dir)
190 utils.compare_to_expectation(os.path.join(tool_dir, MAKEFILE_NAME),
191 os.path.join(TOOL_DIR, MAKEFILE_NAME),
192 self.assertTrue, REBASELINE_MSG)
195 loader = unittest.TestLoader()
196 suite = loader.loadTestsFromTestCase(MakefileWriterTest)
197 results = unittest.TextTestRunner(verbosity=2).run(suite)
199 if not results.wasSuccessful():
200 raise Exception('failed one or more unittests')
204 generate_dummy_makefile(utils.EXPECTATIONS_DIR)
206 vars_dict = generate_dummy_vars_dict(None)
207 for (filename, append, name) in generate_write_local_vars_params():
208 with open(os.path.join(utils.EXPECTATIONS_DIR, filename), 'w') as f:
209 makefile_writer.write_local_vars(f, vars_dict, append, name)
211 generate_dummy_tool_makefile(os.path.join(utils.EXPECTATIONS_DIR, TOOL_DIR))
214 if __name__ == '__main__':
215 parser = argparse.ArgumentParser()
216 parser.add_argument('-r', '--rebaseline', help='Rebaseline expectations.',
218 args = parser.parse_args()