Home | History | Annotate | Download | only in unittests
      1 #!/usr/bin/env python
      2 # Copyright 2017 the V8 project authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 
      6 import os
      7 import subprocess
      8 import sys
      9 import tempfile
     10 import unittest
     11 
     12 TOOLS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
     13 
     14 PREDICTABLE_WRAPPER = os.path.join(
     15     TOOLS_DIR, 'predictable_wrapper.py')
     16 
     17 PREDICTABLE_MOCKED = os.path.join(
     18     TOOLS_DIR, 'unittests', 'testdata', 'predictable_mocked.py')
     19 
     20 def call_wrapper(mode):
     21   """Call the predictable wrapper under test with a mocked file to test.
     22 
     23   Instead of d8, we use python and a python mock script. This mock script is
     24   expecting two arguments, mode (one of 'equal', 'differ' or 'missing') and
     25   a path to a temporary file for simulating non-determinism.
     26   """
     27   fd, state_file = tempfile.mkstemp()
     28   os.close(fd)
     29   try:
     30     args = [
     31       sys.executable,
     32       PREDICTABLE_WRAPPER,
     33       sys.executable,
     34       PREDICTABLE_MOCKED,
     35       mode,
     36       state_file,
     37     ]
     38     proc = subprocess.Popen(args, stdout=subprocess.PIPE)
     39     proc.communicate()
     40     return proc.returncode
     41   finally:
     42     os.unlink(state_file)
     43 
     44 
     45 class PredictableTest(unittest.TestCase):
     46   def testEqualAllocationOutput(self):
     47     self.assertEqual(0, call_wrapper('equal'))
     48 
     49   def testNoAllocationOutput(self):
     50     self.assertEqual(2, call_wrapper('missing'))
     51 
     52   def testDifferentAllocationOutput(self):
     53     self.assertEqual(3, call_wrapper('differ'))
     54 
     55 
     56 if __name__ == '__main__':
     57   unittest.main()
     58