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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
# Copyright (C) 2013 Canonical Ltd.
# Author: Colin Watson <cjwatson@ubuntu.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This file contains code from Python 3.3, released under the Python license
# (http://docs.python.org/3/license.html).
"""Testing helpers."""
from __future__ import print_function
__metaclass__ = type
__all__ = [
'TestCase',
'mkfile',
'touch',
]
import contextlib
from functools import wraps
import json
import os
import re
import shutil
import sys
import tempfile
import unittest
try:
from unittest import mock
except ImportError:
import mock
import gi
gi.require_version('Click', '0.4')
from gi.repository import Click, GLib
from click_package.tests import gimock
def disable_logging(func):
"""Decorator to disable logging e.g. during a test"""
@wraps(func)
def wrapper(*args, **kwargs):
import logging
logging.disable(logging.CRITICAL)
try:
return func(*args, **kwargs)
finally:
logging.disable(logging.NOTSET)
return wrapper
def make_installed_click(db, db_dir, package="test-1", version="1.0",
json_data={}, make_current=True, user="@all"):
"""Create a fake installed click package for the given db/db_dir"""
json_data["name"] = package
json_data["version"] = version
with mkfile_utf8(os.path.join(
db_dir, package, version, ".click", "info",
"%s.manifest" % package)) as f:
json.dump(json_data, f, ensure_ascii=False)
if make_current:
os.symlink(
version, os.path.join(db_dir, package, "current"))
if user == "@all":
registry = Click.User.for_all_users(db)
else:
registry = Click.User.for_user(db, user)
registry.set_version(package, version)
return os.path.join(db_dir, package, version)
class TestCase(gimock.GIMockTestCase):
def setUp(self):
super(TestCase, self).setUp()
self.temp_dir = None
self.save_env = dict(os.environ)
self.maxDiff = None
def tearDown(self):
for key in set(os.environ) - set(self.save_env):
del os.environ[key]
for key, value in os.environ.items():
if value != self.save_env[key]:
os.environ[key] = self.save_env[key]
for key in set(self.save_env) - set(os.environ):
os.environ[key] = self.save_env[key]
def use_temp_dir(self):
if self.temp_dir is not None:
return self.temp_dir
self.temp_dir = tempfile.mkdtemp(prefix="click")
if os.geteuid() == 0:
os.chown(self.temp_dir, 1, 1)
self.assertTrue(os.path.exists(self.temp_dir))
self.addCleanup(shutil.rmtree, self.temp_dir)
return self.temp_dir
# Monkey-patch for Python 2/3 compatibility.
if not hasattr(unittest.TestCase, 'assertCountEqual'):
assertCountEqual = unittest.TestCase.assertItemsEqual
if not hasattr(unittest.TestCase, 'assertRegex'):
assertRegex = unittest.TestCase.assertRegexpMatches
# Renamed in Python 3.2 to omit the trailing 'p'.
if not hasattr(unittest.TestCase, 'assertRaisesRegex'):
assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
def assertRaisesGError(self, domain_name, code, callableObj,
*args, **kwargs):
with self.assertRaises(GLib.GError) as cm:
callableObj(*args, **kwargs)
self.assertEqual(domain_name, cm.exception.domain)
self.assertEqual(code, cm.exception.code)
def assertRaisesFileError(self, code, callableObj, *args, **kwargs):
self.assertRaisesGError(
"g-file-error-quark", code, callableObj, *args, **kwargs)
def assertRaisesDatabaseError(self, code, callableObj, *args, **kwargs):
self.assertRaisesGError(
"click-database-error-quark", code, callableObj, *args, **kwargs)
def assertRaisesFrameworkError(self, code, callableObj, *args, **kwargs):
self.assertRaisesGError(
"click-framework-error-quark", code, callableObj, *args, **kwargs)
def assertRaisesHooksError(self, code, callableObj, *args, **kwargs):
self.assertRaisesGError(
"click-hooks-error-quark", code, callableObj, *args, **kwargs)
def assertRaisesQueryError(self, code, callableObj, *args, **kwargs):
self.assertRaisesGError(
"click-query-error-quark", code, callableObj, *args, **kwargs)
def assertRaisesUserError(self, code, callableObj, *args, **kwargs):
self.assertRaisesGError(
"click-user-error-quark", code, callableObj, *args, **kwargs)
def _setup_frameworks(self, preloads, frameworks_dir=None, frameworks=[]):
frameworks_dir = self._create_mock_framework_dir(frameworks_dir)
shutil.rmtree(frameworks_dir, ignore_errors=True)
for framework in frameworks:
self._create_mock_framework_file(framework)
preloads["click_get_frameworks_dir"].side_effect = (
lambda: self.make_string(frameworks_dir))
def _create_mock_framework_dir(self, frameworks_dir=None):
if frameworks_dir is None:
frameworks_dir = os.path.join(self.temp_dir, "frameworks")
patcher = mock.patch('click_package.paths.frameworks_dir', frameworks_dir)
patcher.start()
self.addCleanup(patcher.stop)
Click.ensuredir(frameworks_dir)
return frameworks_dir
def _create_mock_framework_file(self, framework_name):
self.use_temp_dir()
self._create_mock_framework_dir()
r = r'(?P<name>[a-z]+-sdk)-(?P<ver>[0-9.]+)(-[a-z0-9-]+)?'
match = re.match(r, framework_name)
if match is None:
name = "unknown"
ver = "1.0"
else:
name = match.group("name")
ver = match.group("ver")
framework_filename = os.path.join(
self.temp_dir, "frameworks",
"{0}.framework".format(framework_name))
with open(framework_filename, "w") as f:
f.write("Base-Name: {0}\n".format(name))
f.write("Base-Version: {0}\n".format(ver))
if not hasattr(mock, "call"):
# mock 0.7.2, the version in Ubuntu 12.04 LTS, lacks mock.ANY and
# mock.call. Since it's so convenient, monkey-patch a partial backport
# (from Python 3.3 unittest.mock) into place here.
class _ANY(object):
"A helper object that compares equal to everything."
def __eq__(self, other):
return True
def __ne__(self, other):
return False
def __repr__(self):
return '<ANY>'
mock.ANY = _ANY()
class _Call(tuple):
"""
A tuple for holding the results of a call to a mock, either in the form
`(args, kwargs)` or `(name, args, kwargs)`.
If args or kwargs are empty then a call tuple will compare equal to
a tuple without those values. This makes comparisons less verbose::
_Call(('name', (), {})) == ('name',)
_Call(('name', (1,), {})) == ('name', (1,))
_Call(((), {'a': 'b'})) == ({'a': 'b'},)
The `_Call` object provides a useful shortcut for comparing with call::
_Call(((1, 2), {'a': 3})) == call(1, 2, a=3)
_Call(('foo', (1, 2), {'a': 3})) == call.foo(1, 2, a=3)
If the _Call has no name then it will match any name.
"""
def __new__(cls, value=(), name=None, parent=None, two=False,
from_kall=True):
name = ''
args = ()
kwargs = {}
_len = len(value)
if _len == 3:
name, args, kwargs = value
elif _len == 2:
first, second = value
if isinstance(first, str):
name = first
if isinstance(second, tuple):
args = second
else:
kwargs = second
else:
args, kwargs = first, second
elif _len == 1:
value, = value
if isinstance(value, str):
name = value
elif isinstance(value, tuple):
args = value
else:
kwargs = value
if two:
return tuple.__new__(cls, (args, kwargs))
return tuple.__new__(cls, (name, args, kwargs))
def __init__(self, value=(), name=None, parent=None, two=False,
from_kall=True):
self.name = name
self.parent = parent
self.from_kall = from_kall
def __eq__(self, other):
if other is mock.ANY:
return True
try:
len_other = len(other)
except TypeError:
return False
self_name = ''
if len(self) == 2:
self_args, self_kwargs = self
else:
self_name, self_args, self_kwargs = self
other_name = ''
if len_other == 0:
other_args, other_kwargs = (), {}
elif len_other == 3:
other_name, other_args, other_kwargs = other
elif len_other == 1:
value, = other
if isinstance(value, tuple):
other_args = value
other_kwargs = {}
elif isinstance(value, str):
other_name = value
other_args, other_kwargs = (), {}
else:
other_args = ()
other_kwargs = value
else:
# len 2
# could be (name, args) or (name, kwargs) or (args, kwargs)
first, second = other
if isinstance(first, str):
other_name = first
if isinstance(second, tuple):
other_args, other_kwargs = second, {}
else:
other_args, other_kwargs = (), second
else:
other_args, other_kwargs = first, second
if self_name and other_name != self_name:
return False
# this order is important for ANY to work!
return (other_args, other_kwargs) == (self_args, self_kwargs)
def __ne__(self, other):
return not self.__eq__(other)
def __call__(self, *args, **kwargs):
if self.name is None:
return _Call(('', args, kwargs), name='()')
name = self.name + '()'
return _Call((self.name, args, kwargs), name=name, parent=self)
def __getattr__(self, attr):
if self.name is None:
return _Call(name=attr, from_kall=False)
name = '%s.%s' % (self.name, attr)
return _Call(name=name, parent=self, from_kall=False)
mock.call = _Call(from_kall=False)
@contextlib.contextmanager
def mkfile(path, mode="w"):
Click.ensuredir(os.path.dirname(path))
with open(path, mode) as f:
yield f
@contextlib.contextmanager
def mkfile_utf8(path, mode="w"):
Click.ensuredir(os.path.dirname(path))
if sys.version < "3":
import codecs
with codecs.open(path, mode, "UTF-8") as f:
yield f
else:
# io.open is available from Python 2.6, but we only use it with
# Python 3 because it raises exceptions when passed bytes.
import io
with io.open(path, mode, encoding="UTF-8") as f:
yield f
def touch(path):
with mkfile(path, mode="a"):
pass
def make_file_with_content(filename, content, mode=0o644):
"""Create a file with the given content and mode"""
Click.ensuredir(os.path.dirname(filename))
with open(filename, "w") as f:
f.write(content)
os.chmod(filename, mode)
|