Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions assertpy/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import os
import sys
import pathlib

if sys.version_info[0] == 3:
str_types = (str,)
Expand Down Expand Up @@ -106,7 +107,7 @@ def exists(self):
Raises:
AssertionError: if val does **not** exist
"""
if not isinstance(self.val, str_types):
if not isinstance(self.val, str_types) and not isinstance(self.val, pathlib.PosixPath):
raise TypeError('val is not a path')
if not os.path.exists(self.val):
return self.error('Expected <%s> to exist, but was not found.' % self.val)
Expand All @@ -127,7 +128,7 @@ def does_not_exist(self):
Raises:
AssertionError: if val **does** exist
"""
if not isinstance(self.val, str_types):
if not isinstance(self.val, str_types) and not isinstance(self.val, pathlib.PosixPath):
raise TypeError('val is not a path')
if os.path.exists(self.val):
return self.error('Expected <%s> to not exist, but was found.' % self.val)
Expand Down Expand Up @@ -223,3 +224,60 @@ def is_child_of(self, parent):
if not val_abspath.startswith(parent_abspath):
return self.error('Expected file <%s> to be a child of <%s>, but was not.' % (val_abspath, parent_abspath))
return self

def is_readable(self):
"""Asserts that val is readable.

Examples:
Usage::

assert_that('/path/to/mydir/myfile.txt').is_readable()

Returns:
AssertionBuilder: returns this instance to chain to the next assertion

Raises:
AssertionError: if val is **not** readable
"""
self.exists()
if not os.access(self.val, os.R_OK):
return self.error('Expected <%s> to be readable, but was not.' % self.val)
return self

def is_writable(self):
"""Asserts that val is writable.

Examples:
Usage::

assert_that('/path/to/mydir/myfile.txt').is_writable()

Returns:
AssertionBuilder: returns this instance to chain to the next assertion

Raises:
AssertionError: if val is **not** writable
"""
self.exists()
if not os.access(self.val, os.W_OK):
return self.error('Expected <%s> to be writable, but was not.' % self.val)
return self

def is_executable(self):
"""Asserts that val is executable.

Examples:
Usage::

assert_that('/path/to/mydir/myfile.txt').is_executable()

Returns:
AssertionBuilder: returns this instance to chain to the next assertion

Raises:
AssertionError: if val is **not** executable
"""
self.exists()
if not os.access(self.val, os.X_OK):
return self.error('Expected <%s> to be executable, but was not.' % self.val)
return self