Skip to content

Commit 3053bef

Browse files
committed
Resurrect URL coverage making it work with all tests, and move it to the test runner together with the template coverage report
- Legacy-Id: 6844
1 parent 56f3601 commit 3053bef

2 files changed

Lines changed: 92 additions & 94 deletions

File tree

ietf/redirects/tests.py

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -101,98 +101,9 @@ def test_redirects(self):
101101
location = location[17:]
102102
self.assertEqual(location, dst, (src, dst, location))
103103

104-
def get_patterns(module):
105-
all = []
106-
try:
107-
patterns = module.urlpatterns
108-
except AttributeError:
109-
patterns = []
110-
for item in patterns:
111-
try:
112-
subpatterns = get_patterns(item.urlconf_module)
113-
except:
114-
subpatterns = [""]
115-
for sub in subpatterns:
116-
if not sub:
117-
all.append(item.regex.pattern)
118-
elif sub.startswith("^"):
119-
all.append(item.regex.pattern + sub[1:])
120-
else:
121-
all.append(item.regex.pattern + ".*" + sub)
122-
return all
123-
124-
class UrlCoverageTestCase(unittest.TestCase):
125-
def testUrlCoverage(self):
126-
print " Testing testurl.list coverage"
127-
testtuples = []
128-
for root, dirs, files in os.walk(settings.BASE_DIR):
129-
if "testurl.list" in files:
130-
testtuples += read_testurls(root+"/testurl.list")
131-
132-
patterns = get_patterns(ietf.urls)
133-
covered = []
134-
for codes, testurl, goodurl in testtuples:
135-
for pattern in patterns:
136-
if re.match(pattern, testurl[1:]):
137-
covered.append(pattern)
138-
139-
if not set(patterns) == set(covered):
140-
missing = list(set(patterns) - set(covered))
141-
print "The following URLs are not tested by any testurl.list"
142-
for pattern in missing:
143-
if not pattern[1:].split("/")[0] in [ "admin", "accounts" ]:
144-
print " NoTest", pattern
145-
print ""
146-
else:
147-
print "All URLs are included in some testurl.list"
148-
149104
class MainUrlTests(TestCase):
150105
def test_urls(self):
151106
self.assertEqual(self.client.get("/_doesnotexist/").status_code, 404)
152107
self.assertEqual(self.client.get("/sitemap.xml").status_code, 200)
153108
# Google webmaster tool verification page
154109
self.assertEqual(self.client.get("/googlea30ad1dacffb5e5b.html").status_code, 200)
155-
156-
157-
def get_templates():
158-
templates = set()
159-
# Shoud we teach this to use TEMPLATE_DIRS?
160-
templatepath = os.path.join(settings.BASE_DIR,"templates")
161-
for root, dirs, files in os.walk(templatepath):
162-
if ".svn" in dirs:
163-
dirs.remove(".svn")
164-
relative_path = root[len(templatepath)+1:]
165-
for file in files:
166-
if file.endswith("~") or file.startswith("#"):
167-
continue
168-
if relative_path == "":
169-
templates.add(file)
170-
else:
171-
templates.add(os.path.join(relative_path, file))
172-
return templates
173-
174-
class TemplateCoverageTestCase(unittest.TestCase):
175-
def testTemplateCoverage(self):
176-
if not test_runner.loaded_templates:
177-
print " Skipping template coverage test"
178-
return
179-
180-
print " Testing template coverage"
181-
all_templates = get_templates()
182-
183-
#notexist = list(test_runner.loaded_templates - all_templates)
184-
#if notexist:
185-
# notexist.sort()
186-
# print "The following templates do not exist"
187-
# for x in notexist:
188-
# print "NotExist", x
189-
190-
notloaded = list(all_templates - test_runner.loaded_templates)
191-
if notloaded:
192-
notloaded.sort()
193-
print "The following templates were never loaded during test"
194-
for x in notloaded:
195-
print " NotLoaded", x
196-
else:
197-
print " All templates were loaded during test"
198-

ietf/utils/test_runner.py

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3333
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3434

35-
import socket
35+
import socket, re, os
3636

3737
from django.conf import settings
3838
from django.template import TemplateDoesNotExist
@@ -44,6 +44,7 @@
4444
import ietf.utils.mail
4545

4646
loaded_templates = set()
47+
visited_urls = set()
4748
test_database_name = None
4849
old_destroy = None
4950
old_create = None
@@ -74,25 +75,111 @@ def template_coverage_loader(template_name, dirs):
7475

7576
template_coverage_loader.is_usable = True
7677

