1# 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# found in the LICENSE file. 4 5import tempfile 6import unittest 7 8import md5_check # pylint: disable=W0403 9 10 11class TestMd5Check(unittest.TestCase): 12 def setUp(self): 13 self.called = False 14 15 def testCallAndRecordIfStale(self): 16 input_strings = ['string1', 'string2'] 17 input_file1 = tempfile.NamedTemporaryFile() 18 input_file2 = tempfile.NamedTemporaryFile() 19 file1_contents = 'input file 1' 20 file2_contents = 'input file 2' 21 input_file1.write(file1_contents) 22 input_file1.flush() 23 input_file2.write(file2_contents) 24 input_file2.flush() 25 input_files = [input_file1.name, input_file2.name] 26 27 record_path = tempfile.NamedTemporaryFile(suffix='.stamp') 28 29 def CheckCallAndRecord(should_call, message, force=False): 30 self.called = False 31 def MarkCalled(): 32 self.called = True 33 md5_check.CallAndRecordIfStale( 34 MarkCalled, 35 record_path=record_path.name, 36 input_paths=input_files, 37 input_strings=input_strings, 38 force=force) 39 self.failUnlessEqual(should_call, self.called, message) 40 41 CheckCallAndRecord(True, 'should call when record doesn\'t exist') 42 CheckCallAndRecord(False, 'should not call when nothing changed') 43 CheckCallAndRecord(True, force=True, message='should call when forced') 44 45 input_file1.write('some more input') 46 input_file1.flush() 47 CheckCallAndRecord(True, 'changed input file should trigger call') 48 49 input_files = input_files[::-1] 50 CheckCallAndRecord(False, 'reordering of inputs shouldn\'t trigger call') 51 52 input_files = input_files[:1] 53 CheckCallAndRecord(True, 'removing file should trigger call') 54 55 input_files.append(input_file2.name) 56 CheckCallAndRecord(True, 'added input file should trigger call') 57 58 input_strings[0] = input_strings[0] + ' a bit longer' 59 CheckCallAndRecord(True, 'changed input string should trigger call') 60 61 input_strings = input_strings[::-1] 62 CheckCallAndRecord(True, 'reordering of string inputs should trigger call') 63 64 input_strings = input_strings[:1] 65 CheckCallAndRecord(True, 'removing a string should trigger call') 66 67 input_strings.append('a brand new string') 68 CheckCallAndRecord(True, 'added input string should trigger call') 69 70 71if __name__ == '__main__': 72 unittest.main() 73