Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: tests/trial_dir.py

Issue 6625018: Add trial_dir to manage automatic trial directory creation and deletion. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: fix typo Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/fake_repos.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2 # Copyright (c) 2010 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
7 import atexit
8 import logging
9 import os
10 import sys
11 import tempfile
12 import unittest
13
14 import gclient_utils
15
16
17 class TrialDir(object):
18 """Manages a temporary directory.
19
20 On first object creation, TrialDir.TRIAL_ROOT will be set to a new temporary
21 directory created in /tmp or the equivalent. It will be deleted on process
22 exit unless TrialDir.SHOULD_LEAK is set to True.
23 """
24 # When SHOULD_LEAK is set to True, temporary directories created while the
25 # tests are running aren't deleted at the end of the tests. Expect failures
26 # when running more than one test due to inter-test side-effects. Helps with
27 # debugging.
28 SHOULD_LEAK = False
29
30 # Main root directory.
31 TRIAL_ROOT = None
32
33 def __init__(self, subdir, leak=False):
34 self.leak = self.SHOULD_LEAK or leak
35 self.subdir = subdir
36 self.root_dir = None
37
38 def set_up(self):
39 """All late initialization comes here."""
40 # You can override self.TRIAL_ROOT.
41 if not self.TRIAL_ROOT:
42 # Was not yet initialized.
43 TrialDir.TRIAL_ROOT = os.path.realpath(tempfile.mkdtemp(prefix='trial'))
44 atexit.register(self._clean)
45 self.root_dir = os.path.join(TrialDir.TRIAL_ROOT, self.subdir)
46 gclient_utils.RemoveDirectory(self.root_dir)
47 os.makedirs(self.root_dir)
48
49 def tear_down(self):
50 """Cleans the trial subdirectory for this instance."""
51 if not self.leak:
52 logging.debug('Removing %s' % self.root_dir)
53 gclient_utils.RemoveDirectory(self.root_dir)
54 else:
55 logging.error('Leaking %s' % self.root_dir)
56 self.root_dir = None
57
58 @staticmethod
59 def _clean():
60 """Cleans the root trial directory."""
61 if not TrialDir.SHOULD_LEAK:
62 logging.debug('Removing %s' % TrialDir.TRIAL_ROOT)
63 gclient_utils.RemoveDirectory(TrialDir.TRIAL_ROOT)
64 else:
65 logging.error('Leaking %s' % TrialDir.TRIAL_ROOT)
66
67
68 class TestCase(unittest.TestCase):
69 """Base unittest class that cleans off a trial directory in tearDown()."""
70 def setUp(self):
71 # Create a specific directory just for the test.
72 self.trial = TrialDir(self.id())
73 self.trial.set_up()
74
75 def tearDown(self):
76 self.trial.tear_down()
77
78 @property
79 def root_dir(self):
80 return self.trial.root_dir
81
82
83 if '-l' in sys.argv:
84 # See SHOULD_LEAK definition in TrialDir for its purpose.
85 TrialDir.SHOULD_LEAK = True
86 print 'Leaking!'
87 sys.argv.remove('-l')
OLDNEW
« no previous file with comments | « tests/fake_repos.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698