Coverage for tests/test_resource.py: 12%

83 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-15 02:25 -0700

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 scheme = "resource" 

21 netloc = "lsst.resources" 

22 

23 

24class ResourceReadTestCase(unittest.TestCase): 

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

26 

27 Python package resources are read-only. 

28 """ 

29 

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

31 # python distribution. 

32 scheme = "resource" 

33 netloc = "idlelib" 

34 

35 def setUp(self): 

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

37 self.root_uri = ResourcePath(self.root) 

38 

39 def test_read(self): 

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

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

42 

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

44 self.assertIn("IDLE", content) 

45 

46 with uri.as_local() as local_uri: 

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

48 self.assertTrue(local_uri.exists()) 

49 

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

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

52 

53 # Check that directory determination can work directly without the 

54 # trailing slash. 

55 d = self.root_uri.join("Icons") 

56 self.assertTrue(d.isdir()) 

57 self.assertTrue(d.dirLike) 

58 

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

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

61 self.assertTrue(d.isdir()) 

62 

63 with self.assertRaises(IsADirectoryError): 

64 with d.as_local() as local_uri: 

65 pass 

66 

67 j = d.join("README.txt") 

68 self.assertEqual(uri, j) 

69 self.assertFalse(j.dirLike) 

70 self.assertFalse(j.isdir()) 

71 not_there = d.join("not-there.yaml") 

72 self.assertFalse(not_there.exists()) 

73 

74 bad = ResourcePath(f"{self.scheme}://bad.module/not.yaml") 

75 multi = ResourcePath.mexists([uri, bad, not_there]) 

76 self.assertTrue(multi[uri]) 

77 self.assertFalse(multi[bad]) 

78 self.assertFalse(multi[not_there]) 

79 

80 # Check that the bad URI works as expected. 

81 self.assertFalse(bad.exists()) 

82 self.assertFalse(bad.isdir()) 

83 with self.assertRaises(FileNotFoundError): 

84 bad.read() 

85 with self.assertRaises(FileNotFoundError): 

86 with bad.as_local(): 

87 pass 

88 with self.assertRaises(FileNotFoundError): 

89 with bad.open("r"): 

90 pass 

91 

92 def test_open(self): 

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

94 with uri.open("rb") as buffer: 

95 content = buffer.read() 

96 self.assertEqual(uri.read(), content) 

97 

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

99 content = buffer.read() 

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

101 

102 # Read only. 

103 with self.assertRaises(RuntimeError): 

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

105 pass 

106 

107 def test_walk(self): 

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

109 

110 Try to find resources in this package. Python does not care whether 

111 a resource is a Python file or anything else. 

112 """ 

113 

114 resource = ResourcePath("resource://lsst.resources/") 

115 resources = set(ResourcePath.findFileResources([resource])) 

116 

117 # Do not try to list all possible options. Files can move around 

118 # and cache files can appear. 

119 subset = { 

120 ResourcePath("resource://lsst.resources/_resourceHandles/_s3ResourceHandle.py"), 

121 ResourcePath("resource://lsst.resources/http.py"), 

122 } 

123 for r in subset: 

124 self.assertIn(r, resources) 

125 

126 resources = set( 

127 ResourcePath.findFileResources( 

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

129 ) 

130 ) 

131 self.assertEqual(resources, set()) 

132 

133 # Compare regex with str. 

134 regex = r".*\.py" 

135 py_files_str = list(resource.walk(file_filter=regex)) 

136 py_files_re = list(resource.walk(file_filter=re.compile(regex))) 

137 self.assertGreater(len(py_files_str), 1) 

138 self.assertEqual(py_files_str, py_files_re) 

139 

140 with self.assertRaises(ValueError): 

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

142 

143 bad_dir = ResourcePath(f"{self.scheme}://bad.module/a/dir/") 

144 self.assertTrue(bad_dir.isdir()) 

145 with self.assertRaises(ValueError): 

146 list(bad_dir.walk()) 

147 

148 

149if __name__ == "__main__": 

150 unittest.main()