blob: 578cf40c09cb200b5b4d63c825e7954f0c5729e6 [file] [log] [blame]
Fred Drakeb8ab8b62004-06-17 20:14:50 +00001"""Support code for distutils test cases."""
Tarek Ziadé13699002009-02-14 14:10:23 +00002import os
Fred Drakeb8ab8b62004-06-17 20:14:50 +00003import shutil
4import tempfile
5
Fred Drakeedcac8f2004-08-03 18:53:07 +00006from distutils import log
Tarek Ziadé02386282009-03-31 20:50:59 +00007from distutils.core import Distribution
Fred Drakeedcac8f2004-08-03 18:53:07 +00008
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
Fred Drakeb8ab8b62004-06-17 20:14:50 +000019
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()
Tarek Ziadé13699002009-02-14 14:10:23 +000034 shutil.rmtree(d, os.name in ('nt', 'cygwin'))
Fred Drakeb8ab8b62004-06-17 20:14:50 +000035
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
Tarek Ziadé02386282009-03-31 20:50:59 +000045 def write_file(self, path, content='xxx'):
Tarek Ziadé7dd53392009-02-16 21:38:01 +000046 """Writes a file in the given path.
47
48
49 path can be a string or a sequence.
50 """
51 if isinstance(path, (list, tuple)):
52 path = os.path.join(*path)
53 f = open(path, 'w')
54 try:
55 f.write(content)
56 finally:
57 f.close()
Fred Drakeb8ab8b62004-06-17 20:14:50 +000058
Tarek Ziadé02386282009-03-31 20:50:59 +000059 def create_dist(self, pkg_name='foo', **kw):
60 """Will generate a test environment.
61
62 This function creates:
63 - a Distribution instance using keywords
64 - a temporary directory with a package structure
65
66 It returns the package directory and the distribution
67 instance.
68 """
69 tmp_dir = self.mkdtemp()
70 pkg_dir = os.path.join(tmp_dir, pkg_name)
71 os.mkdir(pkg_dir)
72 dist = Distribution(attrs=kw)
73
74 return pkg_dir, dist
75
Fred Drakeb8ab8b62004-06-17 20:14:50 +000076class DummyCommand:
77 """Class to store options for retrieval via set_undefined_options()."""
78
79 def __init__(self, **kwargs):
80 for kw, val in kwargs.items():
81 setattr(self, kw, val)
82
83 def ensure_finalized(self):
84 pass