4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
30 import SCons.Sig.TimeStamp
35 """A class that simulates a file for testing purposes"""
36 def __init__(self, path, contents, timestamp, builder):
38 self.contents = contents
39 self.timestamp = timestamp
40 self.builder = builder
42 def modify(self, contents, timestamp):
43 self.contents = contents
44 self.timestamp = timestamp
47 """A node workalike for testing purposes"""
49 def __init__(self, file):
52 self.builder = file.builder
55 self.use_signature = 1
62 def has_builder(self):
65 def get_contents(self):
66 # a file that doesn't exist has no contents:
69 return self.file.contents
71 def get_timestamp(self):
72 # a file that doesn't exist has no timestamp:
75 return self.file.timestamp
78 return not self.file.contents is None
80 def cached_exists(self):
82 return self.exists_cache
83 except AttributeError:
84 self.exists_cache = self.exists()
85 return self.exists_cache
88 return not self.file.contents is None
91 return filter(lambda x, i=self.ignore: x not in i,
92 self.sources + self.depends)
94 def all_children(self):
95 return self.sources + self.depends
102 def calc_signature(self, calc):
103 if self.has_builder():
104 return calc.bsig(self)
106 return calc.csig(self)
108 def set_bsig(self, bsig):
114 def store_bsig(self):
117 def set_csig(self, csig):
123 def store_csig(self):
126 def get_prevsiginfo(self):
127 return (self.oldtime, self.oldbsig, self.oldcsig)
129 def get_stored_implicit(self):
132 def store_csig(self):
135 def store_bsig(self):
138 def store_timestamp(self):
141 def builder_sig_adapter(self):
143 def get_contents(self):
145 def get_timestamp(self):
150 def create_files(test):
151 args = [(test.workpath('f1.c'), 'blah blah', 111, 0), #0
152 (test.workpath('f1'), None, 0, 1), #1
153 (test.workpath('d1/f1.c'), 'blah blah', 111, 0), #2
154 (test.workpath('d1/f1.h'), 'blah blah', 111, 0), #3
155 (test.workpath('d1/f2.c'), 'blah blah', 111, 0), #4
156 (test.workpath('d1/f3.h'), 'blah blah', 111, 0), #5
157 (test.workpath('d1/f4.h'), 'blah blah', 111, 0), #6
158 (test.workpath('d1/f1'), None, 0, 1), #7
159 (test.workpath('d1/test.c'), 'blah blah', 111, 0),#8
160 (test.workpath('d1/test.o'), None, 0, 1), #9
161 (test.workpath('d1/test'), None, 0, 1)] #10
163 files = map(lambda x: apply(DummyFile, x), args)
167 def create_nodes(files):
168 nodes = map(DummyNode, files)
170 nodes[0].sources = []
171 nodes[1].sources = [nodes[0]]
172 nodes[2].sources = []
173 nodes[3].sources = []
174 nodes[4].sources = []
175 nodes[5].sources = [nodes[6]]
176 nodes[6].sources = [nodes[5]]
177 nodes[7].sources = [nodes[2], nodes[4], nodes[3], nodes[5]]
178 nodes[8].sources = []
179 nodes[9].sources = [nodes[8]]
180 nodes[10].sources = [nodes[9]]
184 def current(calc, node):
185 return calc.current(node, node.calc_signature(calc))
187 def write(calc, nodes):
189 node.oldtime = node.file.timestamp
190 node.oldbsig = calc.bsig(node)
191 node.oldcsig = calc.csig(node)
202 test = TestCmd.TestCmd(workdir = '')
205 self.files = create_files(test)
209 self.test_modify_same_time()
211 def test_initial(self):
213 nodes = create_nodes(self.files)
214 calc = SCons.Sig.Calculator(self.module)
217 self.failUnless(not current(calc, node),
218 "node %s should not be current" % node.path)
221 self.files[1].modify('built', 222)
222 self.files[7].modify('built', 222)
223 self.files[9].modify('built', 222)
224 self.files[10].modify('built', 222)
226 def test_built(self):
228 nodes = create_nodes(self.files)
230 calc = SCons.Sig.Calculator(self.module)
235 self.failUnless(current(calc, node),
236 "node %s should be current" % node.path)
238 def test_modify(self):
240 nodes = create_nodes(self.files)
242 calc = SCons.Sig.Calculator(self.module)
246 #simulate a modification of some files
247 self.files[0].modify('blah blah blah', 333)
248 self.files[3].modify('blah blah blah', 333)
249 self.files[6].modify('blah blah blah', 333)
250 self.files[8].modify('blah blah blah', 333)
254 self.failUnless(not current(calc, nodes[0]), "modified directly")
255 self.failUnless(not current(calc, nodes[1]), "direct source modified")
256 self.failUnless(current(calc, nodes[2]))
257 self.failUnless(not current(calc, nodes[3]), "modified directly")
258 self.failUnless(current(calc, nodes[4]))
259 self.failUnless(current(calc, nodes[5]))
260 self.failUnless(not current(calc, nodes[6]), "modified directly")
261 self.failUnless(not current(calc, nodes[7]), "indirect source modified")
262 self.failUnless(not current(calc, nodes[8]), "modified directory")
263 self.failUnless(not current(calc, nodes[9]), "direct source modified")
264 self.failUnless(not current(calc, nodes[10]), "indirect source modified")
266 def test_modify_same_time(self):
268 nodes = create_nodes(self.files)
270 calc = SCons.Sig.Calculator(self.module, 0)
274 #simulate a modification of some files without changing the timestamp:
275 self.files[0].modify('blah blah blah blah', 333)
276 self.files[3].modify('blah blah blah blah', 333)
277 self.files[6].modify('blah blah blah blah', 333)
278 self.files[8].modify('blah blah blah blah', 333)
283 self.failUnless(current(calc, node),
284 "node %s should be current" % node.path)
287 class MD5TestCase(unittest.TestCase, SigTestBase):
288 """Test MD5 signatures"""
290 module = SCons.Sig.MD5
292 class TimeStampTestCase(unittest.TestCase, SigTestBase):
293 """Test timestamp signatures"""
295 module = SCons.Sig.TimeStamp
297 class CalcTestCase(unittest.TestCase):
301 def collect(self, signatures):
302 return reduce(lambda x, y: x + y, signatures)
303 def current(self, newsig, oldsig):
304 return newsig == oldsig
305 def signature(self, node):
306 return node.get_csig()
309 def __init__(self, name, bsig, csig):
316 self.use_signature = 1
317 def has_builder(self):
318 return not self.builder is None
320 return filter(lambda x, i=self.ignore: x not in i, self.kids)
321 def all_children(self):
325 def cached_exists(self):
329 def set_bsig(self, bsig):
333 def set_csig(self, csig):
335 def store_csig(self):
337 def store_timestamp(self):
339 def get_prevsiginfo(self):
340 return 0, self.bsig, self.csig
341 def get_stored_implicit(self):
343 def get_timestamp(self):
345 def builder_sig_adapter(self):
349 def get_timestamp(self):
353 self.module = MySigModule()
354 self.nodeclass = MyNode
355 self.test_Calc___init__()
356 self.test_Calc_bsig()
357 self.test_Calc_current()
359 def test_Calc___init__(self):
360 self.calc = SCons.Sig.Calculator(self.module)
361 assert self.calc.module == self.module
363 def test_Calc_bsig(self):
364 n1 = self.nodeclass('n1', 11, 12)
365 n2 = self.nodeclass('n2', 22, 23)
366 n3 = self.nodeclass('n3', 33, 34)
370 assert self.calc.bsig(n1) == 55
374 assert self.calc.bsig(n1) == 33
376 def test_Calc_bsig(self):
377 n = self.nodeclass('n', 11, 12)
379 assert self.calc.csig(n) == 12
381 def test_Calc_current(self):
382 class NN(self.nodeclass):
386 nn = NN('nn', 33, 34)
387 assert not self.calc.current(nn, 30)
388 assert self.calc.current(nn, 33)
390 class SConsignEntryTestCase(unittest.TestCase):
393 e = SCons.Sig.SConsignEntry()
394 assert e.timestamp == None
395 assert e.csig == None
396 assert e.bsig == None
397 assert e.implicit == None
399 class SConsignFileTestCase(unittest.TestCase):
403 def to_string(self, sig):
406 def from_string(self, sig):
410 path = 'not_a_valid_path'
412 f = SCons.Sig.SConsignFile(DummyNode(), DummyModule())
414 assert f.get('foo') == (None, 1, None)
416 assert f.get('foo') == (None, 1, 2)
417 f.set_timestamp('foo', 3)
418 assert f.get('foo') == (3, 1, 2)
419 f.set_implicit('foo', ['bar'])
420 assert f.get('foo') == (3, 1, 2)
421 assert f.get_implicit('foo') == ['bar']
425 suite = unittest.TestSuite()
426 suite.addTest(MD5TestCase())
427 suite.addTest(TimeStampTestCase())
428 suite.addTest(CalcTestCase())
429 suite.addTest(SConsignEntryTestCase())
430 suite.addTest(SConsignFileTestCase())
433 if __name__ == "__main__":
434 runner = unittest.TextTestRunner()
435 result = runner.run(suite())
436 if not result.wasSuccessful():