236
284
assert os.path.sep not in name, (
237
285
'traversed name contains os.path.sep: %s' % name)
238
286
assert name != '..', 'traversing to ..'
239
self.names.append(name)
288
self.names.append(name)
242
291
def browserDefault(self, request):
295
def module_name(self):
296
return '.'.join(self.names)
298
def get_fixtures(self):
300
self.module_name, globals(), locals(), ['_fixtures_'], 0)
301
return module._fixtures_
303
def renderINDEX(self):
304
root = os.path.join(config.root, 'lib')
306
for path in find_tests(root):
307
test_path = '/+yuitest/' + '/'.join(path)
308
module_name = '.'.join(path)
311
module_name, globals(), locals(), ['test_suite'], 0)
313
warning = 'cannot import Python fixture file'
316
suite_factory = module.test_suite
317
except AttributeError:
318
warning = 'cannot find test_suite'
321
suite = suite_factory()
322
except EXPLOSIVE_ERRORS:
325
warning = 'test_suite raises errors'
329
if isinstance(case, YUIAppServerTestCase):
330
root_url = config.appserver_root_url(
332
if root_url != 'None':
333
test_path = root_url + test_path
338
'test suite is not instance of '
339
'YUIAppServerTestCase')
340
link = '<a href="%s">%s</a>' % (test_path, test_path)
342
warning = ' <span class="warning">%s</span>' % warning
343
test_lines.append('<li>%s%s</li>' % (link, warning))
344
return self.index_template % {
346
'tests': '\n'.join(test_lines)}
348
def renderJAVASCRIPT(self):
349
self.request.response.setHeader('Content-Type', 'text/javascript')
350
self.request.response.setHeader('Cache-Control', 'no-cache')
352
os.path.join(config.root, 'lib', self.traversed_path))
354
def renderHTML(self):
355
self.request.response.setHeader('Content-Type', 'text/html')
356
self.request.response.setHeader('Cache-Control', 'no-cache')
357
if ('INTERACTIVE_TESTS' in os.environ and
358
'reload' in self.request.form):
359
# We should try to reload the module.
360
module = sys.modules.get(self.module_name)
361
if module is not None:
362
del module._fixtures_
246
364
return self.page_template % dict(
247
365
test_module='/+yuitest/%s.js' % self.traversed_path,
250
def get_fixtures(self):
251
module_name = '.'.join(self.names)
252
test_module = __import__(
253
module_name, globals(), locals(), ['_fixtures_'], 0)
254
return test_module._fixtures_
368
def renderSETUP(self):
370
fixtures = self.get_fixtures()
372
for fixture_name in self.fixtures:
373
__traceback_info__ = (fixture_name, data)
374
fixtures[fixture_name](self.request, data)
375
except EXPLOSIVE_ERRORS:
378
self.request.response.setStatus(500)
379
result = ''.join(format_exception(*sys.exc_info()))
381
self.request.response.setHeader(
382
'Content-Type', 'application/json')
383
# We use the ProxyFactory so that the restful
384
# redaction code is always used.
385
result = simplejson.dumps(
386
ProxyFactory(data), cls=ResourceJSONEncoder)
389
def renderTEARDOWN(self):
390
data = simplejson.loads(self.request.form['data'])
391
fixtures = self.get_fixtures()
393
for fixture_name in reversed(self.fixtures):
394
__traceback_info__ = (fixture_name, data)
395
fixtures[fixture_name].teardown(self.request, data)
396
except EXPLOSIVE_ERRORS:
399
self.request.response.setStatus(500)
400
result = ''.join(format_exception(*sys.exc_info()))
402
# Remove the session cookie, in case we have one.
403
self.request.response.expireCookie(
404
getUtility(IClientIdManager).namespace)
405
# Blow up the database once we are out of this transaction
406
# by passing a result that will do so when it is iterated
407
# through in asyncore.
408
self.request.response.setHeader('Content-Length', 1)
409
result = CloseDbResult()
256
412
def render(self):
257
if self.action == self.JAVASCRIPT:
258
self.request.response.setHeader('Content-Type', 'text/javascript')
260
os.path.join(config.root, 'lib', self.traversed_path))
261
elif self.action == self.HTML:
262
self.request.response.setHeader('Content-Type', 'text/html')
264
elif self.action == self.SETUP:
266
fixtures = self.get_fixtures()
268
for fixture_name in self.fixtures:
269
__traceback_info__ = (fixture_name, data)
270
fixtures[fixture_name](self.request, data)
271
except EXPLOSIVE_ERRORS:
274
self.request.response.setStatus(500)
275
result = ''.join(format_exception(*sys.exc_info()))
277
self.request.response.setHeader(
278
'Content-Type', 'application/json')
279
# We use the ProxyFactory so that the restful
280
# redaction code is always used.
281
result = simplejson.dumps(
282
ProxyFactory(data), cls=ResourceJSONEncoder)
283
elif self.action == self.TEARDOWN:
284
data = simplejson.loads(self.request.form['data'])
285
fixtures = self.get_fixtures()
287
for fixture_name in reversed(self.fixtures):
288
__traceback_info__ = (fixture_name, data)
289
fixtures[fixture_name].teardown(self.request, data)
290
except EXPLOSIVE_ERRORS:
293
self.request.response.setStatus(500)
294
result = ''.join(format_exception(*sys.exc_info()))
296
# Remove the session cookie, in case we have one.
297
self.request.response.expireCookie(
298
getUtility(IClientIdManager).namespace)
299
# Blow up the database once we are out of this transaction
300
# by passing a result that will do so when it is iterated
301
# through in asyncore.
302
self.request.response.setHeader('Content-Length', 1)
303
result = CloseDbResult()
413
return getattr(self, 'render' + self.action)()
416
def find_tests(root):
417
for dirpath, dirnames, filenames in os.walk(root):
418
dirpath = os.path.relpath(dirpath, root)
419
for filename in filenames:
420
if fnmatchcase(filename, 'test_*.js'):
421
name, ext = os.path.splitext(filename)
422
if name + '.py' in filenames:
423
names = dirpath.split(os.path.sep)
307
428
# This class cannot be imported directly into a test suite because
313
434
layer = YUIAppServerLayer
314
435
_testMethodName = 'runTest'
316
def __init__(self, module_name=None):
437
def __init__(self, module_name, facet='mainsite'):
317
438
self.module_name = module_name
318
440
# This needs to be done early so the "id" is set correctly.
319
441
self.test_path = self.module_name.replace('.', '/')
320
442
super(YUIAppServerTestCase, self).__init__()
323
root_url = LayerProcessController.appserver_root_url()
324
self.html_uri = '%s+yuitest/%s' % (root_url, self.test_path)
445
config = LayerProcessController.appserver_config
446
root_url = config.appserver_root_url(self.facet)
447
self.html_uri = '%s/+yuitest/%s' % (root_url, self.test_path)
325
448
super(YUIAppServerTestCase, self).setUp()
327
450
runTest = AbstractYUITestCase.checkResults
330
def make_suite(module_name):
331
return unittest.TestSuite([YUIAppServerTestCase(module_name)])
453
def make_suite(module_name, facet='mainsite'):
454
return unittest.TestSuite([YUIAppServerTestCase(module_name, facet)])