scripttest – test command-line scripts¶
Helpers for testing command-line scripts
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)¶
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)¶
Creates an environment.
base_pathis 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
template_pathis the directory to look for template files, which are files you’ll explicitly add to the environment. This is done with
environis the operating system environment,
os.environif not given.
cwdis the working directory,
start_clearis true (default) then the
base_pathwill be cleared (all files deleted) when an instance is created. You can also use
.clear()to clear the files.
ignore_pathsis a set of specific filenames that should be ignored when created in the environment.
ignore_hiddenmeans, if true (default) that filenames and directories starting with
'.'will be ignored.
capture_tempwill put temporary files inside the environment (using
$TMPDIR). You can then assert that no temporary files are left using
If you use
capture_tempthen you can use this to make sure no files have been left in the temporary directory
Delete all the files in the base directory.
run(script, *args, **kw)¶
Run the command, with the given arguments. The
scriptargument 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
- Don’t raise an exception if anything is printed to stderr
- Input to the script
- The working directory to run in (default
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)¶
Write a file to the given path. If
contentis given then that text is written, otherwise the file in
frompathis relative to
Objects that are returned¶
These objects are returned when you use
ProcResult object is returned, and it has
.files_deleted which are dictionaries of
FoundFile and FoundDir. The files in
the pre-deletion state of the file; the other files represent the
state of the files after the command is run.
.files_deleted. These objects dictionary
ProcResult(test_env, args, stdin, stdout, stderr, returncode, files_before, files_after)¶
Represents the results of running a command in TestFileEnvironment.
Attributes to pay particular attention to:
Represents a single file found as the result of a command.
- The path of the file, relative to the
- The full path
- The contents of the file.
- The results of
.st_sizeof the stat.
- The modification time of the file.
- The size (in bytes) of the file.
You may use the
inoperator with these objects (tested against the contents of the file), and the
Represents a directory created by a command.