Package CedarBackup2 :: Module testutil
[show private | hide private]
[frames | no frames]

Module CedarBackup2.testutil

Provides unit-testing utilities.

These utilities are kept here, separate from util.py, because they provide common functionality that I do not want exported "publicly" once Cedar Backup is installed on a system. They are only used for unit testing, and are only useful within the source tree.

Many of these functions are in here because they are "good enough" for unit test work but are not robust enough to be real public functions. Others (like removedir do what they are supposed to, but I don't want responsibility for making them available to others.

Author: Kenneth J. Pronovici <pronovic@ieee.org>

Function Summary
  findResources(resources, dataDirs)
Returns a dictionary of locations for various resources.
  buildPath(components)
Builds a complete path from a list of components.
  removedir(tree)
Recursively removes an entire directory.
  extractTar(tmpdir, filepath)
Extracts the indicated tar file to the indicated tmpdir.
  changeFileAge(filename, subtract)
Changes a file age using the os.utime function.
  getMaskAsMode()
Returns the user's current umask inverted to a mode.
  getLogin()
Returns the name of the currently-logged in user.
  failUnlessAssignRaises(testCase, exception, object, property, value)
Equivalent of failUnlessRaises, but used for property assignments instead.
  randomFilename(length, prefix, suffix)
Generates a random filename with the given length.

Function Details

findResources(resources, dataDirs)

Returns a dictionary of locations for various resources.
Parameters:
resources - List of required resources.
dataDirs - List of data directories to search within for resources.
Returns:
Dictionary mapping resource name to resource path.
Raises:
Exception - If some resource cannot be found.

buildPath(components)

Builds a complete path from a list of components. For instance, constructs "/a/b/c" from ["/a", "b", "c",].
Parameters:
components - List of components.
Returns:
String path constructed from components.
Raises:
ValueError - If a path cannot be encoded properly.

removedir(tree)

Recursively removes an entire directory. This is basically taken from an example on python.com.
Parameters:
tree - Directory tree to remove.
Raises:
ValueError - If a path cannot be encoded properly.

extractTar(tmpdir, filepath)

Extracts the indicated tar file to the indicated tmpdir.
Parameters:
tmpdir - Temp directory to extract to.
filepath - Path to tarfile to extract.
Raises:
ValueError - If a path cannot be encoded properly.

changeFileAge(filename, subtract=None)

Changes a file age using the os.utime function.
Parameters:
filename - File to operate on.
subtract - Number of seconds to subtract from the current time.
Raises:
ValueError - If a path cannot be encoded properly.

getMaskAsMode()

Returns the user's current umask inverted to a mode. A mode is mostly a bitwise inversion of a mask, i.e. mask 002 is mode 775.
Returns:
Umask converted to a mode, as an integer.

getLogin()

Returns the name of the currently-logged in user. This might fail under some circumstances - but if it does, our tests would fail anyway.

failUnlessAssignRaises(testCase, exception, object, property, value)

Equivalent of failUnlessRaises, but used for property assignments instead.

It's nice to be able to use failUnlessRaises to check that a method call raises the exception that you expect. Unfortunately, this method can't be used to check Python propery assignments, even though these property assignments are actually implemented underneath as methods.

This function (which can be easily called by unit test classes) provides an easy way to wrap the assignment checks. It's not pretty, or as intuitive as the original check it's modeled on, but it does work.

Let's assume you make this method call:
  testCase.failUnlessAssignRaises(ValueError, collectDir, "absolutePath", absolutePath)
If you do this, a test case failure will be raised unless the assignment:
  collectDir.absolutePath = absolutePath
fails with a ValueError exception. The failure message differentiates between the case where no exception was raised and the case where the wrong exception was raised.
Parameters:
testCase - PyUnit test case object (i.e. self).
exception - Exception that is expected to be raised.
object - Object whose property is to be assigned to.
property - Name of the property, as a string.
value - Value that is to be assigned to the property.

Note: Internally, the missed and instead variables are used rather than directly calling testCase.fail upon noticing a problem because the act of "failure" itself generates an exception that would be caught by the general except clause.

See Also: unittest.TestCase.failUnlessRaises

randomFilename(length, prefix=None, suffix=None)

Generates a random filename with the given length.
Parameters:
length - Length of filename. @return Random filename.

Generated by Epydoc 2.1 on Thu Dec 22 20:45:15 2005 http://epydoc.sf.net