OLD | NEW |
| 1 #!/usr/bin/env python |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 4 # found in the LICENSE file. |
4 | 5 |
5 import tempfile | 6 import tempfile |
6 import unittest | 7 import unittest |
7 | 8 |
8 import md5_check # pylint: disable=W0403 | 9 import md5_check # pylint: disable=W0403 |
9 | 10 |
10 | 11 |
11 class TestMd5Check(unittest.TestCase): | 12 class TestMd5Check(unittest.TestCase): |
12 def setUp(self): | 13 def setUp(self): |
13 self.called = False | 14 self.called = False |
14 | 15 |
15 def testCallAndRecordIfStale(self): | 16 def testCallAndRecordIfStale(self): |
16 input_strings = ['string1', 'string2'] | 17 input_strings = ['string1', 'string2'] |
17 input_file1 = tempfile.NamedTemporaryFile() | 18 input_file1 = tempfile.NamedTemporaryFile() |
18 input_file2 = tempfile.NamedTemporaryFile() | 19 input_file2 = tempfile.NamedTemporaryFile() |
19 file1_contents = 'input file 1' | 20 file1_contents = 'input file 1' |
20 file2_contents = 'input file 2' | 21 file2_contents = 'input file 2' |
21 input_file1.write(file1_contents) | 22 input_file1.write(file1_contents) |
22 input_file1.flush() | 23 input_file1.flush() |
23 input_file2.write(file2_contents) | 24 input_file2.write(file2_contents) |
24 input_file2.flush() | 25 input_file2.flush() |
25 input_files = [input_file1.name, input_file2.name] | 26 input_files = [input_file1.name, input_file2.name] |
26 | 27 |
27 record_path = tempfile.NamedTemporaryFile(suffix='.stamp') | 28 record_path = tempfile.NamedTemporaryFile(suffix='.stamp') |
28 | 29 |
29 def CheckCallAndRecord(should_call, message, force=False): | 30 def CheckCallAndRecord(should_call, message, force=False, |
| 31 outputs_specified=False, outputs_missing=False): |
| 32 output_paths = None |
| 33 if outputs_specified: |
| 34 output_file1 = tempfile.NamedTemporaryFile() |
| 35 if outputs_missing: |
| 36 output_file1.close() # Gets deleted on close(). |
| 37 output_paths = [output_file1.name] |
| 38 |
30 self.called = False | 39 self.called = False |
31 def MarkCalled(): | 40 def MarkCalled(): |
32 self.called = True | 41 self.called = True |
33 md5_check.CallAndRecordIfStale( | 42 md5_check.CallAndRecordIfStale( |
34 MarkCalled, | 43 MarkCalled, |
35 record_path=record_path.name, | 44 record_path=record_path.name, |
36 input_paths=input_files, | 45 input_paths=input_files, |
37 input_strings=input_strings, | 46 input_strings=input_strings, |
| 47 output_paths=output_paths, |
38 force=force) | 48 force=force) |
39 self.failUnlessEqual(should_call, self.called, message) | 49 self.failUnlessEqual(should_call, self.called, message) |
40 | 50 |
41 CheckCallAndRecord(True, 'should call when record doesn\'t exist') | 51 CheckCallAndRecord(True, 'should call when record doesn\'t exist') |
42 CheckCallAndRecord(False, 'should not call when nothing changed') | 52 CheckCallAndRecord(False, 'should not call when nothing changed') |
| 53 CheckCallAndRecord(False, 'should not call when nothing changed2', |
| 54 outputs_specified=True, outputs_missing=False) |
| 55 CheckCallAndRecord(True, 'should call when output missing', |
| 56 outputs_specified=True, outputs_missing=True) |
43 CheckCallAndRecord(True, force=True, message='should call when forced') | 57 CheckCallAndRecord(True, force=True, message='should call when forced') |
44 | 58 |
45 input_file1.write('some more input') | 59 input_file1.write('some more input') |
46 input_file1.flush() | 60 input_file1.flush() |
47 CheckCallAndRecord(True, 'changed input file should trigger call') | 61 CheckCallAndRecord(True, 'changed input file should trigger call') |
48 | 62 |
49 input_files = input_files[::-1] | 63 input_files = input_files[::-1] |
50 CheckCallAndRecord(False, 'reordering of inputs shouldn\'t trigger call') | 64 CheckCallAndRecord(False, 'reordering of inputs shouldn\'t trigger call') |
51 | 65 |
52 input_files = input_files[:1] | 66 input_files = input_files[:1] |
(...skipping 10 matching lines...) Expand all Loading... |
63 | 77 |
64 input_strings = input_strings[:1] | 78 input_strings = input_strings[:1] |
65 CheckCallAndRecord(True, 'removing a string should trigger call') | 79 CheckCallAndRecord(True, 'removing a string should trigger call') |
66 | 80 |
67 input_strings.append('a brand new string') | 81 input_strings.append('a brand new string') |
68 CheckCallAndRecord(True, 'added input string should trigger call') | 82 CheckCallAndRecord(True, 'added input string should trigger call') |
69 | 83 |
70 | 84 |
71 if __name__ == '__main__': | 85 if __name__ == '__main__': |
72 unittest.main() | 86 unittest.main() |
OLD | NEW |