Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# 

2# Developed for the LSST Data Management System. 

3# This product includes software developed by the LSST Project 

4# (https://www.lsst.org). 

5# See the COPYRIGHT file at the top-level directory of this distribution 

6# for details of code ownership. 

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 GNU General Public License 

19# along with this program. If not, see <https://www.gnu.org/licenses/>. 

20# 

21 

22import sys 

23import unittest 

24import os.path 

25import time 

26 

27import lsst.utils.tests 

28 

29 

30""" 

31This file contains tests for lsst.utils.tests.getTempFilePath. 

32 

33The TestNameClashN classes are used to check that getTempFilePath 

34does not use the same name across different test classes in the same 

35file even if they have the same test methods. They are distinct classes 

36with the same test method in an attempt to trigger a race condition 

37whereby context managers use the same name and race to delete the file. 

38The sleeps are there to ensure the race condition occurs in older versions 

39of this package. This should not happen as of DM-13046.""" 

40 

41 

42class GetTempFilePathTestCase(unittest.TestCase): 

43 def testBasics(self): 

44 with lsst.utils.tests.getTempFilePath(".txt") as tmpFile: 

45 # Path will have unique component so do not test full equality 

46 self.assertIn("test_getTempFilePath_testBasics", tmpFile) 

47 self.assertTrue(tmpFile.endswith(".txt")) 

48 f = open(tmpFile, "w") 

49 f.write("foo\n") 

50 f.close() 

51 self.assertFalse(os.path.exists(tmpFile)) 

52 

53 def testMultipleCallDepth(self): 

54 """Test getTempFile with multiple call depth""" 

55 funcName = "testMultipleCallDepth" 

56 self.runGetTempFile(funcName) 

57 self.runLevel2(funcName) 

58 self.runLevel3(funcName) 

59 

60 def runGetTempFile(self, funcName): 

61 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: 

62 # Path will have unique component so do not test full equality 

63 self.assertIn("test_getTempFilePath_%s" % (funcName,), tmpFile) 

64 self.assertTrue(tmpFile.endswith(".fits")) 

65 f = open(tmpFile, "w") 

66 f.write("foo\n") 

67 f.close() 

68 self.assertFalse(os.path.exists(tmpFile)) 

69 

70 def runLevel2(self, funcName): 

71 """Call runGetTempFile""" 

72 self.runGetTempFile(funcName) 

73 

74 def runLevel3(self, funcName): 

75 """Call runLevel2, which calls runGetTempFile""" 

76 self.runLevel2(funcName) 

77 

78 

79class TestNested(unittest.TestCase): 

80 """Tests of the use of getTempFilePath in nested context managers.""" 

81 

82 def testNested(self): 

83 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile1: 

84 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile2: 

85 self.assertNotEqual(tmpFile1, tmpFile2) 

86 with open(tmpFile1, "w") as f1: 

87 f1.write("foo\n") 

88 with open(tmpFile2, "w") as f2: 

89 f2.write("foo\n") 

90 self.assertTrue(os.path.exists(tmpFile1)) 

91 self.assertFalse(os.path.exists(tmpFile2)) 

92 self.assertFalse(os.path.exists(tmpFile1)) 

93 

94 

95class TestExpected(unittest.TestCase): 

96 """Tests that we get files when we expect to get them and we get upset 

97 when we don't get them.""" 

98 

99 def testOutputExpected(self): 

100 with lsst.utils.tests.getTempFilePath(".txt") as tmpFile: 

101 with open(tmpFile, "w") as f: 

102 f.write("foo\n") 

103 self.assertFalse(os.path.exists(tmpFile)) 

104 

105 with self.assertRaises(RuntimeError): 

106 with lsst.utils.tests.getTempFilePath(".txt", expectOutput=True) as tmpFile: 

107 pass 

108 

109 with self.assertRaises(RuntimeError): 

110 with lsst.utils.tests.getTempFilePath(".txt") as tmpFile: 

111 pass 

112 

113 def testOutputUnexpected(self): 

114 with self.assertRaises(RuntimeError): 

115 with lsst.utils.tests.getTempFilePath(".txt", expectOutput=False) as tmpFile: 

116 with open(tmpFile, "w") as f: 

117 f.write("foo\n") 

118 

119 with lsst.utils.tests.getTempFilePath(".txt", expectOutput=False) as tmpFile: 

120 pass 

121 self.assertFalse(os.path.exists(tmpFile)) 

122 

123 

124class TestNameClash1(unittest.TestCase): 

125 

126 def testClash(self): 

127 """Create the temp file and pause before trying to delete it.""" 

128 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: 

129 with open(tmpFile, "w") as f: 

130 f.write("foo\n") 

131 time.sleep(0.2) 

132 self.assertTrue(os.path.exists(tmpFile)) 

133 

134 

135class TestNameClash2(unittest.TestCase): 

136 

137 def testClash(self): 

138 """Pause a little before trying to create the temp file. The pause 

139 time is less than the time that TestNameClash1 is pausing.""" 

140 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: 

141 time.sleep(0.1) 

142 with open(tmpFile, "w") as f: 

143 f.write("foo\n") 

144 self.assertTrue(os.path.exists(tmpFile)) 

145 

146 

147class TestNameClash3(unittest.TestCase): 

148 

149 def testClash(self): 

150 """Create temp file and remove it without pauses.""" 

151 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: 

152 with open(tmpFile, "w") as f: 

153 f.write("foo\n") 

154 self.assertTrue(os.path.exists(tmpFile)) 

155 

156 

157class TestMemory(lsst.utils.tests.MemoryTestCase): 

158 pass 

159 

160 

161def setup_module(module): 

162 lsst.utils.tests.init() 

163 

164 

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

166 setup_module(sys.modules[__name__]) 

167 unittest.main()