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

Side by Side Diff: testing_support/trial_dir.py

Issue 939523004: Copied files from depot_tools. (Closed) Base URL: https://chromium.googlesource.com/infra/testing/testing_support.git@master
Patch Set: Created 5 years, 10 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
« testing_support/super_mox.py ('K') | « testing_support/super_mox.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 # 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
5
6 import atexit
7 import logging
8 import os
9 import sys
10 import tempfile
11
12 from testing_support import auto_stub
13
14 import gclient_utils
iannucci 2015/02/19 00:22:39 does this work?
pgervais 2015/02/19 00:45:57 nope
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.rmtree(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.rmtree(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.rmtree(TrialDir.TRIAL_ROOT)
64 else:
65 logging.error('Leaking %s' % TrialDir.TRIAL_ROOT)
66
67
68 class TrialDirMixIn(object):
69 def setUp(self):
70 # Create a specific directory just for the test.
71 self.trial = TrialDir(self.id())
72 self.trial.set_up()
73
74 def tearDown(self):
75 self.trial.tear_down()
76
77 @property
78 def root_dir(self):
79 return self.trial.root_dir
80
81
82 class TestCase(auto_stub.TestCase, TrialDirMixIn):
83 """Base unittest class that cleans off a trial directory in tearDown()."""
84 def setUp(self):
85 auto_stub.TestCase.setUp(self)
86 TrialDirMixIn.setUp(self)
87
88 def tearDown(self):
89 TrialDirMixIn.tearDown(self)
90 auto_stub.TestCase.tearDown(self)
91
92
93 if '-l' in sys.argv:
iannucci 2015/02/19 00:22:39 ..... !
pgervais 2015/02/19 00:45:57 Dropped :-)
94 # See SHOULD_LEAK definition in TrialDir for its purpose.
95 TrialDir.SHOULD_LEAK = True
96 print 'Leaking!'
97 sys.argv.remove('-l')
OLDNEW
« testing_support/super_mox.py ('K') | « testing_support/super_mox.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698