/usr/lib/python2.7/dist-packages/webassets/test.py is in python-webassets 3:0.10.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """Helpers for testing webassets.
This is included in the webassets package because it is useful for testing
external libraries that use webassets (like the flask-assets wrapper).
"""
from __future__ import print_function
import tempfile
import shutil
import os
from os import path
import time
from webassets import Environment, Bundle
from webassets.six.moves import map
from webassets.six.moves import zip
__all__ = ('TempDirHelper', 'TempEnvironmentHelper',)
class TempDirHelper(object):
"""Base-class for tests which provides a temporary directory
(which is properly deleted after the test is done), and various
helper methods to do filesystem operations within that directory.
"""
default_files = {}
def setup(self):
self._tempdir_created = tempfile.mkdtemp()
self.create_files(self.default_files)
def teardown(self):
shutil.rmtree(self._tempdir_created)
def __enter__(self):
self.setup()
return self
def __exit__(self, type, value, traceback):
self.teardown()
@property
def tempdir(self):
# Use a read-only property here, so the user is
# less likely to modify the attribute, and have
# his data deleted on teardown.
return self._tempdir_created
def create_files(self, files):
"""Helper that allows to quickly create a bunch of files in
the media directory of the current test run.
"""
import codecs
# Allow passing a list of filenames to create empty files
if not hasattr(files, 'items'):
files = dict(map(lambda n: (n, ''), files))
for name, data in files.items():
dirs = path.dirname(self.path(name))
if not path.exists(dirs):
os.makedirs(dirs)
f = codecs.open(self.path(name), 'w', 'utf-8')
f.write(data)
f.close()
def create_directories(self, *dirs):
"""Helper to create directories within the media directory
of the current test's environment.
"""
result = []
for dir in dirs:
full_path = self.path(dir)
result.append(full_path)
os.makedirs(full_path)
return result
def exists(self, name):
"""Ensure the given file exists within the current test run's
media directory.
"""
return path.exists(self.path(name))
def get(self, name):
"""Return the given file's contents.
"""
with open(self.path(name)) as f:
r = f.read()
print(repr(r))
return r
def unlink(self, name):
os.unlink(self.path(name))
def path(self, name):
"""Return the given file's full path."""
return path.join(self._tempdir_created, name)
def setmtime(self, *files, **kwargs):
"""Set the mtime of the given files. Useful helper when
needing to test things like the timestamp updater.
Specify ``mtime`` as a keyword argument, or time.time()
will automatically be used. Returns the mtime used.
Specify ``mod`` as a keyword argument, and the modifier
will be added to the ``mtime`` used.
"""
mtime = kwargs.pop('mtime', time.time())
mtime += kwargs.pop('mod', 0)
assert not kwargs, "Unsupported kwargs: %s" % ', '.join(kwargs.keys())
for f in files:
os.utime(self.path(f), (mtime, mtime))
return mtime
def p(self, *files):
"""Print the contents of the given files to stdout; useful
for some quick debugging.
"""
if not files:
files = ['out'] # This is a often used output filename
for f in files:
content = self.get(f)
print(f)
print("-" * len(f))
print(repr(content))
print(content)
print()
class TempEnvironmentHelper(TempDirHelper):
"""Base-class for tests which provides a pre-created
environment, based in a temporary directory, and utility
methods to do filesystem operations within that directory.
"""
default_files = {'in1': 'A', 'in2': 'B', 'in3': 'C', 'in4': 'D'}
def setup(self):
TempDirHelper.setup(self)
self.env = self._create_environment()
# Unless we explicitly test it, we don't want to use the cache
# during testing.
self.env.cache = False
self.env.manifest = False
def _create_environment(self):
return Environment(self._tempdir_created, '')
def mkbundle(self, *a, **kw):
b = Bundle(*a, **kw)
b.env = self.env
return b
|