blob: c5d75330fc283da547ecbec6538d9f707cb8d008 [file] [log] [blame]
Kevin McNee4c404c52022-02-15 18:04:281#!/usr/bin/env python3
Avi Drissmandfd880852022-09-15 20:11:092# Copyright 2018 The Chromium Authors
Daniel Cheng1b2c7052018-12-10 23:59:113# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
Daniel Cheng1b2c7052018-12-10 23:59:115"""Usage: run_with_dummy_home.py <command>
6
7Helper for running a test with a dummy $HOME, populated with just enough for
8tests to run and pass. Useful for isolating tests from the real $HOME, which
9can contain config files that negatively affect test performance.
10"""
11
12import os
13import shutil
14import subprocess
15import sys
16import tempfile
17
18
19def _set_up_dummy_home(original_home, dummy_home):
20 """Sets up a dummy $HOME that Chromium tests can run in.
21
22 Files are copied, while directories are symlinked.
23 """
24 for filename in ['.Xauthority']:
25 original_path = os.path.join(original_home, filename)
26 if not os.path.exists(original_path):
27 continue
28 shutil.copyfile(original_path, os.path.join(dummy_home, filename))
29
30 # Prevent fontconfig etc. from reconstructing the cache and symlink rr
31 # trace directory.
32 for dirpath in [['.cache'], ['.local', 'share', 'rr'], ['.vpython'],
33 ['.vpython_cipd_cache'], ['.vpython-root']]:
34 original_path = os.path.join(original_home, *dirpath)
35 if not os.path.exists(original_path):
36 continue
37 dummy_parent_path = os.path.join(dummy_home, *dirpath[:-1])
38 if not os.path.isdir(dummy_parent_path):
39 os.makedirs(dummy_parent_path)
40 os.symlink(original_path, os.path.join(dummy_home, *dirpath))
41
42
43def main():
44 try:
45 dummy_home = tempfile.mkdtemp()
Wenbin Zhangc56b1252021-10-05 02:49:0646 print('Creating dummy home in %s' % dummy_home)
Daniel Cheng1b2c7052018-12-10 23:59:1147
48 original_home = os.environ['HOME']
49 os.environ['HOME'] = dummy_home
50 _set_up_dummy_home(original_home, dummy_home)
51
52 return subprocess.call(sys.argv[1:])
53 finally:
54 shutil.rmtree(dummy_home)
55
56
57if __name__ == '__main__':
58 sys.exit(main())