Coverage for tests/test_resource.py: 12%

83 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-17 10:49 +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. 

11 

12import re 

13import unittest 

14 

15from lsst.resources import ResourcePath 

16from lsst.resources.tests import GenericTestCase 

17 

18 

19class ResourceTestCase(GenericTestCase, unittest.TestCase): 

20 """Generic test of resource URIs.""" 

21 

22 scheme = "resource" 

23 netloc = "lsst.resources" 

24 

25 

26class ResourceReadTestCase(unittest.TestCase): 

27 """Test that resource information can be read. 

28 

29 Python package resources are read-only. 

30 """ 

31 

32 # No resources in this package so need a resource in the main 

33 # python distribution. 

34 scheme = "resource" 

35 netloc = "idlelib" 

36 

37 def setUp(self): 

38 self.root = f"{self.scheme}://{self.netloc}" 

39 self.root_uri = ResourcePath(self.root) 

40 

41 def test_read(self): 

42 uri = self.root_uri.join("Icons/README.txt") 

43 self.assertTrue(uri.exists(), f"Check {uri} exists") 

44 

45 content = uri.read().decode() 

46 self.assertIn("IDLE", content) 

47 

48 with uri.as_local() as local_uri: 

49 self.assertEqual(local_uri.scheme, "file") 

50 self.assertTrue(local_uri.exists()) 

51 

52 truncated = uri.read(size=9).decode() 

53 self.assertEqual(truncated, content[:9]) 

54 

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) 

60 

61 d = self.root_uri.join("Icons/", forceDirectory=True) 

62 self.assertTrue(uri.exists(), f"Check directory {d} exists") 

63 self.assertTrue(d.isdir()) 

64 

65 with self.assertRaises(IsADirectoryError): 

66 with d.as_local() as local_uri: 

67 pass 

68 

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()) 

75 

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]) 

81 

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 

93 

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) 

99 

100 with uri.open("r") as buffer: 

101 content = buffer.read() 

102 self.assertEqual(uri.read().decode(), content) 

103 

104 # Read only. 

105 with self.assertRaises(RuntimeError): 

106 with uri.open("w") as buffer: 

107 pass 

108 

109 def test_walk(self): 

110 """Test that we can find file resources. 

111 

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])) 

117 

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) 

126 

127 resources = set( 

128 ResourcePath.findFileResources( 

129 [ResourcePath("resource://lsst.resources/")], file_filter=r".*\.txt" 

130 ) 

131 ) 

132 self.assertEqual(resources, set()) 

133 

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) 

140 

141 with self.assertRaises(ValueError): 

142 list(ResourcePath("resource://lsst.resources/http.py").walk()) 

143 

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()) 

148 

149 

150if __name__ == "__main__": 

151 unittest.main()