PageRenderTime 328ms CodeModel.GetById 150ms app.highlight 5ms RepoModel.GetById 167ms app.codeStats 0ms

/Lib/distutils/tests/support.py

http://unladen-swallow.googlecode.com/
Python | 66 lines | 37 code | 15 blank | 14 comment | 4 complexity | eed3e7cb6ba1a13fc9806c5fe71b0683 MD5 | raw file
 1"""Support code for distutils test cases."""
 2
 3import shutil
 4import tempfile
 5
 6from distutils import log
 7
 8
 9class LoggingSilencer(object):
10
11    def setUp(self):
12        super(LoggingSilencer, self).setUp()
13        self.threshold = log.set_threshold(log.FATAL)
14
15    def tearDown(self):
16        log.set_threshold(self.threshold)
17        super(LoggingSilencer, self).tearDown()
18
19
20class TempdirManager(object):
21    """Mix-in class that handles temporary directories for test cases.
22
23    This is intended to be used with unittest.TestCase.
24    """
25
26    def setUp(self):
27        super(TempdirManager, self).setUp()
28        self.tempdirs = []
29
30    def tearDown(self):
31        super(TempdirManager, self).tearDown()
32        while self.tempdirs:
33            d = self.tempdirs.pop()
34            shutil.rmtree(d)
35
36    def mkdtemp(self):
37        """Create a temporary directory that will be cleaned up.
38
39        Returns the path of the directory.
40        """
41        d = tempfile.mkdtemp()
42        self.tempdirs.append(d)
43        return d
44
45    def write_file(self, path, content='xxx'):
46        """Writes a file in the given path.
47
48        path can be a string or a sequence.
49        """
50        if isinstance(path, (list, tuple)):
51            path = os.path.join(*path)
52        f = open(path, 'w')
53        try:
54            f.write(content)
55        finally:
56            f.close()
57
58class DummyCommand:
59    """Class to store options for retrieval via set_undefined_options()."""
60
61    def __init__(self, **kwargs):
62        for kw, val in kwargs.items():
63            setattr(self, kw, val)
64
65    def ensure_finalized(self):
66        pass