Fix more MSVC warnings, courgette/ edition.
[chromium-blink-merge.git] / testing / test_env.py
blob0729e17ed1112f562a1cb6d9daed18ce7ef986ce
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium 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.
6 """Sets environment variables needed to run a chromium unit test."""
8 import os
9 import stat
10 import subprocess
11 import sys
13 # This is hardcoded to be src/ relative to this script.
14 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
16 CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX'
17 CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox'
20 def should_enable_sandbox(cmd, sandbox_path):
21 """Return a boolean indicating that the current slave is capable of using the
22 sandbox and should enable it. This should return True iff the slave is a
23 Linux host with the sandbox file present and configured correctly."""
24 if not (sys.platform.startswith('linux') and
25 os.path.exists(sandbox_path)):
26 return False
28 # Copy the check in tools/build/scripts/slave/runtest.py.
29 if '--lsan=1' in cmd:
30 return False
32 sandbox_stat = os.stat(sandbox_path)
33 if ((sandbox_stat.st_mode & stat.S_ISUID) and
34 (sandbox_stat.st_mode & stat.S_IRUSR) and
35 (sandbox_stat.st_mode & stat.S_IXUSR) and
36 (sandbox_stat.st_uid == 0)):
37 return True
38 return False
41 def enable_sandbox_if_required(cmd, env, verbose=False):
42 """Checks enables the sandbox if it is required, otherwise it disables it."""
43 chrome_sandbox_path = env.get(CHROME_SANDBOX_ENV, CHROME_SANDBOX_PATH)
45 if should_enable_sandbox(cmd, chrome_sandbox_path):
46 if verbose:
47 print 'Enabling sandbox. Setting environment variable:'
48 print ' %s="%s"' % (CHROME_SANDBOX_ENV, chrome_sandbox_path)
49 env[CHROME_SANDBOX_ENV] = chrome_sandbox_path
50 else:
51 if verbose:
52 print 'Disabling sandbox. Setting environment variable:'
53 print ' CHROME_DEVEL_SANDBOX=""'
54 env['CHROME_DEVEL_SANDBOX'] = ''
57 def fix_python_path(cmd):
58 """Returns the fixed command line to call the right python executable."""
59 out = cmd[:]
60 if out[0] == 'python':
61 out[0] = sys.executable
62 elif out[0].endswith('.py'):
63 out.insert(0, sys.executable)
64 return out
67 def run_executable(cmd, env):
68 """Runs an executable with:
69 - environment variable CR_SOURCE_ROOT set to the root directory.
70 - environment variable LANGUAGE to en_US.UTF-8.
71 - environment variable CHROME_DEVEL_SANDBOX set if need
72 - Reuses sys.executable automatically.
73 """
74 # Many tests assume a English interface...
75 env['LANG'] = 'en_US.UTF-8'
76 # Used by base/base_paths_linux.cc as an override. Just make sure the default
77 # logic is used.
78 env.pop('CR_SOURCE_ROOT', None)
79 enable_sandbox_if_required(cmd, env)
80 # Ensure paths are correctly separated on windows.
81 cmd[0] = cmd[0].replace('/', os.path.sep)
82 cmd = fix_python_path(cmd)
83 try:
84 return subprocess.call(cmd, env=env)
85 except OSError:
86 print >> sys.stderr, 'Failed to start %s' % cmd
87 raise
90 def main():
91 return run_executable(sys.argv[1:], os.environ.copy())
94 if __name__ == '__main__':
95 sys.exit(main())