blob: 3d28e060f5d1d41c7204e116dcbfdb5011de9cb5 [file] [log] [blame]
Daniel Cheng1b2c7052018-12-10 23:59:111#!/usr/bin/env python
2# Copyright 2018 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.
5
6"""Usage: run_with_dummy_home.py <command>
7
8Helper for running a test with a dummy $HOME, populated with just enough for
9tests to run and pass. Useful for isolating tests from the real $HOME, which
10can contain config files that negatively affect test performance.
11"""
12
13import os
14import shutil
15import subprocess
16import sys
17import tempfile
18
19
20def _set_up_dummy_home(original_home, dummy_home):
21 """Sets up a dummy $HOME that Chromium tests can run in.
22
23 Files are copied, while directories are symlinked.
24 """
25 for filename in ['.Xauthority']:
26 original_path = os.path.join(original_home, filename)
27 if not os.path.exists(original_path):
28 continue
29 shutil.copyfile(original_path, os.path.join(dummy_home, filename))
30
31 # Prevent fontconfig etc. from reconstructing the cache and symlink rr
32 # trace directory.
33 for dirpath in [['.cache'], ['.local', 'share', 'rr'], ['.vpython'],
34 ['.vpython_cipd_cache'], ['.vpython-root']]:
35 original_path = os.path.join(original_home, *dirpath)
36 if not os.path.exists(original_path):
37 continue
38 dummy_parent_path = os.path.join(dummy_home, *dirpath[:-1])
39 if not os.path.isdir(dummy_parent_path):
40 os.makedirs(dummy_parent_path)
41 os.symlink(original_path, os.path.join(dummy_home, *dirpath))
42
43
44def main():
45 try:
46 dummy_home = tempfile.mkdtemp()
47 print 'Creating dummy home in %s' % dummy_home
48
49 original_home = os.environ['HOME']
50 os.environ['HOME'] = dummy_home
51 _set_up_dummy_home(original_home, dummy_home)
52
53 return subprocess.call(sys.argv[1:])
54 finally:
55 shutil.rmtree(dummy_home)
56
57
58if __name__ == '__main__':
59 sys.exit(main())