78+
class RecordUrlsMiddleware(object):
79+
def process_request(self, request):
80+
visited_urls.add(request.path)
81+
82+
def get_patterns(module):
83+
all = []
84+
try:
85+
patterns = module.urlpatterns
86+
except AttributeError:
87+
patterns = []
88+
for item in patterns:
89+
try:
90+
subpatterns = get_patterns(item.urlconf_module)
91+
except:
92+
subpatterns = [""]
93+
for sub in subpatterns:
94+
if not sub:
95+
all.append(item.regex.pattern)
96+
elif sub.startswith("^"):
97+
all.append(item.regex.pattern + sub[1:])
98+
else:
99+
all.append(item.regex.pattern + ".*" + sub)
100+
return all
101+
102+
def check_url_coverage():
103+
patterns = get_patterns(ietf.urls)
104+
105+
IGNORED_PATTERNS = ("admin",)
106+
107+
patterns = [(p, re.compile(p)) for p in patterns if p[1:].split("/")[0] not in IGNORED_PATTERNS]
108+
109+
covered = set()
110+
for url in visited_urls:
111+
for pattern, compiled in patterns:
112+
if pattern not in covered and compiled.match(url[1:]): # strip leading /
113+
covered.add(pattern)
114+
break
115+
116+
missing = list(set(p for p, compiled in patterns) - covered)
117+
118+
if missing:
119+
print "The following URL patterns were not tested"
120+
for pattern in sorted(missing):
121+
print " Not tested", pattern
122+
123+
def get_templates():
124+
templates = set()
125+
# Should we teach this to use TEMPLATE_DIRS?
126+
templatepath = os.path.join(settings.BASE_DIR, "templates")
127+
for root, dirs, files in os.walk(templatepath):
128+
if ".svn" in dirs:
129+
dirs.remove(".svn")
130+
relative_path = root[len(templatepath)+1:]
131+
for file in files:
132+
if file.endswith("~") or file.startswith("#"):
133+
continue
134+
if relative_path == "":
135+
templates.add(file)
136+
else:
137+
templates.add(os.path.join(relative_path, file))
138+
return templates
139+
140+
def check_template_coverage():
141+
all_templates = get_templates()
142+
143+
not_loaded = list(all_templates - loaded_templates)
144+
if not_loaded:
145+
print "The following templates were never loaded during test"
146+
for t in sorted(not_loaded):
147+
print " Not loaded", t
148+
77149
def run_tests_1(test_labels, *args, **kwargs):
78150
global old_destroy, old_create, test_database_name
79151
from django.db import connection
80152
old_create = connection.creation.__class__.create_test_db
81153
connection.creation.__class__.create_test_db = safe_create_1
82154
old_destroy = connection.creation.__class__.destroy_test_db
83155
connection.creation.__class__.destroy_test_db = safe_destroy_0_1
84-
if not test_labels:
156+
157+
check_coverage = not test_labels
158+
159+
if check_coverage:
85160
settings.TEMPLATE_LOADERS = ('ietf.utils.test_runner.template_coverage_loader',) + settings.TEMPLATE_LOADERS
86-
test_labels = [x.split(".")[-1] for x in settings.INSTALLED_APPS if x.startswith("ietf")] + ['redirects.TemplateCoverageTestCase',]
161+
settings.MIDDLEWARE_CLASSES = ('ietf.utils.test_runner.RecordUrlsMiddleware',) + settings.MIDDLEWARE_CLASSES
162+
163+
if not test_labels:
164+
test_labels = [x.split(".")[-1] for x in settings.INSTALLED_APPS if x.startswith("ietf")]
165+
87166
if settings.SITE_ID != 1:
88167
print " Changing SITE_ID to '1' during testing."
89168
settings.SITE_ID = 1
169+
90170
if settings.TEMPLATE_STRING_IF_INVALID != '':
91171
print " Changing TEMPLATE_STRING_IF_INVALID to '' during testing."
92172
settings.TEMPLATE_STRING_IF_INVALID = ''
173+
93174
assert(not settings.IDTRACKER_BASE_URL.endswith('/'))
94-
kwargs["verbosity"] = kwargs["verbosity"]
95-
return django_run_tests(test_labels, *args, **kwargs)
175+
176+
results = django_run_tests(test_labels, *args, **kwargs)
177+
178+
if check_coverage:
179+
check_url_coverage()
180+
check_template_coverage()
181+
182+
return results
96183

97184
def run_tests(*args, **kwargs):
98185
# Tests that involve switching back and forth between the real

0 commit comments

Comments
 (0)