trial_dir.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import atexit
  5. import logging
  6. import os
  7. import sys
  8. import tempfile
  9. import unittest
  10. import gclient_utils
  11. class TrialDir(object):
  12. """Manages a temporary directory.
  13. On first object creation, TrialDir.TRIAL_ROOT will be set to a new temporary
  14. directory created in /tmp or the equivalent. It will be deleted on process
  15. exit unless TrialDir.SHOULD_LEAK is set to True.
  16. """
  17. # When SHOULD_LEAK is set to True, temporary directories created while the
  18. # tests are running aren't deleted at the end of the tests. Expect failures
  19. # when running more than one test due to inter-test side-effects. Helps with
  20. # debugging.
  21. SHOULD_LEAK = False
  22. # Main root directory.
  23. TRIAL_ROOT = None
  24. def __init__(self, subdir, leak=False):
  25. self.leak = self.SHOULD_LEAK or leak
  26. self.subdir = subdir
  27. self.root_dir = None
  28. def set_up(self):
  29. """All late initialization comes here."""
  30. # You can override self.TRIAL_ROOT.
  31. if not self.TRIAL_ROOT:
  32. # Was not yet initialized.
  33. TrialDir.TRIAL_ROOT = os.path.realpath(
  34. tempfile.mkdtemp(prefix='trial'))
  35. atexit.register(self._clean)
  36. self.root_dir = os.path.join(TrialDir.TRIAL_ROOT, self.subdir)
  37. gclient_utils.rmtree(self.root_dir)
  38. os.makedirs(self.root_dir)
  39. def tear_down(self):
  40. """Cleans the trial subdirectory for this instance."""
  41. if not self.leak:
  42. logging.debug('Removing %s' % self.root_dir)
  43. gclient_utils.rmtree(self.root_dir)
  44. else:
  45. logging.error('Leaking %s' % self.root_dir)
  46. self.root_dir = None
  47. @staticmethod
  48. def _clean():
  49. """Cleans the root trial directory."""
  50. if not TrialDir.SHOULD_LEAK:
  51. logging.debug('Removing %s' % TrialDir.TRIAL_ROOT)
  52. gclient_utils.rmtree(TrialDir.TRIAL_ROOT)
  53. else:
  54. logging.error('Leaking %s' % TrialDir.TRIAL_ROOT)
  55. class TrialDirMixIn(object):
  56. def setUp(self):
  57. # Create a specific directory just for the test.
  58. self.trial = TrialDir(self.id())
  59. self.trial.set_up()
  60. def tearDown(self):
  61. self.trial.tear_down()
  62. @property
  63. def root_dir(self):
  64. return self.trial.root_dir
  65. class TestCase(unittest.TestCase, TrialDirMixIn):
  66. """Base unittest class that cleans off a trial directory in tearDown()."""
  67. def setUp(self):
  68. unittest.TestCase.setUp(self)
  69. TrialDirMixIn.setUp(self)
  70. def tearDown(self):
  71. TrialDirMixIn.tearDown(self)
  72. unittest.TestCase.tearDown(self)
  73. if '-l' in sys.argv:
  74. # See SHOULD_LEAK definition in TrialDir for its purpose.
  75. TrialDir.SHOULD_LEAK = True
  76. print('Leaking!')
  77. sys.argv.remove('-l')