1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
# Copyright (c) 2015-2019, Activision Publishing, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os
import sys
if sys.version_info[0] == 3:
str_types = (str,)
else:
str_types = (basestring,)
__tracebackhide__ = True
def contents_of(file, encoding='utf-8'):
"""Helper to read the contents of the given file or path into a string with the given encoding.
Args:
file: a *path-like object* (aka a file name) or a *file-like object* (aka a file)
encoding (str): the target encoding. Defaults to ``utf-8``, other useful encodings are ``ascii`` and ``latin-1``.
Examples:
Usage::
from assertpy import assert_that, contents_of
contents = contents_of('foo.txt')
assert_that(contents).starts_with('foo').ends_with('bar').contains('oob')
Returns:
str: returns the file contents as a string
Raises:
IOError: if file not found
TypeError: if file is not a *path-like object* or a *file-like object*
"""
try:
contents = file.read()
except AttributeError:
try:
with open(file, 'r') as fp:
contents = fp.read()
except TypeError:
raise ValueError('val must be file or path, but was type <%s>' % type(file).__name__)
except OSError:
if not isinstance(file, str_types):
raise ValueError('val must be file or path, but was type <%s>' % type(file).__name__)
raise
if sys.version_info[0] == 3 and type(contents) is bytes:
# in PY3 force decoding of bytes to target encoding
return contents.decode(encoding, 'replace')
elif sys.version_info[0] == 2 and encoding == 'ascii':
# in PY2 force encoding back to ascii
return contents.encode('ascii', 'replace')
else:
# in all other cases, try to decode to target encoding
try:
return contents.decode(encoding, 'replace')
except AttributeError:
pass
# if all else fails, just return the contents "as is"
return contents
class FileMixin(object):
"""File assertions mixin."""
def exists(self):
"""Asserts that val is a path and that it exists.
Examples:
Usage::
assert_that('myfile.txt').exists()
assert_that('mydir').exists()
Returns:
AssertionBuilder: returns this instance to chain to the next assertion
Raises:
AssertionError: if val does **not** exist
"""
if not isinstance(self.val, str_types):
raise TypeError('val is not a path')
if not os.path.exists(self.val):
self.error('Expected <%s> to exist, but was not found.' % self.val)
return self
def does_not_exist(self):
"""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:
AssertionBuilder: returns this instance to chain to the next assertion
Raises:
AssertionError: if val **does** exist
"""
if not isinstance(self.val, str_types):
raise TypeError('val is not a path')
if os.path.exists(self.val):
self.error('Expected <%s> to not exist, but was found.' % self.val)
return self
def is_file(self):
"""Asserts that val is a *file* and that it exists.
Examples:
Usage::
assert_that('myfile.txt').is_file()
Returns:
AssertionBuilder: returns this instance to chain to the next assertion
Raises:
AssertionError: if val does **not** exist, or is **not** a file
"""
self.exists()
if not os.path.isfile(self.val):
self.error('Expected <%s> to be a file, but was not.' % self.val)
return self
def is_directory(self):
"""Asserts that val is a *directory* and that it exists.
Examples:
Usage::
assert_that('mydir').is_directory()
Returns:
AssertionBuilder: returns this instance to chain to the next assertion
Raises:
AssertionError: if val does **not** exist, or is **not** a directory
"""
self.exists()
if not os.path.isdir(self.val):
self.error('Expected <%s> to be a directory, but was not.' % self.val)
return self
def is_named(self, filename):
"""Asserts that val is an existing path to a file and that file is named filename.
Args:
filename: the expected filename
Examples:
Usage::
assert_that('/path/to/mydir/myfile.txt').is_named('myfile.txt')
Returns:
AssertionBuilder: returns this instance to chain to the next assertion
Raises:
AssertionError: if val does **not** exist, or is **not** a file, or is **not** named the given filename
"""
self.is_file()
if not isinstance(filename, str_types):
raise TypeError('given filename arg must be a path')
val_filename = os.path.basename(os.path.abspath(self.val))
if val_filename != filename:
self.error('Expected filename <%s> to be equal to <%s>, but was not.' % (val_filename, filename))
return self
def is_child_of(self, parent):
"""Asserts that val is an existing path to a file and that file is a child of parent.
Args:
parent: the expected parent directory
Examples:
Usage::
assert_that('/path/to/mydir/myfile.txt').is_child_of('mydir')
assert_that('/path/to/mydir/myfile.txt').is_child_of('to')
assert_that('/path/to/mydir/myfile.txt').is_child_of('path')
Returns:
AssertionBuilder: returns this instance to chain to the next assertion
Raises:
AssertionError: if val does **not** exist, or is **not** a file, or is **not** a child of the given directory
"""
self.is_file()
if not isinstance(parent, str_types):
raise TypeError('given parent directory arg must be a path')
val_abspath = os.path.abspath(self.val)
parent_abspath = os.path.abspath(parent)
if not val_abspath.startswith(parent_abspath):
self.error('Expected file <%s> to be a child of <%s>, but was not.' % (val_abspath, parent_abspath))
return self
|