Coverage for tests/test_resource.py: 12%
83 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-19 11:17 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-19 11:17 +0000
1# This file is part of lsst-resources.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12import re
13import unittest
15from lsst.resources import ResourcePath
16from lsst.resources.tests import GenericTestCase
19class ResourceTestCase(GenericTestCase, unittest.TestCase):
20 """Generic test of resource URIs."""
22 scheme = "resource"
23 netloc = "lsst.resources"
26class ResourceReadTestCase(unittest.TestCase):
27 """Test that resource information can be read.
29 Python package resources are read-only.
30 """
32 # No resources in this package so need a resource in the main
33 # python distribution.
34 scheme = "resource"
35 netloc = "idlelib"
37 def setUp(self):
38 self.root = f"{self.scheme}://{self.netloc}"
39 self.root_uri = ResourcePath(self.root)
41 def test_read(self):
42 uri = self.root_uri.join("Icons/README.txt")
43 self.assertTrue(uri.exists(), f"Check {uri} exists")
45 content = uri.read().decode()
46 self.assertIn("IDLE", content)
48 with uri.as_local() as local_uri:
49 self.assertEqual(local_uri.scheme, "file")
50 self.assertTrue(local_uri.exists())
52 truncated = uri.read(size=9).decode()
53 self.assertEqual(truncated, content[:9])
55 # Check that directory determination can work directly without the
56 # trailing slash.
57 d = self.root_uri.join("Icons")
58 self.assertTrue(d.isdir())
59 self.assertTrue(d.dirLike)
61 d = self.root_uri.join("Icons/", forceDirectory=True)
62 self.assertTrue(uri.exists(), f"Check directory {d} exists")
63 self.assertTrue(d.isdir())
65 with self.assertRaises(IsADirectoryError):
66 with d.as_local() as local_uri:
67 pass
69 j = d.join("README.txt")
70 self.assertEqual(uri, j)
71 self.assertFalse(j.dirLike)
72 self.assertFalse(j.isdir())
73 not_there = d.join("not-there.yaml")
74 self.assertFalse(not_there.exists())
76 bad = ResourcePath(f"{self.scheme}://bad.module/not.yaml")
77 multi = ResourcePath.mexists([uri, bad, not_there])
78 self.assertTrue(multi[uri])
79 self.assertFalse(multi[bad])
80 self.assertFalse(multi[not_there])
82 # Check that the bad URI works as expected.
83 self.assertFalse(bad.exists())
84 self.assertFalse(bad.isdir())
85 with self.assertRaises(FileNotFoundError):
86 bad.read()
87 with self.assertRaises(FileNotFoundError):
88 with bad.as_local():
89 pass
90 with self.assertRaises(FileNotFoundError):
91 with bad.open("r"):
92 pass
94 def test_open(self):
95 uri = self.root_uri.join("Icons/README.txt")
96 with uri.open("rb") as buffer:
97 content = buffer.read()
98 self.assertEqual(uri.read(), content)
100 with uri.open("r") as buffer:
101 content = buffer.read()
102 self.assertEqual(uri.read().decode(), content)
104 # Read only.
105 with self.assertRaises(RuntimeError):
106 with uri.open("w") as buffer:
107 pass
109 def test_walk(self):
110 """Test that we can find file resources.
112 Try to find resources in this package. Python does not care whether
113 a resource is a Python file or anything else.
114 """
115 resource = ResourcePath("resource://lsst.resources/")
116 resources = set(ResourcePath.findFileResources([resource]))
118 # Do not try to list all possible options. Files can move around
119 # and cache files can appear.
120 subset = {
121 ResourcePath("resource://lsst.resources/_resourceHandles/_s3ResourceHandle.py"),
122 ResourcePath("resource://lsst.resources/http.py"),
123 }
124 for r in subset:
125 self.assertIn(r, resources)
127 resources = set(
128 ResourcePath.findFileResources(
129 [ResourcePath("resource://lsst.resources/")], file_filter=r".*\.txt"
130 )
131 )
132 self.assertEqual(resources, set())
134 # Compare regex with str.
135 regex = r".*\.py"
136 py_files_str = list(resource.walk(file_filter=regex))
137 py_files_re = list(resource.walk(file_filter=re.compile(regex)))
138 self.assertGreater(len(py_files_str), 1)
139 self.assertEqual(py_files_str, py_files_re)
141 with self.assertRaises(ValueError):
142 list(ResourcePath("resource://lsst.resources/http.py").walk())
144 bad_dir = ResourcePath(f"{self.scheme}://bad.module/a/dir/")
145 self.assertTrue(bad_dir.isdir())
146 with self.assertRaises(ValueError):
147 list(bad_dir.walk())
150if __name__ == "__main__":
151 unittest.main()