run_with_dummy_home.py 1.8 KB

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