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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

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

# for details of code ownership. 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

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

# 

 

import sys 

import unittest 

import os.path 

import time 

 

import lsst.utils.tests 

 

 

""" 

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

 

The TestNameClashN classes are used to check that getTempFilePath 

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

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

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

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

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

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

 

 

class GetTempFilePathTestCase(unittest.TestCase): 

def testBasics(self): 

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

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

self.assertIn("test_getTempFilePath_testBasics", tmpFile) 

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

f = open(tmpFile, "w") 

f.write("foo\n") 

f.close() 

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

 

def testMultipleCallDepth(self): 

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

funcName = "testMultipleCallDepth" 

self.runGetTempFile(funcName) 

self.runLevel2(funcName) 

self.runLevel3(funcName) 

 

def runGetTempFile(self, funcName): 

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

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

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

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

f = open(tmpFile, "w") 

f.write("foo\n") 

f.close() 

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

 

def runLevel2(self, funcName): 

"""Call runGetTempFile""" 

self.runGetTempFile(funcName) 

 

def runLevel3(self, funcName): 

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

self.runLevel2(funcName) 

 

 

class TestNested(unittest.TestCase): 

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

 

def testNested(self): 

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

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

self.assertNotEqual(tmpFile1, tmpFile2) 

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

f1.write("foo\n") 

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

f2.write("foo\n") 

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

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

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

 

 

class TestExpected(unittest.TestCase): 

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

when we don't get them.""" 

 

def testOutputExpected(self): 

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

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

f.write("foo\n") 

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

 

with self.assertRaises(RuntimeError): 

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

pass 

 

with self.assertRaises(RuntimeError): 

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

pass 

 

def testOutputUnexpected(self): 

with self.assertRaises(RuntimeError): 

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

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

f.write("foo\n") 

 

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

pass 

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

 

 

class TestNameClash1(unittest.TestCase): 

 

def testClash(self): 

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

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

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

f.write("foo\n") 

time.sleep(0.2) 

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

 

 

class TestNameClash2(unittest.TestCase): 

 

def testClash(self): 

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

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

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

time.sleep(0.1) 

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

f.write("foo\n") 

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

 

 

class TestNameClash3(unittest.TestCase): 

 

def testClash(self): 

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

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

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

f.write("foo\n") 

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

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

setup_module(sys.modules[__name__]) 

unittest.main()