diff options
Diffstat (limited to 'tests/helpers.py')
-rw-r--r-- | tests/helpers.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/helpers.py b/tests/helpers.py index d4dd33ea..c916c8a2 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -7,9 +7,11 @@ import collections import contextlib import glob import os +import os.path import re import subprocess import sys +import textwrap import mock @@ -51,6 +53,46 @@ def run_command(cmd): return status, output +def make_file(filename, text="", bytes=b"", newline=None): + """Create a file for testing. + + `filename` is the relative path to the file, including directories if + desired, which will be created if need be. + + `text` is the content to create in the file, a native string (bytes in + Python 2, unicode in Python 3), or `bytes` are the bytes to write. + + If `newline` is provided, it is a string that will be used as the line + endings in the created file, otherwise the line endings are as provided + in `text`. + + Returns `filename`. + + """ + # pylint: disable=redefined-builtin # bytes + if bytes: + data = bytes + else: + text = textwrap.dedent(text) + if newline: + text = text.replace("\n", newline) + if env.PY3: + data = text.encode('utf8') + else: + data = text + + # Make sure the directories are available. + dirs, _ = os.path.split(filename) + if dirs and not os.path.exists(dirs): + os.makedirs(dirs) + + # Create the file. + with open(filename, 'wb') as f: + f.write(data) + + return filename + + class CheckUniqueFilenames(object): """Asserts the uniqueness of file names passed to a function.""" def __init__(self, wrapped): |