Commit 5f076969 authored by R David Murray's avatar R David Murray

#18206: Fix test for existence of license URL.

It now always checks, instead of only when the LICENSE file doesn't exist.  It
is also protected by the 'network' resource, and uses a HEAD request since we
are only doing an existence check.
parent 0037b04f
...@@ -13,6 +13,8 @@ import os ...@@ -13,6 +13,8 @@ import os
import sys import sys
import re import re
import encodings import encodings
import urllib.request
import urllib.error
import subprocess import subprocess
import sysconfig import sysconfig
from copy import copy from copy import copy
...@@ -407,30 +409,20 @@ class ImportSideEffectTests(unittest.TestCase): ...@@ -407,30 +409,20 @@ class ImportSideEffectTests(unittest.TestCase):
else: else:
self.fail("sitecustomize not imported automatically") self.fail("sitecustomize not imported automatically")
@test.support.requires_resource('network')
class LicenseURL(unittest.TestCase): def test_license_exists_at_url(self):
"""Test accessibility of the license.""" # This test is a bit fragile since it depends on the format of the
# string displayed by license in the absence of a LICENSE file.
@unittest.skipUnless(str(license).startswith('See http://'), url = license._Printer__data.split()[1]
'license is available as a file') req = urllib.request.Request(url, method='HEAD')
def test_license_page(self):
"""urlopen should return the license page"""
pat = r'^See (http://www\.python\.org/download/releases/[^/]+/license/)$'
mo = re.search(pat, str(license))
self.assertIsNotNone(mo, msg='can\'t find appropriate url in license')
if mo is not None:
url = mo.group(1)
with test.support.transient_internet(url):
import urllib.request, urllib.error
try: try:
with urllib.request.urlopen(url) as data: with test.support.transient_internet(url):
with urllib.request.urlopen(req) as data:
code = data.getcode() code = data.getcode()
except urllib.error.HTTPError as e: except urllib.error.HTTPError as e:
code = e.code code = e.code
self.assertEqual(code, 200, msg=url) self.assertEqual(code, 200, msg="Can't find " + url)
def test_main():
run_unittest(HelperFunctionsTests, ImportSideEffectTests, LicenseURL)
if __name__ == "__main__": if __name__ == "__main__":
test_main() unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment