File & path assertions¶
Assertions for filesystem paths and file contents, plus the contents_of helper for reading a file
into a string.
File assertions mixin.
exists ¶
Asserts that val is a path and that it exists.
Examples:
Usage:
assert_that('myfile.txt').exists()
assert_that('mydir').exists()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not exist |
Source code in assertpy2/file.py
does_not_exist ¶
Asserts that val is a path and that it does not exist.
Examples:
Usage:
assert_that('missing.txt').does_not_exist()
assert_that('missing_dir').does_not_exist()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does exist |
Source code in assertpy2/file.py
is_file ¶
Asserts that val is a file and that it exists.
Examples:
Usage:
assert_that('myfile.txt').is_file()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not exist, or is not a file |
Source code in assertpy2/file.py
is_directory ¶
Asserts that val is a directory and that it exists.
Examples:
Usage:
assert_that('mydir').is_directory()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not exist, or is not a directory |
Source code in assertpy2/file.py
is_named ¶
Asserts that val is an existing path to a file and that file is named filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
the expected filename |
required |
Examples:
Usage:
assert_that('/path/to/mydir/myfile.txt').is_named('myfile.txt')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not exist, or is not a file, or is not named the given filename |
Source code in assertpy2/file.py
is_child_of ¶
Asserts that val is an existing path to a file and that file is a child of parent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
object
|
the expected parent directory |
required |
Examples:
Usage:
assert_that('/path/to/mydir/myfile.txt').is_child_of('/path/to/mydir')
assert_that('/path/to/mydir/myfile.txt').is_child_of('/path/to')
assert_that('/path/to/mydir/myfile.txt').is_child_of('/path')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not exist, is not a file, or is not a child of given directory |
Source code in assertpy2/file.py
is_readable ¶
Asserts that val is an existing path and is readable.
Examples:
Usage:
assert_that('/path/to/file.txt').is_readable()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not exist, or is not readable |
Source code in assertpy2/file.py
is_writable ¶
Asserts that val is an existing path and is writable.
Examples:
Usage:
assert_that('/path/to/file.txt').is_writable()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not exist, or is not writable |
Source code in assertpy2/file.py
is_executable ¶
Asserts that val is an existing path and is executable.
Examples:
Usage:
assert_that('/path/to/script.sh').is_executable()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not exist, or is not executable |
Source code in assertpy2/file.py
contents_of ¶
Helper to read the contents of the given file or path into a string with the given encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str | PathLike | IO
|
a path-like object (aka a file name) or a file-like object (aka a file) |
required |
encoding
|
str
|
the target encoding. Defaults to |
'utf-8'
|
Examples:
Usage:
from assertpy2 import assert_that, contents_of
contents = contents_of('foo.txt')
assert_that(contents).starts_with('foo').ends_with('bar').contains('oob')
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
returns the file contents as a string |
Raises:
| Type | Description |
|---|---|
IOError
|
if file not found |
TypeError
|
if file is not a path-like object or a file-like object |