better setup and teardown in test_web, thanks Paolo

This commit is contained in:
mdipierro
2019-05-05 20:53:20 -07:00
parent c59186fa64
commit fc256343a7
2 changed files with 93 additions and 115 deletions
+48 -56
View File
@@ -1,86 +1,76 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""Unit tests for rewrite.py routers option""" """Unit tests for rewrite.py routers option"""
import os import os
import unittest
import tempfile import tempfile
import logging import logging
import unittest
import shutil
from gluon.rewrite import load, filter_url, filter_err, get_effective_router, map_url_out from gluon.rewrite import (load, filter_url, filter_err,
get_effective_router, map_url_out)
from gluon.html import URL from gluon.html import URL
from gluon.fileutils import abspath
from gluon.settings import global_settings from gluon.settings import global_settings
from gluon.http import HTTP from gluon.http import HTTP
from gluon.storage import Storage from gluon.storage import Storage
from gluon._compat import to_bytes, PY2 from gluon._compat import to_bytes, PY2
logger = None
oldcwd = None
root = None
def norm_root(root): logger = None
return root.replace('/', os.sep)
old_root = root = None
def setUpModule(): def setUpModule():
def make_apptree(): def make_apptree(root):
"build a temporary applications tree" "build a temporary applications tree"
# applications/ # applications/
os.mkdir(abspath('applications')) os.mkdir(os.path.join(root, 'applications'))
# applications/app/
# applications/app/
for app in ('admin', 'examples', 'welcome'): for app in ('admin', 'examples', 'welcome'):
os.mkdir(abspath('applications', app)) os.mkdir(os.path.join(root, 'applications', app))
# applications/app/(controllers, static) # applications/app/(controllers, static)
for subdir in ('controllers', 'static'): for subdir in ('controllers', 'static'):
os.mkdir(abspath('applications', app, subdir)) os.mkdir(os.path.join(root, 'applications', app, subdir))
# applications/admin/controllers/*.py
# applications/admin/controllers/*.py base = os.path.join(root, 'applications', 'admin', 'controllers')
for ctr in ('appadmin', 'default', 'gae', 'mercurial', 'shell', 'wizard'): for ctr in ('appadmin', 'default', 'gae', 'mercurial', 'shell', 'wizard'):
open(abspath('applications', 'admin', open(os.path.join(base, '%s.py' % ctr), 'w').close()
'controllers', '%s.py' % ctr), 'w').close() # applications/examples/controllers/*.py
# applications/examples/controllers/*.py base = os.path.join(root, 'applications', 'examples', 'controllers')
for ctr in ('ajax_examples', 'appadmin', 'default', 'global', 'spreadsheet'): for ctr in ('ajax_examples', 'appadmin', 'default', 'global', 'spreadsheet'):
open(abspath('applications', 'examples', open(os.path.join(base, '%s.py' % ctr), 'w').close()
'controllers', '%s.py' % ctr), 'w').close() # applications/welcome/controllers/*.py
# applications/welcome/controllers/*.py # (include controller that collides with another app)
# (include controller that collides with another app) base = os.path.join(root, 'applications', 'welcome', 'controllers')
for ctr in ('appadmin', 'default', 'other', 'admin'): for ctr in ('appadmin', 'default', 'other', 'admin'):
open(abspath('applications', 'welcome', open(os.path.join(base, '%s.py' % ctr), 'w').close()
'controllers', '%s.py' % ctr), 'w').close() # create an app-specific routes.py for examples app
routes = os.path.join(root, 'applications', 'examples', 'routes.py')
# create an app-specific routes.py for examples app with open(routes, 'w') as r:
routes = open(abspath('applications', 'examples', 'routes.py'), 'w') r.write("routers=dict(examples=dict(default_function='exdef'))")
routes.write("routers=dict(examples=dict(default_function='exdef'))") # create language files for examples app
routes.close() base = os.path.join(root, 'applications', 'examples', 'static')
# create language files for examples app
for lang in ('en', 'it'): for lang in ('en', 'it'):
os.mkdir(abspath('applications', 'examples', 'static', lang)) os.mkdir(os.path.join(base, lang))
open(abspath('applications', 'examples', 'static', open(os.path.join(base, lang, 'file'), 'w').close()
lang, 'file'), 'w').close()
global oldcwd global old_root, root, logger
if oldcwd is None: # do this only once if old_root is None: # do this only once
oldcwd = os.getcwd() old_root = global_settings.applications_parent
if not os.path.isdir('gluon'): root = global_settings.applications_parent = tempfile.mkdtemp()
os.chdir(os.path.realpath( make_apptree(root)
'../../')) # run from web2py base directory
import gluon.main # for initialization after chdir
global logger
logger = logging.getLogger('web2py.rewrite') logger = logging.getLogger('web2py.rewrite')
global_settings.applications_parent = tempfile.mkdtemp()
global root
root = global_settings.applications_parent
make_apptree()
def tearDownModule(): def tearDownModule():
global oldcwd if old_root is not None:
if oldcwd is not None: global_settings.applications_parent = old_root
os.chdir(oldcwd) shutil.rmtree(root)
oldcwd = None
def norm_root(root):
return root.replace('/', os.sep)
class TestRouter(unittest.TestCase): class TestRouter(unittest.TestCase):
@@ -1037,12 +1027,14 @@ class TestRouter(unittest.TestCase):
'http://domain.com/app2/static/filename-with_underscore'), 'http://domain.com/app2/static/filename-with_underscore'),
norm_root("%s/applications/app2/static/filename-with_underscore" % root)) norm_root("%s/applications/app2/static/filename-with_underscore" % root))
from gluon.globals import current
current.response.static_version = None
self.assertEqual(str(URL(a='init', c='default', f='a_b')), "/a_b") self.assertEqual(str(URL(a='init', c='default', f='a_b')), "/a_b")
self.assertEqual(str(URL(a='app1', c='default', f='a_b')), "/app1/a-b") self.assertEqual(str(URL(a='app1', c='default', f='a_b')), "/app1/a-b")
self.assertEqual(str(URL(a='app2', c='default', f='a_b')), "/app2/a_b") self.assertEqual(str(URL(a='app2', c='default', f='a_b')), "/app2/a_b")
from gluon.globals import current
if hasattr(current, 'response'):
current.response.static_version = None
self.assertEqual( self.assertEqual(
str(URL(a='app1', c='static', f='a/b_c')), "/app1/static/a/b_c") str(URL(a='app1', c='static', f='a/b_c')), "/app1/static/a/b_c")
self.assertEqual( self.assertEqual(
+45 -59
View File
@@ -1,81 +1,67 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""Unit tests for rewrite.py regex routing option""" """Unit tests for rewrite.py regex routing option"""
import sys
import os import os
import unittest
import tempfile import tempfile
import logging import unittest
import shutil
from gluon.rewrite import load, filter_url, filter_err, get_effective_router, regex_filter_out, regex_select from gluon.rewrite import load, filter_url, filter_err, regex_filter_out
from gluon.html import URL from gluon.html import URL
from gluon.fileutils import abspath
from gluon.settings import global_settings from gluon.settings import global_settings
from gluon.http import HTTP from gluon.http import HTTP
from gluon.storage import Storage from gluon.storage import Storage
from gluon._compat import to_bytes from gluon._compat import to_bytes
logger = None
oldcwd = None old_root = root = None
root = None
def setUpModule():
def make_apptree(root):
"build a temporary applications tree"
# applications/
os.mkdir(os.path.join(root, 'applications'))
# applications/app/
for app in ('admin', 'examples', 'welcome'):
os.mkdir(os.path.join(root, 'applications', app))
# applications/app/(controllers, static)
for subdir in ('controllers', 'static'):
os.mkdir(os.path.join(root, 'applications', app, subdir))
# applications/admin/controllers/*.py
base = os.path.join(root, 'applications', 'admin', 'controllers')
for ctr in ('appadmin', 'default', 'gae', 'mercurial', 'shell', 'wizard'):
open(os.path.join(base, '%s.py' % ctr), 'w').close()
# applications/examples/controllers/*.py
base = os.path.join(root, 'applications', 'examples', 'controllers')
for ctr in ('ajax_examples', 'appadmin', 'default', 'global', 'spreadsheet'):
open(os.path.join(base, '%s.py' % ctr), 'w').close()
# applications/welcome/controllers/*.py
base = os.path.join(root, 'applications', 'welcome', 'controllers')
for ctr in ('appadmin', 'default'):
open(os.path.join(base, '%s.py' % ctr), 'w').close()
# create an app-specific routes.py for examples app
routes = os.path.join(root, 'applications', 'examples', 'routes.py')
with open(routes, 'w') as r:
r.write("default_function='exdef'\n")
global old_root, root
if old_root is None: # do this only once
old_root = global_settings.applications_parent
root = global_settings.applications_parent = tempfile.mkdtemp()
make_apptree(root)
def tearDownModule():
if old_root is not None:
global_settings.applications_parent = old_root
shutil.rmtree(root)
def norm_root(root): def norm_root(root):
return root.replace('/', os.sep) return root.replace('/', os.sep)
def setUpModule():
def make_apptree():
"build a temporary applications tree"
# applications/
os.mkdir(abspath('applications'))
# applications/app/
for app in ('admin', 'examples', 'welcome'):
os.mkdir(abspath('applications', app))
# applications/app/(controllers, static)
for subdir in ('controllers', 'static'):
os.mkdir(abspath('applications', app, subdir))
# applications/admin/controllers/*.py
for ctr in ('appadmin', 'default', 'gae', 'mercurial', 'shell', 'wizard'):
open(abspath('applications', 'admin',
'controllers', '%s.py' % ctr), 'w').close()
# applications/examples/controllers/*.py
for ctr in ('ajax_examples', 'appadmin', 'default', 'global', 'spreadsheet'):
open(abspath('applications', 'examples',
'controllers', '%s.py' % ctr), 'w').close()
# applications/welcome/controllers/*.py
for ctr in ('appadmin', 'default'):
open(abspath('applications', 'welcome',
'controllers', '%s.py' % ctr), 'w').close()
# create an app-specific routes.py for examples app
routes = open(abspath('applications', 'examples', 'routes.py'), 'w')
routes.write("default_function='exdef'\n")
routes.close()
global oldcwd
if oldcwd is None: # do this only once
oldcwd = os.getcwd()
if not os.path.isdir('gluon'):
os.chdir(os.path.realpath(
'../../')) # run from web2py base directory
from gluon import main # for initialization after chdir
global logger
logger = logging.getLogger('web2py.rewrite')
global_settings.applications_parent = tempfile.mkdtemp()
global root
root = global_settings.applications_parent
make_apptree()
def tearDownModule():
global oldcwd
if oldcwd is not None:
os.chdir(oldcwd)
oldcwd = None
class TestRoutes(unittest.TestCase): class TestRoutes(unittest.TestCase):
""" Tests the regex routing logic from gluon.rewrite """ """ Tests the regex routing logic from gluon.rewrite """