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