OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2011 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 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(unittest.TestCase, TrialDirMixIn): | |
83 """Base unittest class that cleans off a trial directory in tearDown().""" | |
84 def setUp(self): | |
85 unittest.TestCase.setUp(self) | |
86 TrialDirMixIn.setUp(self) | |
87 | |
88 def tearDown(self): | |
89 TrialDirMixIn.tearDown(self) | |
90 unittest.TestCase.tearDown(self) | |
91 | |
92 | |
93 if '-l' in sys.argv: | |
94 # See SHOULD_LEAK definition in TrialDir for its purpose. | |
95 TrialDir.SHOULD_LEAK = True | |
96 print 'Leaking!' | |
97 sys.argv.remove('-l') | |
OLD | NEW |