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 plustest-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 thebase_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
-
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
: (defaultexpect_error
)- Don’t raise an exception if anything is printed to stderr
stdin
: (default""
)- Input to the script
cwd
: (defaultself.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.
-
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:
-
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
. Alsomtime
andsize
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.