Coverage for tests/test_getTempFilePath.py: 29%
Shortcuts 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
Shortcuts 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# This file is part of utils.
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.
12import sys
13import unittest
14import os.path
15import time
17import lsst.utils.tests
20"""
21This file contains tests for lsst.utils.tests.getTempFilePath.
23The TestNameClashN classes are used to check that getTempFilePath
24does not use the same name across different test classes in the same
25file even if they have the same test methods. They are distinct classes
26with the same test method in an attempt to trigger a race condition
27whereby context managers use the same name and race to delete the file.
28The sleeps are there to ensure the race condition occurs in older versions
29of this package. This should not happen as of DM-13046."""
32class GetTempFilePathTestCase(unittest.TestCase):
33 def testBasics(self):
34 with lsst.utils.tests.getTempFilePath(".txt") as tmpFile:
35 # Path will have unique component so do not test full equality
36 self.assertIn("test_getTempFilePath_testBasics", tmpFile)
37 self.assertTrue(tmpFile.endswith(".txt"))
38 f = open(tmpFile, "w")
39 f.write("foo\n")
40 f.close()
41 self.assertFalse(os.path.exists(tmpFile))
43 def testMultipleCallDepth(self):
44 """Test getTempFile with multiple call depth"""
45 funcName = "testMultipleCallDepth"
46 self.runGetTempFile(funcName)
47 self.runLevel2(funcName)
48 self.runLevel3(funcName)
50 def runGetTempFile(self, funcName):
51 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
52 # Path will have unique component so do not test full equality
53 self.assertIn("test_getTempFilePath_%s" % (funcName,), tmpFile)
54 self.assertTrue(tmpFile.endswith(".fits"))
55 f = open(tmpFile, "w")
56 f.write("foo\n")
57 f.close()
58 self.assertFalse(os.path.exists(tmpFile))
60 def runLevel2(self, funcName):
61 """Call runGetTempFile"""
62 self.runGetTempFile(funcName)
64 def runLevel3(self, funcName):
65 """Call runLevel2, which calls runGetTempFile"""
66 self.runLevel2(funcName)
69class TestNested(unittest.TestCase):
70 """Tests of the use of getTempFilePath in nested context managers."""
72 def testNested(self):
73 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile1:
74 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile2:
75 self.assertNotEqual(tmpFile1, tmpFile2)
76 with open(tmpFile1, "w") as f1:
77 f1.write("foo\n")
78 with open(tmpFile2, "w") as f2:
79 f2.write("foo\n")
80 self.assertTrue(os.path.exists(tmpFile1))
81 self.assertFalse(os.path.exists(tmpFile2))
82 self.assertFalse(os.path.exists(tmpFile1))
85class TestExpected(unittest.TestCase):
86 """Tests that we get files when we expect to get them and we get upset
87 when we don't get them."""
89 def testOutputExpected(self):
90 with lsst.utils.tests.getTempFilePath(".txt") as tmpFile:
91 with open(tmpFile, "w") as f:
92 f.write("foo\n")
93 self.assertFalse(os.path.exists(tmpFile))
95 with self.assertRaises(RuntimeError):
96 with lsst.utils.tests.getTempFilePath(".txt", expectOutput=True) as tmpFile:
97 pass
99 with self.assertRaises(RuntimeError):
100 with lsst.utils.tests.getTempFilePath(".txt") as tmpFile:
101 pass
103 def testOutputUnexpected(self):
104 with self.assertRaises(RuntimeError):
105 with lsst.utils.tests.getTempFilePath(".txt", expectOutput=False) as tmpFile:
106 with open(tmpFile, "w") as f:
107 f.write("foo\n")
109 with lsst.utils.tests.getTempFilePath(".txt", expectOutput=False) as tmpFile:
110 pass
111 self.assertFalse(os.path.exists(tmpFile))
114class TestNameClash1(unittest.TestCase):
116 def testClash(self):
117 """Create the temp file and pause before trying to delete it."""
118 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
119 with open(tmpFile, "w") as f:
120 f.write("foo\n")
121 time.sleep(0.2)
122 self.assertTrue(os.path.exists(tmpFile))
125class TestNameClash2(unittest.TestCase):
127 def testClash(self):
128 """Pause a little before trying to create the temp file. The pause
129 time is less than the time that TestNameClash1 is pausing."""
130 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
131 time.sleep(0.1)
132 with open(tmpFile, "w") as f:
133 f.write("foo\n")
134 self.assertTrue(os.path.exists(tmpFile))
137class TestNameClash3(unittest.TestCase):
139 def testClash(self):
140 """Create temp file and remove it without pauses."""
141 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
142 with open(tmpFile, "w") as f:
143 f.write("foo\n")
144 self.assertTrue(os.path.exists(tmpFile))
147class TestMemory(lsst.utils.tests.MemoryTestCase):
148 pass
151def setup_module(module):
152 lsst.utils.tests.init()
155if __name__ == "__main__": 155 ↛ 156line 155 didn't jump to line 156, because the condition on line 155 was never true
156 setup_module(sys.modules[__name__])
157 unittest.main()