blob: ab2af9a6aa186bce04dd09fcb36cbee4ccd5e03e [file] [log] [blame]
Fred Drakeb8ab8b62004-06-17 20:14:50 +00001"""Support code for distutils test cases."""
Tarek Ziadéc1375d52009-02-14 14:35:51 +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ébaf51802009-03-31 21:37:16 +00007from distutils.core import Distribution
Fred Drakeedcac8f2004-08-03 18:53:07 +00008
9class LoggingSilencer(object):
10
11 def setUp(self):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000012 super().setUp()
Fred Drakeedcac8f2004-08-03 18:53:07 +000013 self.threshold = log.set_threshold(log.FATAL)
14
15 def tearDown(self):
16 log.set_threshold(self.threshold)
Guido van Rossumcd16bf62007-06-13 18:07:49 +000017 super().tearDown()
Fred Drakeedcac8f2004-08-03 18:53:07 +000018
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):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000027 super().setUp()
Fred Drakeb8ab8b62004-06-17 20:14:50 +000028 self.tempdirs = []
29
30 def tearDown(self):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000031 super().tearDown()
Fred Drakeb8ab8b62004-06-17 20:14:50 +000032 while self.tempdirs:
33 d = self.tempdirs.pop()
Tarek Ziadéc1375d52009-02-14 14:35:51 +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ébaf51802009-03-31 21:37:16 +000045 def write_file(self, path, content='xxx'):
Tarek Ziadé0d0506e2009-02-16 21:49:12 +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ébaf51802009-03-31 21:37:16 +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