OLD | NEW |
---|---|
(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 """Base unittest class that cleans off a trial directory.""" | |
Evan Martin
2011/03/04 19:46:11
Can you elaborate on this? (English note: "cleans
Evan Martin
2011/03/04 21:44:28
This comment is now wrong, too. :)
"""Helper cla
| |
7 | |
8 import atexit | |
9 import logging | |
10 import os | |
11 import sys | |
12 import tempfile | |
13 import unittest | |
14 | |
15 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | |
16 sys.path.insert(0, os.path.dirname(ROOT_DIR)) | |
Evan Martin
2011/03/04 19:46:11
Isn't this implicit in how Python imports work?
M-A Ruel
2011/03/04 20:00:49
No because I'm adding the parent's directory.
| |
17 | |
18 import gclient_utils | |
19 | |
20 | |
21 class TrialDir(object): | |
22 """Manages temporary directories. On first object creation, | |
23 TrialDir.TRIAL_ROOT will be set to a new temporary directory. It will be | |
24 deleted on process exit unless TrialDir.SHOULD_LEAK is set to True. | |
25 """ | |
26 # When SHOULD_LEAK is set to True, temporary directories created while the | |
27 # tests are running aren't deleted at the end of the tests. Expect failures | |
28 # when running more than one test due to inter-test side-effects. Helps with | |
29 # debugging. | |
30 SHOULD_LEAK = False | |
31 | |
32 # Main root directory. | |
33 TRIAL_ROOT = None | |
34 | |
35 def __init__(self, subdir, leak=False): | |
36 self.leak = self.SHOULD_LEAK or leak | |
37 self.subdir = subdir | |
38 self.root_dir = None | |
39 | |
40 def set_up(self): | |
41 """All late initialization comes here. | |
42 | |
43 Note that it deletes all trial_dir() and not only repos_dir. | |
Evan Martin
2011/03/04 19:46:11
repos_dir isn't meaningful in this file, I'm not s
M-A Ruel
2011/03/04 20:00:49
Copy-paste fail.
| |
44 """ | |
45 # You can override self.TRIAL_ROOT. | |
46 if not self.TRIAL_ROOT: | |
47 # Was not yet initialized. | |
48 TrialDir.TRIAL_ROOT = os.path.realpath(tempfile.mkdtemp(prefix='trial')) | |
49 atexit.register(self._clean) | |
50 self.root_dir = os.path.join(TrialDir.TRIAL_ROOT, self.subdir) | |
51 gclient_utils.RemoveDirectory(self.root_dir) | |
52 os.makedirs(self.root_dir) | |
53 | |
54 def tear_down(self): | |
55 """Cleans the trial subdirectory for this instance.""" | |
56 if not self.leak: | |
57 logging.debug('Removing %s' % self.root_dir) | |
58 gclient_utils.RemoveDirectory(self.root_dir) | |
59 else: | |
60 logging.error('Leaking %s' % self.root_dir) | |
61 self.root_dir = None | |
62 | |
63 @staticmethod | |
64 def _clean(): | |
65 """Cleans the root trial directory.""" | |
66 if not TrialDir.SHOULD_LEAK: | |
67 logging.debug('Removing %s' % TrialDir.TRIAL_ROOT) | |
68 gclient_utils.RemoveDirectory(TrialDir.TRIAL_ROOT) | |
69 else: | |
70 logging.error('Leaking' % TrialDir.TRIAL_ROOT) | |
71 | |
72 | |
73 class TestCase(unittest.TestCase): | |
74 def setUp(self): | |
75 # Create a specific directory just for the test. | |
76 self.trial = TrialDir(self.id()) | |
77 self.trial.set_up() | |
78 | |
79 def tearDown(self): | |
80 self.trial.tear_down() | |
81 | |
82 @property | |
83 def root_dir(self): | |
84 return self.trial.root_dir | |
85 | |
86 | |
87 if '-l' in sys.argv: | |
Evan Martin
2011/03/04 19:46:11
This feels fragile, but I'll let you judge. gyp u
M-A Ruel
2011/03/04 20:00:49
It's only used when testing manually to inspect th
| |
88 # See SHOULD_LEAK definition in TrialDir for its purpose. | |
89 TrialDir.SHOULD_LEAK = True | |
90 print 'Leaking!' | |
91 sys.argv.remove('-l') | |
OLD | NEW |