| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
""" Tests for the resource manager. """ |
| 11 |
|
| 12 |
|
| 13 |
# Standard library imports. |
| 14 |
import unittest |
| 15 |
import urllib.request |
| 16 |
from io import StringIO |
| 17 |
from urllib.error import HTTPError |
| 18 |
|
| 19 |
try: |
| 20 |
from importlib.resources import as_file, files |
| 21 |
except ImportError: |
| 22 |
from importlib_resources import as_file, files |
| 23 |
|
| 24 |
|
| 25 |
# Enthought library imports. |
| 26 |
from envisage.resource.api import NoSuchResourceError, ResourceManager |
| 27 |
|
| 28 |
# Module to patch urlopen in during testing. |
| 29 |
url_library = urllib.request |
| 30 |
|
| 31 |
|
| 32 |
# This module's package. |
| 33 |
PKG = "envisage.resource.tests" |
| 34 |
|
| 35 |
|
| 36 |
# mimics `urlopen` for some tests. |
| 37 |
# In setUp it replaces `urlopen` for some tests, |
| 38 |
# and in tearDown, the regular `urlopen` is put back into place. |
| 39 |
def stubout_urlopen(url): |
| 40 |
if "bogus" in url: |
| 41 |
raise HTTPError(url, "404", "No such resource", "", None) |
| 42 |
|
| 43 |
elif "localhost" in url: |
| 44 |
return StringIO("This is a test file.\n") |
| 45 |
|
| 46 |
else: |
| 47 |
raise ValueError("Unexpected URL %r in stubout_urlopen" % url) |
| 48 |
|
| 49 |
|
| 50 |
class ResourceManagerTestCase(unittest.TestCase): |
| 51 |
"""Tests for the resource manager.""" |
| 52 |
|
| 53 |
########################################################################### |
| 54 |
# 'TestCase' interface. |
| 55 |
########################################################################### |
| 56 |
|
| 57 |
def setUp(self): |
| 58 |
"""Prepares the test fixture before each test method is called.""" |
| 59 |
|
| 60 |
self.stored_urlopen = url_library.urlopen |
| 61 |
url_library.urlopen = stubout_urlopen |
| 62 |
|
| 63 |
def tearDown(self): |
| 64 |
"""Called immediately after each test method has been called.""" |
| 65 |
|
| 66 |
url_library.urlopen = self.stored_urlopen |
| 67 |
|
| 68 |
########################################################################### |
| 69 |
# Tests. |
| 70 |
########################################################################### |
| 71 |
|
| 72 |
def test_file_resource(self): |
| 73 |
"""file resource""" |
| 74 |
|
| 75 |
rm = ResourceManager() |
| 76 |
|
| 77 |
# Get the filename of the 'api.py' file. |
| 78 |
resource = files("envisage.resource") / "api.py" |
| 79 |
with as_file(resource) as path: |
| 80 |
# Open a file resource. |
| 81 |
f = rm.file(f"file://{path}") |
| 82 |
self.assertNotEqual(f, None) |
| 83 |
contents = f.read() |
| 84 |
f.close() |
| 85 |
|
| 86 |
# Open the api file via the file system. |
| 87 |
with open(path, "rb") as g: |
| 88 |
self.assertEqual(g.read(), contents) |
| 89 |
|
| 90 |
def test_no_such_file_resource(self): |
| 91 |
"""no such file resource""" |
| 92 |
|
| 93 |
rm = ResourceManager() |
| 94 |
|
| 95 |
# Open a file resource. |
| 96 |
with self.assertRaises(NoSuchResourceError): |
| 97 |
rm.file("file://../bogus.py") |
| 98 |
|
| 99 |
def test_package_resource(self): |
| 100 |
"""package resource""" |
| 101 |
|
| 102 |
rm = ResourceManager() |
| 103 |
|
| 104 |
# Open a package resource. |
| 105 |
f = rm.file("pkgfile://envisage.resource/api.py") |
| 106 |
self.assertNotEqual(f, None) |
| 107 |
contents = f.read() |
| 108 |
f.close() |
| 109 |
|
| 110 |
# Get the bytes of the 'api.py' file. |
| 111 |
resource = files("envisage.resource") / "api.py" |
| 112 |
with resource.open("rb") as g: |
| 113 |
self.assertEqual(g.read(), contents) |
| 114 |
|
| 115 |
def test_package_resource_subdir(self): |
| 116 |
"""package resource""" |
| 117 |
|
| 118 |
rm = ResourceManager() |
| 119 |
|
| 120 |
# Open a package resource. |
| 121 |
f = rm.file("pkgfile://envisage.resource/tests/__init__.py") |
| 122 |
self.assertNotEqual(f, None) |
| 123 |
contents = f.read() |
| 124 |
f.close() |
| 125 |
|
| 126 |
# Get the bytes of the 'api.py' file. |
| 127 |
resource = files("envisage.resource") / "tests" / "__init__.py" |
| 128 |
with resource.open("rb") as g: |
| 129 |
self.assertEqual(g.read(), contents) |
| 130 |
|
| 131 |
def test_no_such_package_resource(self): |
| 132 |
"""no such package resource""" |
| 133 |
|
| 134 |
rm = ResourceManager() |
| 135 |
|
| 136 |
# Open a package resource. |
| 137 |
with self.assertRaises(NoSuchResourceError): |
| 138 |
rm.file("pkgfile://envisage.resource/") |
| 139 |
|
| 140 |
with self.assertRaises(NoSuchResourceError): |
| 141 |
rm.file("pkgfile:///envisage") |
| 142 |
|
| 143 |
with self.assertRaises(NoSuchResourceError): |
| 144 |
rm.file("pkgfile://envisage.resource/bogus.py") |
| 145 |
|
| 146 |
with self.assertRaises(NoSuchResourceError): |
| 147 |
rm.file("pkgfile://completely.bogus/bogus.py") |
| 148 |
|
| 149 |
with self.assertRaises(NoSuchResourceError): |
| 150 |
rm.file("pkgfile://envisage.resource.resource_manager/anything") |
| 151 |
|
| 152 |
def test_http_resource(self): |
| 153 |
"""http resource""" |
| 154 |
|
| 155 |
# Open an HTTP document resource. |
| 156 |
rm = ResourceManager() |
| 157 |
|
| 158 |
f = rm.file("http://localhost:1234/file.dat") |
| 159 |
self.assertNotEqual(f, None) |
| 160 |
contents = f.read() |
| 161 |
f.close() |
| 162 |
|
| 163 |
self.assertEqual(contents, "This is a test file.\n") |
| 164 |
|
| 165 |
def test_no_such_http_resource(self): |
| 166 |
"""no such http resource""" |
| 167 |
|
| 168 |
# Open an HTTP document resource. |
| 169 |
rm = ResourceManager() |
| 170 |
|
| 171 |
with self.assertRaises(NoSuchResourceError): |
| 172 |
rm.file("http://localhost:1234/bogus.dat") |
| 173 |
|
| 174 |
def test_unknown_protocol(self): |
| 175 |
"""unknown protocol""" |
| 176 |
|
| 177 |
# Open an HTTP document resource. |
| 178 |
rm = ResourceManager() |
| 179 |
|
| 180 |
with self.assertRaises(ValueError): |
| 181 |
rm.file("bogus://foo/bar/baz") |