bh@84: # Copyright (C) 2007 by Intevation GmbH bh@84: # Authors: bh@84: # Bernhard Herzog bh@84: # bh@84: # This program is free software under the GPL (>=v2) bh@84: # Read the file COPYING coming with the software for details. bh@84: bh@84: """Support code for the test cases""" bh@84: bh@84: import os bh@84: import shutil bh@84: from treepkg.util import writefile bh@84: bh@84: bh@84: def create_temp_dir(): bh@84: """Create a temporary directory for the test-suite and return its name. bh@84: bh@84: The temporary directory is always called temp and is created in the bh@84: directory where the support module is located. bh@84: bh@84: If the temp directory already exists, just return the name. bh@84: """ bh@84: name = os.path.abspath(os.path.join(os.path.dirname(__file__), "temp")) bh@84: bh@84: # if the directory already exists, we're done bh@84: if os.path.isdir(name): bh@84: return name bh@84: bh@84: # create the directory bh@84: os.mkdir(name) bh@84: return name bh@84: bh@84: bh@84: class FileTestMixin: bh@84: bh@84: """Mixin class for tests that use files in the temporary directory bh@84: """ bh@84: bh@84: def temp_file_name(self, basename, remove=False): bh@84: """Returns the full name of the file named basename in the temp. dir. bh@84: If the remove parameter is true, the file is removed if already exists. bh@84: """ bh@84: filename = os.path.join(create_temp_dir(), basename) bh@84: if remove and os.path.exists(filename): bh@84: os.remove(filename) bh@84: return filename bh@84: bh@84: def create_temp_file(self, basename, contents, mode = None): bh@84: """Creates a file in the temp directory with the given contents. bh@84: The optional parameter mode should either be None (the default) bh@84: or an int specifying the file permissions (same format as the bh@84: second parameter of os.chmod). The method returns the absolute bh@84: name of the created file. bh@84: """ bh@84: filename = self.temp_file_name(basename) bh@84: file = open(filename, "w") bh@84: file.write(contents) bh@84: file.close() bh@84: if mode is not None: bh@84: os.chmod(filename, mode) bh@84: return filename bh@84: bh@84: def create_temp_dir(self, basename, remove=True): bh@84: """Creates the directory basename in the temporary directory. bh@84: If the optional parameter remove is true (the default), the bh@84: directory and all its contents are deleted with shutil.rmtree. bh@84: The method returns the absolute name of the created directory. bh@84: """ bh@84: dirname = os.path.join(create_temp_dir(), basename) bh@84: if remove and os.path.exists(dirname): bh@84: shutil.rmtree(dirname) bh@84: os.mkdir(dirname) bh@84: return dirname bh@84: bh@84: def create_files(self, directory, filedesc): bh@84: """Creates a hieararchy of directories and files in directory. bh@84: The filedesc parameter should be a sequence of (name, contents) bh@84: pairs. Each pair describes one entry of the directory. If bh@84: contents is an instance of list, the entry is a subdirectory and bh@84: the contents is a list in the same format as filedesc and passed bh@84: recursively to the create_files method. If contents is a bh@84: string, the new directory entry is a normal file and contents is bh@84: the contents of the file. bh@84: """ bh@84: for name, contents in filedesc: bh@84: if isinstance(contents, list): bh@84: # a list as contents indicates a directory bh@84: newdir = os.path.join(directory, name) bh@84: os.mkdir(newdir) bh@84: self.create_files(newdir, contents) bh@84: else: bh@84: writefile(os.path.join(directory, name), contents) bh@111: bh@111: def checkFileContents(self, filename, contents): bh@111: """check the contents of a file""" bh@111: self.assertEquals(open(filename).read(), contents)