Mercurial > treepkg
comparison test/filesupport.py @ 185:e1c7cd896310
Rework test/filesupport.py so that test cases always use their own
temporary directory. Before, the test cases had to ensure this
themselves.
Adapt the test cases.
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Thu, 26 Jun 2008 16:23:26 +0000 |
parents | 007d7f2aa184 |
children | e3ab8aca2b08 |
comparison
equal
deleted
inserted
replaced
184:83c77307ffb1 | 185:e1c7cd896310 |
---|---|
31 return name | 31 return name |
32 | 32 |
33 | 33 |
34 class FileTestMixin: | 34 class FileTestMixin: |
35 | 35 |
36 """Mixin class for tests that use files in the temporary directory | 36 """Mixin class for tests that use temporary files. |
37 | |
38 Instances of this class create a test-specific sub-directory under | |
39 the main test temp directory. The name of the sub-directory is the | |
40 return value of the test's id() method. | |
37 """ | 41 """ |
38 | 42 |
39 def temp_file_name(self, basename, remove=False): | 43 _test_specific_directory_created = False |
44 | |
45 def create_test_specific_temp_dir(self): | |
46 """Creates the test specific directory and returns its absolute name. | |
47 When this method is called for the first time and the directory | |
48 exists, if is removed, so that a specific test instance always | |
49 starts with an empty directory. | |
50 """ | |
51 dirname = os.path.join(create_temp_dir(), self.id()) | |
52 if not self._test_specific_directory_created: | |
53 if os.path.exists(dirname): | |
54 shutil.rmtree(dirname) | |
55 os.mkdir(dirname) | |
56 self._test_specific_directory_created = True | |
57 return dirname | |
58 | |
59 def temp_file_name(self, basename): | |
40 """Returns the full name of the file named basename in the temp. dir. | 60 """Returns the full name of the file named basename in the temp. dir. |
41 If the remove parameter is true, the file is removed if already exists. | |
42 """ | 61 """ |
43 filename = os.path.join(create_temp_dir(), basename) | 62 return os.path.join(self.create_test_specific_temp_dir(), basename) |
44 if remove and os.path.exists(filename): | |
45 os.remove(filename) | |
46 return filename | |
47 | 63 |
48 def create_temp_file(self, basename, contents, mode = None): | 64 def create_temp_file(self, basename, contents, permissions=None): |
49 """Creates a file in the temp directory with the given contents. | 65 """Creates a file in the temp directory with the given contents. |
50 The optional parameter mode should either be None (the default) | 66 The optional parameter permissions should either be None (the |
51 or an int specifying the file permissions (same format as the | 67 default) or an int specifying the file permissions (same format |
52 second parameter of os.chmod). The method returns the absolute | 68 as the second parameter of os.chmod). The method returns the |
53 name of the created file. | 69 absolute name of the created file. |
54 """ | 70 """ |
55 filename = self.temp_file_name(basename) | 71 filename = self.temp_file_name(basename) |
56 file = open(filename, "w") | 72 file = open(filename, "w") |
57 file.write(contents) | 73 file.write(contents) |
58 file.close() | 74 file.close() |
59 if mode is not None: | 75 if permissions is not None: |
60 os.chmod(filename, mode) | 76 os.chmod(filename, permissions) |
61 return filename | 77 return filename |
62 | 78 |
63 def create_temp_dir(self, basename, remove=True): | 79 def create_temp_dir(self, basename): |
64 """Creates the directory basename in the temporary directory. | 80 """Creates the directory basename in the temporary directory. |
65 If the optional parameter remove is true (the default), the | |
66 directory and all its contents are deleted with shutil.rmtree. | |
67 The method returns the absolute name of the created directory. | 81 The method returns the absolute name of the created directory. |
68 """ | 82 """ |
69 dirname = os.path.join(create_temp_dir(), basename) | 83 dirname = self.temp_file_name(basename) |
70 if remove and os.path.exists(dirname): | |
71 shutil.rmtree(dirname) | |
72 os.mkdir(dirname) | 84 os.mkdir(dirname) |
73 return dirname | 85 return dirname |
74 | 86 |
75 def create_files(self, directory, filedesc): | 87 def create_files(self, directory, filedesc): |
76 """Creates a hierarchy of directories and files in directory. | 88 """Creates a hierarchy of directories and files in directory. |
82 recursively to the create_files method. If contents is a | 94 recursively to the create_files method. If contents is a |
83 string, the new directory entry is a normal file and contents is | 95 string, the new directory entry is a normal file and contents is |
84 the contents of the file. The permissions if present, should be | 96 the contents of the file. The permissions if present, should be |
85 an int specifying the files permissions (setting permissions | 97 an int specifying the files permissions (setting permissions |
86 doesn't work yet for directories). | 98 doesn't work yet for directories). |
99 | |
100 The method returns the absolute name of the toplevel directory | |
101 created by the method. | |
87 """ | 102 """ |
88 shutil.rmtree(directory, True) | 103 directory = self.temp_file_name(directory) |
89 os.makedirs(directory) | 104 os.makedirs(directory) |
90 for item in filedesc: | 105 for item in filedesc: |
91 if len(item) == 3: | 106 if len(item) == 3: |
92 name, permissions, contents = item | 107 name, permissions, contents = item |
93 else: | 108 else: |
94 name, contents = item | 109 name, contents = item |
95 permissions = None # use default permissions | 110 permissions = None # use default permissions |
96 if isinstance(contents, list): | 111 if isinstance(contents, list): |
97 # a list as contents indicates a directory | 112 # a list as contents indicates a directory |
98 newdir = os.path.join(directory, name) | 113 self.create_files(os.path.join(directory, name), contents) |
99 os.mkdir(newdir) | |
100 self.create_files(newdir, contents) | |
101 else: | 114 else: |
102 writefile(os.path.join(directory, name), contents, permissions) | 115 writefile(os.path.join(directory, name), contents, permissions) |
116 return directory | |
103 | 117 |
104 def checkFileContents(self, filename, contents): | 118 def checkFileContents(self, filename, contents): |
105 """check the contents of a file""" | 119 """check the contents of a file""" |
106 self.assertEquals(open(filename).read(), contents) | 120 self.assertEquals(open(filename).read(), contents) |