scripttest – test command-line scripts

Helpers for testing command-line scripts

Module Contents

class scripttest.TestFileEnvironment(base_path=None, template_path=None, environ=None, cwd=None, start_clear=True, ignore_paths=None, ignore_hidden=True, capture_temp=False, assert_no_temp=False, split_cmd=True)[source]

This represents an environment in which files will be written, and scripts will be run.

__init__(base_path=None, template_path=None, environ=None, cwd=None, start_clear=True, ignore_paths=None, ignore_hidden=True, capture_temp=False, assert_no_temp=False, split_cmd=True)[source]

Creates an environment. base_path is used as the current working directory, and generally where changes are looked for. If not given, it will be the directory of the calling script plus test-output/.

template_path is the directory to look for template files, which are files you’ll explicitly add to the environment. This is done with .writefile().

environ is the operating system environment, os.environ if not given.

cwd is the working directory, base_path by default.

If start_clear is true (default) then the base_path will be cleared (all files deleted) when an instance is created. You can also use .clear() to clear the files.

ignore_paths is a set of specific filenames that should be ignored when created in the environment. ignore_hidden means, if true (default) that filenames and directories starting with '.' will be ignored.

capture_temp will put temporary files inside the environment (using $TMPDIR). You can then assert that no temporary files are left using .assert_no_temp().

assert_no_temp()[source]

If you use capture_temp then you can use this to make sure no files have been left in the temporary directory

clear(force=False)[source]

Delete all the files in the base directory.

run(script, *args, **kw)[source]

Run the command, with the given arguments. The script argument can have space-separated arguments, or you can use the positional arguments.

Keywords allowed are:

expect_error: (default False)
Don’t raise an exception in case of errors
expect_stderr: (default expect_error)
Don’t raise an exception if anything is printed to stderr
stdin: (default "")
Input to the script
cwd: (default self.cwd)
The working directory to run in (default base_path)
quiet: (default False)
When there’s an error (return code != 0), do not print stdout/stderr

Returns a ProcResult object.

writefile(path, content=None, frompath=None)[source]

Write a file to the given path. If content is given then that text is written, otherwise the file in frompath is used. frompath is relative to self.template_path

Objects that are returned

These objects are returned when you use env.run(...). The ProcResult object is returned, and it has .files_updated, .files_created, and .files_deleted which are dictionaries of FoundFile and FoundDir. The files in .files_deleted represent the pre-deletion state of the file; the other files represent the state of the files after the command is run.

and .files_deleted. These objects dictionary
class scripttest.ProcResult(test_env, args, stdin, stdout, stderr, returncode, files_before, files_after)[source]

Represents the results of running a command in TestFileEnvironment.

Attributes to pay particular attention to:

stdout, stderr:
What is produced on those streams.
returncode:
The return code of the script.
files_created, files_deleted, files_updated:
Dictionaries mapping filenames (relative to the base_path) to FoundFile or FoundDir objects.
class scripttest.FoundFile(base_path, path)[source]

Represents a single file found as the result of a command.

Has attributes:

path:
The path of the file, relative to the base_path
full:
The full path
bytes:
The contents of the file.
stat:
The results of os.stat. Also mtime and size contain the .st_mtime and .st_size of the stat.
mtime:
The modification time of the file.
size:
The size (in bytes) of the file.

You may use the in operator with these objects (tested against the contents of the file), and the .mustcontain() method.

class scripttest.FoundDir(base_path, path)[source]

Represents a directory created by a command.