2 # Copyright (c) 2012 The Native Client 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.
7 from os.path import dirname
18 # Some of the test+tools require running the host binaries, but that
19 # does not work on some bots (e.g., the ARM bots).
20 if platform.machine().startswith('arm'):
22 # We also cannot run some of the Windows binaries directly, since
23 # they depend on cygwin DLLs and the cygwin DLLs are only in the
24 # path for the installed drivers bin and not for the binaries.
25 if sys.platform == 'win32':
29 def _SetupLinuxHostDir(env, nacl_dir):
30 # Use the 32-bit path by default, but fall back to 64-bit if the 32-bit does
32 dir_template = os.path.join(nacl_dir, 'toolchain', 'pnacl_linux_x86',
34 dir_32 = dir_template % 'x86_32'
35 dir_64 = dir_template % 'x86_64'
36 driver_tools.AddHostBinarySearchPath(
37 dir_32 if os.path.exists(dir_32) else dir_64)
39 def SetupNaClDir(env):
40 test_dir = os.path.abspath(dirname(__file__))
41 nacl_dir = dirname(dirname(dirname(test_dir)))
42 env.set('BASE_NACL', nacl_dir)
44 def SetupToolchainDir(env):
45 test_dir = os.path.abspath(dirname(__file__))
46 nacl_dir = dirname(dirname(dirname(test_dir)))
47 toolchain_dir = os.path.join(nacl_dir, 'toolchain')
48 env.set('BASE_TOOLCHAIN', toolchain_dir)
50 def SetupHostDir(env):
51 # Some of the tools end up running one of the host binaries. Find the host
52 # dir on the test system and inject it into the search path using the
53 # implementation of -B
54 test_dir = os.path.abspath(dirname(__file__))
55 nacl_dir = dirname(dirname(dirname(test_dir)))
56 if sys.platform == 'darwin':
59 elif sys.platform.startswith('linux'):
60 _SetupLinuxHostDir(env, nacl_dir)
62 elif sys.platform in ('cygwin', 'win32'):
65 host_dir = os.path.join(nacl_dir, 'toolchain',
66 'pnacl_%s_x86' % os_shortname,
67 'host_%s' % host_arch)
68 driver_tools.AddHostBinarySearchPath(host_dir)
70 # A collection of override methods that mock driver_env.Environment.
72 # One thing is we prevent having to read a driver.conf file,
73 # so instead we have a base group of variables set for testing.
74 def TestEnvReset(self, more_overrides={}):
75 # Call to "super" class method (assumed to be driver_env.Environment).
76 # TODO(jvoung): We may want a different way of overriding things.
77 driver_env.Environment.reset(self)
79 self.set('PNACL_RUNNING_UNITTESTS', '1')
81 SetupToolchainDir(self)
83 for k, v in more_overrides.iteritems():
86 def ApplyTestEnvOverrides(env, more_overrides={}):
87 """Register all the override methods and reset the env to a testable state.
89 resetter = lambda self: TestEnvReset(self, more_overrides)
90 driver_env.override_env('reset', resetter)
93 # Utils to prevent driver exit.
95 class DriverExitException(Exception):
99 raise DriverExitException('Stubbed out DriverExit!')
101 # Basic argument parsing.
103 def GetPlatformToTest():
105 if arg.startswith('--platform='):
106 return arg.split('=')[1]
107 raise Exception('Unknown platform')
109 # We would like to be able to use a temp file whether it is open or closed.
110 # However File's __enter__ method requires it to be open. So we override it
111 # to just return the fd regardless.
112 class TempWrapper(object):
113 def __init__(self, fd, close=True):
119 def __exit__(self, exc_type, exc_value, traceback):
120 return self.fd_.__exit__(exc_type, exc_value, traceback)
121 def __getattr__(self, name):
122 return getattr(self.fd_, name)
124 class DriverTesterCommon(unittest.TestCase):
126 super(DriverTesterCommon, self).setUp()
130 for t in self._tempfiles:
134 driver_log.TempFiles.wipe()
135 super(DriverTesterCommon, self).tearDown()
137 def getTemp(self, close=True, **kwargs):
138 """ Get a temporary named file object.
140 # Set delete=False, so that we can close the files and
141 # re-open them. Windows sometimes does not allow you to
142 # re-open an already opened temp file.
143 t = tempfile.NamedTemporaryFile(delete=False, **kwargs)
144 self._tempfiles.append(t)
145 return TempWrapper(t, close=close)