Coverage for tests/test_posixParentSearch.py: 20%

89 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-12 01:29 -0800

1# 

2# LSST Data Management System 

3# Copyright 2016 LSST Corporation. 

4# 

5# This product includes software developed by the 

6# LSST Project (http://www.lsst.org/). 

7# 

8# This program is free software: you can redistribute it and/or modify 

9# it under the terms of the GNU General Public License as published by 

10# the Free Software Foundation, either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# This program is distributed in the hope that it will be useful, 

14# but WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the LSST License Statement and 

19# the GNU General Public License along with this program. If not, 

20# see <http://www.lsstcorp.org/LegalNotices/>. 

21# 

22 

23import os 

24import unittest 

25import lsst.daf.persistence as dafPersist 

26import lsst.utils.tests 

27import shutil 

28import tempfile 

29 

30 

31ROOT = os.path.abspath(os.path.dirname(__file__)) 

32 

33 

34def setup_module(module): 

35 lsst.utils.tests.init() 

36 

37 

38class PosixParentSearch(unittest.TestCase): 

39 """A test case for parentSearch in PosixStorage.""" 

40 

41 def setUp(self): 

42 self.testDir = tempfile.mkdtemp(dir=ROOT, prefix='PosixParentSearch-') 

43 

44 def tearDown(self): 

45 if os.path.exists(self.testDir): 

46 shutil.rmtree(self.testDir) 

47 

48 def testFilePath(self): 

49 """Test that a file can be found; when root is part of the path then root is returned with the path 

50 result. When root is not part of the path then root is not returned with the path result.""" 

51 with open(os.path.join(self.testDir, 'foo.txt'), 'w') as f: 

52 f.write('abc') 

53 storage = dafPersist.PosixStorage(uri=self.testDir, 

54 create=True) 

55 foundName = storage.search(storage.root, 'foo.txt', searchParents=True) 

56 self.assertEqual(foundName, ['foo.txt']) 

57 

58 searchFor = os.path.join(self.testDir, 'foo.txt') 

59 foundName = storage.search(storage.root, searchFor, searchParents=True) 

60 self.assertEqual(foundName, [searchFor]) 

61 

62 def testFilePathWithHeaderExt(self): 

63 """Find a file with a search string that includes a FITS-style header extension.""" 

64 with open(os.path.join(self.testDir, 'foo.txt'), 'w') as f: 

65 f.write('abc') 

66 storage = dafPersist.PosixStorage(uri=self.testDir, 

67 create=True) 

68 foundName = storage.search(storage.root, 'foo.txt[0]', searchParents=True) 

69 self.assertEqual(foundName, ['foo.txt[0]']) 

70 

71 searchFor = os.path.join(self.testDir, 'foo.txt[0]') 

72 foundName = storage.search(storage.root, searchFor, searchParents=True) 

73 self.assertEqual(foundName, [searchFor]) 

74 

75 def testFilePathInParent(self): 

76 """Find a file in a repo that is a grandchild of the repo that has the file""" 

77 parentDir = os.path.join(self.testDir, 'a') 

78 childDir = os.path.join(self.testDir, 'b') 

79 for d in (parentDir, childDir): 

80 os.makedirs(d) 

81 with open(os.path.join(parentDir, 'foo.txt'), 'w') as f: 

82 f.write('abc') 

83 os.symlink('../a', os.path.join(childDir, '_parent')) 

84 storage = dafPersist.PosixStorage(uri=childDir, 

85 create=True) 

86 

87 foundName = storage.search(storage.root, 'foo.txt', searchParents=True) 

88 self.assertEqual(storage.root, childDir) 

89 self.assertEqual(foundName, ['_parent/foo.txt']) 

90 

91 searchFor = os.path.join(childDir, 'foo.txt') 

92 foundName = storage.search(storage.root, searchFor, searchParents=True) 

93 self.assertEqual(storage.root, childDir) 

94 self.assertEqual(foundName, [os.path.join(childDir, '_parent/foo.txt')]) 

95 

96 def testFilePathIn2ndParentParent(self): 

97 """Find a file in a repo that is the parent of a parent of the root repo.""" 

98 grandParentDir = os.path.join(self.testDir, 'a') 

99 parentDir = os.path.join(self.testDir, 'b') 

100 childDir = os.path.join(self.testDir, 'c') 

101 for d in (grandParentDir, parentDir, childDir): 

102 os.makedirs(d) 

103 for name in ('foo.txt', 'bar.txt'): 

104 with open(os.path.join(grandParentDir, name), 'w') as f: 

105 f.write('abc') 

106 os.symlink('../a', os.path.join(parentDir, '_parent')) 

107 os.symlink('../b', os.path.join(childDir, '_parent')) 

108 storage = dafPersist.PosixStorage(uri=childDir, 

109 create=True) 

110 

111 for name in ('foo.txt', 'bar.txt[0]'): 

112 foundName = storage.search(storage.root, name, searchParents=True) 

113 self.assertEqual(storage.root, childDir) 

114 self.assertEqual(foundName, [os.path.join('_parent/_parent/', name)]) 

115 

116 for name in ('foo.txt', 'bar.txt[0]'): 

117 searchFor = os.path.join(childDir, name) 

118 foundName = storage.search(storage.root, searchFor, searchParents=True) 

119 self.assertEqual(storage.root, childDir) 

120 self.assertEqual(foundName, [os.path.join(childDir, '_parent/_parent/', name)]) 

121 

122 def testDoSearchParentFlag(self): 

123 """Test that parent search can be told to follow _parent symlink (or not) when searching.""" 

124 parentDir = os.path.join(self.testDir, 'a') 

125 childDir = os.path.join(self.testDir, 'b') 

126 for d in (parentDir, childDir): 

127 os.makedirs(d) 

128 with open(os.path.join(parentDir, 'foo.txt'), 'w') as f: 

129 f.write('abc') 

130 os.symlink('../a', os.path.join(childDir, '_parent')) 

131 storage = dafPersist.PosixStorage(uri=childDir, create=True) 

132 self.assertEqual(storage.search(storage.root, 'foo.txt', searchParents=True), ['_parent/foo.txt']) 

133 self.assertEqual(storage.search(storage.root, 'foo.txt', searchParents=False), None) 

134 

135 def testNoResults(self): 

136 storage = dafPersist.PosixStorage(uri=self.testDir, create=True) 

137 self.assertIsNone(storage.search(storage.root, 'fileThatDoesNotExist.txt', searchParents=True)) 

138 

139 

140class MemoryTester(lsst.utils.tests.MemoryTestCase): 

141 pass 

142 

143 

144if __name__ == '__main__': 144 ↛ 145line 144 didn't jump to line 145, because the condition on line 144 was never true

145 lsst.utils.tests.init() 

146 unittest.main()